diff --git a/build/main.cjs b/build/main.cjs index 6602499..81e69f3 100644 --- a/build/main.cjs +++ b/build/main.cjs @@ -52,6 +52,25 @@ function fnvHash(str) { return shash; } +// Note that this pads zeros +function toArray32(s,size) { + const res = []; //new Uint32Array(size); //has no unshift + let rem = BigInt(s); + const radix = BigInt(0x100000000); + while (rem) { + res.unshift( Number(rem % radix)); + rem = rem / radix; + } + if (size) { + var i = size - res.length; + while (i>0) { + res.unshift(0); + i--; + } + } + return res; +} + /* globals WebAssembly */ async function builder(code, options) { @@ -83,6 +102,32 @@ async function builder(code, options) { "memory": memory }, runtime: { + exceptionHandler: function(code) { + let errStr; + if (code == 1) { + errStr = "Signal not found. "; + } else if (code == 2) { + errStr = "Too many signals set. "; + } else if (code == 3) { + errStr = "Signal already set. "; + } else if (code == 4) { + errStr = "Assert Failed. "; + } else if (code == 5) { + errStr = "Not enough memory. "; + } else { + errStr = "Unknown error."; + } + console.log("ERROR: ", code, errStr); + throw new Error(errStr); + }, + showSharedRWMemory: function() { + 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]); + for (let i=0; i0) { + res.unshift(0); + i--; + } + } + return res; +} diff --git a/js/witness_calculator.js b/js/witness_calculator.js index 243496f..dcd25b7 100644 --- a/js/witness_calculator.js +++ b/js/witness_calculator.js @@ -17,7 +17,7 @@ limitations under the License. */ -import { flatArray, fnvHash } from "./utils.js"; +import { flatArray, fnvHash, toArray32 } from "./utils.js"; import { Scalar, F1Field } from "ffjavascript"; export default async function builder(code, options) { @@ -49,6 +49,32 @@ export default async function builder(code, options) { "memory": memory }, runtime: { + exceptionHandler: function(code) { + let errStr; + if (code == 1) { + errStr = "Signal not found. "; + } else if (code == 2) { + errStr = "Too many signals set. "; + } else if (code == 3) { + errStr = "Signal already set. "; + } else if (code == 4) { + errStr = "Assert Failed. "; + } else if (code == 5) { + errStr = "Not enough memory. "; + } else { + errStr = "Unknown error."; + } + console.log("ERROR: ", code, errStr); + throw new Error(errStr); + }, + showSharedRWMemory: function() { + 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]); + for (let i=0; i