Skip to content

Commit 8260800

Browse files
apoelstrajonasnick
authored andcommitted
add chacha20 function
1 parent ed59fbe commit 8260800

File tree

5 files changed

+309
-0
lines changed

5 files changed

+309
-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] = LE32((uint64_t) x0) << 32 | LE32(x1);
1030+
r1->d[2] = LE32((uint64_t) x2) << 32 | LE32(x3);
1031+
r1->d[1] = LE32((uint64_t) x4) << 32 | LE32(x5);
1032+
r1->d[0] = LE32((uint64_t) x6) << 32 | LE32(x7);
1033+
r2->d[3] = LE32((uint64_t) x8) << 32 | LE32(x9);
1034+
r2->d[2] = LE32((uint64_t) x10) << 32 | LE32(x11);
1035+
r2->d[1] = LE32((uint64_t) x12) << 32 | LE32(x13);
1036+
r2->d[0] = LE32((uint64_t) x14) << 32 | LE32(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] = LE32(x0);
805+
r1->d[6] = LE32(x1);
806+
r1->d[5] = LE32(x2);
807+
r1->d[4] = LE32(x3);
808+
r1->d[3] = LE32(x4);
809+
r1->d[2] = LE32(x5);
810+
r1->d[1] = LE32(x6);
811+
r1->d[0] = LE32(x7);
812+
r2->d[7] = LE32(x8);
813+
r2->d[6] = LE32(x9);
814+
r2->d[5] = LE32(x10);
815+
r2->d[4] = LE32(x11);
816+
r2->d[3] = LE32(x12);
817+
r2->d[2] = LE32(x13);
818+
r2->d[1] = LE32(x14);
819+
r2->d[0] = LE32(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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,12 +1007,122 @@ void scalar_test(void) {
10071007

10081008
}
10091009

1010+
void scalar_chacha_tests(void) {
1011+
/* Test vectors 1 to 4 from https://tools.ietf.org/html/rfc8439#appendix-A
1012+
* Note that scalar_set_b32 and scalar_get_b32 represent integers
1013+
* underlying the scalar in big-endian format. */
1014+
unsigned char expected1[64] = {
1015+
0xad, 0xe0, 0xb8, 0x76, 0x90, 0x3d, 0xf1, 0xa0,
1016+
0xe5, 0x6a, 0x5d, 0x40, 0x28, 0xbd, 0x86, 0x53,
1017+
0xb8, 0x19, 0xd2, 0xbd, 0x1a, 0xed, 0x8d, 0xa0,
1018+
0xcc, 0xef, 0x36, 0xa8, 0xc7, 0x0d, 0x77, 0x8b,
1019+
0x7c, 0x59, 0x41, 0xda, 0x8d, 0x48, 0x57, 0x51,
1020+
0x3f, 0xe0, 0x24, 0x77, 0x37, 0x4a, 0xd8, 0xb8,
1021+
0xf4, 0xb8, 0x43, 0x6a, 0x1c, 0xa1, 0x18, 0x15,
1022+
0x69, 0xb6, 0x87, 0xc3, 0x86, 0x65, 0xee, 0xb2
1023+
};
1024+
unsigned char expected2[64] = {
1025+
0xbe, 0xe7, 0x07, 0x9f, 0x7a, 0x38, 0x51, 0x55,
1026+
0x7c, 0x97, 0xba, 0x98, 0x0d, 0x08, 0x2d, 0x73,
1027+
0xa0, 0x29, 0x0f, 0xcb, 0x69, 0x65, 0xe3, 0x48,
1028+
0x3e, 0x53, 0xc6, 0x12, 0xed, 0x7a, 0xee, 0x32,
1029+
0x76, 0x21, 0xb7, 0x29, 0x43, 0x4e, 0xe6, 0x9c,
1030+
0xb0, 0x33, 0x71, 0xd5, 0xd5, 0x39, 0xd8, 0x74,
1031+
0x28, 0x1f, 0xed, 0x31, 0x45, 0xfb, 0x0a, 0x51,
1032+
0x1f, 0x0a, 0xe1, 0xac, 0x6f, 0x4d, 0x79, 0x4b
1033+
};
1034+
unsigned char seed3[32] = {
1035+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1036+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1037+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1038+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
1039+
};
1040+
unsigned char expected3[64] = {
1041+
0x24, 0x52, 0xeb, 0x3a, 0x92, 0x49, 0xf8, 0xec,
1042+
0x8d, 0x82, 0x9d, 0x9b, 0xdd, 0xd4, 0xce, 0xb1,
1043+
0xe8, 0x25, 0x20, 0x83, 0x60, 0x81, 0x8b, 0x01,
1044+
0xf3, 0x84, 0x22, 0xb8, 0x5a, 0xaa, 0x49, 0xc9,
1045+
0xbb, 0x00, 0xca, 0x8e, 0xda, 0x3b, 0xa7, 0xb4,
1046+
0xc4, 0xb5, 0x92, 0xd1, 0xfd, 0xf2, 0x73, 0x2f,
1047+
0x44, 0x36, 0x27, 0x4e, 0x25, 0x61, 0xb3, 0xc8,
1048+
0xeb, 0xdd, 0x4a, 0xa6, 0xa0, 0x13, 0x6c, 0x00
1049+
};
1050+
unsigned char seed4[32] = {
1051+
0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1052+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1053+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1054+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1055+
};
1056+
unsigned char expected4[64] = {
1057+
0xfb, 0x4d, 0xd5, 0x72, 0x4b, 0xc4, 0x2e, 0xf1,
1058+
0xdf, 0x92, 0x26, 0x36, 0x32, 0x7f, 0x13, 0x94,
1059+
0xa7, 0x8d, 0xea, 0x8f, 0x5e, 0x26, 0x90, 0x39,
1060+
0xa1, 0xbe, 0xbb, 0xc1, 0xca, 0xf0, 0x9a, 0xae,
1061+
0xa2, 0x5a, 0xb2, 0x13, 0x48, 0xa6, 0xb4, 0x6c,
1062+
0x1b, 0x9d, 0x9b, 0xcb, 0x09, 0x2c, 0x5b, 0xe6,
1063+
0x54, 0x6c, 0xa6, 0x24, 0x1b, 0xec, 0x45, 0xd5,
1064+
0x87, 0xf4, 0x74, 0x73, 0x96, 0xf0, 0x99, 0x2e
1065+
};
1066+
unsigned char seed5[32] = {
1067+
0x32, 0x56, 0x56, 0xf4, 0x29, 0x02, 0xc2, 0xf8,
1068+
0xa3, 0x4b, 0x96, 0xf5, 0xa7, 0xf7, 0xe3, 0x6c,
1069+
0x92, 0xad, 0xa5, 0x18, 0x1c, 0xe3, 0x41, 0xae,
1070+
0xc3, 0xf3, 0x18, 0xd0, 0xfa, 0x5b, 0x72, 0x53
1071+
};
1072+
unsigned char expected5[64] = {
1073+
0xe7, 0x56, 0xd3, 0x28, 0xe9, 0xc6, 0x19, 0x5c,
1074+
0x6f, 0x17, 0x8e, 0x21, 0x8c, 0x1e, 0x72, 0x11,
1075+
0xe7, 0xbd, 0x17, 0x0d, 0xac, 0x14, 0xad, 0xe9,
1076+
0x3d, 0x9f, 0xb6, 0x92, 0xd6, 0x09, 0x20, 0xfb,
1077+
0x43, 0x8e, 0x3b, 0x6d, 0xe3, 0x33, 0xdc, 0xc7,
1078+
0x6c, 0x07, 0x6f, 0xbb, 0x1f, 0xb4, 0xc8, 0xb5,
1079+
0xe3, 0x6c, 0xe5, 0x12, 0xd9, 0xd7, 0x64, 0x0c,
1080+
0xf5, 0xa7, 0x0d, 0xab, 0x79, 0x03, 0xf1, 0x81
1081+
};
1082+
1083+
secp256k1_scalar exp_r1, exp_r2;
1084+
secp256k1_scalar r1, r2;
1085+
unsigned char seed0[32] = { 0 };
1086+
1087+
secp256k1_scalar_chacha20(&r1, &r2, seed0, 0);
1088+
secp256k1_scalar_set_b32(&exp_r1, &expected1[0], NULL);
1089+
secp256k1_scalar_set_b32(&exp_r2, &expected1[32], NULL);
1090+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1091+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1092+
1093+
secp256k1_scalar_chacha20(&r1, &r2, seed0, 1);
1094+
secp256k1_scalar_set_b32(&exp_r1, &expected2[0], NULL);
1095+
secp256k1_scalar_set_b32(&exp_r2, &expected2[32], NULL);
1096+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1097+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1098+
1099+
secp256k1_scalar_chacha20(&r1, &r2, seed3, 1);
1100+
secp256k1_scalar_set_b32(&exp_r1, &expected3[0], NULL);
1101+
secp256k1_scalar_set_b32(&exp_r2, &expected3[32], NULL);
1102+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1103+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1104+
1105+
secp256k1_scalar_chacha20(&r1, &r2, seed4, 2);
1106+
secp256k1_scalar_set_b32(&exp_r1, &expected4[0], NULL);
1107+
secp256k1_scalar_set_b32(&exp_r2, &expected4[32], NULL);
1108+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1109+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1110+
1111+
secp256k1_scalar_chacha20(&r1, &r2, seed5, 0x6ff8602a7a78e2f2ULL);
1112+
secp256k1_scalar_set_b32(&exp_r1, &expected5[0], NULL);
1113+
secp256k1_scalar_set_b32(&exp_r2, &expected5[32], NULL);
1114+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1115+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1116+
}
1117+
10101118
void run_scalar_tests(void) {
10111119
int i;
10121120
for (i = 0; i < 128 * count; i++) {
10131121
scalar_test();
10141122
}
10151123

1124+
scalar_chacha_tests();
1125+
10161126
{
10171127
/* (-1)+1 should be zero. */
10181128
secp256k1_scalar s, o;

0 commit comments

Comments
 (0)