Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update. Support circom 2 1 #8

Merged
merged 6 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 67 additions & 19 deletions js/witness_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,61 @@ module.exports = async function builder(code, options) {

options = options || {};

const wasmModule = await WebAssembly.compile(code);
let wasmModule;
try {
wasmModule = await WebAssembly.compile(code);
} catch (err) {
console.log(err);
console.log("\nTry to run circom --c in order to generate c++ code instead\n");
throw new Error(err);
}

let wc;

let errStr = "";
let msgStr = "";

const instance = await WebAssembly.instantiate(wasmModule, {
runtime: {
exceptionHandler : function(code) {
let errStr;
let err;
if (code == 1) {
errStr= "Signal not found. ";
err = "Signal not found.\n";
} else if (code == 2) {
errStr= "Too many signals set. ";
err = "Too many signals set.\n";
} else if (code == 3) {
errStr= "Signal already set. ";
err = "Signal already set.\n";
} else if (code == 4) {
errStr= "Assert Failed. ";
err = "Assert Failed.\n";
} else if (code == 5) {
errStr= "Not enough memory. ";
err = "Not enough memory.\n";
} else if (code == 6) {
err = "Input signal array access exceeds the size.\n";
} else {
errStr= "Unknown error\n";
err = "Unknown error.\n";
}
// get error message from wasm
errStr += getMessage();
throw new Error(errStr);
throw new Error(err + errStr);
},
showSharedRWMemory: function() {
printErrorMessage : function() {
errStr += getMessage() + "\n";
// console.error(getMessage());
},
writeBufferMessage : function() {
const msg = getMessage();
// Any calls to `log()` will always end with a `\n`, so that's when we print and reset
if (msg === "\n") {
console.log(msgStr);
msgStr = "";
} else {
// If we've buffered other content, put a space in between the items
if (msgStr !== "") {
msgStr += " "
}
// Then append the message to the message we are creating
msgStr += msg;
}
},
showSharedRWMemory : function() {
printSharedRWMemory ();
}

Expand Down Expand Up @@ -66,8 +94,14 @@ module.exports = async function builder(code, options) {
for (let j=0; j<shared_rw_memory_size; j++) {
arr[shared_rw_memory_size-1-j] = instance.exports.readSharedRWMemory(j);
}
console.log(fromArray32(arr));
}

// 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
msgStr += (fromArray32(arr).toString());
}

};

Expand All @@ -79,7 +113,7 @@ class WitnessCalculator {
this.n32 = this.instance.exports.getFieldNumLen32();

this.instance.exports.getRawPrime();
const arr = new Array(this.n32);
const arr = new Uint32Array(this.n32);
for (let i=0; i<this.n32; i++) {
arr[this.n32-1-i] = this.instance.exports.readSharedRWMemory(i);
}
Expand All @@ -98,25 +132,40 @@ class WitnessCalculator {
//input is assumed to be a map from signals to arrays of bigints
this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
const keys = Object.keys(input);
var input_counter = 0;
keys.forEach( (k) => {
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]);
let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
if (signalSize < 0){
throw new Error(`Signal ${k} not found\n`);
}
if (fArr.length < signalSize) {
throw new Error(`Not enough values for input signal ${k}\n`);
}
if (fArr.length > signalSize) {
throw new Error(`Too many values for input signal ${k}\n`);
}
for (let i=0; i<fArr.length; i++) {
const arrFr = toArray32(fArr[i],this.n32)
for (let j=0; j<this.n32; j++) {
const arrFr = toArray32(BigInt(fArr[i])%this.prime,this.n32)
for (let j=0; j<this.n32; j++) {
this.instance.exports.writeSharedRWMemory(j,arrFr[this.n32-1-j]);
}
try {
this.instance.exports.setInputSignal(hMSB, hLSB,i);
input_counter++;
} catch (err) {
// console.log(`After adding signal ${i} of ${k}`)
throw new Error(err);
}
}

});
if (input_counter < this.instance.exports.getInputSize()) {
throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
}
}

async calculateWitness(input, sanityCheck) {
Expand Down Expand Up @@ -225,9 +274,8 @@ class WitnessCalculator {
}


function toArray32(s,size) {
function toArray32(rem,size) {
const res = []; //new Uint32Array(size); //has no unshift
let rem = BigInt(s);
const radix = BigInt(0x100000000);
while (rem) {
res.unshift( Number(rem % radix));
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/handlers/zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (h *ZKHandler) GenerateProof(w http.ResponseWriter, r *http.Request) {
fullProof, err := proof.GenerateZkProof(r.Context(), circuitPath, req.Inputs, h.ProverConfig.UseRapidsnark)

if err != nil {
rest.ErrorJSON(w, r, http.StatusInternalServerError, err, "can't generate proof", 0)
rest.ErrorJSON(w, r, http.StatusInternalServerError, err, "can't generate identifier", 0)
return
}

Expand Down