Skip to content

Commit

Permalink
Fix memory for WitnessCalculatorCircom1. Added checks for loading cod…
Browse files Browse the repository at this point in the history
…e from wasm instance.
  • Loading branch information
OBrezhniev committed Sep 7, 2024
1 parent 02276e7 commit fe9d0af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
17 changes: 12 additions & 5 deletions build/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function toArray32(s,size) {
async function builder(code, options) {
let instance;
let wc;
let memory;

options = options || {};

// 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)
Expand All @@ -93,13 +96,13 @@ async function builder(code, options) {
// If we can't lookup the patch version, assume the lowest
let patchVersion = 0;

let codeIsWebAssemblyInstance = false;

if (code instanceof WebAssembly.Instance) {
instance = code;
codeIsWebAssemblyInstance = true;
} else {
options = options || {};

let memorySize = 32767;
let memory;
let memoryAllocated = false;
while (!memoryAllocated){
try{
Expand Down Expand Up @@ -249,9 +252,13 @@ async function builder(code, options) {
// We explicitly check for major version 2 in case there's a circom v3 in the future
if (majorVersion === 2) {
wc = new WitnessCalculatorCircom2(instance, sanityCheck);
} else {
// TODO: Maybe we want to check for the explicit version 1 before choosing this?
} else if (majorVersion === 1) {
if (codeIsWebAssemblyInstance) {
throw new Error('Loading code from WebAssembly instance is not supported for circom version 1');
}
wc = new WitnessCalculatorCircom1(memory, instance, sanityCheck);
} else {
throw new Error(`Unsupported circom version: ${majorVersion}`);
}
return wc;

Expand Down
17 changes: 12 additions & 5 deletions js/witness_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { Scalar, F1Field } from "ffjavascript";
export default async function builder(code, options) {
let instance;
let wc;
let memory;

options = options || {};

// 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)
Expand All @@ -33,13 +36,13 @@ export default async function builder(code, options) {
// If we can't lookup the patch version, assume the lowest
let patchVersion = 0;

let codeIsWebAssemblyInstance = false;

if (code instanceof WebAssembly.Instance) {
instance = code;
codeIsWebAssemblyInstance = true;
} else {
options = options || {};

let memorySize = 32767;
let memory;
let memoryAllocated = false;
while (!memoryAllocated){
try{
Expand Down Expand Up @@ -189,9 +192,13 @@ export default async function builder(code, options) {
// We explicitly check for major version 2 in case there's a circom v3 in the future
if (majorVersion === 2) {
wc = new WitnessCalculatorCircom2(instance, sanityCheck);
} else {
// TODO: Maybe we want to check for the explicit version 1 before choosing this?
} else if (majorVersion === 1) {
if (codeIsWebAssemblyInstance) {
throw new Error('Loading code from WebAssembly instance is not supported for circom version 1');
}
wc = new WitnessCalculatorCircom1(memory, instance, sanityCheck);
} else {
throw new Error(`Unsupported circom version: ${majorVersion}`);
}
return wc;

Expand Down

0 comments on commit fe9d0af

Please sign in to comment.