From 968aaa5c7cf9f22dba72d939632778d78e57c56f Mon Sep 17 00:00:00 2001 From: martyall Date: Mon, 1 Jul 2024 08:06:07 -0700 Subject: [PATCH] refactor build method to take either a code buffer or an already instantiated wasm instance --- build/main.cjs | 82 ++++++++++++++++++++-------------------- js/witness_calculator.js | 60 +++++++++++++++++------------ 2 files changed, 77 insertions(+), 65 deletions(-) diff --git a/build/main.cjs b/build/main.cjs index 279af4f..7289ac6 100644 --- a/build/main.cjs +++ b/build/main.cjs @@ -80,9 +80,8 @@ function toArray32(s,size) { /* globals WebAssembly */ -async function builder(code, options) { - options = options || {}; +async function createWASMInstance(code, options) { let memorySize = 32767; let memory; @@ -107,16 +106,7 @@ async function builder(code, options) { let errStr = ""; let msgStr = ""; - // Only circom 2 implements version lookup through exports in the WASM - // We default to `1` and update if we see the `getVersion` export (major version) - // These are updated after the instance is instantiated, assuming the functions are available - let majorVersion = 1; - // After Circom 2.0.7, Blaine added exported functions for getting minor and patch versions - let minorVersion = 0; - // If we can't lookup the patch version, assume the lowest - let patchVersion = 0; - - const instance = await WebAssembly.instantiate(wasmModule, { + let importsObject = { env: { "memory": memory }, @@ -170,15 +160,7 @@ async function builder(code, options) { // In circom 2.0.7, they changed the log() function to allow strings and changed the // output API. This smoothes over the breaking change. - if (majorVersion >= 2 && (minorVersion >= 1 || patchVersion >= 7)) { - // If we've buffered other content, put a space in between the items - if (msgStr !== "") { - msgStr += " "; - } - // Then append the value to the message we are creating - const msg = (ffjavascript.Scalar.fromArray(arr, 0x100000000).toString()); - msgStr += msg; - } else { + { console.log(ffjavascript.Scalar.fromArray(arr, 0x100000000)); } }, @@ -220,7 +202,44 @@ async function builder(code, options) { } } } - }); + }; + + WebAssembly.instantiate(wasmModule, importsObject); + + function getMessage() { + var message = ""; + var c = instance.exports.getMessageChar(); + while ( c != 0 ) { + message += String.fromCharCode(c); + c = instance.exports.getMessageChar(); + } + return message; + } + + function p2str(p) { + const i8 = new Uint8Array(memory.buffer); + + const bytes = []; + + for (let i=0; i8[p+i]>0; i++) bytes.push(i8[p+i]); + + return String.fromCharCode.apply(null, bytes); + } + +} +async function builder(codeOrWasmInstance, options) { + + options = options || {}; + + let instance; + + if (codeOrWasmInstance instanceof Buffer) { + instance = await createWASMInstance(codeOrWasm, options); + } else if (codeOrWasmInstance instanceof WebAssembly.Instance) { + instance = codeOrWasmInstance; + } else { + throw new Error('Invalid argument: argument must be a wasm code Buffer or WebAssembly instance'); + } if (typeof instance.exports.getVersion == 'function') { majorVersion = instance.exports.getVersion(); @@ -251,25 +270,6 @@ async function builder(code, options) { } return wc; - function getMessage() { - var message = ""; - var c = instance.exports.getMessageChar(); - while ( c != 0 ) { - message += String.fromCharCode(c); - c = instance.exports.getMessageChar(); - } - return message; - } - - function p2str(p) { - const i8 = new Uint8Array(memory.buffer); - - const bytes = []; - - for (let i=0; i8[p+i]>0; i++) bytes.push(i8[p+i]); - - return String.fromCharCode.apply(null, bytes); - } } class WitnessCalculatorCircom1 { constructor(memory, instance, sanityCheck) { diff --git a/js/witness_calculator.js b/js/witness_calculator.js index d430082..0aa228a 100644 --- a/js/witness_calculator.js +++ b/js/witness_calculator.js @@ -20,9 +20,8 @@ limitations under the License. import { flatArray, fnvHash, toArray32, normalize } from "./utils.js"; import { Scalar, F1Field } from "ffjavascript"; -export default async function builder(code, options) { - options = options || {}; +async function createWASMInstance(code, options) { let memorySize = 32767; let memory; @@ -162,10 +161,42 @@ export default async function builder(code, options) { } }; - const instance = await WebAssembly.instantiate(wasmModule, { ...importsObject, ...options.additionalWASMImports }); + WebAssembly.instantiate(wasmModule, importsObject); + + function getMessage() { + var message = ""; + var c = instance.exports.getMessageChar(); + while ( c != 0 ) { + message += String.fromCharCode(c); + c = instance.exports.getMessageChar(); + } + return message; + } + + function p2str(p) { + const i8 = new Uint8Array(memory.buffer); + + const bytes = []; + + for (let i=0; i8[p+i]>0; i++) bytes.push(i8[p+i]); - if (options.initializeWasiReactorModuleInstance) { - options.initializeWasiReactorModuleInstance(instance); + return String.fromCharCode.apply(null, bytes); + } + +}; + +export default async function builder(codeOrWasmInstance, options) { + + options = options || {}; + + let instance; + + if (codeOrWasmInstance instanceof Buffer) { + instance = await createWASMInstance(codeOrWasm, options); + } else if (codeOrWasmInstance instanceof WebAssembly.Instance) { + instance = codeOrWasmInstance; + } else { + throw new Error('Invalid argument: argument must be a wasm code Buffer or WebAssembly instance'); } if (typeof instance.exports.getVersion == 'function') { @@ -197,25 +228,6 @@ export default async function builder(code, options) { } return wc; - function getMessage() { - var message = ""; - var c = instance.exports.getMessageChar(); - while ( c != 0 ) { - message += String.fromCharCode(c); - c = instance.exports.getMessageChar(); - } - return message; - } - - function p2str(p) { - const i8 = new Uint8Array(memory.buffer); - - const bytes = []; - - for (let i=0; i8[p+i]>0; i++) bytes.push(i8[p+i]); - - return String.fromCharCode.apply(null, bytes); - } }; class WitnessCalculatorCircom1 {