aboutsummaryrefslogtreecommitdiff
path: root/src/field_impl.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2015-03-27 14:03:36 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2015-03-27 14:03:36 -0700
commit9d09322b41776a0d6ecde182f731eff77d0f052b (patch)
tree1c8df9fa9ddfb2b035ac0327fe074b634e458f56 /src/field_impl.h
parent7873633b5752621548b8d38fc175f5a5f2f1e5d6 (diff)
Squashed 'src/secp256k1/' changes from 50cc6ab..1897b8e
1897b8e Merge pull request #229 efc571c Add simple testcases for signing with rfc6979 extra entropy. 1573a10 Add ability to pass extra entropy to rfc6979 3087bc4 Merge pull request #228 d9b9f11 Merge pull request #218 0065a8f Eliminate multiple-returns from secp256k1.c. 354ffa3 Make secp256k1_ec_pubkey_create reject oversized secrets. 27bc131 Silence some warnings from pedantic static analysis tools, improve compatibility with C++. 3b7ea63 Merge pull request #221 f789c5b Merge pull request #215 4bc273b Merge pull request #222 137a8ec Merge pull request #216 7c3771d Disable overlength-strings warnings. 8956111 use 128-bit hex seed 02efd06 Use RFC6979 for test PRNGs ae55e85 Use faster byteswapping and avoid alignment-increasing casts. 443cd4b Get rid of hex format and some binary conversions 0bada0e Merge #214: Improve signing API documentation & specification 8030d7c Improve signing API documentation & specification 7b2fc1c Merge #213: Removed gotos, which are hard to trace and maintain. 11690d3 Removed gotos, which are hard to trace and maintain. 122a1ec Merge pull request #205 035406d Merge pull request #206 2d4cd53 Merge pull request #161 34b898d Additional comments for the testing PRNG and a seeding fix. 6efd6e7 Some comments explaining some of the constants in the code. ffccfd2 x86_64 assembly optimization for scalar_4x64 67cbdf0 Merge pull request #207 039723d Benchmarks for all internal operations 6cc8425 Include a comment on secp256k1_ecdsa_sign explaining low-s. f88343f Merge pull request #203 d61e899 Add group operation counts 2473f17 Merge pull request #202 b5bbce6 Some readme updates, e.g. removal of the GMP field. f0d851e Merge pull request #201 a0ea884 Merge pull request #200 f735446 Convert the rest of the codebase to C89. bf2e1ac Convert tests to C89. (also fixes a use of bare "inline" in field) fc8285f Merge pull request #199 fff412e Merge pull request #197 4be8d6f Centralize the definition of uint128_t and use it uniformly. d9543c9 Switch scalar code to C89. fcc48c4 Remove the non-storage cmov 55422b6 Switch ecmult_gen to use storage types 41f8455 Use group element storage type in EC multiplications e68d720 Add group element storage type ff889f7 Field storage type 7137be8 Merge pull request #196 0768bd5 Get rid of variable-length hex string conversions e84e761 Merge pull request #195 792bcdb Covert several more files to C89. 45cdf44 Merge pull request #193 17db09e Merge pull request #194 402878a fix ifdef/ifndef 25b35c7 Convert field code to strict C89 (+ long long, +__int128) 3627437 C89 nits and dead code removal. a9f350d Merge pull request #191 4732d26 Convert the field/group/ecdsa constant initialization to static consts 19f3e76 Remove unused secp256k1_fe_inner_{start, stop} functions f1ebfe3 Convert the scalar constant initialization to static consts git-subtree-dir: src/secp256k1 git-subtree-split: 1897b8e90bbbdcd919427c9a8ae35b420e919d8f
Diffstat (limited to 'src/field_impl.h')
-rw-r--r--src/field_impl.h187
1 files changed, 63 insertions, 124 deletions
diff --git a/src/field_impl.h b/src/field_impl.h
index 4e2c24aa15..047914cf28 100644
--- a/src/field_impl.h
+++ b/src/field_impl.h
@@ -21,49 +21,6 @@
#error "Please select field implementation"
#endif
-static void secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a) {
- if (*rlen < 65) {
- *rlen = 65;
- return;
- }
- *rlen = 65;
- unsigned char tmp[32];
- secp256k1_fe_t b = *a;
- secp256k1_fe_normalize(&b);
- secp256k1_fe_get_b32(tmp, &b);
- for (int i=0; i<32; i++) {
- static const char *c = "0123456789ABCDEF";
- r[2*i] = c[(tmp[i] >> 4) & 0xF];
- r[2*i+1] = c[(tmp[i]) & 0xF];
- }
- r[64] = 0x00;
-}
-
-static int secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen) {
- unsigned char tmp[32] = {};
- static const int cvt[256] = {0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 1, 2, 3, 4, 5, 6,7,8,9,0,0,0,0,0,0,
- 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
- 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0};
- for (int i=0; i<32; i++) {
- if (alen > i*2)
- tmp[32 - alen/2 + i] = (cvt[(unsigned char)a[2*i]] << 4) + cvt[(unsigned char)a[2*i+1]];
- }
- return secp256k1_fe_set_b32(r, tmp);
-}
-
SECP256K1_INLINE static int secp256k1_fe_equal_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b) {
secp256k1_fe_t na;
secp256k1_fe_negate(&na, a, 1);
@@ -72,62 +29,62 @@ SECP256K1_INLINE static int secp256k1_fe_equal_var(const secp256k1_fe_t *a, cons
}
static int secp256k1_fe_sqrt_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
+ secp256k1_fe_t x2, x3, x6, x9, x11, x22, x44, x88, x176, x220, x223, t1;
+ int j;
/** The binary representation of (p + 1)/4 has 3 blocks of 1s, with lengths in
* { 2, 22, 223 }. Use an addition chain to calculate 2^n - 1 for each block:
* 1, [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
*/
- secp256k1_fe_t x2;
secp256k1_fe_sqr(&x2, a);
secp256k1_fe_mul(&x2, &x2, a);
- secp256k1_fe_t x3;
secp256k1_fe_sqr(&x3, &x2);
secp256k1_fe_mul(&x3, &x3, a);
- secp256k1_fe_t x6 = x3;
- for (int j=0; j<3; j++) secp256k1_fe_sqr(&x6, &x6);
+ x6 = x3;
+ for (j=0; j<3; j++) secp256k1_fe_sqr(&x6, &x6);
secp256k1_fe_mul(&x6, &x6, &x3);
- secp256k1_fe_t x9 = x6;
- for (int j=0; j<3; j++) secp256k1_fe_sqr(&x9, &x9);
+ x9 = x6;
+ for (j=0; j<3; j++) secp256k1_fe_sqr(&x9, &x9);
secp256k1_fe_mul(&x9, &x9, &x3);
- secp256k1_fe_t x11 = x9;
- for (int j=0; j<2; j++) secp256k1_fe_sqr(&x11, &x11);
+ x11 = x9;
+ for (j=0; j<2; j++) secp256k1_fe_sqr(&x11, &x11);
secp256k1_fe_mul(&x11, &x11, &x2);
- secp256k1_fe_t x22 = x11;
- for (int j=0; j<11; j++) secp256k1_fe_sqr(&x22, &x22);
+ x22 = x11;
+ for (j=0; j<11; j++) secp256k1_fe_sqr(&x22, &x22);
secp256k1_fe_mul(&x22, &x22, &x11);
- secp256k1_fe_t x44 = x22;
- for (int j=0; j<22; j++) secp256k1_fe_sqr(&x44, &x44);
+ x44 = x22;
+ for (j=0; j<22; j++) secp256k1_fe_sqr(&x44, &x44);
secp256k1_fe_mul(&x44, &x44, &x22);
- secp256k1_fe_t x88 = x44;
- for (int j=0; j<44; j++) secp256k1_fe_sqr(&x88, &x88);
+ x88 = x44;
+ for (j=0; j<44; j++) secp256k1_fe_sqr(&x88, &x88);
secp256k1_fe_mul(&x88, &x88, &x44);
- secp256k1_fe_t x176 = x88;
- for (int j=0; j<88; j++) secp256k1_fe_sqr(&x176, &x176);
+ x176 = x88;
+ for (j=0; j<88; j++) secp256k1_fe_sqr(&x176, &x176);
secp256k1_fe_mul(&x176, &x176, &x88);
- secp256k1_fe_t x220 = x176;
- for (int j=0; j<44; j++) secp256k1_fe_sqr(&x220, &x220);
+ x220 = x176;
+ for (j=0; j<44; j++) secp256k1_fe_sqr(&x220, &x220);
secp256k1_fe_mul(&x220, &x220, &x44);
- secp256k1_fe_t x223 = x220;
- for (int j=0; j<3; j++) secp256k1_fe_sqr(&x223, &x223);
+ x223 = x220;
+ for (j=0; j<3; j++) secp256k1_fe_sqr(&x223, &x223);
secp256k1_fe_mul(&x223, &x223, &x3);
/* The final result is then assembled using a sliding window over the blocks. */
- secp256k1_fe_t t1 = x223;
- for (int j=0; j<23; j++) secp256k1_fe_sqr(&t1, &t1);
+ t1 = x223;
+ for (j=0; j<23; j++) secp256k1_fe_sqr(&t1, &t1);
secp256k1_fe_mul(&t1, &t1, &x22);
- for (int j=0; j<6; j++) secp256k1_fe_sqr(&t1, &t1);
+ for (j=0; j<6; j++) secp256k1_fe_sqr(&t1, &t1);
secp256k1_fe_mul(&t1, &t1, &x2);
secp256k1_fe_sqr(&t1, &t1);
secp256k1_fe_sqr(r, &t1);
@@ -139,66 +96,66 @@ static int secp256k1_fe_sqrt_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
}
static void secp256k1_fe_inv(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
+ secp256k1_fe_t x2, x3, x6, x9, x11, x22, x44, x88, x176, x220, x223, t1;
+ int j;
/** The binary representation of (p - 2) has 5 blocks of 1s, with lengths in
* { 1, 2, 22, 223 }. Use an addition chain to calculate 2^n - 1 for each block:
* [1], [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
*/
- secp256k1_fe_t x2;
secp256k1_fe_sqr(&x2, a);
secp256k1_fe_mul(&x2, &x2, a);
- secp256k1_fe_t x3;
secp256k1_fe_sqr(&x3, &x2);
secp256k1_fe_mul(&x3, &x3, a);
- secp256k1_fe_t x6 = x3;
- for (int j=0; j<3; j++) secp256k1_fe_sqr(&x6, &x6);
+ x6 = x3;
+ for (j=0; j<3; j++) secp256k1_fe_sqr(&x6, &x6);
secp256k1_fe_mul(&x6, &x6, &x3);
- secp256k1_fe_t x9 = x6;
- for (int j=0; j<3; j++) secp256k1_fe_sqr(&x9, &x9);
+ x9 = x6;
+ for (j=0; j<3; j++) secp256k1_fe_sqr(&x9, &x9);
secp256k1_fe_mul(&x9, &x9, &x3);
- secp256k1_fe_t x11 = x9;
- for (int j=0; j<2; j++) secp256k1_fe_sqr(&x11, &x11);
+ x11 = x9;
+ for (j=0; j<2; j++) secp256k1_fe_sqr(&x11, &x11);
secp256k1_fe_mul(&x11, &x11, &x2);
- secp256k1_fe_t x22 = x11;
- for (int j=0; j<11; j++) secp256k1_fe_sqr(&x22, &x22);
+ x22 = x11;
+ for (j=0; j<11; j++) secp256k1_fe_sqr(&x22, &x22);
secp256k1_fe_mul(&x22, &x22, &x11);
- secp256k1_fe_t x44 = x22;
- for (int j=0; j<22; j++) secp256k1_fe_sqr(&x44, &x44);
+ x44 = x22;
+ for (j=0; j<22; j++) secp256k1_fe_sqr(&x44, &x44);
secp256k1_fe_mul(&x44, &x44, &x22);
- secp256k1_fe_t x88 = x44;
- for (int j=0; j<44; j++) secp256k1_fe_sqr(&x88, &x88);
+ x88 = x44;
+ for (j=0; j<44; j++) secp256k1_fe_sqr(&x88, &x88);
secp256k1_fe_mul(&x88, &x88, &x44);
- secp256k1_fe_t x176 = x88;
- for (int j=0; j<88; j++) secp256k1_fe_sqr(&x176, &x176);
+ x176 = x88;
+ for (j=0; j<88; j++) secp256k1_fe_sqr(&x176, &x176);
secp256k1_fe_mul(&x176, &x176, &x88);
- secp256k1_fe_t x220 = x176;
- for (int j=0; j<44; j++) secp256k1_fe_sqr(&x220, &x220);
+ x220 = x176;
+ for (j=0; j<44; j++) secp256k1_fe_sqr(&x220, &x220);
secp256k1_fe_mul(&x220, &x220, &x44);
- secp256k1_fe_t x223 = x220;
- for (int j=0; j<3; j++) secp256k1_fe_sqr(&x223, &x223);
+ x223 = x220;
+ for (j=0; j<3; j++) secp256k1_fe_sqr(&x223, &x223);
secp256k1_fe_mul(&x223, &x223, &x3);
/* The final result is then assembled using a sliding window over the blocks. */
- secp256k1_fe_t t1 = x223;
- for (int j=0; j<23; j++) secp256k1_fe_sqr(&t1, &t1);
+ t1 = x223;
+ for (j=0; j<23; j++) secp256k1_fe_sqr(&t1, &t1);
secp256k1_fe_mul(&t1, &t1, &x22);
- for (int j=0; j<5; j++) secp256k1_fe_sqr(&t1, &t1);
+ for (j=0; j<5; j++) secp256k1_fe_sqr(&t1, &t1);
secp256k1_fe_mul(&t1, &t1, a);
- for (int j=0; j<3; j++) secp256k1_fe_sqr(&t1, &t1);
+ for (j=0; j<3; j++) secp256k1_fe_sqr(&t1, &t1);
secp256k1_fe_mul(&t1, &t1, &x2);
- for (int j=0; j<2; j++) secp256k1_fe_sqr(&t1, &t1);
+ for (j=0; j<2; j++) secp256k1_fe_sqr(&t1, &t1);
secp256k1_fe_mul(r, a, &t1);
}
@@ -206,13 +163,21 @@ static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
#if defined(USE_FIELD_INV_BUILTIN)
secp256k1_fe_inv(r, a);
#elif defined(USE_FIELD_INV_NUM)
+ secp256k1_num_t n, m;
+ /* secp256k1 field prime, value p defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
+ static const unsigned char prime[32] = {
+ 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
+ 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
+ 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
+ 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F
+ };
unsigned char b[32];
secp256k1_fe_t c = *a;
secp256k1_fe_normalize_var(&c);
secp256k1_fe_get_b32(b, &c);
- secp256k1_num_t n;
secp256k1_num_set_bin(&n, b, 32);
- secp256k1_num_mod_inverse(&n, &n, &secp256k1_fe_consts->p);
+ secp256k1_num_set_bin(&m, prime, 32);
+ secp256k1_num_mod_inverse(&n, &n, &m);
secp256k1_num_get_bin(b, 32, &n);
VERIFY_CHECK(secp256k1_fe_set_b32(r, b));
#else
@@ -220,7 +185,9 @@ static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
#endif
}
-static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t r[len], const secp256k1_fe_t a[len]) {
+static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t *r, const secp256k1_fe_t *a) {
+ secp256k1_fe_t u;
+ size_t i;
if (len < 1)
return;
@@ -228,12 +195,12 @@ static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t r[len], const se
r[0] = a[0];
- size_t i = 0;
+ i = 0;
while (++i < len) {
secp256k1_fe_mul(&r[i], &r[i - 1], &a[i]);
}
- secp256k1_fe_t u; secp256k1_fe_inv_var(&u, &r[--i]);
+ secp256k1_fe_inv_var(&u, &r[--i]);
while (i > 0) {
int j = i--;
@@ -244,32 +211,4 @@ static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t r[len], const se
r[0] = u;
}
-static void secp256k1_fe_start(void) {
-#ifndef USE_NUM_NONE
- static const unsigned char secp256k1_fe_consts_p[] = {
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F
- };
-#endif
- if (secp256k1_fe_consts == NULL) {
- secp256k1_fe_inner_start();
- secp256k1_fe_consts_t *ret = (secp256k1_fe_consts_t*)checked_malloc(sizeof(secp256k1_fe_consts_t));
-#ifndef USE_NUM_NONE
- secp256k1_num_set_bin(&ret->p, secp256k1_fe_consts_p, sizeof(secp256k1_fe_consts_p));
-#endif
- secp256k1_fe_consts = ret;
- }
-}
-
-static void secp256k1_fe_stop(void) {
- if (secp256k1_fe_consts != NULL) {
- secp256k1_fe_consts_t *c = (secp256k1_fe_consts_t*)secp256k1_fe_consts;
- free((void*)c);
- secp256k1_fe_consts = NULL;
- secp256k1_fe_inner_stop();
- }
-}
-
#endif