Skip to content

Commit 3ffb067

Browse files
peterdettmanFabcien
authored andcommitted
[SECP256K1] Add safegcd based modular inverse modules
Summary: Comes with full documentation and tests. Partial backport of [[bitcoin-core/secp256k1#831 | secp256k1#831]]: bitcoin-core/secp256k1@8e415ac bitcoin-core/secp256k1@d8a92fc bitcoin-core/secp256k1@151aac0 Depends on D9401. Test Plan: ninja check-secp256k1 Reviewers: #bitcoin_abc, majcosta Reviewed By: #bitcoin_abc, majcosta Differential Revision: https://reviews.bitcoinabc.org/D9402
1 parent 4e1e602 commit 3ffb067

File tree

7 files changed

+2076
-0
lines changed

7 files changed

+2076
-0
lines changed

Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ noinst_HEADERS += src/field_5x52.h
3838
noinst_HEADERS += src/field_5x52_impl.h
3939
noinst_HEADERS += src/field_5x52_int128_impl.h
4040
noinst_HEADERS += src/field_5x52_asm_impl.h
41+
noinst_HEADERS += src/modinv32.h
42+
noinst_HEADERS += src/modinv32_impl.h
43+
noinst_HEADERS += src/modinv64.h
44+
noinst_HEADERS += src/modinv64_impl.h
4145
noinst_HEADERS += src/assumptions.h
4246
noinst_HEADERS += src/util.h
4347
noinst_HEADERS += src/scratch.h

doc/safegcd_implementation.md

Lines changed: 750 additions & 0 deletions
Large diffs are not rendered by default.

src/modinv32.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/***********************************************************************
2+
* Copyright (c) 2020 Peter Dettman *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
**********************************************************************/
6+
7+
#ifndef SECP256K1_MODINV32_H
8+
#define SECP256K1_MODINV32_H
9+
10+
#if defined HAVE_CONFIG_H
11+
#include "libsecp256k1-config.h"
12+
#endif
13+
14+
#include "util.h"
15+
16+
/* A signed 30-bit limb representation of integers.
17+
*
18+
* Its value is sum(v[i] * 2^(30*i), i=0..8). */
19+
typedef struct {
20+
int32_t v[9];
21+
} secp256k1_modinv32_signed30;
22+
23+
typedef struct {
24+
/* The modulus in signed30 notation, must be odd and in [3, 2^256]. */
25+
secp256k1_modinv32_signed30 modulus;
26+
27+
/* modulus^{-1} mod 2^30 */
28+
uint32_t modulus_inv30;
29+
} secp256k1_modinv32_modinfo;
30+
31+
/* Replace x with its modular inverse mod modinfo->modulus. x must be in range [0, modulus).
32+
* If x is zero, the result will be zero as well. If not, the inverse must exist (i.e., the gcd of
33+
* x and modulus must be 1). These rules are automatically satisfied if the modulus is prime.
34+
*
35+
* On output, all of x's limbs will be in [0, 2^30).
36+
*/
37+
static void secp256k1_modinv32_var(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo);
38+
39+
/* Same as secp256k1_modinv32_var, but constant time in x (not in the modulus). */
40+
static void secp256k1_modinv32(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo);
41+
42+
#endif /* SECP256K1_MODINV32_H */

0 commit comments

Comments
 (0)