From 2f1e74dd386b0b8618cc9c38c59b10f1b94fe854 Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Wed, 12 Sep 2018 11:02:50 +0200 Subject: [PATCH] First iteration sha256 --- circuits/gates.circom | 18 - circuits/sha256/ch.circom | 27 + circuits/sha256/gates.circom | 49 + circuits/sha256/main.circom | 51 +- circuits/sha256/maj.circom | 25 + circuits/sha256/sha256_2.circom | 8 +- circuits/sha256/sha256compression.circom | 92 +- circuits/sha256/sigma.circom | 6 +- circuits/sha256/sigmaplus.circom | 4 +- circuits/sha256/t1.circom | 33 + circuits/sha256/t2.circom | 28 + circuits/sha256/xor3.circom | 25 + package-lock.json | 16 +- test/circuits/out.json | 2371 ++++++++++++++++++++++ test/circuits/sha256_2_test.circom | 15 + test/sha256.js | 18 + 16 files changed, 2658 insertions(+), 128 deletions(-) create mode 100644 circuits/sha256/ch.circom create mode 100644 circuits/sha256/gates.circom create mode 100644 circuits/sha256/maj.circom create mode 100644 circuits/sha256/xor3.circom create mode 100644 test/circuits/out.json create mode 100644 test/circuits/sha256_2_test.circom 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))); + }); });