From 236047767f261289159bf16ba5235683990c203f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Tue, 18 Feb 2020 13:01:14 +0100 Subject: [PATCH] move mask to common --- constantine/config/common.nim | 5 ++++- constantine/io/io_bigints.nim | 4 ++-- constantine/math/precomputed.nim | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/constantine/config/common.nim b/constantine/config/common.nim index 5d0945f..e3a1ace 100644 --- a/constantine/config/common.nim +++ b/constantine/config/common.nim @@ -31,12 +31,15 @@ const Zero* = Word(0) One* = Word(1) - MaxWord* = (not Zero) shr 1 + MaxWord* = (not Zero) shr (WordPhysBitSize - WordBitSize) ## This represents 0x7F_FF_FF_FF__FF_FF_FF_FF ## also 0b0111...1111 ## This biggest representable number in our limbs. ## i.e. The most significant bit is never set at the end of each function +template mask*(w: Word): Word = + w and MaxWord + # ############################################################ # # Instrumentation diff --git a/constantine/io/io_bigints.nim b/constantine/io/io_bigints.nim index 564175b..e68e8d3 100644 --- a/constantine/io/io_bigints.nim +++ b/constantine/io/io_bigints.nim @@ -51,7 +51,7 @@ func fromRawUintLE( # if full, dump if acc_len >= WordBitSize: - dst.limbs[dst_idx] = acc and MaxWord + dst.limbs[dst_idx] = mask(acc) inc dst_idx acc_len -= WordBitSize acc = src_byte shr (8 - acc_len) @@ -88,7 +88,7 @@ func fromRawUintBE( # if full, dump if acc_len >= WordBitSize: - dst.limbs[dst_idx] = acc and MaxWord + dst.limbs[dst_idx] = mask(acc) inc dst_idx acc_len -= WordBitSize acc = src_byte shr (8 - acc_len) diff --git a/constantine/math/precomputed.nim b/constantine/math/precomputed.nim index e6c7e28..dcbdef9 100644 --- a/constantine/math/precomputed.nim +++ b/constantine/math/precomputed.nim @@ -39,14 +39,14 @@ func double(a: var BigInt): bool = for i in 0 ..< a.limbs.len: var z = BaseType(a.limbs[i]) * 2 + BaseType(result) result = z.isMsbSet() - a.limbs[i] = Word(z) and MaxWord + a.limbs[i] = mask(Word(z)) func sub(a: var BigInt, b: BigInt, ctl: bool): bool = ## In-place optional substraction for i in 0 ..< a.limbs.len: let new_a = BaseType(a.limbs[i]) - BaseType(b.limbs[i]) - BaseType(result) result = new_a.isMsbSet() - a.limbs[i] = if ctl: new_a.Word and MaxWord + a.limbs[i] = if ctl: new_a.Word.mask() else: a.limbs[i] func doubleMod(a: var BigInt, M: BigInt) =