Skip to content

Commit fa30274

Browse files
apoelstrajonasnick
authored andcommitted
add chacha20 function
1 parent 1e6f1f5 commit fa30274

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-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, size_t idx);
108+
106109
#endif /* SECP256K1_SCALAR_H */

src/scalar_4x64_impl.h

Lines changed: 92 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 "scalar.h"
11+
1012
/* Limbs of the secp256k1 order. */
1113
#define SECP256K1_N_0 ((uint64_t)0xBFD25E8CD0364141ULL)
1214
#define SECP256K1_N_1 ((uint64_t)0xBAAEDCE6AF48A03BULL)
@@ -946,4 +948,94 @@ SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r,
946948
secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 6] >> ((shift - 1) & 0x3f)) & 1);
947949
}
948950

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

962962
}
963963

964+
void scalar_chacha_tests(void) {
965+
/* Test vectors from
966+
* https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
967+
* */
968+
unsigned char expected1[64] = {
969+
0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
970+
0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
971+
0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
972+
0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
973+
0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
974+
0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
975+
0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
976+
0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86
977+
};
978+
unsigned char expected2[64] = {
979+
0x45, 0x40, 0xf0, 0x5a, 0x9f, 0x1f, 0xb2, 0x96,
980+
0xd7, 0x73, 0x6e, 0x7b, 0x20, 0x8e, 0x3c, 0x96,
981+
0xeb, 0x4f, 0xe1, 0x83, 0x46, 0x88, 0xd2, 0x60,
982+
0x4f, 0x45, 0x09, 0x52, 0xed, 0x43, 0x2d, 0x41,
983+
0xbb, 0xe2, 0xa0, 0xb6, 0xea, 0x75, 0x66, 0xd2,
984+
0xa5, 0xd1, 0xe7, 0xe2, 0x0d, 0x42, 0xaf, 0x2c,
985+
0x53, 0xd7, 0x92, 0xb1, 0xc4, 0x3f, 0xea, 0x81,
986+
0x7e, 0x9a, 0xd2, 0x75, 0xae, 0x54, 0x69, 0x63
987+
};
988+
unsigned char expected3[64] = {
989+
0x47, 0x4a, 0x4f, 0x35, 0x4f, 0xee, 0x93, 0x59,
990+
0xbb, 0x65, 0x81, 0xe5, 0xd9, 0x15, 0xa6, 0x01,
991+
0xb6, 0x8c, 0x68, 0x03, 0x38, 0xff, 0x65, 0xe6,
992+
0x56, 0x4a, 0x3e, 0x65, 0x59, 0xfc, 0x12, 0x3f,
993+
0xa9, 0xb2, 0xf9, 0x3e, 0x57, 0xc3, 0xa5, 0xcb,
994+
0xe0, 0x72, 0x74, 0x27, 0x88, 0x1c, 0x23, 0xdf,
995+
0xe2, 0xb6, 0xcc, 0xfb, 0x93, 0xed, 0xcb, 0x02,
996+
0xd7, 0x50, 0x52, 0x45, 0x84, 0x88, 0xbb, 0xea
997+
};
998+
999+
secp256k1_scalar exp_r1, exp_r2;
1000+
secp256k1_scalar r1, r2;
1001+
unsigned char seed1[32] = { 0 };
1002+
1003+
secp256k1_scalar_chacha20(&r1, &r2, seed1, 0);
1004+
secp256k1_scalar_set_b32(&exp_r1, &expected1[0], NULL);
1005+
secp256k1_scalar_set_b32(&exp_r2, &expected1[32], NULL);
1006+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1007+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1008+
1009+
seed1[31] = 1;
1010+
secp256k1_scalar_chacha20(&r1, &r2, seed1, 0);
1011+
secp256k1_scalar_set_b32(&exp_r1, &expected2[0], NULL);
1012+
secp256k1_scalar_set_b32(&exp_r2, &expected2[32], NULL);
1013+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1014+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1015+
1016+
secp256k1_scalar_chacha20(&r1, &r2, seed1, 100);
1017+
secp256k1_scalar_set_b32(&exp_r1, &expected3[0], NULL);
1018+
secp256k1_scalar_set_b32(&exp_r2, &expected3[32], NULL);
1019+
CHECK(secp256k1_scalar_eq(&exp_r1, &r1));
1020+
CHECK(secp256k1_scalar_eq(&exp_r2, &r2));
1021+
}
1022+
9641023
void run_scalar_tests(void) {
9651024
int i;
9661025
for (i = 0; i < 128 * count; i++) {
9671026
scalar_test();
9681027
}
9691028

1029+
scalar_chacha_tests();
1030+
9701031
{
9711032
/* (-1)+1 should be zero. */
9721033
secp256k1_scalar s, o;

0 commit comments

Comments
 (0)