print montgomery magics at compile-time

This commit is contained in:
Mamy André-Ratsimbazafy 2020-02-16 12:32:34 +01:00
parent f1430915fe
commit 7740bfbae4
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
2 changed files with 29 additions and 1 deletions

View File

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

View File

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