diff --git a/.gitignore b/.gitignore index 50d8b98..fa410ff 100644 --- a/.gitignore +++ b/.gitignore @@ -62,4 +62,8 @@ typings/ tmp -.DS_Store \ No newline at end of file +.DS_Store + +package-lock.json + +*.r1cs diff --git a/c/tester.js b/c/tester.js index 99c6269..0c39dfc 100644 --- a/c/tester.js +++ b/c/tester.js @@ -206,6 +206,54 @@ class CTester { } return lines.join("\n"); } + async getOutput(witness, outputs) { + const outputs_iter = parse(outputs); + const self = this; + if (!self.symbols) await self.loadSymbols(); + // new a dictionary which map [name] to [value] + let res = {}; + for (let n of outputs_iter) { + let v; + // prefix n with "main." + let tmp_n = "main." + n; + if (witness[self.symbols[tmp_n].varIdx] !== undefined) { + v = witness[self.symbols[tmp_n].varIdx].toString(); + } else { + assert(false, "Output variable not defined: " + n); + } + // add {name: value} to the dictionary + res[n] = v; + } + // parse "a, b[3]" to "a, b[0], b[1], b[2]" + function parse(inputArray) { + const outputArray = []; + for (const item of inputArray) { + // Check if the item matches the pattern with brackets and a number + const match = item.match(/^(.+)\[(\d+)\]$/); + if (match) { + // If there's a match, expand the item + const base = match[1]; // The base string (e.g., "b" or "d") + const count = parseInt(match[2], 10); // The number inside the brackets + // Range all the element in the array, then add the expanded items to the output array + for (let i = 0; i < count; i++) { + // Check for nested brackets + if (base.includes("[")) { + assert(false, "Multi-dimension array is not supported"); + } else { + // Otherwise, just add the expanded items to the output array + outputArray.push(`${base}[${i}]`); + } + } + } else { + // If there's no match, add the item as it is a singla varialbe + outputArray.push(item); + } + } + + return outputArray; + } + return res; + } async checkConstraints(witness) { const self = this; diff --git a/wasm/tester.js b/wasm/tester.js index 8e19877..dea34d1 100644 --- a/wasm/tester.js +++ b/wasm/tester.js @@ -7,6 +7,7 @@ const path = require("path"); const util = require("util"); const {F1Field} = require("ffjavascript"); +const { c } = require(".."); const exec = util.promisify(require("child_process").exec); const readR1cs = require("r1csfile").readR1cs; @@ -178,7 +179,7 @@ class WasmTester { if (!self.symbols) await self.loadSymbols(); for (let n in self.symbols) { let v; - if (utils.isDefined(witness[self.symbols[n].varIdx])) { + if (witness[self.symbols[n].varIdx] !== undefined) { v = witness[self.symbols[n].varIdx].toString(); } else { v = "undefined"; @@ -187,6 +188,54 @@ class WasmTester { } return lines.join("\n"); } + async getOutput(witness, outputs) { + const outputs_iter = parse(outputs); + const self = this; + if (!self.symbols) await self.loadSymbols(); + // new a dictionary which map [name] to [value] + let res = {}; + for (let n of outputs_iter) { + let v; + // prefix n with "main." + let tmp_n = "main." + n; + if (witness[self.symbols[tmp_n].varIdx] !== undefined) { + v = witness[self.symbols[tmp_n].varIdx].toString(); + } else { + assert(false, "Output variable not defined: " + n); + } + // add {name: value} to the dictionary + res[n] = v; + } + // parse "a, b[3]" to "a, b[0], b[1], b[2]" + function parse(inputArray) { + const outputArray = []; + for (const item of inputArray) { + // Check if the item matches the pattern with brackets and a number + const match = item.match(/^(.+)\[(\d+)\]$/); + if (match) { + // If there's a match, expand the item + const base = match[1]; // The base string (e.g., "b" or "d") + const count = parseInt(match[2], 10); // The number inside the brackets + // Range all the element in the array, then add the expanded items to the output array + for (let i = 0; i < count; i++) { + // Check for nested brackets + if (base.includes("[")) { + assert(false, "Multi-dimension array is not supported"); + } else { + // Otherwise, just add the expanded items to the output array + outputArray.push(`${base}[${i}]`); + } + } + } else { + // If there's no match, add the item as it is a singla varialbe + outputArray.push(item); + } + } + + return outputArray; + } + return res; + } async checkConstraints(witness) { const self = this;