circomlib/circuits/sha256/sha256compression.circom
circuits/sha256/sha256compression.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 "t1.circom";
22include "t2.circom";
23include "../binsum.circom";
24include "sigmaplus.circom";
25include "sha256compression_function.circom";
26
27
28template Sha256compression() {
29 signal input hin[256];
30 signal input inp[512];
31 signal output out[256];
32 signal a[65][32];
33 signal b[65][32];
34 signal c[65][32];
35 signal d[65][32];
36 signal e[65][32];
37 signal f[65][32];
38 signal g[65][32];
39 signal h[65][32];
40 signal w[64][32];
41
42
43 var outCalc[256] = sha256compression(hin, inp);
44
45 var i;
46 for (i=0; i<256; i++) out[i] <-- outCalc[i];
47
48 component sigmaPlus[48];
49 for (i=0; i<48; i++) sigmaPlus[i] = SigmaPlus();
50
51 component ct_k[64];
52 for (i=0; i<64; i++) ct_k[i] = K(i);
53
54 component t1[64];
55 for (i=0; i<64; i++) t1[i] = T1();
56
57 component t2[64];
58 for (i=0; i<64; i++) t2[i] = T2();
59
60 component suma[64];
61 for (i=0; i<64; i++) suma[i] = BinSum(32, 2);
62
63 component sume[64];
64 for (i=0; i<64; i++) sume[i] = BinSum(32, 2);
65
66 component fsum[8];
67 for (i=0; i<8; i++) fsum[i] = BinSum(32, 2);
68
69 var k;
70 var t;
71
72 for (t=0; t<64; t++) {
73 if (t<16) {
74 for (k=0; k<32; k++) {
75 w[t][k] <== inp[t*32+31-k];
76 }
77 } else {
78 for (k=0; k<32; k++) {
79 sigmaPlus[t-16].in2[k] <== w[t-2][k];
80 sigmaPlus[t-16].in7[k] <== w[t-7][k];
81 sigmaPlus[t-16].in15[k] <== w[t-15][k];
82 sigmaPlus[t-16].in16[k] <== w[t-16][k];
83 }
84
85 for (k=0; k<32; k++) {
86 w[t][k] <== sigmaPlus[t-16].out[k];
87 }
88 }
89 }
90
91 for (k=0; k<32; k++ ) {
92 a[0][k] <== hin[k];
93 b[0][k] <== hin[32*1 + k];
94 c[0][k] <== hin[32*2 + k];
95 d[0][k] <== hin[32*3 + k];
96 e[0][k] <== hin[32*4 + k];
97 f[0][k] <== hin[32*5 + k];
98 g[0][k] <== hin[32*6 + k];
99 h[0][k] <== hin[32*7 + k];
100 }
101
102 for (t = 0; t<64; t++) {
103 for (k=0; k<32; k++) {
104 t1[t].h[k] <== h[t][k];
105 t1[t].e[k] <== e[t][k];
106 t1[t].f[k] <== f[t][k];
107 t1[t].g[k] <== g[t][k];
108 t1[t].k[k] <== ct_k[t].out[k];
109 t1[t].w[k] <== w[t][k];
110
111 t2[t].a[k] <== a[t][k];
112 t2[t].b[k] <== b[t][k];
113 t2[t].c[k] <== c[t][k];
114 }
115
116 for (k=0; k<32; k++) {
117 sume[t].in[0][k] <== d[t][k];
118 sume[t].in[1][k] <== t1[t].out[k];
119
120 suma[t].in[0][k] <== t1[t].out[k];
121 suma[t].in[1][k] <== t2[t].out[k];
122 }
123
124 for (k=0; k<32; k++) {
125 h[t+1][k] <== g[t][k];
126 g[t+1][k] <== f[t][k];
127 f[t+1][k] <== e[t][k];
128 e[t+1][k] <== sume[t].out[k];
129 d[t+1][k] <== c[t][k];
130 c[t+1][k] <== b[t][k];
131 b[t+1][k] <== a[t][k];
132 a[t+1][k] <== suma[t].out[k];
133 }
134 }
135
136 for (k=0; k<32; k++) {
137 fsum[0].in[0][k] <== hin[32*0+k];
138 fsum[0].in[1][k] <== a[64][k];
139 fsum[1].in[0][k] <== hin[32*1+k];
140 fsum[1].in[1][k] <== b[64][k];
141 fsum[2].in[0][k] <== hin[32*2+k];
142 fsum[2].in[1][k] <== c[64][k];
143 fsum[3].in[0][k] <== hin[32*3+k];
144 fsum[3].in[1][k] <== d[64][k];
145 fsum[4].in[0][k] <== hin[32*4+k];
146 fsum[4].in[1][k] <== e[64][k];
147 fsum[5].in[0][k] <== hin[32*5+k];
148 fsum[5].in[1][k] <== f[64][k];
149 fsum[6].in[0][k] <== hin[32*6+k];
150 fsum[6].in[1][k] <== g[64][k];
151 fsum[7].in[0][k] <== hin[32*7+k];
152 fsum[7].in[1][k] <== h[64][k];
153 }
154
155 for (k=0; k<32; k++) {
156 out[31-k] === fsum[0].out[k];
157 out[32+31-k] === fsum[1].out[k];
158 out[64+31-k] === fsum[2].out[k];
159 out[96+31-k] === fsum[3].out[k];
160 out[128+31-k] === fsum[4].out[k];
161 out[160+31-k] === fsum[5].out[k];
162 out[192+31-k] === fsum[6].out[k];
163 out[224+31-k] === fsum[7].out[k];
164 }
165}