mirror of
https://github.com/codex-storage/constantine.git
synced 2025-01-13 20:44:49 +00:00
Terminology nres -> mres, montgomeryResidue, montyResidue
This commit is contained in:
parent
d3ad4acb3a
commit
e2333dce3c
@ -95,7 +95,7 @@ macro declareCurves*(curves: untyped): untyped =
|
||||
nnkObjConstr.newTree(
|
||||
nnkBracketExpr.newTree(Fq, curve),
|
||||
nnkExprColonExpr.newTree(
|
||||
ident"nres",
|
||||
ident"mres",
|
||||
newCall(
|
||||
bindSym"fromHex",
|
||||
nnkBracketExpr.newTree(bindSym"BigInt", bitSize),
|
||||
@ -151,7 +151,7 @@ macro declareCurves*(curves: untyped): untyped =
|
||||
# ## P being the prime modulus of the Curve C
|
||||
# ## Internally, data is stored in Montgomery n-residue form
|
||||
# ## with the magic constant chosen for convenient division (a power of 2 depending on P bitsize)
|
||||
# nres*: matchingBigInt(C)
|
||||
# mres*: matchingBigInt(C)
|
||||
result.add nnkTypeSection.newTree(
|
||||
nnkTypeDef.newTree(
|
||||
nnkPostfix.newTree(ident"*", Fq),
|
||||
@ -164,7 +164,7 @@ macro declareCurves*(curves: untyped): untyped =
|
||||
newEmptyNode(),
|
||||
nnkRecList.newTree(
|
||||
newIdentDefs(
|
||||
nnkPostfix.newTree(ident"*", ident"nres"),
|
||||
nnkPostfix.newTree(ident"*", ident"mres"),
|
||||
newCall(matchingBigInt, C)
|
||||
)
|
||||
)
|
||||
|
@ -21,8 +21,8 @@ func fromUint*(dst: var Fq,
|
||||
src: SomeUnsignedInt) =
|
||||
## Parse a regular unsigned integer
|
||||
## and store it into a BigInt of size `bits`
|
||||
dst.nres.fromRawUint(cast[array[sizeof(src), byte]](src), cpuEndian)
|
||||
dst.nres.unsafeMontgomeryResidue(Fq.C.Mod.nres)
|
||||
dst.mres.fromRawUint(cast[array[sizeof(src), byte]](src), cpuEndian)
|
||||
dst.mres.unsafeMontyResidue(Fq.C.Mod.mres)
|
||||
|
||||
func serializeRawUint*(dst: var openarray[byte],
|
||||
src: Fq,
|
||||
|
@ -111,26 +111,26 @@ func reduce*[aBits, mBits](r: var BigInt[mBits], a: BigInt[aBits], M: BigInt[mBi
|
||||
# pass a pointer+length to a fixed session of the BSS.
|
||||
reduce(r.view, a.view, M.view)
|
||||
|
||||
func unsafeMontgomeryResidue*[mBits](nres: var BigInt[mBits], N: BigInt[mBits]) =
|
||||
func unsafeMontyResidue*[mBits](mres: var BigInt[mBits], N: BigInt[mBits]) =
|
||||
## Convert a BigInt from its natural representation
|
||||
## to the Montgomery n-residue form
|
||||
##
|
||||
## `nres` is modified in-place
|
||||
## `mres` is modified in-place
|
||||
##
|
||||
## Caller must take care of properly switching between
|
||||
## the natural and montgomery domain.
|
||||
## Nesting Montgomery form is possible by applying this function twice.
|
||||
montgomeryResidue(nres.view, N.view)
|
||||
montyResidue(mres.view, N.view)
|
||||
|
||||
func unsafeRedc*[mBits](nres: var BigInt[mBits], N: BigInt[mBits], negInvModWord: static BaseType) =
|
||||
func unsafeRedc*[mBits](mres: var BigInt[mBits], N: BigInt[mBits], negInvModWord: static BaseType) =
|
||||
## Convert a BigInt from its Montgomery n-residue form
|
||||
## to the natural representation
|
||||
##
|
||||
## `nres` is modified in-place
|
||||
## `mres` is modified in-place
|
||||
##
|
||||
## Caller must take care of properly switching between
|
||||
## the natural and montgomery domain.
|
||||
redc(nres.view, N.view, Word(negInvModWord))
|
||||
redc(mres.view, N.view, Word(negInvModWord))
|
||||
|
||||
func montyMul*[mBits](r: var BigInt[mBits], a, b, M: BigInt[mBits], negInvModWord: static BaseType) =
|
||||
## Compute r <- a*b (mod M) in the Montgomery domain
|
||||
|
@ -393,7 +393,7 @@ func reduce*(r: BigIntViewMut, a: BigIntViewAny, M: BigIntViewConst) =
|
||||
#
|
||||
# ############################################################
|
||||
|
||||
func montgomeryResidue*(a: BigIntViewMut, N: BigIntViewConst) =
|
||||
func montyResidue*(a: BigIntViewMut, N: BigIntViewConst) =
|
||||
## Transform a bigint ``a`` from it's natural representation (mod N)
|
||||
## to a the Montgomery n-residue representation
|
||||
##
|
||||
|
@ -31,13 +31,13 @@ import
|
||||
# ## P being the prime modulus of the Curve C
|
||||
# ## Internally, data is stored in Montgomery n-residue form
|
||||
# ## with the magic constant chosen for convenient division (a power of 2 depending on P bitsize)
|
||||
# nres*: matchingBigInt(C)
|
||||
# mres*: matchingBigInt(C)
|
||||
export Fq # defined in ../config/curves to avoid recursive module dependencies
|
||||
|
||||
debug:
|
||||
func `==`*(a, b: Fq): CTBool[Word] =
|
||||
## Returns true if 2 big ints are equal
|
||||
a.nres == b.nres
|
||||
a.mres == b.mres
|
||||
|
||||
# No exceptions allowed
|
||||
{.push raises: [].}
|
||||
@ -51,13 +51,13 @@ debug:
|
||||
|
||||
func fromBig*(T: type Fq, src: BigInt): T =
|
||||
## Convert a BigInt to its Montgomery form
|
||||
result.nres = src
|
||||
result.nres.unsafeMontgomeryResidue(Fq.C.Mod)
|
||||
result.mres = src
|
||||
result.mres.unsafeMontyResidue(Fq.C.Mod)
|
||||
|
||||
func toBig*(src: Fq): auto =
|
||||
## Convert a finite-field element to a BigInt in natral representation
|
||||
result = src.nres
|
||||
result.unsafeRedC(Fq.C.Mod.nres, Fq.C.Mod.nres.negInvModWord())
|
||||
result = src.mres
|
||||
result.unsafeRedC(Fq.C.Mod.mres, Fq.C.Mod.mres.negInvModWord())
|
||||
|
||||
# ############################################################
|
||||
#
|
||||
@ -72,7 +72,7 @@ template add(a: var Fq, b: Fq, ctl: CTBool[Word]): CTBool[Word] =
|
||||
##
|
||||
## a and b MAY be the same buffer
|
||||
## a and b MUST have the same announced bitlength (i.e. `bits` static parameters)
|
||||
add(a.nres, b.nres, ctl)
|
||||
add(a.mres, b.mres, ctl)
|
||||
|
||||
template sub(a: var Fq, b: Fq, ctl: CTBool[Word]): CTBool[Word] =
|
||||
## Constant-time big integer in-place optional substraction
|
||||
@ -81,7 +81,7 @@ template sub(a: var Fq, b: Fq, ctl: CTBool[Word]): CTBool[Word] =
|
||||
##
|
||||
## a and b MAY be the same buffer
|
||||
## a and b MUST have the same announced bitlength (i.e. `bits` static parameters)
|
||||
sub(a.nres, b.nres, ctl)
|
||||
sub(a.mres, b.mres, ctl)
|
||||
|
||||
# ############################################################
|
||||
#
|
||||
@ -114,5 +114,5 @@ func `*`*(a, b: Fq): Fq {.noInit.} =
|
||||
## It is recommended to assign with {.noInit.}
|
||||
## as Fq elements are usually large and this
|
||||
## routine will zero init internally the result.
|
||||
result.nres.setInternalBitLength()
|
||||
result.nres.montyMul(a.nres, b.nres, Fq.C.Mod.nres, Fq.C.Mod.nres.negInvModWord())
|
||||
result.mres.setInternalBitLength()
|
||||
result.mres.montyMul(a.mres, b.mres, Fq.C.Mod.mres, Fq.C.Mod.mres.negInvModWord())
|
||||
|
Loading…
x
Reference in New Issue
Block a user