Skip to content

Commit 600c5ad

Browse files
committed
clean up in-comment Sage code (refer to secp256k1_params.sage, update to Python3)
Some of the C source files contain contain in-comment Sage code calculating secp256k1 parameters that are already defined in the file secp256k1_params.sage. Replace that by a corresponding load instruction and access the necessary variables. In ecdsa_impl.h, update the comment to use a one-line shell command calling sage to get the values. The remaining code (test `test_add_neg_y_diff_x` in tests.c) is updated to work with a current version based on Python3 (Sage 9.0+, see https://wiki.sagemath.org/Python3-Switch). The latter can be seen as a small follow-up to PR #849 (commit 13c88ef).
1 parent 60556c9 commit 600c5ad

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

src/ecdsa_impl.h

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,8 @@
1616
#include "ecdsa.h"
1717

1818
/** Group order for secp256k1 defined as 'n' in "Standards for Efficient Cryptography" (SEC2) 2.7.1
19-
* sage: for t in xrange(1023, -1, -1):
20-
* .. p = 2**256 - 2**32 - t
21-
* .. if p.is_prime():
22-
* .. print '%x'%p
23-
* .. break
24-
* 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'
25-
* sage: a = 0
26-
* sage: b = 7
27-
* sage: F = FiniteField (p)
28-
* sage: '%x' % (EllipticCurve ([F (a), F (b)]).order())
29-
* 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'
19+
* $ sage -c 'load("secp256k1_params.sage"); print(hex(N))'
20+
* 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
3021
*/
3122
static const secp256k1_fe secp256k1_ecdsa_const_order_as_fe = SECP256K1_FE_CONST(
3223
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
@@ -35,12 +26,8 @@ static const secp256k1_fe secp256k1_ecdsa_const_order_as_fe = SECP256K1_FE_CONST
3526

3627
/** Difference between field and order, values 'p' and 'n' values defined in
3728
* "Standards for Efficient Cryptography" (SEC2) 2.7.1.
38-
* sage: p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
39-
* sage: a = 0
40-
* sage: b = 7
41-
* sage: F = FiniteField (p)
42-
* sage: '%x' % (p - EllipticCurve ([F (a), F (b)]).order())
43-
* '14551231950b75fc4402da1722fc9baee'
29+
* $ sage -c 'load("secp256k1_params.sage"); print(hex(P-N))'
30+
* 0x14551231950b75fc4402da1722fc9baee
4431
*/
4532
static const secp256k1_fe secp256k1_ecdsa_const_p_minus_order = SECP256K1_FE_CONST(
4633
0, 0, 0, 1, 0x45512319UL, 0x50B75FC4UL, 0x402DA172UL, 0x2FC9BAEEUL

src/tests.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4009,22 +4009,15 @@ static void test_add_neg_y_diff_x(void) {
40094009
* which this test is a regression test for.
40104010
*
40114011
* These points were generated in sage as
4012-
* # secp256k1 params
4013-
* F = FiniteField (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F)
4014-
* C = EllipticCurve ([F (0), F (7)])
4015-
* G = C.lift_x(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798)
4016-
* N = FiniteField(G.order())
40174012
*
4018-
* # endomorphism values (lambda is 1^{1/3} in N, beta is 1^{1/3} in F)
4019-
* x = polygen(N)
4020-
* lam = (1 - x^3).roots()[1][0]
4013+
* load("secp256k1_params.sage")
40214014
*
40224015
* # random "bad pair"
40234016
* P = C.random_element()
4024-
* Q = -int(lam) * P
4025-
* print " P: %x %x" % P.xy()
4026-
* print " Q: %x %x" % Q.xy()
4027-
* print "P + Q: %x %x" % (P + Q).xy()
4017+
* Q = -int(LAMBDA) * P
4018+
* print(" P: %x %x" % P.xy())
4019+
* print(" Q: %x %x" % Q.xy())
4020+
* print("P + Q: %x %x" % (P + Q).xy())
40284021
*/
40294022
secp256k1_gej aj = SECP256K1_GEJ_CONST(
40304023
0x8d24cd95, 0x0a355af1, 0x3c543505, 0x44238d30,

0 commit comments

Comments
 (0)