| 1 |
// 20240502 djb: more explicitly initialize pivots to silence an incorrect compiler warning (tnx simon josefsson) |
| 2 |
// 20221230 djb: add linker lines |
| 3 |
// 20221230 djb: split out of operations.c |
| 4 |
|
| 5 |
// linker define operation_keypair |
| 6 |
// linker use controlbitsfrompermutation genpoly_gen pk_gen |
| 7 |
|
| 8 |
#include "operations.h" |
| 9 |
|
| 10 |
#include "controlbits.h" |
| 11 |
#include "randombytes.h" |
| 12 |
#include "hash.h" |
| 13 |
#include "params.h" |
| 14 |
#include "sk_gen.h" |
| 15 |
#include "pk_gen.h" |
| 16 |
#include "util.h" |
| 17 |
|
| 18 |
#include <stdint.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
void operation_keypair |
| 22 |
( |
| 23 |
unsigned char *pk, |
| 24 |
unsigned char *sk |
| 25 |
) |
| 26 |
{ |
| 27 |
int i; |
| 28 |
unsigned char seed[ 33 ] = {64}; |
| 29 |
unsigned char r[ SYS_N/8 + (1 << GFBITS)*sizeof(uint32_t) + SYS_T*2 + 32 ]; |
| 30 |
unsigned char *rp, *skp; |
| 31 |
uint64_t pivots = 0; |
| 32 |
|
| 33 |
gf f[ SYS_T ]; // element in GF(2^mt) |
| 34 |
gf irr[ SYS_T ]; // Goppa polynomial |
| 35 |
uint32_t perm[ 1 << GFBITS ]; // random permutation as 32-bit integers |
| 36 |
int16_t pi[ 1 << GFBITS ]; // random permutation |
| 37 |
|
| 38 |
randombytes(seed+1, 32); |
| 39 |
|
| 40 |
while (1) |
| 41 |
{ |
| 42 |
rp = &r[ sizeof(r)-32 ]; |
| 43 |
skp = sk; |
| 44 |
|
| 45 |
// expanding and updating the seed |
| 46 |
|
| 47 |
shake(r, sizeof(r), seed, 33); |
| 48 |
memcpy(skp, seed+1, 32); |
| 49 |
skp += 32 + 8; |
| 50 |
memcpy(seed+1, &r[ sizeof(r)-32 ], 32); |
| 51 |
|
| 52 |
// generating irreducible polynomial |
| 53 |
|
| 54 |
rp -= sizeof(f); |
| 55 |
|
| 56 |
for (i = 0; i < SYS_T; i++) |
| 57 |
f[i] = load_gf(rp + i*2); |
| 58 |
|
| 59 |
if (genpoly_gen(irr, f)) |
| 60 |
continue; |
| 61 |
|
| 62 |
for (i = 0; i < SYS_T; i++) |
| 63 |
store_gf(skp + i*2, irr[i]); |
| 64 |
|
| 65 |
skp += IRR_BYTES; |
| 66 |
|
| 67 |
// generating permutation |
| 68 |
|
| 69 |
rp -= sizeof(perm); |
| 70 |
|
| 71 |
for (i = 0; i < (1 << GFBITS); i++) |
| 72 |
perm[i] = load4(rp + i*4); |
| 73 |
|
| 74 |
if (pk_gen(pk, skp - IRR_BYTES, perm, pi, &pivots)) |
| 75 |
continue; |
| 76 |
|
| 77 |
controlbitsfrompermutation(skp, pi, GFBITS, 1 << GFBITS); |
| 78 |
skp += COND_BYTES; |
| 79 |
|
| 80 |
// storing the random string s |
| 81 |
|
| 82 |
rp -= SYS_N/8; |
| 83 |
memcpy(skp, rp, SYS_N/8); |
| 84 |
|
| 85 |
// storing positions of the 32 pivots |
| 86 |
|
| 87 |
store8(sk + 32, pivots); |
| 88 |
|
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
|