Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions src/scalar_4x64_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int
#endif
}

static void secp256k1_scalar_sub(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) {
uint128_t t = (uint128_t)a->d[0] - b->d[0];
r->d[0] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64; t |= t << 64;
t += (uint128_t)a->d[1] - b->d[1];
r->d[1] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64; t |= t << 64;
t += (uint128_t)a->d[2] - b->d[2];
r->d[2] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64; t |= t << 64;
t += (uint128_t)a->d[3] - b->d[3];
r->d[3] = t & 0xFFFFFFFFFFFFFFFFULL;
VERIFY_CHECK((t >> 64) == 0);
}

static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
int over;
r->d[0] = (uint64_t)b32[31] | (uint64_t)b32[30] << 8 | (uint64_t)b32[29] << 16 | (uint64_t)b32[28] << 24 | (uint64_t)b32[27] << 32 | (uint64_t)b32[26] << 40 | (uint64_t)b32[25] << 48 | (uint64_t)b32[24] << 56;
Expand Down Expand Up @@ -181,31 +193,6 @@ static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag) {
return 2 * (mask == 0) - 1;
}

static int secp256k1_scalar_complement(secp256k1_scalar *r, const secp256k1_scalar *a) {
uint128_t t = 1;
t += ~a->d[0];
r->d[0] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
t += ~a->d[1];
r->d[1] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
t += ~a->d[2];
r->d[2] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
t += ~a->d[3];
r->d[3] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
return t;
}

static int secp256k1_scalar_binadd(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) {
uint128_t t = (uint128_t)a->d[0] + b->d[0];
r->d[0] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
t += (uint128_t)a->d[1] + b->d[1];
r->d[1] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
t += (uint128_t)a->d[2] + b->d[2];
r->d[2] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
t += (uint128_t)a->d[3] + b->d[3];
r->d[3] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
return t;
}

/* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */

/** Add a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
Expand Down
61 changes: 20 additions & 41 deletions src/scalar_8x32_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int
#endif
}

static void secp256k1_scalar_sub(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) {
uint64_t t = (uint64_t)a->d[0] - b->d[0];
r->d[0] = t & 0xFFFFFFFFULL; t >>= 32; t |= t << 32;
t += (uint64_t)a->d[1] - b->d[1];
r->d[1] = t & 0xFFFFFFFFULL; t >>= 32; t |= t << 32;
t += (uint64_t)a->d[2] - b->d[2];
r->d[2] = t & 0xFFFFFFFFULL; t >>= 32; t |= t << 32;
t += (uint64_t)a->d[3] - b->d[3];
r->d[3] = t & 0xFFFFFFFFULL; t >>= 32; t |= t << 32;
t += (uint64_t)a->d[4] - b->d[4];
r->d[4] = t & 0xFFFFFFFFULL; t >>= 32; t |= t << 32;
t += (uint64_t)a->d[5] - b->d[5];
r->d[5] = t & 0xFFFFFFFFULL; t >>= 32; t |= t << 32;
t += (uint64_t)a->d[6] - b->d[6];
r->d[6] = t & 0xFFFFFFFFULL; t >>= 32; t |= t << 32;
t += (uint64_t)a->d[7] - b->d[7];
r->d[7] = t & 0xFFFFFFFFULL;
VERIFY_CHECK((t >> 32) == 0);
}

static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
int over;
r->d[0] = (uint32_t)b32[31] | (uint32_t)b32[30] << 8 | (uint32_t)b32[29] << 16 | (uint32_t)b32[28] << 24;
Expand Down Expand Up @@ -259,47 +279,6 @@ static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag) {
return 2 * (mask == 0) - 1;
}

static int secp256k1_scalar_complement(secp256k1_scalar *r, const secp256k1_scalar *a) {
uint64_t t = 1;
t += ~a->d[0];
r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
t += ~a->d[1];
r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
t += ~a->d[2];
r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
t += ~a->d[3];
r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
t += ~a->d[4];
r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
t += ~a->d[5];
r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
t += ~a->d[6];
r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
t += ~a->d[7];
r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
return t;
}

static int secp256k1_scalar_binadd(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) {
uint64_t t = (uint64_t)a->d[0] + b->d[0];
r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
t += (uint64_t)a->d[1] + b->d[1];
r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
t += (uint64_t)a->d[2] + b->d[2];
r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
t += (uint64_t)a->d[3] + b->d[3];
r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
t += (uint64_t)a->d[4] + b->d[4];
r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
t += (uint64_t)a->d[5] + b->d[5];
r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
t += (uint64_t)a->d[6] + b->d[6];
r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
t += (uint64_t)a->d[7] + b->d[7];
r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
return t;
}

/* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */

/** Add a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
Expand Down
78 changes: 32 additions & 46 deletions src/scalar_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,25 @@ SECP256K1_INLINE static int secp256k1_scalar_shr_zeros(secp256k1_scalar *r) {
}

static int secp256k1_scalar_eea_inverse(secp256k1_scalar *r, const secp256k1_scalar *n) {
secp256k1_scalar u, v, i, j, acomp, negx;
secp256k1_scalar *a, *b, *x0, *x1, *tmp;
int ka, kb;
secp256k1_scalar u = *n;
secp256k1_scalar v = SECP256K1_SCALAR_CONST(
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364141UL);
secp256k1_scalar i = secp256k1_scalar_one;
/* a = n */
secp256k1_scalar *a = &u;
/* b = p */
secp256k1_scalar *b = &v;
/* x0 = 1 */
secp256k1_scalar *x0 = &i;
/* x1 = 0 */
secp256k1_scalar *x1 = r;
int k;

secp256k1_scalar_set_int(r, 0);

/* zero is not invertible */
if (secp256k1_scalar_is_zero(n)) {
secp256k1_scalar_set_int(r, 0);
return 0;
}
if (secp256k1_scalar_is_zero(a)) return 0;

/**
* The extended euclidian algorithm compute x, y and gcd(a, b) such as
Expand All @@ -327,66 +337,42 @@ static int secp256k1_scalar_eea_inverse(secp256k1_scalar *r, const secp256k1_sca
* So the equation simplify to a*x = 1, and x = a^-1.
*/

/* a = n */
u = *n;
a = &u;

/* Because 2 is not a common factor between a and b, we can detect
* multiples of 2 using the LSB and eliminate them aggressively. */
ka = secp256k1_scalar_shr_zeros(a);

/* b = p - a */
secp256k1_scalar_negate(&v, a);
b = &v;

/* x0 = 1 */
secp256k1_scalar_set_int(&i, 1);
secp256k1_scalar_negate(&negx, &i);
x0 = &i;

/* x1 = 0 */
secp256k1_scalar_set_int(&j, 0);
x1 = &j;

if (secp256k1_scalar_is_one(a)) {
goto done;
}

/* For a and b, we use 2 comlement math and ensure no overflow happens. */
secp256k1_scalar_complement(&acomp, a);
goto bzero;
k = secp256k1_scalar_shr_zeros(a);
secp256k1_scalar_pow2_div(x0, x0, k);

while (!secp256k1_scalar_is_one(a)) {
secp256k1_scalar_complement(&acomp, a);
secp256k1_scalar negx;
secp256k1_scalar_negate(&negx, x0);

VERIFY_CHECK(secp256k1_scalar_cmp_var(b, a) > 0);
do {
secp256k1_scalar_binadd(b, b, &acomp);
secp256k1_scalar_sub(b, b, a);

bzero:
/* We ensure that a and b are odd, so b must be even after subtracting a. */
VERIFY_CHECK(secp256k1_scalar_is_even(b));
kb = secp256k1_scalar_shr_zeros(b);
k = secp256k1_scalar_shr_zeros(b);
secp256k1_scalar_add(x1, x1, &negx);
secp256k1_scalar_pow2_div(x1, x1, kb);
secp256k1_scalar_pow2_div(x1, x1, k);
} while (secp256k1_scalar_cmp_var(b, a) > 0);

/* a and b can never be equal, so if we exited, it is because a > b. */
VERIFY_CHECK(secp256k1_scalar_cmp_var(a, b) > 0);

/* In order to speed things up, we only swap pointers */
tmp = a;
a = b;
b = tmp;
{
secp256k1_scalar *tmp = a;
a = b;
b = tmp;

tmp = x0;
x0 = x1;
x1 = tmp;
tmp = x0;
x0 = x1;
x1 = tmp;
}
}

done:
secp256k1_scalar_pow2_div(r, x0, ka);
*r = *x0;
return 1;
}
#endif
Expand Down