From 408bc9b6f3776c8c89e8cfb337f8a4198a2d6da9 Mon Sep 17 00:00:00 2001 From: mratsim Date: Sun, 2 Dec 2018 13:01:54 +0100 Subject: [PATCH] Introduce convention, all for loop should have an explicit static range --- constantine/bigints.nim | 4 ++-- constantine/field_fp.nim | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/constantine/bigints.nim b/constantine/bigints.nim index bde92c1..c0d5c73 100644 --- a/constantine/bigints.nim +++ b/constantine/bigints.nim @@ -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]) = ## Constant-time big integer in-place addition ## 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) result = new_a.isMsbSet() 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]) = ## Constant-time big integer in-place substraction ## 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) result = new_a.isMsbSet() a[i] = ctl.mux(new_a and highLimb, a) diff --git a/constantine/field_fp.nim b/constantine/field_fp.nim index 94db60f..fa72a02 100644 --- a/constantine/field_fp.nim +++ b/constantine/field_fp.nim @@ -122,5 +122,5 @@ func montyMagic*(M: static BigInt): static Limb = k = fastLog2(LimbBitSize) 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)