From 653d8b2e260e8b51fe372ccf4095928dcd120c95 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 3 Apr 2024 10:50:22 -0400 Subject: [PATCH] Avoid overflowing shift by special casing inverse of 1 --- src/int_utils.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/int_utils.h b/src/int_utils.h index bd70c90..2b3d8cb 100644 --- a/src/int_utils.h +++ b/src/int_utils.h @@ -194,6 +194,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); } @@ -252,7 +253,7 @@ template inline constexpr I GFMul(con template 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);