Skip to content

Commit 8fcee9a

Browse files
apoelstrajonasnick
authored andcommitted
add chacha20 function
1 parent 96cd94e commit 8fcee9a

File tree

5 files changed

+311
-0
lines changed

5 files changed

+311
-0
lines changed

src/scalar.h

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

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

src/scalar_4x64_impl.h

Lines changed: 93 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)
@@ -946,4 +949,94 @@ SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r,
946949
secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 6] >> ((shift - 1) & 0x3f)) & 1);
947950
}
948951

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

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

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

src/tests.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,12 +1077,122 @@ void scalar_test(void) {
10771077

10781078
}
10791079

1080+
void scalar_chacha_tests(void) {
1081+
/* Test vectors 1 to 4 from https://tools.ietf.org/html/rfc8439#appendix-A
1082+
* Note that scalar_set_b32 and scalar_get_b32 represent integers
1083+
* underlying the scalar in big-endian format. */
1084+
unsigned char expected1[64] = {
1085+
0xad, 0xe0, 0xb8, 0x76, 0x90, 0x3d, 0xf1, 0xa0,
1086+
0xe5, 0x6a, 0x5d, 0x40, 0x28, 0xbd, 0x86, 0x53,
1087+
0xb8, 0x19, 0xd2, 0xbd, 0x1a, 0xed, 0x8d, 0xa0,
1088+
0xcc, 0xef, 0x36, 0xa8, 0xc7, 0x0d, 0x77, 0x8b,
1089+
0x7c, 0x59, 0x41, 0xda, 0x8d, 0x48, 0x57, 0x51,
1090+
0x3f, 0xe0, 0x24, 0x77, 0x37, 0x4a, 0xd8, 0xb8,
1091+
0xf4, 0xb8, 0x43, 0x6a, 0x1c, 0xa1, 0x18, 0x15,
1092+
0x69, 0xb6, 0x87, 0xc3, 0x86, 0x65, 0xee, 0xb2
1093+
};
1094+
unsigned char expected2[64] = {
1095+
0xbe, 0xe7, 0x07, 0x9f, 0x7a, 0x38, 0x51, 0x55,
1096+
0x7c, 0x97, 0xba, 0x98, 0x0d, 0x08, 0x2d, 0x73,
1097+
0xa0, 0x29, 0x0f, 0xcb, 0x69, 0x65, 0xe3, 0x48,
1098+
0x3e, 0x53, 0xc6, 0x12, 0xed, 0x7a, 0xee, 0x32,
1099+
0x76, 0x21, 0xb7, 0x29, 0x43, 0x4e, 0xe6, 0x9c,
1100+
0xb0, 0x33, 0x71, 0xd5, 0xd5, 0x39, 0xd8, 0x74,
1101+
0x28, 0x1f, 0xed, 0x31, 0x45, 0xfb, 0x0a, 0x51,
1102+
0x1f, 0x0a, 0xe1, 0xac, 0x6f, 0x4d, 0x79, 0x4b
1103+
};
1104+
unsigned char seed3[32] = {
1105+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1106+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1107+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1108+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
1109+
};
1110+
unsigned char expected3[64] = {
1111+
0x24, 0x52, 0xeb, 0x3a, 0x92, 0x49, 0xf8, 0xec,
1112+
0x8d, 0x82, 0x9d, 0x9b, 0xdd, 0xd4, 0xce, 0xb1,
1113+
0xe8, 0x25, 0x20, 0x83, 0x60, 0x81, 0x8b, 0x01,
1114+
0xf3, 0x84, 0x22, 0xb8, 0x5a, 0xaa, 0x49, 0xc9,
1115+
0xbb, 0x00, 0xca, 0x8e, 0xda, 0x3b, 0xa7, 0xb4,
1116+
0xc4, 0xb5, 0x92, 0xd1, 0xfd, 0xf2, 0x73, 0x2f,
1117+
0x44, 0x36, 0x27, 0x4e, 0x25, 0x61, 0xb3, 0xc8,
1118+
0xeb, 0xdd, 0x4a, 0xa6, 0xa0, 0x13, 0x6c, 0x00
1119+
};
1120+
unsigned char seed4[32] = {
1121+
0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1122+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1123+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1124+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1125+
};
1126+
unsigned char expected4[64] = {
1127+
0xfb, 0x4d, 0xd5, 0x72, 0x4b, 0xc4, 0x2e, 0xf1,
1128+
0xdf, 0x92, 0x26, 0x36, 0x32, 0x7f, 0x13, 0x94,
1129+
0xa7, 0x8d, 0xea, 0x8f, 0x5e, 0x26, 0x90, 0x39,
1130+
0xa1, 0xbe, 0xbb, 0xc1, 0xca, 0xf0, 0x9a, 0xae,
1131+
0xa2, 0x5a, 0xb2, 0x13, 0x48, 0xa6, 0xb4, 0x6c,
1132+
0x1b, 0x9d, 0x9b, 0xcb, 0x09, 0x2c, 0x5b, 0xe6,
1133+
0x54, 0x6c, 0xa6, 0x24, 0x1b, 0xec, 0x45, 0xd5,
1134+
0x87, 0xf4, 0x74, 0x73, 0x96, 0xf0, 0x99, 0x2e
1135+
};
1136+
unsigned char seed5[32] = {
1137+
0x32, 0x56, 0x56, 0xf4, 0x29, 0x02, 0xc2, 0xf8,
1138+
0xa3, 0x4b, 0x96, 0xf5, 0xa7, 0xf7, 0xe3, 0x6c,
1139+
0x92, 0xad, 0xa5, 0x18, 0x1c, 0xe3, 0x41, 0xae,
1140+
0xc3, 0xf3, 0x18, 0xd0, 0xfa, 0x5b, 0x72, 0x53
1141+
};
1142+
unsigned char expected5[64] = {
1143+
0xe7, 0x56, 0xd3, 0x28, 0xe9, 0xc6, 0x19, 0x5c,
1144+
0x6f, 0x17, 0x8e, 0x21, 0x8c, 0x1e, 0x72, 0x11,
1145+
0xe7, 0xbd, 0x17, 0x0d, 0xac, 0x14, 0xad, 0xe9,
1146+
0x3d, 0x9f, 0xb6, 0x92, 0xd6, 0x09, 0x20, 0xfb,
1147+
0x43, 0x8e, 0x3b, 0x6d, 0xe3, 0x33, 0xdc, 0xc7,
1148+
0x6c, 0x07, 0x6f, 0xbb, 0x1f, 0xb4, 0xc8, 0xb5,
1149+
0xe3, 0x6c, 0xe5, 0x12, 0xd9, 0xd7, 0x64, 0x0c,
1150+
0xf5, 0xa7, 0x0d, 0xab, 0x79, 0x03, 0xf1, 0x81
1151+
};
1152+
1153+
secp256k1_scalar exp_r1, exp_r2;
1154+
secp256k1_scalar r1, r2;
1155+
unsigned char seed0[32] = { 0 };
1156+
1157+
secp256k1_scalar_chacha20(&r1, &r2, seed0, 0);
1158+
secp256k1_scalar_set_b32(&exp_r1, &expected1[0], NULL);
1159+
secp256k1_scalar_set_b32(&exp_r2, &expected1[32], NULL);
1160+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1161+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1162+
1163+
secp256k1_scalar_chacha20(&r1, &r2, seed0, 1);
1164+
secp256k1_scalar_set_b32(&exp_r1, &expected2[0], NULL);
1165+
secp256k1_scalar_set_b32(&exp_r2, &expected2[32], NULL);
1166+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1167+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1168+
1169+
secp256k1_scalar_chacha20(&r1, &r2, seed3, 1);
1170+
secp256k1_scalar_set_b32(&exp_r1, &expected3[0], NULL);
1171+
secp256k1_scalar_set_b32(&exp_r2, &expected3[32], NULL);
1172+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1173+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1174+
1175+
secp256k1_scalar_chacha20(&r1, &r2, seed4, 2);
1176+
secp256k1_scalar_set_b32(&exp_r1, &expected4[0], NULL);
1177+
secp256k1_scalar_set_b32(&exp_r2, &expected4[32], NULL);
1178+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1179+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1180+
1181+
secp256k1_scalar_chacha20(&r1, &r2, seed5, 0x6ff8602a7a78e2f2ULL);
1182+
secp256k1_scalar_set_b32(&exp_r1, &expected5[0], NULL);
1183+
secp256k1_scalar_set_b32(&exp_r2, &expected5[32], NULL);
1184+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1185+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1186+
}
1187+
10801188
void run_scalar_tests(void) {
10811189
int i;
10821190
for (i = 0; i < 128 * count; i++) {
10831191
scalar_test();
10841192
}
10851193

1194+
scalar_chacha_tests();
1195+
10861196
{
10871197
/* (-1)+1 should be zero. */
10881198
secp256k1_scalar s, o;

0 commit comments

Comments
 (0)