Skip to content

Commit 9ab2cbe

Browse files
committed
Add scalar_set_b32_seckey which does the same as scalar_set_b32 and also returns whether it's a valid secret key
1 parent 4f27e34 commit 9ab2cbe

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/scalar.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, uns
3939
*/
4040
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow);
4141

42+
/** Set a scalar from a big endian byte array and returns 1 if it is a valid
43+
* seckey and 0 otherwise. */
44+
static int secp256k1_scalar_set_b32_seckey(secp256k1_scalar *r, const unsigned char *bin);
45+
4246
/** Set a scalar to an unsigned integer. */
4347
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v);
4448

src/scalar_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ static void secp256k1_scalar_order_get_num(secp256k1_num *r) {
5555
}
5656
#endif
5757

58+
static int secp256k1_scalar_set_b32_seckey(secp256k1_scalar *r, const unsigned char *bin) {
59+
int overflow;
60+
secp256k1_scalar_set_b32(r, bin, &overflow);
61+
return (!overflow) & (!secp256k1_scalar_is_zero(r));
62+
}
63+
5864
static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x) {
5965
#if defined(EXHAUSTIVE_TEST_ORDER)
6066
int i;

src/tests.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ void random_scalar_order(secp256k1_scalar *num) {
140140
} while(1);
141141
}
142142

143+
void random_scalar_order_b32(unsigned char *b32) {
144+
secp256k1_scalar num;
145+
random_scalar_order(&num);
146+
secp256k1_scalar_get_b32(b32, &num);
147+
}
148+
143149
void run_context_tests(int use_prealloc) {
144150
secp256k1_pubkey pubkey;
145151
secp256k1_pubkey zero_pubkey;
@@ -1077,11 +1083,31 @@ void scalar_test(void) {
10771083

10781084
}
10791085

1086+
void run_scalar_set_b32_seckey_tests(void) {
1087+
unsigned char b32[32];
1088+
secp256k1_scalar s1;
1089+
secp256k1_scalar s2;
1090+
1091+
/* Usually set_b32 and set_b32_seckey give the same result */
1092+
random_scalar_order_b32(b32);
1093+
secp256k1_scalar_set_b32(&s1, b32, NULL);
1094+
CHECK(secp256k1_scalar_set_b32_seckey(&s2, b32) == 1);
1095+
CHECK(secp256k1_scalar_eq(&s1, &s2) == 1);
1096+
1097+
memset(b32, 0, sizeof(b32));
1098+
CHECK(secp256k1_scalar_set_b32_seckey(&s2, b32) == 0);
1099+
memset(b32, 0xFF, sizeof(b32));
1100+
CHECK(secp256k1_scalar_set_b32_seckey(&s2, b32) == 0);
1101+
}
1102+
10801103
void run_scalar_tests(void) {
10811104
int i;
10821105
for (i = 0; i < 128 * count; i++) {
10831106
scalar_test();
10841107
}
1108+
for (i = 0; i < count; i++) {
1109+
run_scalar_set_b32_seckey_tests();
1110+
}
10851111

10861112
{
10871113
/* (-1)+1 should be zero. */

0 commit comments

Comments
 (0)