Skip to content

Commit 9bc43c3

Browse files
committed
Add scalar_chacha20
This is in preparation for schnorrsig_batch_verify.
1 parent ac5a0a1 commit 9bc43c3

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. */
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)
@@ -956,4 +959,91 @@ static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const se
956959
r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
957960
}
958961

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

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

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

src/tests.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,114 @@ void run_scalar_set_b32_seckey_tests(void) {
11231123
CHECK(secp256k1_scalar_set_b32_seckey(&s2, b32) == 0);
11241124
}
11251125

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

1243+
scalar_chacha_tests();
1244+
11351245
{
11361246
/* (-1)+1 should be zero. */
11371247
secp256k1_scalar s, o;

0 commit comments

Comments
 (0)