Skip to content

Commit 9e3dc93

Browse files
committed
refactor: preserve unsigned arithmetic and add VERIFY_CHECKs before casts in scalar_4x64_impl.h
1 parent 4a84fb7 commit 9e3dc93

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/scalar_4x64_impl.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar *r, unsigne
9292
}
9393

9494
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) {
95-
int overflow;
95+
uint64_t overflow;
9696
secp256k1_uint128 t;
9797
SECP256K1_SCALAR_VERIFY(a);
9898
SECP256K1_SCALAR_VERIFY(b);
@@ -109,12 +109,12 @@ static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a,
109109
secp256k1_u128_accum_u64(&t, a->d[3]);
110110
secp256k1_u128_accum_u64(&t, b->d[3]);
111111
r->d[3] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
112-
overflow = (int)secp256k1_u128_to_u64(&t) + secp256k1_scalar_check_overflow(r);
112+
overflow = secp256k1_u128_to_u64(&t) + secp256k1_scalar_check_overflow(r);
113113
VERIFY_CHECK(overflow == 0 || overflow == 1);
114-
secp256k1_scalar_reduce(r, overflow);
114+
secp256k1_scalar_reduce(r, (unsigned int)overflow);
115115

116116
SECP256K1_SCALAR_VERIFY(r);
117-
return overflow;
117+
return (int)overflow;
118118
}
119119

120120
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
@@ -654,8 +654,8 @@ static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l)
654654
muladd_fast(m6, SECP256K1_N_C_1);
655655
sumadd_fast(m5);
656656
extract_fast(p3);
657+
VERIFY_CHECK(c0 <= 1 && m6 <= 1);
657658
p4 = (uint32_t)c0 + m6;
658-
VERIFY_CHECK(p4 <= 2);
659659

660660
/* Reduce 258 bits into 256. */
661661
/* r[0..3] = p[0..3] + p[4] * SECP256K1_N_C. */
@@ -674,6 +674,7 @@ static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l)
674674
#endif
675675

676676
/* Final reduction of r. */
677+
VERIFY_CHECK(c <= 1);
677678
secp256k1_scalar_reduce(r, (unsigned int)c + secp256k1_scalar_check_overflow(r));
678679
}
679680

0 commit comments

Comments
 (0)