Prepare for testing finite fields, comment cleanups

This commit is contained in:
Mamy André-Ratsimbazafy 2020-02-09 00:52:24 +01:00
parent edd728610c
commit 9db77ad0eb
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
5 changed files with 34 additions and 21 deletions

View File

@ -6,16 +6,17 @@ license = "MIT or Apache License 2.0"
srcDir = "src" srcDir = "src"
### Dependencies ### Dependencies
requires "nim >= 0.18.0" requires "nim >= 1.0.6"
### Helper functions ### Helper functions
proc test(name: string, defaultLang = "c") = proc test(fakeCurves: string, path: string, lang = "c") =
if not dirExists "build": if not dirExists "build":
mkDir "build" mkDir "build"
--run exec "nim " & lang & fakeCurves & " --outdir:build -r --hints:off --warnings:off " & path
switch("out", ("./build/" & name))
setCommand defaultLang, "tests/" & name & ".nim"
### tasks ### tasks
task test, "Run all tests": task test, "Run all tests":
test "all_tests" test "", "tests/test_word_types.nim"
test "", "tests/test_io.nim"
test "", "tests/test_bigints.nim"
test "-d:testingCurves", "tests/test_field_fp.nim"

View File

@ -36,7 +36,6 @@
# We internally order the limbs in little-endian # We internally order the limbs in little-endian
# So the least significant limb is limb[0] # So the least significant limb is limb[0]
# This is independent from the base type endianness. # This is independent from the base type endianness.
# TODO: hexdumps
import ./word_types import ./word_types
@ -49,6 +48,16 @@ const WordBitSize* = sizeof(Word) * 8 - 1
func wordsRequired(bits: int): int {.compileTime.}= func wordsRequired(bits: int): int {.compileTime.}=
(bits + WordBitSize - 1) div WordBitSize (bits + WordBitSize - 1) div WordBitSize
# TODO: Currently the library is instantiation primitives like "add"
# for each "bits" size supported. This will lead to duplication
# if many sizes (for example for scp256k1, bn254 and BLS12-381)
# are required.
# It could be avoided by having the bitsize be a runtime field
# of the bigint. However the tradeoff would be:
# - overhead of this additional field
# - limbs have to be stored in an UncheckedArray instead of an array
# introducing memory management issues
type type
BigInt*[bits: static int] = object BigInt*[bits: static int] = object
## Fixed-precision big integer ## Fixed-precision big integer

View File

@ -35,11 +35,18 @@ import
# - proc MontyMagic(curve: static Curve): static Word = # - proc MontyMagic(curve: static Curve): static Word =
# which returns the Montgomery magic constant # which returns the Montgomery magic constant
# associated with the curve modulus # associated with the curve modulus
declareCurves: when not defined(testingCurves):
# Barreto-Naehrig curve, Prime 254 bit, 128-bit security, https://eprint.iacr.org/2013/879.pdf declareCurves:
# Usage: Zero-Knowledge Proofs / zkSNARKs in ZCash and Ethereum 1 # Barreto-Naehrig curve, Prime 254 bit, 128-bit security, https://eprint.iacr.org/2013/879.pdf
# https://eips.ethereum.org/EIPS/eip-196 # Usage: Zero-Knowledge Proofs / zkSNARKs in ZCash and Ethereum 1
curve BN254: # https://eips.ethereum.org/EIPS/eip-196
bitsize: 254 curve BN254:
modulus: "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47" bitsize: 254
# Equation: Y^2 = X^3 + 3 modulus: "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"
# Equation: Y^2 = X^3 + 3
else:
# Fake curve for testing field arithmetic
declareCurves:
curve Fake101:
bitsize: 101
modulus: "0x65" # 101 in hex

View File

@ -17,8 +17,6 @@
import ./word_types, ./bigints, ./curves_config import ./word_types, ./bigints, ./curves_config
from ./private/word_types_internal import unsafe_div2n1n from ./private/word_types_internal import unsafe_div2n1n
static: echo CurveBitSize
type type
Fp*[C: static Curve] = object Fp*[C: static Curve] = object
## P is the prime modulus of the Curve C ## P is the prime modulus of the Curve C

View File

@ -6,7 +6,5 @@
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). # * 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. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import import unittest, random,
test_word_types, ../constantine/[io, bigints, word_types, field_fp]
test_io,
test_bigints