Introduce convention, all for loop should have an explicit static range

This commit is contained in:
mratsim 2018-12-02 13:01:54 +01:00
parent 71e5b576c4
commit 408bc9b6f3
2 changed files with 3 additions and 3 deletions

View File

@ -75,7 +75,7 @@ const highLimb* = (not Ct[uint64](0)) shr 1
template addImpl[bits](result: CTBool[Limb], a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Limb]) = template addImpl[bits](result: CTBool[Limb], a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Limb]) =
## Constant-time big integer in-place addition ## Constant-time big integer in-place addition
## Returns if addition carried ## Returns if addition carried
for i in a.limbs.len: for i in static(0 ..< a.limbs.len):
let new_a = a.limbs[i] + b.limbs[i] + Limb(result) let new_a = a.limbs[i] + b.limbs[i] + Limb(result)
result = new_a.isMsbSet() result = new_a.isMsbSet()
a[i] = ctl.mux(new_a and highLimb, a) a[i] = ctl.mux(new_a and highLimb, a)
@ -93,7 +93,7 @@ func add*[bits](a: var BigInt[bits], b: static BigInt[bits], ctl: CTBool[Limb]):
template subImpl[bits](result: CTBool[Limb], a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Limb]) = template subImpl[bits](result: CTBool[Limb], a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Limb]) =
## Constant-time big integer in-place substraction ## Constant-time big integer in-place substraction
## Returns the "borrow flag" ## Returns the "borrow flag"
for i in a.limbs.len: for i in static(0 ..< a.limbs.len):
let new_a = a.limbs[i] - b.limbs[i] - Limb(result) let new_a = a.limbs[i] - b.limbs[i] - Limb(result)
result = new_a.isMsbSet() result = new_a.isMsbSet()
a[i] = ctl.mux(new_a and highLimb, a) a[i] = ctl.mux(new_a and highLimb, a)

View File

@ -122,5 +122,5 @@ func montyMagic*(M: static BigInt): static Limb =
k = fastLog2(LimbBitSize) k = fastLog2(LimbBitSize)
result = M0 # Start from an inverse of M0 modulo 2, M0 is odd and it's own inverse result = M0 # Start from an inverse of M0 modulo 2, M0 is odd and it's own inverse
for _ in 0 ..< k: for _ in static(0 ..< k):
result *= 2 + M * result # x' = x(2 + ax) (`+` to avoid negating at the end) result *= 2 + M * result # x' = x(2 + ax) (`+` to avoid negating at the end)