From 9db77ad0eb181e4430f8650d284697e547277bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Sun, 9 Feb 2020 00:52:24 +0100 Subject: [PATCH] Prepare for testing finite fields, comment cleanups --- constantine.nimble | 13 ++++++------ constantine/bigints.nim | 11 ++++++++++- constantine/curves_config.nim | 23 ++++++++++++++-------- constantine/field_fp.nim | 2 -- tests/{all_tests.nim => test_field_fp.nim} | 6 ++---- 5 files changed, 34 insertions(+), 21 deletions(-) rename tests/{all_tests.nim => test_field_fp.nim} (85%) diff --git a/constantine.nimble b/constantine.nimble index bf151d8..30d5fdb 100644 --- a/constantine.nimble +++ b/constantine.nimble @@ -6,16 +6,17 @@ license = "MIT or Apache License 2.0" srcDir = "src" ### Dependencies -requires "nim >= 0.18.0" +requires "nim >= 1.0.6" ### Helper functions -proc test(name: string, defaultLang = "c") = +proc test(fakeCurves: string, path: string, lang = "c") = if not dirExists "build": mkDir "build" - --run - switch("out", ("./build/" & name)) - setCommand defaultLang, "tests/" & name & ".nim" + exec "nim " & lang & fakeCurves & " --outdir:build -r --hints:off --warnings:off " & path ### tasks 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" diff --git a/constantine/bigints.nim b/constantine/bigints.nim index 22ddeba..2f8fa98 100644 --- a/constantine/bigints.nim +++ b/constantine/bigints.nim @@ -36,7 +36,6 @@ # We internally order the limbs in little-endian # So the least significant limb is limb[0] # This is independent from the base type endianness. -# TODO: hexdumps import ./word_types @@ -49,6 +48,16 @@ const WordBitSize* = sizeof(Word) * 8 - 1 func wordsRequired(bits: int): int {.compileTime.}= (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 BigInt*[bits: static int] = object ## Fixed-precision big integer diff --git a/constantine/curves_config.nim b/constantine/curves_config.nim index 616924a..670e76b 100644 --- a/constantine/curves_config.nim +++ b/constantine/curves_config.nim @@ -35,11 +35,18 @@ import # - proc MontyMagic(curve: static Curve): static Word = # which returns the Montgomery magic constant # associated with the curve modulus -declareCurves: - # Barreto-Naehrig curve, Prime 254 bit, 128-bit security, https://eprint.iacr.org/2013/879.pdf - # Usage: Zero-Knowledge Proofs / zkSNARKs in ZCash and Ethereum 1 - # https://eips.ethereum.org/EIPS/eip-196 - curve BN254: - bitsize: 254 - modulus: "0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47" - # Equation: Y^2 = X^3 + 3 +when not defined(testingCurves): + declareCurves: + # Barreto-Naehrig curve, Prime 254 bit, 128-bit security, https://eprint.iacr.org/2013/879.pdf + # Usage: Zero-Knowledge Proofs / zkSNARKs in ZCash and Ethereum 1 + # https://eips.ethereum.org/EIPS/eip-196 + curve BN254: + bitsize: 254 + 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 diff --git a/constantine/field_fp.nim b/constantine/field_fp.nim index 78bb5cb..7e845e6 100644 --- a/constantine/field_fp.nim +++ b/constantine/field_fp.nim @@ -17,8 +17,6 @@ import ./word_types, ./bigints, ./curves_config from ./private/word_types_internal import unsafe_div2n1n -static: echo CurveBitSize - type Fp*[C: static Curve] = object ## P is the prime modulus of the Curve C diff --git a/tests/all_tests.nim b/tests/test_field_fp.nim similarity index 85% rename from tests/all_tests.nim rename to tests/test_field_fp.nim index 20a28b1..702e1e7 100644 --- a/tests/all_tests.nim +++ b/tests/test_field_fp.nim @@ -6,7 +6,5 @@ # * 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 - test_word_types, - test_io, - test_bigints +import unittest, random, + ../constantine/[io, bigints, word_types, field_fp]