math.big: fix the order of calculations in mod_pow() to improve performance (#24935)

This commit is contained in:
Mike 2025-07-20 15:27:20 +03:00 committed by GitHub
parent e3d047822d
commit 987c211517
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -605,9 +605,9 @@ pub fn (base Integer) mod_pow(exponent u32, modulus Integer) Integer {
mut y := one_int mut y := one_int
for n > 1 { for n > 1 {
if n & 1 == 1 { if n & 1 == 1 {
y *= x % modulus y = (y * x) % modulus
} }
x *= x % modulus x = (x * x) % modulus
n >>= 1 n >>= 1
} }
return x * y % modulus return x * y % modulus