123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- /**********************************************************************
- * Copyright (c) 2013, 2014 Pieter Wuille *
- * Distributed under the MIT software license, see the accompanying *
- * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
- **********************************************************************/
- #ifndef _SECP256K1_FIELD_REPR_IMPL_H_
- #define _SECP256K1_FIELD_REPR_IMPL_H_
- #if defined HAVE_CONFIG_H
- #include "libsecp256k1-config.h"
- #endif
- #include <string.h>
- #include "util.h"
- #include "num.h"
- #include "field.h"
- #if defined(USE_ASM_X86_64)
- #include "field_5x52_asm_impl.h"
- #else
- #include "field_5x52_int128_impl.h"
- #endif
- /** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,
- * represented as 5 uint64_t's in base 2^52. The values are allowed to contain >52 each. In particular,
- * each FieldElem has a 'magnitude' associated with it. Internally, a magnitude M means each element
- * is at most M*(2^53-1), except the most significant one, which is limited to M*(2^49-1). All operations
- * accept any input with magnitude at most M, and have different rules for propagating magnitude to their
- * output.
- */
- #ifdef VERIFY
- static void secp256k1_fe_verify(const secp256k1_fe_t *a) {
- const uint64_t *d = a->n;
- int m = a->normalized ? 1 : 2 * a->magnitude, r = 1;
- /* secp256k1 'p' value defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
- r &= (d[0] <= 0xFFFFFFFFFFFFFULL * m);
- r &= (d[1] <= 0xFFFFFFFFFFFFFULL * m);
- r &= (d[2] <= 0xFFFFFFFFFFFFFULL * m);
- r &= (d[3] <= 0xFFFFFFFFFFFFFULL * m);
- r &= (d[4] <= 0x0FFFFFFFFFFFFULL * m);
- r &= (a->magnitude >= 0);
- r &= (a->magnitude <= 2048);
- if (a->normalized) {
- r &= (a->magnitude <= 1);
- if (r && (d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
- r &= (d[0] < 0xFFFFEFFFFFC2FULL);
- }
- }
- VERIFY_CHECK(r == 1);
- }
- #else
- static void secp256k1_fe_verify(const secp256k1_fe_t *a) {
- (void)a;
- }
- #endif
- static void secp256k1_fe_normalize(secp256k1_fe_t *r) {
- uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
- /* Reduce t4 at the start so there will be at most a single carry from the first pass */
- uint64_t m;
- uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
- /* The first pass ensures the magnitude is 1, ... */
- t0 += x * 0x1000003D1ULL;
- t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
- t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
- t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
- t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
- /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
- VERIFY_CHECK(t4 >> 49 == 0);
- /* At most a single final reduction is needed; check if the value is >= the field characteristic */
- x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
- & (t0 >= 0xFFFFEFFFFFC2FULL));
- /* Apply the final reduction (for constant-time behaviour, we do it always) */
- t0 += x * 0x1000003D1ULL;
- t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
- t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
- t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
- t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
- /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
- VERIFY_CHECK(t4 >> 48 == x);
- /* Mask off the possible multiple of 2^256 from the final reduction */
- t4 &= 0x0FFFFFFFFFFFFULL;
- r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
- #ifdef VERIFY
- r->magnitude = 1;
- r->normalized = 1;
- secp256k1_fe_verify(r);
- #endif
- }
- static void secp256k1_fe_normalize_weak(secp256k1_fe_t *r) {
- uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
- /* Reduce t4 at the start so there will be at most a single carry from the first pass */
- uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
- /* The first pass ensures the magnitude is 1, ... */
- t0 += x * 0x1000003D1ULL;
- t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
- t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
- t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
- t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
- /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
- VERIFY_CHECK(t4 >> 49 == 0);
- r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
- #ifdef VERIFY
- r->magnitude = 1;
- secp256k1_fe_verify(r);
- #endif
- }
- static void secp256k1_fe_normalize_var(secp256k1_fe_t *r) {
- uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
- /* Reduce t4 at the start so there will be at most a single carry from the first pass */
- uint64_t m;
- uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
- /* The first pass ensures the magnitude is 1, ... */
- t0 += x * 0x1000003D1ULL;
- t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
- t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
- t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
- t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
- /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
- VERIFY_CHECK(t4 >> 49 == 0);
- /* At most a single final reduction is needed; check if the value is >= the field characteristic */
- x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
- & (t0 >= 0xFFFFEFFFFFC2FULL));
- if (x) {
- t0 += 0x1000003D1ULL;
- t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
- t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
- t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
- t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
- /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
- VERIFY_CHECK(t4 >> 48 == x);
- /* Mask off the possible multiple of 2^256 from the final reduction */
- t4 &= 0x0FFFFFFFFFFFFULL;
- }
- r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
- #ifdef VERIFY
- r->magnitude = 1;
- r->normalized = 1;
- secp256k1_fe_verify(r);
- #endif
- }
- static int secp256k1_fe_normalizes_to_zero(secp256k1_fe_t *r) {
- uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
- /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
- uint64_t z0, z1;
- /* Reduce t4 at the start so there will be at most a single carry from the first pass */
- uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
- /* The first pass ensures the magnitude is 1, ... */
- t0 += x * 0x1000003D1ULL;
- t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL; z0 = t0; z1 = t0 ^ 0x1000003D0ULL;
- t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
- t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
- t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
- z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
- /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
- VERIFY_CHECK(t4 >> 49 == 0);
- return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
- }
- static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe_t *r) {
- uint64_t t0, t1, t2, t3, t4;
- uint64_t z0, z1;
- uint64_t x;
- t0 = r->n[0];
- t4 = r->n[4];
- /* Reduce t4 at the start so there will be at most a single carry from the first pass */
- x = t4 >> 48;
- /* The first pass ensures the magnitude is 1, ... */
- t0 += x * 0x1000003D1ULL;
- /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
- z0 = t0 & 0xFFFFFFFFFFFFFULL;
- z1 = z0 ^ 0x1000003D0ULL;
- /* Fast return path should catch the majority of cases */
- if ((z0 != 0ULL) & (z1 != 0xFFFFFFFFFFFFFULL)) {
- return 0;
- }
- t1 = r->n[1];
- t2 = r->n[2];
- t3 = r->n[3];
- t4 &= 0x0FFFFFFFFFFFFULL;
- t1 += (t0 >> 52); t0 = z0;
- t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
- t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
- t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
- z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
- /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
- VERIFY_CHECK(t4 >> 49 == 0);
- return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
- }
- SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe_t *r, int a) {
- r->n[0] = a;
- r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
- #ifdef VERIFY
- r->magnitude = 1;
- r->normalized = 1;
- secp256k1_fe_verify(r);
- #endif
- }
- SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe_t *a) {
- const uint64_t *t = a->n;
- #ifdef VERIFY
- VERIFY_CHECK(a->normalized);
- secp256k1_fe_verify(a);
- #endif
- return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
- }
- SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe_t *a) {
- #ifdef VERIFY
- VERIFY_CHECK(a->normalized);
- secp256k1_fe_verify(a);
- #endif
- return a->n[0] & 1;
- }
- SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe_t *a) {
- int i;
- #ifdef VERIFY
- a->magnitude = 0;
- a->normalized = 1;
- #endif
- for (i=0; i<5; i++) {
- a->n[i] = 0;
- }
- }
- static int secp256k1_fe_cmp_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b) {
- int i;
- #ifdef VERIFY
- VERIFY_CHECK(a->normalized);
- VERIFY_CHECK(b->normalized);
- secp256k1_fe_verify(a);
- secp256k1_fe_verify(b);
- #endif
- for (i = 4; i >= 0; i--) {
- if (a->n[i] > b->n[i]) {
- return 1;
- }
- if (a->n[i] < b->n[i]) {
- return -1;
- }
- }
- return 0;
- }
- static int secp256k1_fe_set_b32(secp256k1_fe_t *r, const unsigned char *a) {
- int i;
- r->n[0] = r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
- for (i=0; i<32; i++) {
- int j;
- for (j=0; j<2; j++) {
- int limb = (8*i+4*j)/52;
- int shift = (8*i+4*j)%52;
- r->n[limb] |= (uint64_t)((a[31-i] >> (4*j)) & 0xF) << shift;
- }
- }
- if (r->n[4] == 0x0FFFFFFFFFFFFULL && (r->n[3] & r->n[2] & r->n[1]) == 0xFFFFFFFFFFFFFULL && r->n[0] >= 0xFFFFEFFFFFC2FULL) {
- return 0;
- }
- #ifdef VERIFY
- r->magnitude = 1;
- r->normalized = 1;
- secp256k1_fe_verify(r);
- #endif
- return 1;
- }
- /** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
- static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a) {
- int i;
- #ifdef VERIFY
- VERIFY_CHECK(a->normalized);
- secp256k1_fe_verify(a);
- #endif
- for (i=0; i<32; i++) {
- int j;
- int c = 0;
- for (j=0; j<2; j++) {
- int limb = (8*i+4*j)/52;
- int shift = (8*i+4*j)%52;
- c |= ((a->n[limb] >> shift) & 0xF) << (4 * j);
- }
- r[31-i] = c;
- }
- }
- SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m) {
- #ifdef VERIFY
- VERIFY_CHECK(a->magnitude <= m);
- secp256k1_fe_verify(a);
- #endif
- r->n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) - a->n[0];
- r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[1];
- r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[2];
- r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[3];
- r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) - a->n[4];
- #ifdef VERIFY
- r->magnitude = m + 1;
- r->normalized = 0;
- secp256k1_fe_verify(r);
- #endif
- }
- SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe_t *r, int a) {
- r->n[0] *= a;
- r->n[1] *= a;
- r->n[2] *= a;
- r->n[3] *= a;
- r->n[4] *= a;
- #ifdef VERIFY
- r->magnitude *= a;
- r->normalized = 0;
- secp256k1_fe_verify(r);
- #endif
- }
- SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
- #ifdef VERIFY
- secp256k1_fe_verify(a);
- #endif
- r->n[0] += a->n[0];
- r->n[1] += a->n[1];
- r->n[2] += a->n[2];
- r->n[3] += a->n[3];
- r->n[4] += a->n[4];
- #ifdef VERIFY
- r->magnitude += a->magnitude;
- r->normalized = 0;
- secp256k1_fe_verify(r);
- #endif
- }
- static void secp256k1_fe_mul(secp256k1_fe_t *r, const secp256k1_fe_t *a, const secp256k1_fe_t * SECP256K1_RESTRICT b) {
- #ifdef VERIFY
- VERIFY_CHECK(a->magnitude <= 8);
- VERIFY_CHECK(b->magnitude <= 8);
- secp256k1_fe_verify(a);
- secp256k1_fe_verify(b);
- VERIFY_CHECK(r != b);
- #endif
- secp256k1_fe_mul_inner(r->n, a->n, b->n);
- #ifdef VERIFY
- r->magnitude = 1;
- r->normalized = 0;
- secp256k1_fe_verify(r);
- #endif
- }
- static void secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
- #ifdef VERIFY
- VERIFY_CHECK(a->magnitude <= 8);
- secp256k1_fe_verify(a);
- #endif
- secp256k1_fe_sqr_inner(r->n, a->n);
- #ifdef VERIFY
- r->magnitude = 1;
- r->normalized = 0;
- secp256k1_fe_verify(r);
- #endif
- }
- static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k1_fe_t *a, int flag) {
- uint64_t mask0, mask1;
- mask0 = flag + ~((uint64_t)0);
- mask1 = ~mask0;
- r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
- r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
- r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
- r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
- r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
- #ifdef VERIFY
- r->magnitude = (r->magnitude & mask0) | (a->magnitude & mask1);
- r->normalized = (r->normalized & mask0) | (a->normalized & mask1);
- #endif
- }
- static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage_t *r, const secp256k1_fe_storage_t *a, int flag) {
- uint64_t mask0, mask1;
- mask0 = flag + ~((uint64_t)0);
- mask1 = ~mask0;
- r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
- r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
- r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
- r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
- }
- static void secp256k1_fe_to_storage(secp256k1_fe_storage_t *r, const secp256k1_fe_t *a) {
- #ifdef VERIFY
- VERIFY_CHECK(a->normalized);
- #endif
- r->n[0] = a->n[0] | a->n[1] << 52;
- r->n[1] = a->n[1] >> 12 | a->n[2] << 40;
- r->n[2] = a->n[2] >> 24 | a->n[3] << 28;
- r->n[3] = a->n[3] >> 36 | a->n[4] << 16;
- }
- static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe_t *r, const secp256k1_fe_storage_t *a) {
- r->n[0] = a->n[0] & 0xFFFFFFFFFFFFFULL;
- r->n[1] = a->n[0] >> 52 | ((a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
- r->n[2] = a->n[1] >> 40 | ((a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
- r->n[3] = a->n[2] >> 28 | ((a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
- r->n[4] = a->n[3] >> 16;
- #ifdef VERIFY
- r->magnitude = 1;
- r->normalized = 1;
- #endif
- }
- #endif
|