Skip to content

Commit 21c188b

Browse files
Merge bitcoin-core/secp256k1#943: VERIFY_CHECK precondition for secp256k1_fe_set_int.
2888640 VERIFY_CHECK precondition for secp256k1_fe_set_int. (Russell O'Connor) d49011f Make _set_fe_int( . , 0 ) set magnitude to 0 (Tim Ruffing) Pull request description: Also set the magnitude to 0 when setting the value to 0. ACKs for top commit: real-or-random: ACK 2888640 jonasnick: ACK 2888640 Tree-SHA512: 6ec9b3485380503b11c00f30bfa79f92ba3facb93ee4f3df582b881c4e19fb8ae8b5acd5aeb6326497c290cd0904230d0356f33bd136ca577d2f25616279e090
2 parents 3e7b2ea + 2888640 commit 21c188b

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/field.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* - Each field element can be normalized or not.
1515
* - Each field element has a magnitude, which represents how far away
1616
* its representation is away from normalization. Normalized elements
17-
* always have a magnitude of 1, but a magnitude of 1 doesn't imply
18-
* normality.
17+
* always have a magnitude of 0 or 1, but a magnitude of 1 doesn't
18+
* imply normality.
1919
*/
2020

2121
#if defined HAVE_CONFIG_H
@@ -50,7 +50,9 @@ static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r);
5050
* without constant-time guarantee. */
5151
static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r);
5252

53-
/** Set a field element equal to a small integer. Resulting field element is normalized. */
53+
/** Set a field element equal to a small (not greater than 0x7FFF), non-negative integer.
54+
* Resulting field element is normalized; it has magnitude 0 if a == 0, and magnitude 1 otherwise.
55+
*/
5456
static void secp256k1_fe_set_int(secp256k1_fe *r, int a);
5557

5658
/** Sets a field element equal to zero, initializing all fields. */

src/field_10x26_impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,11 @@ static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r) {
264264
}
265265

266266
SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
267+
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
267268
r->n[0] = a;
268269
r->n[1] = r->n[2] = r->n[3] = r->n[4] = r->n[5] = r->n[6] = r->n[7] = r->n[8] = r->n[9] = 0;
269270
#ifdef VERIFY
270-
r->magnitude = 1;
271+
r->magnitude = (a != 0);
271272
r->normalized = 1;
272273
secp256k1_fe_verify(r);
273274
#endif

src/field_5x52_impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,11 @@ static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r) {
227227
}
228228

229229
SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe *r, int a) {
230+
VERIFY_CHECK(0 <= a && a <= 0x7FFF);
230231
r->n[0] = a;
231232
r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
232233
#ifdef VERIFY
233-
r->magnitude = 1;
234+
r->magnitude = (a != 0);
234235
r->normalized = 1;
235236
secp256k1_fe_verify(r);
236237
#endif

0 commit comments

Comments
 (0)