Skip to content

Commit 53bc63f

Browse files
committed
Merge #245: scalar: Remove unused secp256k1_scalar_chacha20
860360e scalar: Remove unused secp256k1_scalar_chacha20 (Tim Ruffing) 3970a72 rangeproof: Use util functions for writing big endian (Tim Ruffing) Pull request description: ACKs for top commit: jonasnick: ACK 860360e Tree-SHA512: 0e74fe6f4aa5ab12eea1dece3b43aa2f98c188096ab4bb59324f86f1c88d4f13fc81b9fe852701682a9c4c1fccdc17e3bdbc1d3df7cc341822ac022772037c29
2 parents afe7e64 + 860360e commit 53bc63f

File tree

7 files changed

+6
-337
lines changed

7 files changed

+6
-337
lines changed

src/modules/rangeproof/borromean_impl.h

+6-12
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,18 @@
2020
#include <limits.h>
2121
#include <string.h>
2222

23-
#if defined(SECP256K1_BIG_ENDIAN)
24-
#define BE32(x) (x)
25-
#elif defined(SECP256K1_LITTLE_ENDIAN)
26-
#define BE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
27-
#endif
28-
2923
SECP256K1_INLINE static void secp256k1_borromean_hash(unsigned char *hash, const unsigned char *m, size_t mlen, const unsigned char *e, size_t elen,
3024
size_t ridx, size_t eidx) {
31-
uint32_t ring;
32-
uint32_t epos;
25+
unsigned char ring[4];
26+
unsigned char epos[4];
3327
secp256k1_sha256 sha256_en;
3428
secp256k1_sha256_initialize(&sha256_en);
35-
ring = BE32((uint32_t)ridx);
36-
epos = BE32((uint32_t)eidx);
29+
secp256k1_write_be32(ring, (uint32_t)ridx);
30+
secp256k1_write_be32(epos, (uint32_t)eidx);
3731
secp256k1_sha256_write(&sha256_en, e, elen);
3832
secp256k1_sha256_write(&sha256_en, m, mlen);
39-
secp256k1_sha256_write(&sha256_en, (unsigned char*)&ring, 4);
40-
secp256k1_sha256_write(&sha256_en, (unsigned char*)&epos, 4);
33+
secp256k1_sha256_write(&sha256_en, ring, 4);
34+
secp256k1_sha256_write(&sha256_en, epos, 4);
4135
secp256k1_sha256_finalize(&sha256_en, hash);
4236
}
4337

src/scalar.h

-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,4 @@ static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r, const secp256k1_
108108
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
109109
static void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag);
110110

111-
/** Generate two scalars from a 32-byte seed and an integer using the chacha20 stream cipher */
112-
static void secp256k1_scalar_chacha20(secp256k1_scalar *r1, secp256k1_scalar *r2, const unsigned char *seed, uint64_t idx);
113-
114111
#endif /* SECP256K1_SCALAR_H */

src/scalar_4x64_impl.h

-87
Original file line numberDiff line numberDiff line change
@@ -970,93 +970,6 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
970970
r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
971971
}
972972

973-
#define ROTL32(x,n) ((x) << (n) | (x) >> (32-(n)))
974-
#define QUARTERROUND(a,b,c,d) \
975-
a += b; d = ROTL32(d ^ a, 16); \
976-
c += d; b = ROTL32(b ^ c, 12); \
977-
a += b; d = ROTL32(d ^ a, 8); \
978-
c += d; b = ROTL32(b ^ c, 7);
979-
980-
#if defined(SECP256K1_BIG_ENDIAN)
981-
#define LE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
982-
#elif defined(SECP256K1_LITTLE_ENDIAN)
983-
#define LE32(p) (p)
984-
#endif
985-
986-
static void secp256k1_scalar_chacha20(secp256k1_scalar *r1, secp256k1_scalar *r2, const unsigned char *seed, uint64_t idx) {
987-
size_t n;
988-
size_t over_count = 0;
989-
uint32_t seed32[8];
990-
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
991-
int over1, over2;
992-
993-
memcpy((void *) seed32, (const void *) seed, 32);
994-
do {
995-
x0 = 0x61707865;
996-
x1 = 0x3320646e;
997-
x2 = 0x79622d32;
998-
x3 = 0x6b206574;
999-
x4 = LE32(seed32[0]);
1000-
x5 = LE32(seed32[1]);
1001-
x6 = LE32(seed32[2]);
1002-
x7 = LE32(seed32[3]);
1003-
x8 = LE32(seed32[4]);
1004-
x9 = LE32(seed32[5]);
1005-
x10 = LE32(seed32[6]);
1006-
x11 = LE32(seed32[7]);
1007-
x12 = idx;
1008-
x13 = idx >> 32;
1009-
x14 = 0;
1010-
x15 = over_count;
1011-
1012-
n = 10;
1013-
while (n--) {
1014-
QUARTERROUND(x0, x4, x8,x12)
1015-
QUARTERROUND(x1, x5, x9,x13)
1016-
QUARTERROUND(x2, x6,x10,x14)
1017-
QUARTERROUND(x3, x7,x11,x15)
1018-
QUARTERROUND(x0, x5,x10,x15)
1019-
QUARTERROUND(x1, x6,x11,x12)
1020-
QUARTERROUND(x2, x7, x8,x13)
1021-
QUARTERROUND(x3, x4, x9,x14)
1022-
}
1023-
1024-
x0 += 0x61707865;
1025-
x1 += 0x3320646e;
1026-
x2 += 0x79622d32;
1027-
x3 += 0x6b206574;
1028-
x4 += LE32(seed32[0]);
1029-
x5 += LE32(seed32[1]);
1030-
x6 += LE32(seed32[2]);
1031-
x7 += LE32(seed32[3]);
1032-
x8 += LE32(seed32[4]);
1033-
x9 += LE32(seed32[5]);
1034-
x10 += LE32(seed32[6]);
1035-
x11 += LE32(seed32[7]);
1036-
x12 += idx;
1037-
x13 += idx >> 32;
1038-
x14 += 0;
1039-
x15 += over_count;
1040-
1041-
r1->d[3] = (((uint64_t) x0) << 32) | x1;
1042-
r1->d[2] = (((uint64_t) x2) << 32) | x3;
1043-
r1->d[1] = (((uint64_t) x4) << 32) | x5;
1044-
r1->d[0] = (((uint64_t) x6) << 32) | x7;
1045-
r2->d[3] = (((uint64_t) x8) << 32) | x9;
1046-
r2->d[2] = (((uint64_t) x10) << 32) | x11;
1047-
r2->d[1] = (((uint64_t) x12) << 32) | x13;
1048-
r2->d[0] = (((uint64_t) x14) << 32) | x15;
1049-
1050-
over1 = secp256k1_scalar_check_overflow(r1);
1051-
over2 = secp256k1_scalar_check_overflow(r2);
1052-
over_count++;
1053-
} while (over1 | over2);
1054-
}
1055-
1056-
#undef ROTL32
1057-
#undef QUARTERROUND
1058-
#undef LE32
1059-
1060973
static void secp256k1_scalar_from_signed62(secp256k1_scalar *r, const secp256k1_modinv64_signed62 *a) {
1061974
const uint64_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4];
1062975

src/scalar_8x32_impl.h

-95
Original file line numberDiff line numberDiff line change
@@ -749,101 +749,6 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
749749
r->d[7] = (r->d[7] & mask0) | (a->d[7] & mask1);
750750
}
751751

752-
#define ROTL32(x,n) ((x) << (n) | (x) >> (32-(n)))
753-
#define QUARTERROUND(a,b,c,d) \
754-
a += b; d = ROTL32(d ^ a, 16); \
755-
c += d; b = ROTL32(b ^ c, 12); \
756-
a += b; d = ROTL32(d ^ a, 8); \
757-
c += d; b = ROTL32(b ^ c, 7);
758-
759-
#if defined(SECP256K1_BIG_ENDIAN)
760-
#define LE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
761-
#elif defined(SECP256K1_LITTLE_ENDIAN)
762-
#define LE32(p) (p)
763-
#endif
764-
765-
static void secp256k1_scalar_chacha20(secp256k1_scalar *r1, secp256k1_scalar *r2, const unsigned char *seed, uint64_t idx) {
766-
size_t n;
767-
size_t over_count = 0;
768-
uint32_t seed32[8];
769-
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
770-
int over1, over2;
771-
772-
memcpy((void *) seed32, (const void *) seed, 32);
773-
do {
774-
x0 = 0x61707865;
775-
x1 = 0x3320646e;
776-
x2 = 0x79622d32;
777-
x3 = 0x6b206574;
778-
x4 = LE32(seed32[0]);
779-
x5 = LE32(seed32[1]);
780-
x6 = LE32(seed32[2]);
781-
x7 = LE32(seed32[3]);
782-
x8 = LE32(seed32[4]);
783-
x9 = LE32(seed32[5]);
784-
x10 = LE32(seed32[6]);
785-
x11 = LE32(seed32[7]);
786-
x12 = idx;
787-
x13 = idx >> 32;
788-
x14 = 0;
789-
x15 = over_count;
790-
791-
n = 10;
792-
while (n--) {
793-
QUARTERROUND(x0, x4, x8,x12)
794-
QUARTERROUND(x1, x5, x9,x13)
795-
QUARTERROUND(x2, x6,x10,x14)
796-
QUARTERROUND(x3, x7,x11,x15)
797-
QUARTERROUND(x0, x5,x10,x15)
798-
QUARTERROUND(x1, x6,x11,x12)
799-
QUARTERROUND(x2, x7, x8,x13)
800-
QUARTERROUND(x3, x4, x9,x14)
801-
}
802-
803-
x0 += 0x61707865;
804-
x1 += 0x3320646e;
805-
x2 += 0x79622d32;
806-
x3 += 0x6b206574;
807-
x4 += LE32(seed32[0]);
808-
x5 += LE32(seed32[1]);
809-
x6 += LE32(seed32[2]);
810-
x7 += LE32(seed32[3]);
811-
x8 += LE32(seed32[4]);
812-
x9 += LE32(seed32[5]);
813-
x10 += LE32(seed32[6]);
814-
x11 += LE32(seed32[7]);
815-
x12 += idx;
816-
x13 += idx >> 32;
817-
x14 += 0;
818-
x15 += over_count;
819-
820-
r1->d[7] = x0;
821-
r1->d[6] = x1;
822-
r1->d[5] = x2;
823-
r1->d[4] = x3;
824-
r1->d[3] = x4;
825-
r1->d[2] = x5;
826-
r1->d[1] = x6;
827-
r1->d[0] = x7;
828-
r2->d[7] = x8;
829-
r2->d[6] = x9;
830-
r2->d[5] = x10;
831-
r2->d[4] = x11;
832-
r2->d[3] = x12;
833-
r2->d[2] = x13;
834-
r2->d[1] = x14;
835-
r2->d[0] = x15;
836-
837-
over1 = secp256k1_scalar_check_overflow(r1);
838-
over2 = secp256k1_scalar_check_overflow(r2);
839-
over_count++;
840-
} while (over1 | over2);
841-
}
842-
843-
#undef ROTL32
844-
#undef QUARTERROUND
845-
#undef LE32
846-
847752
static void secp256k1_scalar_from_signed30(secp256k1_scalar *r, const secp256k1_modinv32_signed30 *a) {
848753
const uint32_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4],
849754
a5 = a->v[5], a6 = a->v[6], a7 = a->v[7], a8 = a->v[8];

src/scalar_low_impl.h

-5
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
127127
*r = (*r & mask0) | (*a & mask1);
128128
}
129129

130-
SECP256K1_INLINE static void secp256k1_scalar_chacha20(secp256k1_scalar *r1, secp256k1_scalar *r2, const unsigned char *seed, uint64_t n) {
131-
*r1 = (seed[0] + n) % EXHAUSTIVE_TEST_ORDER;
132-
*r2 = (seed[1] + n) % EXHAUSTIVE_TEST_ORDER;
133-
}
134-
135130
static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x) {
136131
int i;
137132
*r = 0;

src/tests.c

-110
Original file line numberDiff line numberDiff line change
@@ -1949,114 +1949,6 @@ void run_scalar_set_b32_seckey_tests(void) {
19491949
CHECK(secp256k1_scalar_set_b32_seckey(&s2, b32) == 0);
19501950
}
19511951

1952-
void scalar_chacha_tests(void) {
1953-
/* Test vectors 1 to 4 from https://tools.ietf.org/html/rfc8439#appendix-A
1954-
* Note that scalar_set_b32 and scalar_get_b32 represent integers
1955-
* underlying the scalar in big-endian format. */
1956-
unsigned char expected1[64] = {
1957-
0xad, 0xe0, 0xb8, 0x76, 0x90, 0x3d, 0xf1, 0xa0,
1958-
0xe5, 0x6a, 0x5d, 0x40, 0x28, 0xbd, 0x86, 0x53,
1959-
0xb8, 0x19, 0xd2, 0xbd, 0x1a, 0xed, 0x8d, 0xa0,
1960-
0xcc, 0xef, 0x36, 0xa8, 0xc7, 0x0d, 0x77, 0x8b,
1961-
0x7c, 0x59, 0x41, 0xda, 0x8d, 0x48, 0x57, 0x51,
1962-
0x3f, 0xe0, 0x24, 0x77, 0x37, 0x4a, 0xd8, 0xb8,
1963-
0xf4, 0xb8, 0x43, 0x6a, 0x1c, 0xa1, 0x18, 0x15,
1964-
0x69, 0xb6, 0x87, 0xc3, 0x86, 0x65, 0xee, 0xb2
1965-
};
1966-
unsigned char expected2[64] = {
1967-
0xbe, 0xe7, 0x07, 0x9f, 0x7a, 0x38, 0x51, 0x55,
1968-
0x7c, 0x97, 0xba, 0x98, 0x0d, 0x08, 0x2d, 0x73,
1969-
0xa0, 0x29, 0x0f, 0xcb, 0x69, 0x65, 0xe3, 0x48,
1970-
0x3e, 0x53, 0xc6, 0x12, 0xed, 0x7a, 0xee, 0x32,
1971-
0x76, 0x21, 0xb7, 0x29, 0x43, 0x4e, 0xe6, 0x9c,
1972-
0xb0, 0x33, 0x71, 0xd5, 0xd5, 0x39, 0xd8, 0x74,
1973-
0x28, 0x1f, 0xed, 0x31, 0x45, 0xfb, 0x0a, 0x51,
1974-
0x1f, 0x0a, 0xe1, 0xac, 0x6f, 0x4d, 0x79, 0x4b
1975-
};
1976-
unsigned char seed3[32] = {
1977-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1978-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1979-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1980-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
1981-
};
1982-
unsigned char expected3[64] = {
1983-
0x24, 0x52, 0xeb, 0x3a, 0x92, 0x49, 0xf8, 0xec,
1984-
0x8d, 0x82, 0x9d, 0x9b, 0xdd, 0xd4, 0xce, 0xb1,
1985-
0xe8, 0x25, 0x20, 0x83, 0x60, 0x81, 0x8b, 0x01,
1986-
0xf3, 0x84, 0x22, 0xb8, 0x5a, 0xaa, 0x49, 0xc9,
1987-
0xbb, 0x00, 0xca, 0x8e, 0xda, 0x3b, 0xa7, 0xb4,
1988-
0xc4, 0xb5, 0x92, 0xd1, 0xfd, 0xf2, 0x73, 0x2f,
1989-
0x44, 0x36, 0x27, 0x4e, 0x25, 0x61, 0xb3, 0xc8,
1990-
0xeb, 0xdd, 0x4a, 0xa6, 0xa0, 0x13, 0x6c, 0x00
1991-
};
1992-
unsigned char seed4[32] = {
1993-
0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1994-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1995-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1996-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1997-
};
1998-
unsigned char expected4[64] = {
1999-
0xfb, 0x4d, 0xd5, 0x72, 0x4b, 0xc4, 0x2e, 0xf1,
2000-
0xdf, 0x92, 0x26, 0x36, 0x32, 0x7f, 0x13, 0x94,
2001-
0xa7, 0x8d, 0xea, 0x8f, 0x5e, 0x26, 0x90, 0x39,
2002-
0xa1, 0xbe, 0xbb, 0xc1, 0xca, 0xf0, 0x9a, 0xae,
2003-
0xa2, 0x5a, 0xb2, 0x13, 0x48, 0xa6, 0xb4, 0x6c,
2004-
0x1b, 0x9d, 0x9b, 0xcb, 0x09, 0x2c, 0x5b, 0xe6,
2005-
0x54, 0x6c, 0xa6, 0x24, 0x1b, 0xec, 0x45, 0xd5,
2006-
0x87, 0xf4, 0x74, 0x73, 0x96, 0xf0, 0x99, 0x2e
2007-
};
2008-
unsigned char seed5[32] = {
2009-
0x32, 0x56, 0x56, 0xf4, 0x29, 0x02, 0xc2, 0xf8,
2010-
0xa3, 0x4b, 0x96, 0xf5, 0xa7, 0xf7, 0xe3, 0x6c,
2011-
0x92, 0xad, 0xa5, 0x18, 0x1c, 0xe3, 0x41, 0xae,
2012-
0xc3, 0xf3, 0x18, 0xd0, 0xfa, 0x5b, 0x72, 0x53
2013-
};
2014-
unsigned char expected5[64] = {
2015-
0xe7, 0x56, 0xd3, 0x28, 0xe9, 0xc6, 0x19, 0x5c,
2016-
0x6f, 0x17, 0x8e, 0x21, 0x8c, 0x1e, 0x72, 0x11,
2017-
0xe7, 0xbd, 0x17, 0x0d, 0xac, 0x14, 0xad, 0xe9,
2018-
0x3d, 0x9f, 0xb6, 0x92, 0xd6, 0x09, 0x20, 0xfb,
2019-
0x43, 0x8e, 0x3b, 0x6d, 0xe3, 0x33, 0xdc, 0xc7,
2020-
0x6c, 0x07, 0x6f, 0xbb, 0x1f, 0xb4, 0xc8, 0xb5,
2021-
0xe3, 0x6c, 0xe5, 0x12, 0xd9, 0xd7, 0x64, 0x0c,
2022-
0xf5, 0xa7, 0x0d, 0xab, 0x79, 0x03, 0xf1, 0x81
2023-
};
2024-
2025-
secp256k1_scalar exp_r1, exp_r2;
2026-
secp256k1_scalar r1, r2;
2027-
unsigned char seed0[32] = { 0 };
2028-
2029-
secp256k1_scalar_chacha20(&r1, &r2, seed0, 0);
2030-
secp256k1_scalar_set_b32(&exp_r1, &expected1[0], NULL);
2031-
secp256k1_scalar_set_b32(&exp_r2, &expected1[32], NULL);
2032-
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
2033-
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
2034-
2035-
secp256k1_scalar_chacha20(&r1, &r2, seed0, 1);
2036-
secp256k1_scalar_set_b32(&exp_r1, &expected2[0], NULL);
2037-
secp256k1_scalar_set_b32(&exp_r2, &expected2[32], NULL);
2038-
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
2039-
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
2040-
2041-
secp256k1_scalar_chacha20(&r1, &r2, seed3, 1);
2042-
secp256k1_scalar_set_b32(&exp_r1, &expected3[0], NULL);
2043-
secp256k1_scalar_set_b32(&exp_r2, &expected3[32], NULL);
2044-
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
2045-
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
2046-
2047-
secp256k1_scalar_chacha20(&r1, &r2, seed4, 2);
2048-
secp256k1_scalar_set_b32(&exp_r1, &expected4[0], NULL);
2049-
secp256k1_scalar_set_b32(&exp_r2, &expected4[32], NULL);
2050-
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
2051-
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
2052-
2053-
secp256k1_scalar_chacha20(&r1, &r2, seed5, 0x6ff8602a7a78e2f2ULL);
2054-
secp256k1_scalar_set_b32(&exp_r1, &expected5[0], NULL);
2055-
secp256k1_scalar_set_b32(&exp_r2, &expected5[32], NULL);
2056-
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
2057-
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
2058-
}
2059-
20601952
void run_scalar_tests(void) {
20611953
int i;
20621954
for (i = 0; i < 128 * count; i++) {
@@ -2066,8 +1958,6 @@ void run_scalar_tests(void) {
20661958
run_scalar_set_b32_seckey_tests();
20671959
}
20681960

2069-
scalar_chacha_tests();
2070-
20711961
{
20721962
/* (-1)+1 should be zero. */
20731963
secp256k1_scalar s, o;

0 commit comments

Comments
 (0)