makefile: support out of tree builds
[mkp224o.git] / worker_batch.inc.h
blobc838e28c396acb33fa81772f25b4ddc32f642427
2 void *worker_batch(void *task)
4 union pubonionunion pubonion;
5 u8 * const pk = &pubonion.raw[PKPREFIX_SIZE];
6 u8 secret[SKPREFIX_SIZE + SECRET_LEN];
7 u8 * const sk = &secret[SKPREFIX_SIZE];
8 u8 seed[SEED_LEN];
9 u8 hashsrc[checksumstrlen + PUBLIC_LEN + 1];
10 u8 wpk[PUBLIC_LEN + 1];
11 ge_p3 ge_public;
12 char *sname;
14 ge_p3 ge_batch[BATCHNUM];
15 fe *(batchgez)[BATCHNUM];
16 fe tmp_batch[BATCHNUM];
17 bytes32 pk_batch[BATCHNUM];
19 size_t counter;
20 size_t i;
22 #ifdef STATISTICS
23 struct statstruct *st = (struct statstruct *)task;
24 #endif
26 for (size_t b = 0;b < BATCHNUM;++b)
27 batchgez[b] = &GEZ(ge_batch[b]);
29 PREFILTER
31 memcpy(secret,skprefix,SKPREFIX_SIZE);
32 wpk[PUBLIC_LEN] = 0;
33 memset(&pubonion,0,sizeof(pubonion));
34 memcpy(pubonion.raw,pkprefix,PKPREFIX_SIZE);
35 // write version later as it will be overwritten by hash
36 memcpy(hashsrc,checksumstr,checksumstrlen);
37 hashsrc[checksumstrlen + PUBLIC_LEN] = 0x03; // version
39 sname = makesname();
41 initseed:
42 #ifdef STATISTICS
43 ++st->numrestart.v;
44 #endif
45 randombytes(seed,sizeof(seed));
46 ed25519_seckey_expand(sk,seed);
48 ge_scalarmult_base(&ge_public,sk);
50 for (counter = 0;counter < SIZE_MAX-(8*BATCHNUM);counter += 8*BATCHNUM) {
51 ge_p1p1 sum;
53 if (unlikely(endwork))
54 goto end;
56 for (size_t b = 0;b < BATCHNUM;++b) {
57 ge_batch[b] = ge_public;
58 ge_add(&sum,&ge_public,&ge_eightpoint);
59 ge_p1p1_to_p3(&ge_public,&sum);
61 // NOTE: leaves unfinished
62 ge_p3_batchtobytes_destructive_1(pk_batch,ge_batch,batchgez,tmp_batch,BATCHNUM);
64 #ifdef STATISTICS
65 st->numcalc.v += BATCHNUM;
66 #endif
68 for (size_t b = 0;b < BATCHNUM;++b) {
69 DOFILTER(i,pk_batch[b],{
70 if (numwords > 1) {
71 shiftpk(wpk,pk_batch[b],filter_len(i));
72 size_t j;
73 for (int w = 1;;) {
74 DOFILTER(j,wpk,goto secondfind);
75 goto next;
76 secondfind:
77 if (++w >= numwords)
78 break;
79 shiftpk(wpk,wpk,filter_len(j));
82 // found!
83 // finish it up
84 ge_p3_batchtobytes_destructive_finish(pk_batch[b],&ge_batch[b]);
85 // copy public key
86 memcpy(pk,pk_batch[b],PUBLIC_LEN);
87 // update secret key with counter
88 addsztoscalar32(sk,counter + (b * 8));
89 // sanity check
90 if ((sk[0] & 248) != sk[0] || ((sk[31] & 63) | 64) != sk[31])
91 goto initseed;
93 ADDNUMSUCCESS;
95 // calc checksum
96 memcpy(&hashsrc[checksumstrlen],pk,PUBLIC_LEN);
97 FIPS202_SHA3_256(hashsrc,sizeof(hashsrc),&pk[PUBLIC_LEN]);
98 // version byte
99 pk[PUBLIC_LEN + 2] = 0x03;
100 // full name
101 strcpy(base32_to(&sname[direndpos],pk,PUBONION_LEN),".onion");
102 onionready(sname,secret,pubonion.raw);
103 pk[PUBLIC_LEN] = 0; // what is this for?
104 // don't reuse same seed
105 goto initseed;
107 next:
111 goto initseed;
113 end:
114 free(sname);
115 POSTFILTER
116 sodium_memzero(secret,sizeof(secret));
117 sodium_memzero(seed,sizeof(seed));
118 return 0;