Expose the equality proc beyond the debugConstantine flag

This commit is contained in:
Mamy André-Ratsimbazafy 2020-02-26 00:08:57 +01:00
parent 9f7c8515a4
commit e2096297cf
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
9 changed files with 34 additions and 19 deletions

View File

@ -67,13 +67,6 @@ template view*(a: var BigInt): BigIntViewMut =
debug: debug:
import strutils import strutils
func `==`*(a, b: BigInt): CTBool[Word] =
## Returns true if 2 big ints are equal
var accum: Word
for i in static(0 ..< a.limbs.len):
accum = accum or (a.limbs[i] xor b.limbs[i])
result = accum.isZero
func `$`*(a: BigInt): string = func `$`*(a: BigInt): string =
result = "BigInt[" result = "BigInt["
result.add $BigInt.bits result.add $BigInt.bits
@ -97,6 +90,14 @@ func setInternalBitLength*(a: var BigInt) =
## to that computed value. ## to that computed value.
a.bitLength = uint32 static(a.bits + a.bits div WordBitSize) a.bitLength = uint32 static(a.bits + a.bits div WordBitSize)
func `==`*(a, b: BigInt): CTBool[Word] =
## Returns true if 2 big ints are equal
## Comparison is constant-time
var accum: Word
for i in static(0 ..< a.limbs.len):
accum = accum or (a.limbs[i] xor b.limbs[i])
result = accum.isZero
func isZero*(a: BigInt): CTBool[Word] = func isZero*(a: BigInt): CTBool[Word] =
## Returns true if a big int is equal to zero ## Returns true if a big int is equal to zero
a.view.isZero a.view.isZero

View File

@ -216,6 +216,15 @@ debug:
# #
# ############################################################ # ############################################################
func `==`*(a, b: distinct BigIntViewAny): CTBool[Word] =
## Returns true if 2 big ints are equal
## Comparison is constant-time
checkMatchingBitlengths(a, b)
var accum: Word
for i in 0 ..< a.numLimbs():
accum = accum or (a[i] xor b[i])
result = accum.isZero
func isZero*(a: BigIntViewAny): CTBool[Word] = func isZero*(a: BigIntViewAny): CTBool[Word] =
## Returns true if a big int is equal to zero ## Returns true if a big int is equal to zero
var accum: Word var accum: Word
@ -336,7 +345,7 @@ func sub*(a: BigIntViewMut, b: BigIntViewAny): CTBool[Word] =
result = a[i].isMsbSet() result = a[i].isMsbSet()
a[i] = a[i].mask() a[i] = a[i].mask()
func sum*(r: BigIntViewMut, a, b: BigIntViewAny): CTBool[Word] = func sum*(r: BigIntViewMut, a, b: distinct BigIntViewAny): CTBool[Word] =
## Sum `a` and `b` into `r`. ## Sum `a` and `b` into `r`.
## `r` is initialized/overwritten ## `r` is initialized/overwritten
## ##
@ -350,7 +359,7 @@ func sum*(r: BigIntViewMut, a, b: BigIntViewAny): CTBool[Word] =
result = a[i].isMsbSet() result = a[i].isMsbSet()
r[i] = r[i].mask() r[i] = r[i].mask()
func diff*(r: BigIntViewMut, a, b: BigIntViewAny): CTBool[Word] = func diff*(r: BigIntViewMut, a, b: distinct BigIntViewAny): CTBool[Word] =
## Substract `b` from `a` and store the result into `r`. ## Substract `b` from `a` and store the result into `r`.
## `r` is initialized/overwritten ## `r` is initialized/overwritten
## ##

View File

@ -39,10 +39,6 @@ import
export Fp # defined in ../config/curves to avoid recursive module dependencies export Fp # defined in ../config/curves to avoid recursive module dependencies
debug: debug:
func `==`*(a, b: Fp): CTBool[Word] =
## Returns true if 2 big ints are equal
a.mres == b.mres
func `$`*[C: static Curve](a: Fp[C]): string = func `$`*[C: static Curve](a: Fp[C]): string =
result = "Fp[" & $C result = "Fp[" & $C
result.add "](" result.add "]("
@ -87,6 +83,10 @@ func toBig*(src: Fp): auto {.noInit.} =
# - Golden Primes (φ^2 - φ - 1 with φ = 2^k for example Ed448-Goldilocks: 2^448 - 2^224 - 1) # - Golden Primes (φ^2 - φ - 1 with φ = 2^k for example Ed448-Goldilocks: 2^448 - 2^224 - 1)
# exist and can be implemented with compile-time specialization. # exist and can be implemented with compile-time specialization.
func `==`*(a, b: Fp): CTBool[Word] =
## Constant-time equality check
a.mres == b.mres
func setZero*(a: var Fp) = func setZero*(a: var Fp) =
## Set ``a`` to zero ## Set ``a`` to zero
a.mres.setZero() a.mres.setZero()

View File

@ -7,7 +7,9 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import import
../arithmetic/finite_fields ../arithmetic/finite_fields,
../config/common,
../primitives/constant_time
# ############################################################ # ############################################################
# #
@ -40,6 +42,10 @@ type
x.c0 is BaseField x.c0 is BaseField
x.c1 is BaseField x.c1 is BaseField
func `==`*(a, b: QuadExtAddGroup): CTBool[Word] =
## Constant-time equality check
(a.c0 == b.c0) and (a.c1 == b.c1)
func setZero*(a: var QuadExtAddGroup) = func setZero*(a: var QuadExtAddGroup) =
## Set ``a`` to zero in the extension field ## Set ``a`` to zero in the extension field
## Coordinates 0 + 0 𝛼 ## Coordinates 0 + 0 𝛼
@ -93,6 +99,10 @@ type
x.c1 is BaseField x.c1 is BaseField
x.c2 is BaseField x.c2 is BaseField
func `==`*(a, b: CubicExtAddGroup): CTBool[Word] =
## Constant-time equality check
(a.c0 == b.c0) and (a.c1 == b.c1) and (a.c2 == b.c2)
func setZero*(a: var CubicExtAddGroup) = func setZero*(a: var CubicExtAddGroup) =
## Set ``a`` to zero in the extension field ## Set ``a`` to zero in the extension field
## Coordinates 0 + 0 w + 0 w² ## Coordinates 0 + 0 w + 0 w²

View File

@ -1 +0,0 @@
-d:debugConstantine

View File

@ -1 +0,0 @@
-d:debugConstantine

View File

@ -1,2 +1 @@
-d:testingCurves -d:testingCurves
-d:debugConstantine

View File

@ -1,2 +1 @@
-d:testingCurves -d:testingCurves
-d:debugConstantine

View File

@ -1,2 +1 @@
-d:testingCurves -d:testingCurves
-d:debugConstantine