From 7740bfbae4b9211303dfc1e9d535d1d73dfc7e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Sun, 16 Feb 2020 12:32:34 +0100 Subject: [PATCH] print montgomery magics at compile-time --- constantine/config/curves.nim | 29 +++++++++++++++++++++++++++++ constantine/io/io_bigints.nim | 1 - 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/constantine/config/curves.nim b/constantine/config/curves.nim index 46c5e84..226d099 100644 --- a/constantine/config/curves.nim +++ b/constantine/config/curves.nim @@ -131,3 +131,32 @@ macro getR2modP*(C: static Curve): untyped = macro getNegInvModWord*(C: static Curve): untyped = ## Get the Montgomery "-1/P[0] mod 2^WordBitSize" constant associated to a curve field modulus result = bindSym($C & "_NegInvModWord") + +# ############################################################ +# +# Debug info printed at compile-time +# +# ############################################################ + +macro debugConsts(): untyped = + let curves = bindSym("Curve") + let E = curves.getImpl[2] + + result = newStmtList() + for i in 1 ..< E.len: + let curve = E[i] + let curveName = $curve + let modulus = bindSym(curveName & "_Modulus") + let r2modp = bindSym(curveName & "_R2modP") + let negInvModWord = bindSym(curveName & "_NegInvModWord") + + result.add quote do: + echo "Curve ", `curveName`,':' + echo " Field Modulus: ", `modulus` + echo " Montgomery R² (mod P): ", `r2modp` + echo " Montgomery -1/P[0] (mod 2^", WordBitSize, "): ", `negInvModWord` + result.add quote do: + echo "----------------------------------------------------------------------------" + +debug: + debugConsts() diff --git a/constantine/io/io_bigints.nim b/constantine/io/io_bigints.nim index a1f48d5..1a8b660 100644 --- a/constantine/io/io_bigints.nim +++ b/constantine/io/io_bigints.nim @@ -330,7 +330,6 @@ func fromHex*(T: type BigInt, s: string): T = # 1. Convert to canonical uint const canonLen = (T.bits + 8 - 1) div 8 var bytes: array[canonLen, byte] - debugEcho "canonLen: ", canonLen hexToPaddedByteArray(s, bytes, littleEndian) # 2. Convert canonical uint to Big Int