mirror of
https://github.com/logos-storage/constantine.git
synced 2026-01-03 05:33:07 +00:00
* Lay out the refactoring objectives and tradeoffs * Refactor the 32 and 64-bit primitives [skip ci] * BigInts and Modular BigInts compile * Make the bigints test compile * Fix modular reduction * Fix reduction tests vs GMP * Implement montegomery mul, pow, inverse, WIP finite field compilation * Make FiniteField compile * Fix exponentiation compilation * Fix Montgomery magic constant computation for 2^64 words * Fix typo in non-optimized CIOS - passing finite fields IO tests * Add limbs comparisons [skip ci] * Fix on precomputation of the Montgomery magic constant * Passing all tests including 𝔽p2 * modular addition, the test for mersenne prime was wrong * update benches * Fix "nimble test" + typo on out-of-place field addition * bigint division, normalization is needed: https://travis-ci.com/github/mratsim/constantine/jobs/298359743 * missing conversion in subborrow non-x86 fallback - https://travis-ci.com/github/mratsim/constantine/jobs/298359744 * Fix little-endian serialization * Constantine32 flag to run 32-bit constantine on 64-bit machines * IO Field test, ensure that BaseType is used instead of uint64 when the prime can field in uint32 * Implement proper addcarry and subborrow fallback for the compile-time VM * Fix export issue when the logical wordbitwidth == physical wordbitwidth - passes all tests (32-bit and 64-bit) * Fix uint128 on ARM * Fix C++ conditional copy and ARM addcarry/subborrow * Add investigation for SIGFPE in Travis * Fix debug display for unsafeDiv2n1n * multiplexer typo * moveMem bug in glibc of Ubuntu 16.04? * Was probably missing an early clobbered register annotation on conditional mov * Note on Montgomery-friendly moduli * Strongly suspect a GCC before GCC 7 codegen bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87139) * hex conversion was (for debugging) not taking requested order into account + inlining comment * Use 32-bit limbs on ARM64, uint128 builtin __udivti4 bug? * Revert "Use 32-bit limbs on ARM64, uint128 builtin __udivti4 bug?" This reverts commit 087f9aa7fb40bbd058d05cbd8eec7fc082911f49. * Fix subborrow fallback for non-x86 (need to maks the borrow)
168 lines
4.8 KiB
Nim
168 lines
4.8 KiB
Nim
# Constantine
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy
|
|
# Licensed and distributed under either of
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
import
|
|
# Standard library
|
|
macros,
|
|
# Internal
|
|
../io/io_bigints, ../arithmetic/bigints
|
|
|
|
# Macro to parse declarative curves configuration.
|
|
|
|
macro declareCurves*(curves: untyped): untyped =
|
|
## Parse curve configuration and generates
|
|
##
|
|
## type Curve = enum
|
|
## BN254
|
|
## ...
|
|
##
|
|
## const CurveBitSize* = array[
|
|
## BN254: 254,
|
|
## ...
|
|
## ]
|
|
##
|
|
## TODO: Ensure that the modulus is not inlined at runtime
|
|
## to avoid codesize explosion.
|
|
## const BN254_Modulus = fromHex(BigInt[254], "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
|
|
##
|
|
## func fieldModulus*(curve: static Curve): auto =
|
|
## when curve == BN254_Modulus: BN254_Modulus
|
|
## ...
|
|
curves.expectKind(nnkStmtList)
|
|
|
|
# curve BN254:
|
|
# bitsize: 254
|
|
# modulus: "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"
|
|
#
|
|
# is parsed into
|
|
#
|
|
# StmtList
|
|
# Command
|
|
# Ident "curve"
|
|
# Ident "BN254"
|
|
# StmtList
|
|
# Call
|
|
# Ident "bitsize"
|
|
# StmtList
|
|
# IntLit 254
|
|
# Call
|
|
# Ident "modulus"
|
|
# StmtList
|
|
# StrLit "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"
|
|
|
|
var Curves: seq[NimNode]
|
|
var CurveBitSize = nnKBracket.newTree()
|
|
var curveModStmts = newStmtList()
|
|
|
|
let Fp = ident"Fp"
|
|
|
|
for curveDesc in curves:
|
|
curveDesc.expectKind(nnkCommand)
|
|
doAssert curveDesc[0].eqIdent"curve"
|
|
curveDesc[1].expectKind(nnkIdent) # Curve name
|
|
curveDesc[2].expectKind(nnkStmtList)
|
|
curveDesc[2][0].expectKind(nnkCall)
|
|
curveDesc[2][1].expectKind(nnkCall)
|
|
|
|
let curve = curveDesc[1]
|
|
|
|
let sizeSection = curveDesc[2][0]
|
|
doAssert sizeSection[0].eqIdent"bitsize"
|
|
sizeSection[1].expectKind(nnkStmtList)
|
|
let bitSize = sizeSection[1][0]
|
|
|
|
let modSection = curveDesc[2][1]
|
|
doAssert modSection[0].eqIdent"modulus"
|
|
modSection[1].expectKind(nnkStmtList)
|
|
let modulus = modSection[1][0]
|
|
|
|
Curves.add curve
|
|
# "BN254: 254" for array construction
|
|
CurveBitSize.add nnkExprColonExpr.newTree(
|
|
curve, bitSize
|
|
)
|
|
|
|
# const BN254_Modulus = Fp[BN254](value: fromHex(BigInt[254], "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"))
|
|
let modulusID = ident($curve & "_Modulus")
|
|
curveModStmts.add newConstStmt(
|
|
modulusID,
|
|
nnkObjConstr.newTree(
|
|
nnkBracketExpr.newTree(Fp, curve),
|
|
nnkExprColonExpr.newTree(
|
|
ident"mres",
|
|
newCall(
|
|
bindSym"fromHex",
|
|
nnkBracketExpr.newTree(bindSym"BigInt", bitSize),
|
|
modulus
|
|
)
|
|
)
|
|
)
|
|
)
|
|
|
|
# end for ---------------------------------------------------
|
|
|
|
result = newStmtList()
|
|
|
|
# type Curve = enum
|
|
let Curve = ident"Curve"
|
|
result.add newEnum(
|
|
name = Curve,
|
|
fields = Curves,
|
|
public = true,
|
|
pure = false
|
|
)
|
|
|
|
# const CurveBitSize: array[Curve, int] = ...
|
|
let cbs = ident("CurveBitSize")
|
|
result.add newConstStmt(
|
|
cbs, CurveBitSize
|
|
)
|
|
|
|
# Need template indirection in the type section to avoid Nim sigmatch bug
|
|
# template matchingBigInt(C: static Curve): untyped =
|
|
# BigInt[CurveBitSize[C]]
|
|
let C = ident"C"
|
|
let matchingBigInt = genSym(nskTemplate, "matchingBigInt")
|
|
result.add newProc(
|
|
name = matchingBigInt,
|
|
params = [ident"untyped", newIdentDefs(C, nnkStaticTy.newTree(Curve))],
|
|
body = nnkBracketExpr.newTree(bindSym"BigInt", nnkBracketExpr.newTree(cbs, C)),
|
|
procType = nnkTemplateDef
|
|
)
|
|
|
|
# type
|
|
# `Fp`*[C: static Curve] = object
|
|
# ## All operations on a field are modulo P
|
|
# ## 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)
|
|
# mres*: matchingBigInt(C)
|
|
result.add nnkTypeSection.newTree(
|
|
nnkTypeDef.newTree(
|
|
nnkPostfix.newTree(ident"*", Fp),
|
|
nnkGenericParams.newTree(newIdentDefs(
|
|
C, nnkStaticTy.newTree(Curve), newEmptyNode()
|
|
)),
|
|
# TODO: where should I put the nnkCommentStmt?
|
|
nnkObjectTy.newTree(
|
|
newEmptyNode(),
|
|
newEmptyNode(),
|
|
nnkRecList.newTree(
|
|
newIdentDefs(
|
|
nnkPostfix.newTree(ident"*", ident"mres"),
|
|
newCall(matchingBigInt, C)
|
|
)
|
|
)
|
|
)
|
|
)
|
|
)
|
|
|
|
result.add curveModStmts
|
|
|
|
# echo result.toStrLit()
|