Skip to content

Commit

Permalink
refactor build method to take either a code buffer or an already inst…
Browse files Browse the repository at this point in the history
…antiated wasm instance
  • Loading branch information
martyall committed Jul 1, 2024
1 parent bbfe3ee commit 968aaa5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 65 deletions.
82 changes: 41 additions & 41 deletions build/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
},
Expand Down Expand Up @@ -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));
}
},
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
60 changes: 36 additions & 24 deletions js/witness_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 968aaa5

Please sign in to comment.