Skip to content

Commit

Permalink
Avoid overflowing shift by special casing inverse of 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and fanquake committed Apr 10, 2024
1 parent e97dfd1 commit c03fd4e
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/minisketch/src/int_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class BitsInt {
}

static constexpr inline bool IsZero(I a) { return a == 0; }
static constexpr inline bool IsOne(I a) { return a == 1; }
static constexpr inline I Mask(I val) { return val & MASK; }
static constexpr inline I Shift(I val, int bits) { return ((val << bits) & MASK); }
static constexpr inline I UnsafeShift(I val, int bits) { return (val << bits); }
Expand Down Expand Up @@ -234,7 +235,7 @@ template<typename I, int N, typename L, typename F> inline constexpr I GFMul(con
template<typename I, typename F, int BITS, uint32_t MOD>
inline I InvExtGCD(I x)
{
if (F::IsZero(x)) return x;
if (F::IsZero(x) || F::IsOne(x)) return x;
I t(0), newt(1);
I r(MOD), newr = x;
int rlen = BITS + 1, newrlen = F::Bits(newr, BITS);
Expand Down

0 comments on commit c03fd4e

Please sign in to comment.