Skip to content

Commit aa84990

Browse files
rico666gmaxwell
authored andcommitted
Unroll secp256k1_fe_(get|set)_b32 for 5x52.
field_get_b32: min 0.647us / avg 0.666us / max 0.751us field_set_b32: min 0.551us / avg 0.571us / max 0.624us becomes field_get_b32: min 0us / avg 0.0000000477us / max 0.000000238us field_set_b32: min 0us / avg 0.0000000238us / max 0.000000238us (Patch from https://bitcointalk.org/index.php?topic=1740973.0 _get was reversed from the patch because this order appeared somewhat faster in testing.) Signed-off-by: Gregory Maxwell <[email protected]>
1 parent 1199492 commit aa84990

File tree

1 file changed

+66
-21
lines changed

1 file changed

+66
-21
lines changed

src/field_5x52_impl.h

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,40 @@ static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
284284
}
285285

286286
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a) {
287-
int i;
288-
r->n[0] = r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
289-
for (i=0; i<32; i++) {
290-
int j;
291-
for (j=0; j<2; j++) {
292-
int limb = (8*i+4*j)/52;
293-
int shift = (8*i+4*j)%52;
294-
r->n[limb] |= (uint64_t)((a[31-i] >> (4*j)) & 0xF) << shift;
295-
}
296-
}
287+
r->n[0] = (uint64_t)a[31]
288+
| ((uint64_t)a[30] << 8)
289+
| ((uint64_t)a[29] << 16)
290+
| ((uint64_t)a[28] << 24)
291+
| ((uint64_t)a[27] << 32)
292+
| ((uint64_t)a[26] << 40)
293+
| ((uint64_t)(a[25] & 0xF) << 48);
294+
r->n[1] = (uint64_t)((a[25] >> 4) & 0xF)
295+
| ((uint64_t)a[24] << 4)
296+
| ((uint64_t)a[23] << 12)
297+
| ((uint64_t)a[22] << 20)
298+
| ((uint64_t)a[21] << 28)
299+
| ((uint64_t)a[20] << 36)
300+
| ((uint64_t)a[19] << 44);
301+
r->n[2] = (uint64_t)a[18]
302+
| ((uint64_t)a[17] << 8)
303+
| ((uint64_t)a[16] << 16)
304+
| ((uint64_t)a[15] << 24)
305+
| ((uint64_t)a[14] << 32)
306+
| ((uint64_t)a[13] << 40)
307+
| ((uint64_t)(a[12] & 0xF) << 48);
308+
r->n[3] = (uint64_t)((a[12] >> 4) & 0xF)
309+
| ((uint64_t)a[11] << 4)
310+
| ((uint64_t)a[10] << 12)
311+
| ((uint64_t)a[9] << 20)
312+
| ((uint64_t)a[8] << 28)
313+
| ((uint64_t)a[7] << 36)
314+
| ((uint64_t)a[6] << 44);
315+
r->n[4] = (uint64_t)a[5]
316+
| ((uint64_t)a[4] << 8)
317+
| ((uint64_t)a[3] << 16)
318+
| ((uint64_t)a[2] << 24)
319+
| ((uint64_t)a[1] << 32)
320+
| ((uint64_t)a[0] << 40);
297321
if (r->n[4] == 0x0FFFFFFFFFFFFULL && (r->n[3] & r->n[2] & r->n[1]) == 0xFFFFFFFFFFFFFULL && r->n[0] >= 0xFFFFEFFFFFC2FULL) {
298322
return 0;
299323
}
@@ -307,21 +331,42 @@ static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a) {
307331

308332
/** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
309333
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a) {
310-
int i;
311334
#ifdef VERIFY
312335
VERIFY_CHECK(a->normalized);
313336
secp256k1_fe_verify(a);
314337
#endif
315-
for (i=0; i<32; i++) {
316-
int j;
317-
int c = 0;
318-
for (j=0; j<2; j++) {
319-
int limb = (8*i+4*j)/52;
320-
int shift = (8*i+4*j)%52;
321-
c |= ((a->n[limb] >> shift) & 0xF) << (4 * j);
322-
}
323-
r[31-i] = c;
324-
}
338+
r[0] = (a->n[4] >> 40) & 0xFF;
339+
r[1] = (a->n[4] >> 32) & 0xFF;
340+
r[2] = (a->n[4] >> 24) & 0xFF;
341+
r[3] = (a->n[4] >> 16) & 0xFF;
342+
r[4] = (a->n[4] >> 8) & 0xFF;
343+
r[5] = a->n[4] & 0xFF;
344+
r[6] = (a->n[3] >> 44) & 0xFF;
345+
r[7] = (a->n[3] >> 36) & 0xFF;
346+
r[8] = (a->n[3] >> 28) & 0xFF;
347+
r[9] = (a->n[3] >> 20) & 0xFF;
348+
r[10] = (a->n[3] >> 12) & 0xFF;
349+
r[11] = (a->n[3] >> 4) & 0xFF;
350+
r[12] = ((a->n[2] >> 48) & 0xF) | ((a->n[3] & 0xF) << 4);
351+
r[13] = (a->n[2] >> 40) & 0xFF;
352+
r[14] = (a->n[2] >> 32) & 0xFF;
353+
r[15] = (a->n[2] >> 24) & 0xFF;
354+
r[16] = (a->n[2] >> 16) & 0xFF;
355+
r[17] = (a->n[2] >> 8) & 0xFF;
356+
r[18] = a->n[2] & 0xFF;
357+
r[19] = (a->n[1] >> 44) & 0xFF;
358+
r[20] = (a->n[1] >> 36) & 0xFF;
359+
r[21] = (a->n[1] >> 28) & 0xFF;
360+
r[22] = (a->n[1] >> 20) & 0xFF;
361+
r[23] = (a->n[1] >> 12) & 0xFF;
362+
r[24] = (a->n[1] >> 4) & 0xFF;
363+
r[25] = ((a->n[0] >> 48) & 0xF) | ((a->n[1] & 0xF) << 4);
364+
r[26] = (a->n[0] >> 40) & 0xFF;
365+
r[27] = (a->n[0] >> 32) & 0xFF;
366+
r[28] = (a->n[0] >> 24) & 0xFF;
367+
r[29] = (a->n[0] >> 16) & 0xFF;
368+
r[30] = (a->n[0] >> 8) & 0xFF;
369+
r[31] = a->n[0] & 0xFF;
325370
}
326371

327372
SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m) {

0 commit comments

Comments
 (0)