-
Notifications
You must be signed in to change notification settings - Fork 327
Open
Description
circom/code_producers/src/wasm_elements/common/witness_calculator.js
Lines 131 to 169 in c133004
async _doCalculateWitness(input, sanityCheck) { | |
//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(normalize(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()}`); | |
} | |
} |
circom/code_producers/src/wasm_elements/common/witness_calculator.js
Lines 208 to 273 in c133004
async calculateWTNSBin(input, sanityCheck) { | |
const buff32 = new Uint32Array(this.witnessSize*this.n32+this.n32+11); | |
const buff = new Uint8Array( buff32.buffer); | |
await this._doCalculateWitness(input, sanityCheck); | |
//"wtns" | |
buff[0] = "w".charCodeAt(0) | |
buff[1] = "t".charCodeAt(0) | |
buff[2] = "n".charCodeAt(0) | |
buff[3] = "s".charCodeAt(0) | |
//version 2 | |
buff32[1] = 2; | |
//number of sections: 2 | |
buff32[2] = 2; | |
//id section 1 | |
buff32[3] = 1; | |
const n8 = this.n32*4; | |
//id section 1 length in 64bytes | |
const idSection1length = 8 + n8; | |
const idSection1lengthHex = idSection1length.toString(16); | |
buff32[4] = parseInt(idSection1lengthHex.slice(0,8), 16); | |
buff32[5] = parseInt(idSection1lengthHex.slice(8,16), 16); | |
//this.n32 | |
buff32[6] = n8; | |
//prime number | |
this.instance.exports.getRawPrime(); | |
var pos = 7; | |
for (let j=0; j<this.n32; j++) { | |
buff32[pos+j] = this.instance.exports.readSharedRWMemory(j); | |
} | |
pos += this.n32; | |
// witness size | |
buff32[pos] = this.witnessSize; | |
pos++; | |
//id section 2 | |
buff32[pos] = 2; | |
pos++; | |
// section 2 length | |
const idSection2length = n8*this.witnessSize; | |
const idSection2lengthHex = idSection2length.toString(16); | |
buff32[pos] = parseInt(idSection2lengthHex.slice(0,8), 16); | |
buff32[pos+1] = parseInt(idSection2lengthHex.slice(8,16), 16); | |
pos += 2; | |
for (let i=0; i<this.witnessSize; i++) { | |
this.instance.exports.getWitness(i); | |
for (let j=0; j<this.n32; j++) { | |
buff32[pos+j] = this.instance.exports.readSharedRWMemory(j); | |
} | |
pos += this.n32; | |
} | |
return buff; | |
} | |
I’m not quite sure if the term ‘promise safety’ is appropriate here, but when we make multiple witness generation requests simultaneously using promises, we might end up with duplicate witness results that don’t match our input values. Is there a way to optimize this issue?
Thank you very much for your time and help.
Metadata
Metadata
Assignees
Labels
No labels