87bddb7a3a
b0210a9 Merge pull request #135 ee3eb4b Fix a memory leak and add a number of small tests. 4d879a3 Merge pull request #134 d5e8362 Merge pull request #127 7b92cf6 Merge pull request #132 0bf70a5 Merge pull request #133 29ae131 Make scalar_add_bit test's overflow detection exact 9048def Avoid undefined shift behaviour efb7d4b Use constant-time conditional moves instead of byte slicing d220062 Merge pull request #131 82f9254 Fix typo 601ca04 Merge pull request #129 35399e0 Bugfix: b is restricted, not r c35ff1e Convert lambda splitter to pure scalar code. cc604e9 Avoid division when decomposing scalars ff8746d Add secp256k1_scalar_mul_shift_var bd313f7 Merge pull request #119 276f987 Merge pull request #124 25d125e Merge pull request #126 24b3c65 Add a test case for ECDSA recomputing infinity 32600e5 Add a test for r >= order signature handling 4d4eeea Make secp256k1_fe_mul_inner use the r != property be82e92 Require that r and b are different for field multiplication. 597128d Make num optional 659b554 Make constant initializers independent from num 0af5b47 Merge pull request #120 e2e8a36 Merge pull request #117 c76be9e Remove unused num functions 4285a98 Move lambda-splitting code to scalar. f24041d Switch all EC/ECDSA logic from num to scalar 6794be6 Add scalar splitting functions d1502eb Add secp256k1_scalar_inverse_var which delegates to GMP b5c9ee7 Make test_point_times_order test meaningful again 0b73059 Switch wnaf splitting from num-based to scalar-based 1e6c77c Generalize secp256k1_scalar_get_bits 5213207 Add secp256k1_scalar_add_bit 3c0ae43 Merge pull request #122 6e05287 Do signature recovery/verification with 4 possible recid case e3d692f Explain why no y=0 check is necessary for doubling f7dc1c6 Optimize doubling: secp256k1 has no y=0 point 666d3b5 Merge pull request #121 2a54f9b Correct typo in comment 9d64145 Merge pull request #114 99f0728 Fix secp256k1_num_set_bin handling of 0 d907ebc Add bounds checking to field element setters bb2cd94 Merge pull request #116 665775b Don't split the g factor when not using endomorphism 9431d6b Merge pull request #115 e2274c5 build: osx: attempt to work with homebrew keg-only packages git-subtree-dir: src/secp256k1 git-subtree-split: b0210a95da433e048a11d298efbcc14eb423c95f
298 lines
9.4 KiB
C
298 lines
9.4 KiB
C
/**********************************************************************
|
|
* 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_IMPL_H_
|
|
#define _SECP256K1_FIELD_IMPL_H_
|
|
|
|
#if defined HAVE_CONFIG_H
|
|
#include "libsecp256k1-config.h"
|
|
#endif
|
|
|
|
#include "util.h"
|
|
|
|
#if defined(USE_FIELD_GMP)
|
|
#include "field_gmp_impl.h"
|
|
#elif defined(USE_FIELD_10X26)
|
|
#include "field_10x26_impl.h"
|
|
#elif defined(USE_FIELD_5X52)
|
|
#include "field_5x52_impl.h"
|
|
#else
|
|
#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);
|
|
}
|
|
|
|
static int secp256k1_fe_sqrt(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
|
|
|
|
/** 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);
|
|
secp256k1_fe_mul(&x6, &x6, &x3);
|
|
|
|
secp256k1_fe_t x9 = x6;
|
|
for (int 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);
|
|
secp256k1_fe_mul(&x11, &x11, &x2);
|
|
|
|
secp256k1_fe_t x22 = x11;
|
|
for (int 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);
|
|
secp256k1_fe_mul(&x44, &x44, &x22);
|
|
|
|
secp256k1_fe_t x88 = x44;
|
|
for (int 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);
|
|
secp256k1_fe_mul(&x176, &x176, &x88);
|
|
|
|
secp256k1_fe_t x220 = x176;
|
|
for (int 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);
|
|
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);
|
|
secp256k1_fe_mul(&t1, &t1, &x22);
|
|
for (int 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);
|
|
|
|
/* Check that a square root was actually calculated */
|
|
|
|
secp256k1_fe_sqr(&t1, r);
|
|
secp256k1_fe_negate(&t1, &t1, 1);
|
|
secp256k1_fe_add(&t1, a);
|
|
secp256k1_fe_normalize(&t1);
|
|
return secp256k1_fe_is_zero(&t1);
|
|
}
|
|
|
|
static void secp256k1_fe_inv(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
|
|
|
|
/** 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);
|
|
secp256k1_fe_mul(&x6, &x6, &x3);
|
|
|
|
secp256k1_fe_t x9 = x6;
|
|
for (int 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);
|
|
secp256k1_fe_mul(&x11, &x11, &x2);
|
|
|
|
secp256k1_fe_t x22 = x11;
|
|
for (int 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);
|
|
secp256k1_fe_mul(&x44, &x44, &x22);
|
|
|
|
secp256k1_fe_t x88 = x44;
|
|
for (int 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);
|
|
secp256k1_fe_mul(&x176, &x176, &x88);
|
|
|
|
secp256k1_fe_t x220 = x176;
|
|
for (int 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);
|
|
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);
|
|
secp256k1_fe_mul(&t1, &t1, &x22);
|
|
for (int 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);
|
|
secp256k1_fe_mul(&t1, &t1, &x2);
|
|
for (int j=0; j<2; j++) secp256k1_fe_sqr(&t1, &t1);
|
|
secp256k1_fe_mul(r, a, &t1);
|
|
}
|
|
|
|
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)
|
|
unsigned char b[32];
|
|
secp256k1_fe_t c = *a;
|
|
secp256k1_fe_normalize(&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_get_bin(b, 32, &n);
|
|
VERIFY_CHECK(secp256k1_fe_set_b32(r, b));
|
|
#else
|
|
#error "Please select field inverse implementation"
|
|
#endif
|
|
}
|
|
|
|
static void secp256k1_fe_inv_all(size_t len, secp256k1_fe_t r[len], const secp256k1_fe_t a[len]) {
|
|
if (len < 1)
|
|
return;
|
|
|
|
VERIFY_CHECK((r + len <= a) || (a + len <= r));
|
|
|
|
r[0] = a[0];
|
|
|
|
size_t i = 0;
|
|
while (++i < len) {
|
|
secp256k1_fe_mul(&r[i], &r[i - 1], &a[i]);
|
|
}
|
|
|
|
secp256k1_fe_t u; secp256k1_fe_inv(&u, &r[--i]);
|
|
|
|
while (i > 0) {
|
|
int j = i--;
|
|
secp256k1_fe_mul(&r[j], &r[i], &u);
|
|
secp256k1_fe_mul(&u, &u, &a[j]);
|
|
}
|
|
|
|
r[0] = u;
|
|
}
|
|
|
|
static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t r[len], const secp256k1_fe_t a[len]) {
|
|
if (len < 1)
|
|
return;
|
|
|
|
VERIFY_CHECK((r + len <= a) || (a + len <= r));
|
|
|
|
r[0] = a[0];
|
|
|
|
size_t 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]);
|
|
|
|
while (i > 0) {
|
|
int j = i--;
|
|
secp256k1_fe_mul(&r[j], &r[i], &u);
|
|
secp256k1_fe_mul(&u, &u, &a[j]);
|
|
}
|
|
|
|
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*)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
|