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