From 2f6b492ef132ab6a9d5fa306492cf0f1b440e351 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sun, 8 Oct 2023 16:19:59 -0700 Subject: [PATCH 1/2] fix: Support negative numbers with circom2 implementation --- js/utils.js | 7 +++++++ js/witness_calculator.js | 34 ++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/js/utils.js b/js/utils.js index 2f9ac33..a11fe24 100644 --- a/js/utils.js +++ b/js/utils.js @@ -32,6 +32,13 @@ export function flatArray(a) { } } +// Ref https://github.com/iden3/circom/commit/ec6388cf6eb62463539cb4c40cc3ceae9826de19 +export function normalize(n, prime) { + let res = BigInt(n) % prime + if (res < 0) res += prime + return res +} + export function fnvHash(str) { const uint64_max = BigInt(2) ** BigInt(64); let hash = BigInt("0xCBF29CE484222325"); diff --git a/js/witness_calculator.js b/js/witness_calculator.js index 7db0bf8..0cfb0b2 100644 --- a/js/witness_calculator.js +++ b/js/witness_calculator.js @@ -17,7 +17,7 @@ limitations under the License. */ -import { flatArray, fnvHash, toArray32 } from "./utils.js"; +import { flatArray, fnvHash, toArray32, normalize } from "./utils.js"; import { Scalar, F1Field } from "ffjavascript"; export default async function builder(code, options) { @@ -420,18 +420,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 Date: Sun, 8 Oct 2023 16:21:48 -0700 Subject: [PATCH 2/2] rebuild --- build/main.cjs | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) 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