diff --git a/circuits/gates.circom b/circuits/gates.circom index b81ed3d..0eccda0 100644 --- a/circuits/gates.circom +++ b/circuits/gates.circom @@ -46,22 +46,4 @@ template NOR() { out <== a*b + 1 - a - b; } -template Xor3(n) { - signal input a[n]; - signal input b[n]; - signal input c[n]; - signal output out[n]; - component xor1[n] = XOR(); - component xor2[n] = XOR(); - - for (var k=0; k + +out = a*(b-c) + c + +*/ + +template Ch(n) { + signal input a[n]; + signal input b[n]; + signal input c[n]; + signal output out[n]; + + for (var k=0; k + +out = a*b + a*c + b*c - 2*a*b*c => + +out = a*( b + c - 2*b*c ) + b*c => + +mid = b*c +out = a*( b + c - 2*mid ) + mid + +*/ + +template Maj(n) { + signal input a[n]; + signal input b[n]; + signal input c[n]; + signal output out[n]; + signal mid[n]; + + for (var k=0; k + +out = a+b+c - 2*a*b - 2*a*c - 2*b*c + 4*a*b*c => + +out = a*( 1 - 2*b - 2*c + 4*b*c ) + b + c - 2*b*c => + +mid = b*c +out = a*( 1 - 2*b -2*c + 4*mid ) + b + c - 2 * mid + +*/ + +template Xor3(n) { + signal input a[n]; + signal input b[n]; + signal input c[n]; + signal output out[n]; + signal mid[n]; + + for (var k=0; k { assert(witness[0].equals(zkSnark.bigInt(1))); assert(witness[1].equals(zkSnark.bigInt("333"))); }); + it("Should calculate a hash", async () => { + const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom")); + const circuit = new zkSnark.Circuit(cirDef); + + const witness = circuit.calculateWitness({ "a": "1", "b": "2" }); + + const b = new Buffer.alloc(432); + b[115] = 1; + b[431] = 2; + + const hash = crypto.createHash("sha256") + .update(b) + .digest("hex"); + const r = "0x" + hash.slice(40); + + assert(witness[1].equals(zkSnark.bigInt(r))); + }); });