diff --git a/code_producers/src/wasm_elements/common/generate_witness.js b/code_producers/src/wasm_elements/common/generate_witness.js index eabb86e58..9efaee95e 100755 --- a/code_producers/src/wasm_elements/common/generate_witness.js +++ b/code_producers/src/wasm_elements/common/generate_witness.js @@ -5,16 +5,16 @@ if (process.argv.length != 5) { console.log("Usage: node generate_witness.js "); } else { const input = JSON.parse(readFileSync(process.argv[3], "utf8")); - + const buffer = readFileSync(process.argv[2]); wc(buffer).then(async witnessCalculator => { - // const w= await witnessCalculator.calculateWitness(input,0); - // for (let i=0; i< w.length; i++){ - // console.log(w[i]); - // } - const buff= await witnessCalculator.calculateWTNSBin(input,0); - writeFile(process.argv[4], buff, function(err) { - if (err) throw err; - }); + // const w= await witnessCalculator.calculateWitness(input,0); + // for (let i=0; i< w.length; i++){ + // console.log(w[i]); + // } + const buff= await witnessCalculator.calculateWTNSBin(input,0); + writeFile(process.argv[4], buff, function(err) { + if (err) throw err; + }); }); } diff --git a/code_producers/src/wasm_elements/common/witness_calculator.js b/code_producers/src/wasm_elements/common/witness_calculator.js index 20e6e20ad..efd7821dc 100755 --- a/code_producers/src/wasm_elements/common/witness_calculator.js +++ b/code_producers/src/wasm_elements/common/witness_calculator.js @@ -4,60 +4,60 @@ module.exports = async function builder(code, options) { let wasmModule; try { - wasmModule = await WebAssembly.compile(code); + wasmModule = await WebAssembly.compile(code); } catch (err) { - console.log(err); - console.log("\nTry to run circom --c in order to generate c++ code instead\n"); - throw new Error(err); + console.log(err); + console.log("\nTry to run circom --c in order to generate c++ code instead\n"); + throw new Error(err); } let wc; let errStr = ""; let msgStr = ""; - + const instance = await WebAssembly.instantiate(wasmModule, { runtime: { exceptionHandler : function(code) { - let err; + let err; if (code == 1) { err = "Signal not found.\n"; } else if (code == 2) { err = "Too many signals set.\n"; } else if (code == 3) { err = "Signal already set.\n"; - } else if (code == 4) { + } else if (code == 4) { err = "Assert Failed.\n"; - } else if (code == 5) { + } else if (code == 5) { err = "Not enough memory.\n"; - } else if (code == 6) { + } else if (code == 6) { err = "Input signal array access exceeds the size.\n"; - } else { - err = "Unknown error.\n"; + } else { + err = "Unknown error.\n"; } throw new Error(err + errStr); }, - printErrorMessage : function() { - errStr += getMessage() + "\n"; + printErrorMessage : function() { + errStr += getMessage() + "\n"; // console.error(getMessage()); - }, - writeBufferMessage : function() { - const msg = getMessage(); - // Any calls to `log()` will always end with a `\n`, so that's when we print and reset - if (msg === "\n") { - console.log(msgStr); - msgStr = ""; - } else { - // If we've buffered other content, put a space in between the items - if (msgStr !== "") { - msgStr += " " - } - // Then append the message to the message we are creating - msgStr += msg; - } - }, - showSharedRWMemory : function() { - printSharedRWMemory (); + }, + writeBufferMessage : function() { + const msg = getMessage(); + // Any calls to `log()` will always end with a `\n`, so that's when we print and reset + if (msg === "\n") { + console.log(msgStr); + msgStr = ""; + } else { + // If we've buffered other content, put a space in between the items + if (msgStr !== "") { + msgStr += " " + } + // Then append the message to the message we are creating + msgStr += msg; + } + }, + showSharedRWMemory : function() { + printSharedRWMemory (); } } @@ -74,42 +74,41 @@ module.exports = async function builder(code, options) { // options.logFinishComponent // ); - + wc = new WitnessCalculator(instance, sanityCheck); return wc; function getMessage() { var message = ""; - var c = instance.exports.getMessageChar(); + var c = instance.exports.getMessageChar(); while ( c != 0 ) { - message += String.fromCharCode(c); - c = instance.exports.getMessageChar(); - } + message += String.fromCharCode(c); + c = instance.exports.getMessageChar(); + } return message; } - + function printSharedRWMemory () { - const shared_rw_memory_size = instance.exports.getFieldNumLen32(); - const arr = new Uint32Array(shared_rw_memory_size); - for (let j=0; j { const h = fnvHash(k); const hMSB = parseInt(h.slice(0,8), 16); const hLSB = parseInt(h.slice(8,16), 16); const fArr = flatArray(input[k]); - let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB); - if (signalSize < 0){ - throw new Error(`Signal ${k} not found\n`); - } - if (fArr.length < signalSize) { - throw new Error(`Not enough values for input signal ${k}\n`); - } - if (fArr.length > signalSize) { - throw new Error(`Too many values for input signal ${k}\n`); - } + let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB); + if (signalSize < 0){ + throw new Error(`Signal ${k} not found\n`); + } + if (fArr.length < signalSize) { + throw new Error(`Not enough values for input signal ${k}\n`); + } + if (fArr.length > signalSize) { + throw new Error(`Too many values for input signal ${k}\n`); + } for (let i=0; i0) { - res.unshift(0); - i--; - } + var i = size - res.length; + while (i>0) { + res.unshift(0); + i--; + } } return res; } @@ -326,9 +321,9 @@ function fnvHash(str) { const uint64_max = BigInt(2) ** BigInt(64); let hash = BigInt("0xCBF29CE484222325"); for (var i = 0; i < str.length; i++) { - hash ^= BigInt(str[i].charCodeAt()); - hash *= BigInt(0x100000001B3); - hash %= uint64_max; + hash ^= BigInt(str[i].charCodeAt()); + hash *= BigInt(0x100000001B3); + hash %= uint64_max; } let shash = hash.toString(16); let n = 16 - shash.length;