From 1d92ab1f480c8029e8bf2402040504d65c8346a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Sun, 9 Feb 2020 13:02:09 +0100 Subject: [PATCH] Remove the limb accessor templates, introduce config file for debug template --- constantine/bigints.nim | 16 +++++----------- constantine/config.nim | 17 +++++++++++++++++ constantine/io.nim | 6 +++--- tests/test_io.nim | 8 ++++---- 4 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 constantine/config.nim diff --git a/constantine/bigints.nim b/constantine/bigints.nim index 8c7e9c0..fde379f 100644 --- a/constantine/bigints.nim +++ b/constantine/bigints.nim @@ -37,7 +37,7 @@ # So the least significant limb is limb[0] # This is independent from the base type endianness. -import ./word_types +import ./word_types, ./config type Word* = Ct[uint64] type BaseType* = uint64 # Exported type for conversion in "normal integers" @@ -79,12 +79,6 @@ const MaxWord* = (not Ct[uint64](0)) shr 1 ## This biggest representable number in our limbs. ## i.e. The most significant bit is never set at the end of each function -template `[]`*(a: Bigint, idx: int): Word = - a.limbs[idx] - -template `[]=`*(a: var Bigint, idx: int, w: Word) = - a.limbs[idx] = w - # ############################################################ # # BigInt primitives @@ -113,20 +107,20 @@ func `==`*(a, b: BigInt): CTBool[Word] = # if it is a placebo operation. It stills performs the # same memory accesses to be side-channel attack resistant. -func add*[bits](a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Word]): CTBool[Word] {.raises: [].}= +func add*[bits](a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Word]): CTBool[Word] = ## Constant-time big integer in-place optional addition ## The addition is only performed if ctl is "true" ## The result carry is always computed. for i in static(0 ..< a.limbs.len): let new_a = a.limbs[i] + b.limbs[i] + Word(result) result = new_a.isMsbSet() - a[i] = ctl.mux(new_a and MaxWord, a[i]) + a.limbs[i] = ctl.mux(new_a and MaxWord, a.limbs[i]) -func sub*[bits](a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Word]): CTBool[Word] {.raises: [].}= +func sub*[bits](a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Word]): CTBool[Word] = ## Constant-time big integer in-place optional substraction ## The substraction is only performed if ctl is "true" ## The result carry is always computed. for i in static(0 ..< a.limbs.len): let new_a = a.limbs[i] - b.limbs[i] - Word(result) result = new_a.isMsbSet() - a[i] = ctl.mux(new_a and MaxWord, a[i]) + a.limbs[i] = ctl.mux(new_a and MaxWord, a.limbs[i]) diff --git a/constantine/config.nim b/constantine/config.nim new file mode 100644 index 0000000..dbe30ff --- /dev/null +++ b/constantine/config.nim @@ -0,0 +1,17 @@ +# Constantine +# Copyright (c) 2018-2019 Status Research & Development GmbH +# Copyright (c) 2020-Present Mamy André-Ratsimbazafy +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +# ############################################################ +# +# Common configuration +# +# ############################################################ + +template debug*(body: untyped): untyped = + when defined(debugConstantine): + body diff --git a/constantine/io.nim b/constantine/io.nim index cb6d4bc..8430a58 100644 --- a/constantine/io.nim +++ b/constantine/io.nim @@ -44,13 +44,13 @@ func parseRawUintLE( # if full, dump if acc_len >= WordBitSize: - result[dst_idx] = acc and MaxWord + result.limbs[dst_idx] = acc and MaxWord inc dst_idx acc_len -= WordBitSize acc = src_byte shr (8 - acc_len) if acc_len != 0: - result[dst_idx] = acc + result.limbs[dst_idx] = acc func parseRawUint*( src: openarray[byte], @@ -103,7 +103,7 @@ func dumpRawUintLE( var tail = dst.len while tail > 0: - let w = if src_idx < src.limbs.len: src[src_idx].BaseType + let w = if src_idx < src.limbs.len: src.limbs[src_idx].BaseType else: 0 inc src_idx diff --git a/tests/test_io.nim b/tests/test_io.nim index fe9db54..afa1467 100644 --- a/tests/test_io.nim +++ b/tests/test_io.nim @@ -20,8 +20,8 @@ suite "IO": let big = parseRawUint(x_bytes, 64, cpuEndian) check: - T(big[0]) == 0 - T(big[1]) == 0 + T(big.limbs[0]) == 0 + T(big.limbs[1]) == 0 block: # 2^63 is properly represented on 2 limbs let x = 1'u64 shl 63 @@ -29,8 +29,8 @@ suite "IO": let big = parseRawUint(x_bytes, 64, cpuEndian) check: - T(big[0]) == 0 - T(big[1]) == 1 + T(big.limbs[0]) == 0 + T(big.limbs[1]) == 1 test "Parsing and dumping round-trip on uint64": block: