circomlib/circuits/sha256/sha256_2.circom
circuits/sha256/sha256_2.circomRaw
1/*
2 Copyright 2018 0KIMS association.
3
4 This file is part of circom (Zero Knowledge Circuit Compiler).
5
6 circom is a free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 circom is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with circom. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20include "constants.circom";
21include "sha256compression.circom";
22include "../bitify.circom"
23
24template Sha256_2() {
25 signal input a;
26 signal input b;
27 signal output out;
28
29 var i;
30 var k;
31
32 component bits2num = Bits2Num(216);
33 component num2bits[2];
34
35 num2bits[0] = Num2Bits(216);
36 num2bits[1] = Num2Bits(216);
37
38 num2bits[0].in <== a;
39 num2bits[1].in <== b;
40
41
42 component sha256compression = Sha256compression() ;
43
44 component ha0 = H(0);
45 component hb0 = H(1);
46 component hc0 = H(2);
47 component hd0 = H(3);
48 component he0 = H(4);
49 component hf0 = H(5);
50 component hg0 = H(6);
51 component hh0 = H(7);
52
53 for (k=0; k<32; k++ ) {
54 sha256compression.hin[0*32+k] <== ha0.out[k];
55 sha256compression.hin[1*32+k] <== hb0.out[k];
56 sha256compression.hin[2*32+k] <== hc0.out[k];
57 sha256compression.hin[3*32+k] <== hd0.out[k];
58 sha256compression.hin[4*32+k] <== he0.out[k];
59 sha256compression.hin[5*32+k] <== hf0.out[k];
60 sha256compression.hin[6*32+k] <== hg0.out[k];
61 sha256compression.hin[7*32+k] <== hh0.out[k];
62 }
63
64 for (i=0; i<216; i++) {
65 sha256compression.inp[i] <== num2bits[0].out[215-i];
66 sha256compression.inp[i+216] <== num2bits[1].out[215-i];
67 }
68
69 sha256compression.inp[432] <== 1;
70
71 for (i=433; i<503; i++) {
72 sha256compression.inp[i] <== 0;
73 }
74
75 sha256compression.inp[503] <== 1;
76 sha256compression.inp[504] <== 1;
77 sha256compression.inp[505] <== 0;
78 sha256compression.inp[506] <== 1;
79 sha256compression.inp[507] <== 1;
80 sha256compression.inp[508] <== 0;
81 sha256compression.inp[509] <== 0;
82 sha256compression.inp[510] <== 0;
83 sha256compression.inp[511] <== 0;
84
85 for (i=0; i<216; i++) {
86 bits2num.in[i] <== sha256compression.out[255-i];
87 }
88
89 out <== bits2num.out;
90}