From e2096297cf35b21513cff3282f3680c4be059a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Wed, 26 Feb 2020 00:08:57 +0100 Subject: [PATCH] Expose the equality proc beyond the debugConstantine flag --- constantine/arithmetic/bigints_checked.nim | 15 ++++++++------- constantine/arithmetic/bigints_raw.nim | 13 +++++++++++-- constantine/arithmetic/finite_fields.nim | 8 ++++---- .../tower_field_extensions/abelian_groups.nim | 12 +++++++++++- tests/test_bigints.nim.cfg | 1 - tests/test_bigints_multimod.nim.cfg | 1 - tests/test_finite_fields.nim.cfg | 1 - tests/test_finite_fields_powinv.nim.cfg | 1 - tests/test_io_fields.nim.cfg | 1 - 9 files changed, 34 insertions(+), 19 deletions(-) delete mode 100644 tests/test_bigints.nim.cfg delete mode 100644 tests/test_bigints_multimod.nim.cfg diff --git a/constantine/arithmetic/bigints_checked.nim b/constantine/arithmetic/bigints_checked.nim index 4e09c85..f496bdc 100644 --- a/constantine/arithmetic/bigints_checked.nim +++ b/constantine/arithmetic/bigints_checked.nim @@ -67,13 +67,6 @@ template view*(a: var BigInt): BigIntViewMut = debug: 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 = result = "BigInt[" result.add $BigInt.bits @@ -97,6 +90,14 @@ func setInternalBitLength*(a: var BigInt) = ## to that computed value. 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] = ## Returns true if a big int is equal to zero a.view.isZero diff --git a/constantine/arithmetic/bigints_raw.nim b/constantine/arithmetic/bigints_raw.nim index 9ba725b..57f59ce 100644 --- a/constantine/arithmetic/bigints_raw.nim +++ b/constantine/arithmetic/bigints_raw.nim @@ -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] = ## Returns true if a big int is equal to zero var accum: Word @@ -336,7 +345,7 @@ func sub*(a: BigIntViewMut, b: BigIntViewAny): CTBool[Word] = result = a[i].isMsbSet() 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`. ## `r` is initialized/overwritten ## @@ -350,7 +359,7 @@ func sum*(r: BigIntViewMut, a, b: BigIntViewAny): CTBool[Word] = result = a[i].isMsbSet() 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`. ## `r` is initialized/overwritten ## diff --git a/constantine/arithmetic/finite_fields.nim b/constantine/arithmetic/finite_fields.nim index e9271a5..e66f3ce 100644 --- a/constantine/arithmetic/finite_fields.nim +++ b/constantine/arithmetic/finite_fields.nim @@ -39,10 +39,6 @@ import export Fp # defined in ../config/curves to avoid recursive module dependencies 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 = result = "Fp[" & $C 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) # 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) = ## Set ``a`` to zero a.mres.setZero() diff --git a/constantine/tower_field_extensions/abelian_groups.nim b/constantine/tower_field_extensions/abelian_groups.nim index 9c13061..21540b1 100644 --- a/constantine/tower_field_extensions/abelian_groups.nim +++ b/constantine/tower_field_extensions/abelian_groups.nim @@ -7,7 +7,9 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - ../arithmetic/finite_fields + ../arithmetic/finite_fields, + ../config/common, + ../primitives/constant_time # ############################################################ # @@ -40,6 +42,10 @@ type x.c0 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) = ## Set ``a`` to zero in the extension field ## Coordinates 0 + 0 𝛼 @@ -93,6 +99,10 @@ type x.c1 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) = ## Set ``a`` to zero in the extension field ## Coordinates 0 + 0 w + 0 w² diff --git a/tests/test_bigints.nim.cfg b/tests/test_bigints.nim.cfg deleted file mode 100644 index dd68656..0000000 --- a/tests/test_bigints.nim.cfg +++ /dev/null @@ -1 +0,0 @@ --d:debugConstantine diff --git a/tests/test_bigints_multimod.nim.cfg b/tests/test_bigints_multimod.nim.cfg deleted file mode 100644 index dd68656..0000000 --- a/tests/test_bigints_multimod.nim.cfg +++ /dev/null @@ -1 +0,0 @@ --d:debugConstantine diff --git a/tests/test_finite_fields.nim.cfg b/tests/test_finite_fields.nim.cfg index 92fac8a..0922c18 100644 --- a/tests/test_finite_fields.nim.cfg +++ b/tests/test_finite_fields.nim.cfg @@ -1,2 +1 @@ -d:testingCurves --d:debugConstantine diff --git a/tests/test_finite_fields_powinv.nim.cfg b/tests/test_finite_fields_powinv.nim.cfg index 92fac8a..0922c18 100644 --- a/tests/test_finite_fields_powinv.nim.cfg +++ b/tests/test_finite_fields_powinv.nim.cfg @@ -1,2 +1 @@ -d:testingCurves --d:debugConstantine diff --git a/tests/test_io_fields.nim.cfg b/tests/test_io_fields.nim.cfg index 92fac8a..0922c18 100644 --- a/tests/test_io_fields.nim.cfg +++ b/tests/test_io_fields.nim.cfg @@ -1,2 +1 @@ -d:testingCurves --d:debugConstantine