diff --git a/src/fields/generic_common_impl.h b/src/fields/generic_common_impl.h index c98f11b..58b971e 100644 --- a/src/fields/generic_common_impl.h +++ b/src/fields/generic_common_impl.h @@ -44,7 +44,7 @@ templatetemplate Map(a); } /** Compute the inverse of x1. */ - Elem Inv(Elem a) const { return InvExtGCD(a); } + Elem Inv(Elem a) const { return InvExtGCD(a, B, MOD); } /** Generate a random field element. */ Elem FromSeed(uint64_t seed) const { diff --git a/src/int_utils.h b/src/int_utils.h index cbf983f..7e18024 100644 --- a/src/int_utils.h +++ b/src/int_utils.h @@ -211,13 +211,13 @@ template struct GFMulHelper template inline constexpr I GFMul(const I& a, const I& b) { return GFMulHelper::Run(a, b); } /** Compute the inverse of x using an extgcd algorithm. */ -template -inline I InvExtGCD(I x) +template +inline I InvExtGCD(I x, int bits, uint32_t mod) { if (F::IsZero(x)) return x; I t(0), newt(1); - I r(MOD), newr = x; - int rlen = BITS + 1, newrlen = F::Bits(newr, BITS); + I r(mod), newr = x; + int rlen = bits + 1, newrlen = F::Bits(newr, bits); while (newr) { int q = rlen - newrlen; r ^= F::Shift(newr, q);