diff --git a/build/main.cjs b/build/main.cjs index 235f1b6..80cf23b 100644 --- a/build/main.cjs +++ b/build/main.cjs @@ -38,6 +38,13 @@ function flatArray(a) { } } +// Ref https://github.com/iden3/circom/commit/ec6388cf6eb62463539cb4c40cc3ceae9826de19 +function normalize(n, prime) { + let res = BigInt(n) % prime; + if (res < 0) res += prime; + return res +} + function fnvHash(str) { const uint64_max = BigInt(2) ** BigInt(64); let hash = BigInt("0xCBF29CE484222325"); @@ -472,18 +479,32 @@ class WitnessCalculatorCircom2 { const hMSB = parseInt(h.slice(0,8), 16); const hLSB = parseInt(h.slice(8,16), 16); const fArr = flatArray(input[k]); + // Slight deviation from https://github.com/iden3/circom/blob/v2.1.6/code_producers/src/wasm_elements/common/witness_calculator.js + // because I don't know when this exported function was added + if (typeof this.instance.exports.getInputSignalSize === 'function') { + 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; i signalSize) { + throw new Error(`Too many values for input signal ${k}\n`); + } + } for (let i=0; i