Skip to content
Snippets Groups Projects
Commit ae9f25aa authored by Éloïs's avatar Éloïs
Browse files

[perf] sign block with buggy nacl for perf & verify sig when found proof

parent 00f753da
No related branches found
No related tags found
1 merge request!1283Resolve "Duniter uses a buggy version of TweetNaCl"
...@@ -17,7 +17,7 @@ import {hashf} from "../../../lib/common" ...@@ -17,7 +17,7 @@ import {hashf} from "../../../lib/common"
import {DBBlock} from "../../../lib/db/DBBlock" import {DBBlock} from "../../../lib/db/DBBlock"
import {ConfDTO} from "../../../lib/dto/ConfDTO" import {ConfDTO} from "../../../lib/dto/ConfDTO"
import {ProverConstants} from "./constants" import {ProverConstants} from "./constants"
import {KeyGen} from "../../../lib/common-libs/crypto/keyring" import {KeyGen, verify, verifyBuggy} from "../../../lib/common-libs/crypto/keyring"
import {dos2unix} from "../../../lib/common-libs/dos2unix" import {dos2unix} from "../../../lib/common-libs/dos2unix"
import {rawer} from "../../../lib/common-libs/index" import {rawer} from "../../../lib/common-libs/index"
import {ProcessCpuProfiler} from "../../../ProcessCpuProfiler" import {ProcessCpuProfiler} from "../../../ProcessCpuProfiler"
...@@ -36,7 +36,9 @@ export function createPowWorker() { ...@@ -36,7 +36,9 @@ export function createPowWorker() {
// By default, we do not prefix the PoW by any number // By default, we do not prefix the PoW by any number
let prefix = 0; let prefix = 0;
let signatureFunc:any, lastSecret:any, lastVersion: number, currentCPU = 1; let sigFuncSaved: (msg:string) => string;
let verifyFuncSaved: (msg:string, sig:string) => boolean;
let lastSecret:any, lastVersion: number, currentCPU:number = 1;
process.on('uncaughtException', (err:any) => { process.on('uncaughtException', (err:any) => {
console.error(err.stack || Error(err)) console.error(err.stack || Error(err))
...@@ -115,24 +117,36 @@ export function createPowWorker() { ...@@ -115,24 +117,36 @@ export function createPowWorker() {
prefix *= 100 * ProverConstants.NONCE_RANGE prefix *= 100 * ProverConstants.NONCE_RANGE
} }
const highMark = stuff.highMark; const highMark = stuff.highMark;
// Define sigFunc
// Use Buggy version for performance reasons
let sigFunc = null; let sigFunc = null;
if (signatureFunc && lastSecret === pair.sec && lastVersion === block.version) { if (sigFuncSaved && lastSecret === pair.sec) {
sigFunc = signatureFunc; sigFunc = sigFuncSaved;
} else {
lastSecret = pair.sec;
sigFunc = (msg:string) => KeyGen(pair.pub, pair.sec).signSyncBuggy(msg)
}
// Define verifyFunc
let verifyFunc = null;
if (verifyFuncSaved && lastSecret === pair.sec && lastVersion === block.version) {
verifyFunc = verifyFuncSaved;
} else { } else {
lastSecret = pair.sec; lastSecret = pair.sec;
lastVersion = block.version; lastVersion = block.version;
if (block.version >= 12) { if (block.version >= 12) {
sigFunc = (msg:string) => KeyGen(pair.pub, pair.sec).signSync(msg) verifyFunc = (msg:string, sig:string) => verify(msg, sig, pair.pub)
} else { } else {
sigFunc = (msg:string) => KeyGen(pair.pub, pair.sec).signSyncBuggy(msg) verifyFunc = (msg:string, sig:string) => verifyBuggy(msg, sig, pair.pub)
} }
} }
let pow = "", sig = "", raw = "";
/***************** /*****************
* GO! * GO!
****************/ ****************/
let pow = "", sig = "", raw = "";
let pausePeriod = 1; let pausePeriod = 1;
let testsCount = 0; let testsCount = 0;
let found = false; let found = false;
...@@ -198,6 +212,12 @@ export function createPowWorker() { ...@@ -198,6 +212,12 @@ export function createPowWorker() {
} }
if (charOK) { if (charOK) {
found = !!(pow[nbZeros].match(new RegExp('[0-' + highMark + ']'))) found = !!(pow[nbZeros].match(new RegExp('[0-' + highMark + ']')))
if (found) {
let sigOk = verifyFunc(raw, sig);
if (!sigOk) {
found = false;
}
}
} }
if (!found && nbZeros > 0 && j - 1 >= ProverConstants.POW_MINIMAL_TO_SHOW) { if (!found && nbZeros > 0 && j - 1 >= ProverConstants.POW_MINIMAL_TO_SHOW) {
pSend({ pow: { pow: pow, block: block, nbZeros: nbZeros }}); pSend({ pow: { pow: pow, block: block, nbZeros: nbZeros }});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment