interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / ng / bench / kernels / sha.c

38 lines · 2413 bytes Raw Blame History
1/* bench oracle: plain FIPS 180-4 SHA-256 in portable C, same 64 MiB input as sha.om */
2#include <stdint.h>
3#include <string.h>
4#include <unistd.h>
5static const uint32_t K[64] = {
60x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
70xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
80xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
90x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
100x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
110xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
120x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
130x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2};
14#define R(x,n) (((x)>>(n))|((x)<<(32-(n))))
15static uint32_t H[8] = {0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19};
16static void blk(const uint8_t *m) {
17 uint32_t w[64], a,b,c,d,e,f,g,h;
18 _Pragma("GCC unroll 64") for (int i = 0; i < 16; i++) w[i] = (uint32_t)m[4*i]<<24 | (uint32_t)m[4*i+1]<<16 | (uint32_t)m[4*i+2]<<8 | m[4*i+3];
19 _Pragma("GCC unroll 64") for (int i = 16; i < 64; i++) {
20 uint32_t s0 = R(w[i-15],7)^R(w[i-15],18)^(w[i-15]>>3), s1 = R(w[i-2],17)^R(w[i-2],19)^(w[i-2]>>10);
21 w[i] = w[i-16]+s0+w[i-7]+s1; }
22 a=H[0];b=H[1];c=H[2];d=H[3];e=H[4];f=H[5];g=H[6];h=H[7];
23 _Pragma("GCC unroll 64") for (int i = 0; i < 64; i++) {
24 uint32_t t1 = h + (R(e,6)^R(e,11)^R(e,25)) + ((e&f)^(~e&g)) + K[i] + w[i];
25 uint32_t t2 = (R(a,2)^R(a,13)^R(a,22)) + ((a&b)^(a&c)^(b&c));
26 h=g;g=f;f=e;e=d+t1;d=c;c=b;b=a;a=t1+t2; }
27 H[0]+=a;H[1]+=b;H[2]+=c;H[3]+=d;H[4]+=e;H[5]+=f;H[6]+=g;H[7]+=h;
28}
29static uint8_t buf[1048576];
30int main(void) {
31 for (uint64_t i = 0; i < sizeof buf; i++) buf[i] = (uint8_t)((i * 31 + 7) & 255);
32 for (int r = 0; r < 64; r++) for (size_t o = 0; o < sizeof buf; o += 64) blk(buf + o);
33 uint8_t p[64] = {0x80}; uint64_t bits = 64ull * sizeof buf * 8;
34 for (int i = 0; i < 8; i++) p[63 - i] = (uint8_t)(bits >> (8 * i));
35 blk(p);
36 uint8_t o[32]; for (int i = 0; i < 8; i++) { o[4*i]=H[i]>>24; o[4*i+1]=H[i]>>16; o[4*i+2]=H[i]>>8; o[4*i+3]=H[i]; }
37 return write(1, o, 32) == 32 ? 0 : 1;
38}