Remove the limb accessor templates, introduce config file for debug template

This commit is contained in:
Mamy André-Ratsimbazafy 2020-02-09 13:02:09 +01:00
parent ff8b22e1d1
commit 1d92ab1f48
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
4 changed files with 29 additions and 18 deletions

View File

@ -37,7 +37,7 @@
# So the least significant limb is limb[0] # So the least significant limb is limb[0]
# This is independent from the base type endianness. # This is independent from the base type endianness.
import ./word_types import ./word_types, ./config
type Word* = Ct[uint64] type Word* = Ct[uint64]
type BaseType* = uint64 # Exported type for conversion in "normal integers" 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. ## This biggest representable number in our limbs.
## i.e. The most significant bit is never set at the end of each function ## 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 # BigInt primitives
@ -113,20 +107,20 @@ func `==`*(a, b: BigInt): CTBool[Word] =
# if it is a placebo operation. It stills performs the # if it is a placebo operation. It stills performs the
# same memory accesses to be side-channel attack resistant. # 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 ## Constant-time big integer in-place optional addition
## The addition is only performed if ctl is "true" ## The addition is only performed if ctl is "true"
## The result carry is always computed. ## The result carry is always computed.
for i in static(0 ..< a.limbs.len): for i in static(0 ..< a.limbs.len):
let new_a = a.limbs[i] + b.limbs[i] + Word(result) let new_a = a.limbs[i] + b.limbs[i] + Word(result)
result = new_a.isMsbSet() 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 ## Constant-time big integer in-place optional substraction
## The substraction is only performed if ctl is "true" ## The substraction is only performed if ctl is "true"
## The result carry is always computed. ## The result carry is always computed.
for i in static(0 ..< a.limbs.len): for i in static(0 ..< a.limbs.len):
let new_a = a.limbs[i] - b.limbs[i] - Word(result) let new_a = a.limbs[i] - b.limbs[i] - Word(result)
result = new_a.isMsbSet() 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])

17
constantine/config.nim Normal file
View File

@ -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

View File

@ -44,13 +44,13 @@ func parseRawUintLE(
# if full, dump # if full, dump
if acc_len >= WordBitSize: if acc_len >= WordBitSize:
result[dst_idx] = acc and MaxWord result.limbs[dst_idx] = acc and MaxWord
inc dst_idx inc dst_idx
acc_len -= WordBitSize acc_len -= WordBitSize
acc = src_byte shr (8 - acc_len) acc = src_byte shr (8 - acc_len)
if acc_len != 0: if acc_len != 0:
result[dst_idx] = acc result.limbs[dst_idx] = acc
func parseRawUint*( func parseRawUint*(
src: openarray[byte], src: openarray[byte],
@ -103,7 +103,7 @@ func dumpRawUintLE(
var tail = dst.len var tail = dst.len
while tail > 0: 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 else: 0
inc src_idx inc src_idx

View File

@ -20,8 +20,8 @@ suite "IO":
let big = parseRawUint(x_bytes, 64, cpuEndian) let big = parseRawUint(x_bytes, 64, cpuEndian)
check: check:
T(big[0]) == 0 T(big.limbs[0]) == 0
T(big[1]) == 0 T(big.limbs[1]) == 0
block: # 2^63 is properly represented on 2 limbs block: # 2^63 is properly represented on 2 limbs
let x = 1'u64 shl 63 let x = 1'u64 shl 63
@ -29,8 +29,8 @@ suite "IO":
let big = parseRawUint(x_bytes, 64, cpuEndian) let big = parseRawUint(x_bytes, 64, cpuEndian)
check: check:
T(big[0]) == 0 T(big.limbs[0]) == 0
T(big[1]) == 1 T(big.limbs[1]) == 1
test "Parsing and dumping round-trip on uint64": test "Parsing and dumping round-trip on uint64":
block: block: