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