Skip to content

Commit cb02d22

Browse files
committed
Add scalar_chacha20
This is in preparation for schnorrsig_batch_verify.
1 parent 2c8e321 commit cb02d22

File tree

5 files changed

+305
-0
lines changed

5 files changed

+305
-0
lines changed

src/scalar.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,7 @@ static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r, const secp256k1_
114114
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
115115
static void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag);
116116

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

src/scalar_4x64_impl.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#ifndef SECP256K1_SCALAR_REPR_IMPL_H
88
#define SECP256K1_SCALAR_REPR_IMPL_H
99

10+
#include "scalar.h"
11+
#include <string.h>
12+
1013
/* Limbs of the secp256k1 order. */
1114
#define SECP256K1_N_0 ((uint64_t)0xBFD25E8CD0364141ULL)
1215
#define SECP256K1_N_1 ((uint64_t)0xBAAEDCE6AF48A03BULL)
@@ -957,4 +960,91 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
957960
r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
958961
}
959962

963+
#define ROTL32(x,n) ((x) << (n) | (x) >> (32-(n)))
964+
#define QUARTERROUND(a,b,c,d) \
965+
a += b; d = ROTL32(d ^ a, 16); \
966+
c += d; b = ROTL32(b ^ c, 12); \
967+
a += b; d = ROTL32(d ^ a, 8); \
968+
c += d; b = ROTL32(b ^ c, 7);
969+
970+
#ifdef WORDS_BIGENDIAN
971+
#define LE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
972+
#else
973+
#define LE32(p) (p)
974+
#endif
975+
976+
static void secp256k1_scalar_chacha20(secp256k1_scalar *r1, secp256k1_scalar *r2, const unsigned char *seed, uint64_t idx) {
977+
size_t n;
978+
size_t over_count = 0;
979+
uint32_t seed32[8];
980+
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
981+
int over1, over2;
982+
983+
memcpy((void *) seed32, (const void *) seed, 32);
984+
do {
985+
x0 = 0x61707865;
986+
x1 = 0x3320646e;
987+
x2 = 0x79622d32;
988+
x3 = 0x6b206574;
989+
x4 = LE32(seed32[0]);
990+
x5 = LE32(seed32[1]);
991+
x6 = LE32(seed32[2]);
992+
x7 = LE32(seed32[3]);
993+
x8 = LE32(seed32[4]);
994+
x9 = LE32(seed32[5]);
995+
x10 = LE32(seed32[6]);
996+
x11 = LE32(seed32[7]);
997+
x12 = idx;
998+
x13 = idx >> 32;
999+
x14 = 0;
1000+
x15 = over_count;
1001+
1002+
n = 10;
1003+
while (n--) {
1004+
QUARTERROUND(x0, x4, x8,x12)
1005+
QUARTERROUND(x1, x5, x9,x13)
1006+
QUARTERROUND(x2, x6,x10,x14)
1007+
QUARTERROUND(x3, x7,x11,x15)
1008+
QUARTERROUND(x0, x5,x10,x15)
1009+
QUARTERROUND(x1, x6,x11,x12)
1010+
QUARTERROUND(x2, x7, x8,x13)
1011+
QUARTERROUND(x3, x4, x9,x14)
1012+
}
1013+
1014+
x0 += 0x61707865;
1015+
x1 += 0x3320646e;
1016+
x2 += 0x79622d32;
1017+
x3 += 0x6b206574;
1018+
x4 += LE32(seed32[0]);
1019+
x5 += LE32(seed32[1]);
1020+
x6 += LE32(seed32[2]);
1021+
x7 += LE32(seed32[3]);
1022+
x8 += LE32(seed32[4]);
1023+
x9 += LE32(seed32[5]);
1024+
x10 += LE32(seed32[6]);
1025+
x11 += LE32(seed32[7]);
1026+
x12 += idx;
1027+
x13 += idx >> 32;
1028+
x14 += 0;
1029+
x15 += over_count;
1030+
1031+
r1->d[3] = (((uint64_t) x0) << 32) | x1;
1032+
r1->d[2] = (((uint64_t) x2) << 32) | x3;
1033+
r1->d[1] = (((uint64_t) x4) << 32) | x5;
1034+
r1->d[0] = (((uint64_t) x6) << 32) | x7;
1035+
r2->d[3] = (((uint64_t) x8) << 32) | x9;
1036+
r2->d[2] = (((uint64_t) x10) << 32) | x11;
1037+
r2->d[1] = (((uint64_t) x12) << 32) | x13;
1038+
r2->d[0] = (((uint64_t) x14) << 32) | x15;
1039+
1040+
over1 = secp256k1_scalar_check_overflow(r1);
1041+
over2 = secp256k1_scalar_check_overflow(r2);
1042+
over_count++;
1043+
} while (over1 | over2);
1044+
}
1045+
1046+
#undef ROTL32
1047+
#undef QUARTERROUND
1048+
#undef LE32
1049+
9601050
#endif /* SECP256K1_SCALAR_REPR_IMPL_H */

src/scalar_8x32_impl.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef SECP256K1_SCALAR_REPR_IMPL_H
88
#define SECP256K1_SCALAR_REPR_IMPL_H
99

10+
#include <string.h>
11+
1012
/* Limbs of the secp256k1 order. */
1113
#define SECP256K1_N_0 ((uint32_t)0xD0364141UL)
1214
#define SECP256K1_N_1 ((uint32_t)0xBFD25E8CUL)
@@ -733,4 +735,99 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
733735
r->d[7] = (r->d[7] & mask0) | (a->d[7] & mask1);
734736
}
735737

738+
#define ROTL32(x,n) ((x) << (n) | (x) >> (32-(n)))
739+
#define QUARTERROUND(a,b,c,d) \
740+
a += b; d = ROTL32(d ^ a, 16); \
741+
c += d; b = ROTL32(b ^ c, 12); \
742+
a += b; d = ROTL32(d ^ a, 8); \
743+
c += d; b = ROTL32(b ^ c, 7);
744+
745+
#ifdef WORDS_BIGENDIAN
746+
#define LE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
747+
#else
748+
#define LE32(p) (p)
749+
#endif
750+
751+
static void secp256k1_scalar_chacha20(secp256k1_scalar *r1, secp256k1_scalar *r2, const unsigned char *seed, uint64_t idx) {
752+
size_t n;
753+
size_t over_count = 0;
754+
uint32_t seed32[8];
755+
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
756+
int over1, over2;
757+
758+
memcpy((void *) seed32, (const void *) seed, 32);
759+
do {
760+
x0 = 0x61707865;
761+
x1 = 0x3320646e;
762+
x2 = 0x79622d32;
763+
x3 = 0x6b206574;
764+
x4 = LE32(seed32[0]);
765+
x5 = LE32(seed32[1]);
766+
x6 = LE32(seed32[2]);
767+
x7 = LE32(seed32[3]);
768+
x8 = LE32(seed32[4]);
769+
x9 = LE32(seed32[5]);
770+
x10 = LE32(seed32[6]);
771+
x11 = LE32(seed32[7]);
772+
x12 = idx;
773+
x13 = idx >> 32;
774+
x14 = 0;
775+
x15 = over_count;
776+
777+
n = 10;
778+
while (n--) {
779+
QUARTERROUND(x0, x4, x8,x12)
780+
QUARTERROUND(x1, x5, x9,x13)
781+
QUARTERROUND(x2, x6,x10,x14)
782+
QUARTERROUND(x3, x7,x11,x15)
783+
QUARTERROUND(x0, x5,x10,x15)
784+
QUARTERROUND(x1, x6,x11,x12)
785+
QUARTERROUND(x2, x7, x8,x13)
786+
QUARTERROUND(x3, x4, x9,x14)
787+
}
788+
789+
x0 += 0x61707865;
790+
x1 += 0x3320646e;
791+
x2 += 0x79622d32;
792+
x3 += 0x6b206574;
793+
x4 += LE32(seed32[0]);
794+
x5 += LE32(seed32[1]);
795+
x6 += LE32(seed32[2]);
796+
x7 += LE32(seed32[3]);
797+
x8 += LE32(seed32[4]);
798+
x9 += LE32(seed32[5]);
799+
x10 += LE32(seed32[6]);
800+
x11 += LE32(seed32[7]);
801+
x12 += idx;
802+
x13 += idx >> 32;
803+
x14 += 0;
804+
x15 += over_count;
805+
806+
r1->d[7] = x0;
807+
r1->d[6] = x1;
808+
r1->d[5] = x2;
809+
r1->d[4] = x3;
810+
r1->d[3] = x4;
811+
r1->d[2] = x5;
812+
r1->d[1] = x6;
813+
r1->d[0] = x7;
814+
r2->d[7] = x8;
815+
r2->d[6] = x9;
816+
r2->d[5] = x10;
817+
r2->d[4] = x11;
818+
r2->d[3] = x12;
819+
r2->d[2] = x13;
820+
r2->d[1] = x14;
821+
r2->d[0] = x15;
822+
823+
over1 = secp256k1_scalar_check_overflow(r1);
824+
over2 = secp256k1_scalar_check_overflow(r2);
825+
over_count++;
826+
} while (over1 | over2);
827+
}
828+
829+
#undef ROTL32
830+
#undef QUARTERROUND
831+
#undef LE32
832+
736833
#endif /* SECP256K1_SCALAR_REPR_IMPL_H */

src/scalar_low_impl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,9 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
122122
*r = (*r & mask0) | (*a & mask1);
123123
}
124124

125+
SECP256K1_INLINE static void secp256k1_scalar_chacha20(secp256k1_scalar *r1, secp256k1_scalar *r2, const unsigned char *seed, uint64_t n) {
126+
*r1 = (seed[0] + n) % EXHAUSTIVE_TEST_ORDER;
127+
*r2 = (seed[1] + n) % EXHAUSTIVE_TEST_ORDER;
128+
}
129+
125130
#endif /* SECP256K1_SCALAR_REPR_IMPL_H */

src/tests.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,114 @@ void run_scalar_set_b32_seckey_tests(void) {
11121112
CHECK(secp256k1_scalar_set_b32_seckey(&s2, b32) == 0);
11131113
}
11141114

1115+
void scalar_chacha_tests(void) {
1116+
/* Test vectors 1 to 4 from https://tools.ietf.org/html/rfc8439#appendix-A
1117+
* Note that scalar_set_b32 and scalar_get_b32 represent integers
1118+
* underlying the scalar in big-endian format. */
1119+
unsigned char expected1[64] = {
1120+
0xad, 0xe0, 0xb8, 0x76, 0x90, 0x3d, 0xf1, 0xa0,
1121+
0xe5, 0x6a, 0x5d, 0x40, 0x28, 0xbd, 0x86, 0x53,
1122+
0xb8, 0x19, 0xd2, 0xbd, 0x1a, 0xed, 0x8d, 0xa0,
1123+
0xcc, 0xef, 0x36, 0xa8, 0xc7, 0x0d, 0x77, 0x8b,
1124+
0x7c, 0x59, 0x41, 0xda, 0x8d, 0x48, 0x57, 0x51,
1125+
0x3f, 0xe0, 0x24, 0x77, 0x37, 0x4a, 0xd8, 0xb8,
1126+
0xf4, 0xb8, 0x43, 0x6a, 0x1c, 0xa1, 0x18, 0x15,
1127+
0x69, 0xb6, 0x87, 0xc3, 0x86, 0x65, 0xee, 0xb2
1128+
};
1129+
unsigned char expected2[64] = {
1130+
0xbe, 0xe7, 0x07, 0x9f, 0x7a, 0x38, 0x51, 0x55,
1131+
0x7c, 0x97, 0xba, 0x98, 0x0d, 0x08, 0x2d, 0x73,
1132+
0xa0, 0x29, 0x0f, 0xcb, 0x69, 0x65, 0xe3, 0x48,
1133+
0x3e, 0x53, 0xc6, 0x12, 0xed, 0x7a, 0xee, 0x32,
1134+
0x76, 0x21, 0xb7, 0x29, 0x43, 0x4e, 0xe6, 0x9c,
1135+
0xb0, 0x33, 0x71, 0xd5, 0xd5, 0x39, 0xd8, 0x74,
1136+
0x28, 0x1f, 0xed, 0x31, 0x45, 0xfb, 0x0a, 0x51,
1137+
0x1f, 0x0a, 0xe1, 0xac, 0x6f, 0x4d, 0x79, 0x4b
1138+
};
1139+
unsigned char seed3[32] = {
1140+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1141+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1142+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1143+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
1144+
};
1145+
unsigned char expected3[64] = {
1146+
0x24, 0x52, 0xeb, 0x3a, 0x92, 0x49, 0xf8, 0xec,
1147+
0x8d, 0x82, 0x9d, 0x9b, 0xdd, 0xd4, 0xce, 0xb1,
1148+
0xe8, 0x25, 0x20, 0x83, 0x60, 0x81, 0x8b, 0x01,
1149+
0xf3, 0x84, 0x22, 0xb8, 0x5a, 0xaa, 0x49, 0xc9,
1150+
0xbb, 0x00, 0xca, 0x8e, 0xda, 0x3b, 0xa7, 0xb4,
1151+
0xc4, 0xb5, 0x92, 0xd1, 0xfd, 0xf2, 0x73, 0x2f,
1152+
0x44, 0x36, 0x27, 0x4e, 0x25, 0x61, 0xb3, 0xc8,
1153+
0xeb, 0xdd, 0x4a, 0xa6, 0xa0, 0x13, 0x6c, 0x00
1154+
};
1155+
unsigned char seed4[32] = {
1156+
0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1157+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1158+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1159+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1160+
};
1161+
unsigned char expected4[64] = {
1162+
0xfb, 0x4d, 0xd5, 0x72, 0x4b, 0xc4, 0x2e, 0xf1,
1163+
0xdf, 0x92, 0x26, 0x36, 0x32, 0x7f, 0x13, 0x94,
1164+
0xa7, 0x8d, 0xea, 0x8f, 0x5e, 0x26, 0x90, 0x39,
1165+
0xa1, 0xbe, 0xbb, 0xc1, 0xca, 0xf0, 0x9a, 0xae,
1166+
0xa2, 0x5a, 0xb2, 0x13, 0x48, 0xa6, 0xb4, 0x6c,
1167+
0x1b, 0x9d, 0x9b, 0xcb, 0x09, 0x2c, 0x5b, 0xe6,
1168+
0x54, 0x6c, 0xa6, 0x24, 0x1b, 0xec, 0x45, 0xd5,
1169+
0x87, 0xf4, 0x74, 0x73, 0x96, 0xf0, 0x99, 0x2e
1170+
};
1171+
unsigned char seed5[32] = {
1172+
0x32, 0x56, 0x56, 0xf4, 0x29, 0x02, 0xc2, 0xf8,
1173+
0xa3, 0x4b, 0x96, 0xf5, 0xa7, 0xf7, 0xe3, 0x6c,
1174+
0x92, 0xad, 0xa5, 0x18, 0x1c, 0xe3, 0x41, 0xae,
1175+
0xc3, 0xf3, 0x18, 0xd0, 0xfa, 0x5b, 0x72, 0x53
1176+
};
1177+
unsigned char expected5[64] = {
1178+
0xe7, 0x56, 0xd3, 0x28, 0xe9, 0xc6, 0x19, 0x5c,
1179+
0x6f, 0x17, 0x8e, 0x21, 0x8c, 0x1e, 0x72, 0x11,
1180+
0xe7, 0xbd, 0x17, 0x0d, 0xac, 0x14, 0xad, 0xe9,
1181+
0x3d, 0x9f, 0xb6, 0x92, 0xd6, 0x09, 0x20, 0xfb,
1182+
0x43, 0x8e, 0x3b, 0x6d, 0xe3, 0x33, 0xdc, 0xc7,
1183+
0x6c, 0x07, 0x6f, 0xbb, 0x1f, 0xb4, 0xc8, 0xb5,
1184+
0xe3, 0x6c, 0xe5, 0x12, 0xd9, 0xd7, 0x64, 0x0c,
1185+
0xf5, 0xa7, 0x0d, 0xab, 0x79, 0x03, 0xf1, 0x81
1186+
};
1187+
1188+
secp256k1_scalar exp_r1, exp_r2;
1189+
secp256k1_scalar r1, r2;
1190+
unsigned char seed0[32] = { 0 };
1191+
1192+
secp256k1_scalar_chacha20(&r1, &r2, seed0, 0);
1193+
secp256k1_scalar_set_b32(&exp_r1, &expected1[0], NULL);
1194+
secp256k1_scalar_set_b32(&exp_r2, &expected1[32], NULL);
1195+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1196+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1197+
1198+
secp256k1_scalar_chacha20(&r1, &r2, seed0, 1);
1199+
secp256k1_scalar_set_b32(&exp_r1, &expected2[0], NULL);
1200+
secp256k1_scalar_set_b32(&exp_r2, &expected2[32], NULL);
1201+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1202+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1203+
1204+
secp256k1_scalar_chacha20(&r1, &r2, seed3, 1);
1205+
secp256k1_scalar_set_b32(&exp_r1, &expected3[0], NULL);
1206+
secp256k1_scalar_set_b32(&exp_r2, &expected3[32], NULL);
1207+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1208+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1209+
1210+
secp256k1_scalar_chacha20(&r1, &r2, seed4, 2);
1211+
secp256k1_scalar_set_b32(&exp_r1, &expected4[0], NULL);
1212+
secp256k1_scalar_set_b32(&exp_r2, &expected4[32], NULL);
1213+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1214+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1215+
1216+
secp256k1_scalar_chacha20(&r1, &r2, seed5, 0x6ff8602a7a78e2f2ULL);
1217+
secp256k1_scalar_set_b32(&exp_r1, &expected5[0], NULL);
1218+
secp256k1_scalar_set_b32(&exp_r2, &expected5[32], NULL);
1219+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1220+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1221+
}
1222+
11151223
void run_scalar_tests(void) {
11161224
int i;
11171225
for (i = 0; i < 128 * count; i++) {
@@ -1121,6 +1229,8 @@ void run_scalar_tests(void) {
11211229
run_scalar_set_b32_seckey_tests();
11221230
}
11231231

1232+
scalar_chacha_tests();
1233+
11241234
{
11251235
/* (-1)+1 should be zero. */
11261236
secp256k1_scalar s, o;

0 commit comments

Comments
 (0)