Big PFC::hash_to_group(char *buffer, int len) { Big h,p; char s[HASH_LEN]; int i,j; sha256 sh; shs256_init(&sh); for (i=0; i < len; i++) { shs256_process(&sh,buffer[i]); } shs256_hash(&sh,s); p=get_modulus(); h=1; j=0; i=1; forever { h*=256; if (j==HASH_LEN) {h+=i++; j=0;} else h+=(unsigned char)s[j++]; if (h>=p) break; } h%=p; return h % (*ord); }
Big H1(char *string) { // Hash a zero-terminated string to a number < modulus Big h,p; char s[HASH_LEN]; int i,j; sha256 sh; shs256_init(&sh); for (i=0;;i++) { if (string[i]==0) break; shs256_process(&sh,string[i]); } shs256_hash(&sh,s); p=get_modulus(); h=1; j=0; i=1; forever { h*=256; if (j==HASH_LEN) {h+=i++; j=0;} else h+=s[j++]; if (h>=p) break; } h%=p; return h; }
Big H2(ZZn12 x) { // Compress and hash an Fp12 to a big number sha256 sh; ZZn4 u; ZZn2 h,l; Big a,hash,p,xx[4]; char s[HASH_LEN]; int i,j,m; shs256_init(&sh); x.get(u); // compress to single ZZn4 u.get(l,h); xx[0]=real(l); xx[1]=imaginary(l); xx[2]=real(h); xx[3]=imaginary(h); for (i=0;i<4;i++) { a=xx[i]; while (a>0) { m=a%256; shs256_process(&sh,m); a/=256; } } shs256_hash(&sh,s); hash=from_binary(HASH_LEN,s); return hash; }
Big H2(ZZn18 x) { // Compress and hash an Fp18 to a big number sha256 sh; ZZn6 u; ZZn3 h,l; Big a,hash; ZZn xx[6]; char s[HASH_LEN]; int i,j,m; shs256_init(&sh); x.get(u); // compress to single ZZn6 u.get(l,h); l.get(xx[0],xx[1],xx[2]); h.get(xx[3],xx[4],xx[5]); for (i=0;i<6;i++) { a=(Big)xx[i]; while (a>0) { m=a%256; shs256_process(&sh,m); a/=256; } } shs256_hash(&sh,s); hash=from_binary(HASH_LEN,s); return hash; }
Big H1(char *string) { // Hash a zero-terminated string to a number < modulus Big h,p; char s[HASH_LEN]; int i,j,M; sha256 sh; shs256_init(&sh); for (i=0;;i++) { if (string[i]==0) break; shs256_process(&sh,string[i]); } shs256_hash(&sh,s); M=get_mip()->M; h=1; j=0; i=1; forever { h*=256; if (j==HASH_LEN) {h+=i++; j=0;} else h+=s[j++]; if (h>=p) break; } while (bits(h)<M) h*=257; while (bits(h)>=M) h/=2; return h; }
Big H2(GF2m4x x) { sha256 sh; Big a,hash; GF2m xx[4]; char s[HASH_LEN]; int i,j,m; shs256_init(&sh); x.get(xx[0],xx[1],xx[2],xx[3]); for (i=0;i<4;i++) { a=xx[i]; while (a>0) { m=a%256; shs256_process(&sh,m); a/=256; } } shs256_hash(&sh,s); hash=from_binary(HASH_LEN,s); return hash; }
static void fill_pool(csprng *rng) { /* hash down output of RNG to re-fill the pool */ int i; sha256 sh; shs256_init(&sh); for (i=0;i<128;i++) shs256_process(&sh,sbrand(rng)); shs256_hash(&sh,rng->pool); rng->pool_ptr=0; }
void shs256_hash(sha256 *sh,char hash[32]) { /* pad message and finish - supply digest */ int i; mr_unsign32 len0,len1; len0=sh->length[0]; len1=sh->length[1]; shs256_process(sh,PAD); while ((sh->length[0]%512)!=448) shs256_process(sh,ZERO); sh->w[14]=len1; sh->w[15]=len0; shs_transform(sh); for (i=0; i<32; i++) { /* convert to bytes */ hash[i]=(char)((sh->h[i/4]>>(8*(3-i%4))) & 0xffL); } shs256_init(sh); }
Big H2(ZZn2 y) { // Hash and compress an Fp2 to a big number sha256 sh; Big a,h; char s[HASH_LEN]; int m; shs256_init(&sh); y.get(a); while (a>0) { m=a%256; shs256_process(&sh,m); a/=256; } shs256_hash(&sh,s); h=from_binary(HASH_LEN,s); return h; }
void strong_init(csprng *rng,int rawlen,char *raw,mr_unsign32 tod) { /* initialise from at least 128 byte string of raw * * random (keyboard?) input, and 32-bit time-of-day */ int i; mr_unsign32 hash[MR_HASH_BYTES/4]; sha256 sh; rng->pool_ptr=0; for (i=0;i<NK;i++) rng->ira[i]=0; if (rawlen>0) { shs256_init(&sh); for (i=0;i<rawlen;i++) shs256_process(&sh,raw[i]); shs256_hash(&sh,(char *)hash); /* initialise PRNG from distilled randomness */ for (i=0;i<MR_HASH_BYTES/4;i++) sirand(rng,hash[i]); } sirand(rng,tod); fill_pool(rng); }
void PFC::start_hash(void) { shs256_init(&SH); }