arnaucube
arnaucube/circomPublic Self-hosted repository
circom/circuits/sha256/sha256compression.circom
circuits/sha256/sha256compression.circomRaw
  1
  2include "constants.circom";
  3include "t1.circom";
  4include "t2.circom";
  5include "binsum.circom";
  6include "sigmaplus.circom";
  7
  8template Sha256compression() {
  9    signal input inp[512];
 10    signal output out[256];
 11    signal a[64][32];
 12    signal b[64][32];
 13    signal c[64][32];
 14    signal d[64][32];
 15    signal e[64][32];
 16    signal f[64][32];
 17    signal g[64][32];
 18    signal h[64][32];
 19    signal w[64][32];
 20
 21    var i;
 22
 23    component sigmaPlus[48] = SigmaPlus();
 24
 25    component ct_k[64];
 26    for (i=0; i<64; i++) ct_k[i] = K(i);
 27
 28    component ha0 = H0(0);
 29    component hb0 = H0(1);
 30    component hc0 = H0(2);
 31    component hd0 = H0(3);
 32    component he0 = H0(4);
 33    component hf0 = H0(5);
 34    component hg0 = H0(6);
 35    component hh0 = H0(7);
 36
 37    component t1[64] = T1();
 38    component t2[64] = T2();
 39
 40    component suma[64] = Sum(32, 2);
 41    component sume[64] = Sum(32, 2);
 42    component fsum[8]  = Sum(32, 2);
 43
 44    var k;
 45    var t;
 46
 47    for (t=0; t<64; t++) {
 48        if (t<16) {
 49            for (k=0; k<32; k++) {
 50                w[t][k] <== inp[t*32+k];
 51            }
 52        } else {
 53            for (k=0; k<32; k++) {
 54                sigmaPlus[t-16].in2[k] <== w[t-2][k];
 55                sigmaPlus[t-16].in7[k] <== w[t-2][k];
 56                sigmaPlus[t-16].in15[k] <== w[t-15][k];
 57                sigmaPlus[t-16].in16[k] <== w[t-16][k];
 58                w[t][k] <== sigmaPlus[t-16].out[k];
 59            }
 60        }
 61    }
 62
 63    for (k=0; k<32; k++ ) {
 64        a[0][k] <== ha0.out[k]
 65        b[0][k] <== hb0.out[k]
 66        c[0][k] <== hc0.out[k]
 67        d[0][k] <== hd0.out[k]
 68        e[0][k] <== he0.out[k]
 69        f[0][k] <== hf0.out[k]
 70        g[0][k] <== hg0.out[k]
 71        h[0][k] <== hh0.out[k]
 72    }
 73
 74    for (t = 0; t<63; t++) {
 75        for (k=0; k<32; k++) {
 76            t1[t].h[k] <== h[k];
 77            t1[t].e[k] <== e[k];
 78            t1[t].f[k] <== f[k];
 79            t1[t].g[k] <== g[k];
 80            t1[t].k[k] <== ct_K[t].out[k];
 81            t1[t].w[k] <== w[t][k];
 82
 83            t2[t].a[k] <== a[k];
 84            t2[t].b[k] <== b[k];
 85            t2[t].c[k] <== c[k];
 86        }
 87
 88        for (k=0; k<32; k++) {
 89            sume[t].a[k] <== d[k];
 90            sume[t].b[k] <== t1[t].out[k];
 91
 92            suma[t].a[k] <== t1[t].out[k];
 93            suma[t].b[k] <== t2[t].out[k];
 94        }
 95
 96        for (k=0; k<32; k++) {
 97            h[t+1] <== g[t];
 98            g[t+1] <== f[t];
 99            f[t+1] <== e[t];
100            e[t+1] <== sume[t].out[k];
101            d[t+1] <== c[t];
102            c[t+1] <== b[t];
103            b[t+1] <== a[t];
104            a[t+1] <== suma[t].out[k];
105        }
106    }
107
108    for (k=0; k<32; k++) {
109        fsum[0].in[0][k] <==  ha0.out[k];
110        fsum[0].in[1][k] <==  a[64][k];
111        fsum[1].in[0][k] <==  hb0.out[k];
112        fsum[1].in[1][k] <==  b[64][k];
113        fsum[2].in[0][k] <==  hc0.out[k];
114        fsum[2].in[1][k] <==  c[64][k];
115        fsum[3].in[0][k] <==  hd0.out[k];
116        fsum[3].in[1][k] <==  d[64][k];
117        fsum[4].in[0][k] <==  he0.out[k];
118        fsum[4].in[1][k] <==  e[64][k];
119        fsum[5].in[0][k] <==  hf0.out[k];
120        fsum[5].in[1][k] <==  f[64][k];
121        fsum[6].in[0][k] <==  hg0.out[k];
122        fsum[6].in[1][k] <==  g[64][k];
123        fsum[7].in[0][k] <==  hh0.out[k];
124        fsum[7].in[1][k] <==  h[64][k];
125    }
126
127    for (k=0; k<32; k++) {
128        out[k]     <== fsum[0].out[k];
129        out[32+k]  <== fsum[1].out[k];
130        out[64+k]  <== fsum[2].out[k];
131        out[96+k]  <== fsum[3].out[k];
132        out[128+k] <== fsum[4].out[k];
133        out[160+k] <== fsum[5].out[k];
134        out[192+k] <== fsum[6].out[k];
135        out[224+k] <== fsum[7].out[k];
136    }
137}