Skip to content

Commit c3794f9

Browse files
committed
add chacha20 function
1 parent e38335a commit c3794f9

File tree

5 files changed

+257
-0
lines changed

5 files changed

+257
-0
lines changed

src/scalar.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,7 @@ static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar
106106
/** Multiply a and b (without taking the modulus!), divide by 2**shift, and round to the nearest integer. Shift must be at least 256. */
107107
static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b, unsigned int shift);
108108

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

src/scalar_4x64_impl.h

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

1010
#include "scalar.h"
11+
#include <string.h>
1112

1213
/* Limbs of the secp256k1 order. */
1314
#define SECP256K1_N_0 ((uint64_t)0xBFD25E8CD0364141ULL)
@@ -955,4 +956,94 @@ SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r,
955956
secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 6] >> ((shift - 1) & 0x3f)) & 1);
956957
}
957958

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

src/scalar_8x32_impl.h

Lines changed: 100 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)
@@ -729,4 +731,102 @@ SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r,
729731
secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
730732
}
731733

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

115+
SECP256K1_INLINE static void secp256k1_scalar_chacha20(secp256k1_scalar *r1, secp256k1_scalar *r2, const unsigned char *seed, uint64_t n) {
116+
*r1 = (seed[0] + n) % EXHAUSTIVE_TEST_ORDER;
117+
*r2 = (seed[1] + n) % EXHAUSTIVE_TEST_ORDER;
118+
}
119+
115120
#endif /* SECP256K1_SCALAR_REPR_IMPL_H */

src/tests.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,12 +1007,70 @@ void scalar_test(void) {
10071007

10081008
}
10091009

1010+
void scalar_chacha_tests(void) {
1011+
unsigned char expected1[64] = {
1012+
0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
1013+
0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
1014+
0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
1015+
0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
1016+
0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
1017+
0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
1018+
0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
1019+
0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86
1020+
};
1021+
unsigned char expected2[64] = {
1022+
0x45, 0x40, 0xf0, 0x5a, 0x9f, 0x1f, 0xb2, 0x96,
1023+
0xd7, 0x73, 0x6e, 0x7b, 0x20, 0x8e, 0x3c, 0x96,
1024+
0xeb, 0x4f, 0xe1, 0x83, 0x46, 0x88, 0xd2, 0x60,
1025+
0x4f, 0x45, 0x09, 0x52, 0xed, 0x43, 0x2d, 0x41,
1026+
0xbb, 0xe2, 0xa0, 0xb6, 0xea, 0x75, 0x66, 0xd2,
1027+
0xa5, 0xd1, 0xe7, 0xe2, 0x0d, 0x42, 0xaf, 0x2c,
1028+
0x53, 0xd7, 0x92, 0xb1, 0xc4, 0x3f, 0xea, 0x81,
1029+
0x7e, 0x9a, 0xd2, 0x75, 0xae, 0x54, 0x69, 0x63
1030+
};
1031+
unsigned char expected3[64] = {
1032+
0x47, 0x4a, 0x4f, 0x35, 0x4f, 0xee, 0x93, 0x59,
1033+
0xbb, 0x65, 0x81, 0xe5, 0xd9, 0x15, 0xa6, 0x01,
1034+
0xb6, 0x8c, 0x68, 0x03, 0x38, 0xff, 0x65, 0xe6,
1035+
0x56, 0x4a, 0x3e, 0x65, 0x59, 0xfc, 0x12, 0x3f,
1036+
0xa9, 0xb2, 0xf9, 0x3e, 0x57, 0xc3, 0xa5, 0xcb,
1037+
0xe0, 0x72, 0x74, 0x27, 0x88, 0x1c, 0x23, 0xdf,
1038+
0xe2, 0xb6, 0xcc, 0xfb, 0x93, 0xed, 0xcb, 0x02,
1039+
0xd7, 0x50, 0x52, 0x45, 0x84, 0x88, 0xbb, 0xea
1040+
};
1041+
1042+
secp256k1_scalar exp_r1, exp_r2;
1043+
secp256k1_scalar r1, r2;
1044+
unsigned char seed1[32] = { 0 };
1045+
1046+
secp256k1_scalar_chacha20(&r1, &r2, seed1, 0);
1047+
secp256k1_scalar_set_b32(&exp_r1, &expected1[0], NULL);
1048+
secp256k1_scalar_set_b32(&exp_r2, &expected1[32], NULL);
1049+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1050+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1051+
1052+
seed1[31] = 1;
1053+
secp256k1_scalar_chacha20(&r1, &r2, seed1, 0);
1054+
secp256k1_scalar_set_b32(&exp_r1, &expected2[0], NULL);
1055+
secp256k1_scalar_set_b32(&exp_r2, &expected2[32], NULL);
1056+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1057+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1058+
1059+
secp256k1_scalar_chacha20(&r1, &r2, seed1, 100);
1060+
secp256k1_scalar_set_b32(&exp_r1, &expected3[0], NULL);
1061+
secp256k1_scalar_set_b32(&exp_r2, &expected3[32], NULL);
1062+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1063+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1064+
}
1065+
10101066
void run_scalar_tests(void) {
10111067
int i;
10121068
for (i = 0; i < 128 * count; i++) {
10131069
scalar_test();
10141070
}
10151071

1072+
scalar_chacha_tests();
1073+
10161074
{
10171075
/* (-1)+1 should be zero. */
10181076
secp256k1_scalar s, o;

0 commit comments

Comments
 (0)