From 0db0b56872dc024cc22f14f57e2e19dc88ecc4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Sat, 5 Jun 2021 00:21:20 +0100 Subject: [PATCH] arithmetic: fix PerThing::pow --- primitives/arithmetic/src/per_things.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/primitives/arithmetic/src/per_things.rs b/primitives/arithmetic/src/per_things.rs index 81cefac5141da..f305ed95c5840 100644 --- a/primitives/arithmetic/src/per_things.rs +++ b/primitives/arithmetic/src/per_things.rs @@ -639,21 +639,22 @@ macro_rules! implement_per_thing { impl Pow for $name { type Output = Self; - fn pow(self, exp: usize) -> Self::Output { + fn pow(mut self, mut exp: usize) -> Self::Output { if exp == 0 || self.is_one() { return Self::one() } - let mut result = self; - let mut exp = exp - 1; - while exp > 0 && !result.is_zero() { - if exp % 2 == 0 { - result = result.square(); - exp /= 2; - } else { + + let mut result = Self::one(); + while exp > 0 { + if exp % 2 != 0 { result = result * self; exp -= 1; } + + self = self.square(); + exp /= 2; } + result } }