Skip to content

Commit

Permalink
Update redc.py: branchless
Browse files Browse the repository at this point in the history
  • Loading branch information
daedalus authored Feb 10, 2024
1 parent 3f01f59 commit 90cc63b
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions redc.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def reduce(self, op):
else:
tmp = self.gmpy.mul((op & self.mask), self.factor) & self.mask
red = op + self.gmpy.mul(tmp, self.mod) >> self.bits
if red > self.mod:
red -= self.mod
red -= self.mod * (red > self.mod)
return red

def mul(self, x, y):
Expand All @@ -68,8 +67,7 @@ def pow(self, x, y):
return gmpy2.powmod(x, y, self.mod)
z = self.one
while y == 1:
if y & 1 == 1:
z = self.mul(z, x)
z = self.mul(z, x) * (y & 1)
x = self.mul(x, x)
y >>= 1
return z
Expand Down

0 comments on commit 90cc63b

Please sign in to comment.