From 4be89d309f6202e18ee54302aa0fa76d543f5323 Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Thu, 12 Jan 2023 20:25:57 +0100 Subject: [PATCH] chore: remove stew/byteutils dependencies and unneeded imports --- .github/workflows/ci.yml | 4 +- .travis.yml | 11 +---- README.md | 48 ++++++++++++++++++- azure-pipelines.yml | 2 +- benchmarks/bench_ec_g1.nim | 4 +- benchmarks/bench_ec_g1_batch.nim | 4 +- benchmarks/bench_fp12.nim | 4 +- benchmarks/bench_fp2.nim | 4 +- benchmarks/bench_fp4.nim | 4 +- benchmarks/bench_fp6.nim | 4 +- benchmarks/bench_fp_double_precision.nim | 2 +- benchmarks/bench_pairing_bls12_377.nim | 4 +- benchmarks/bench_pairing_bls12_381.nim | 4 +- benchmarks/bench_pairing_bn254_nogami.nim | 4 +- benchmarks/bench_pairing_bn254_snarks.nim | 4 +- benchmarks/bench_summary_bls12_377.nim | 4 +- benchmarks/bench_summary_bls12_381.nim | 4 +- benchmarks/bench_summary_bn254_nogami.nim | 4 +- benchmarks/bench_summary_bn254_snarks.nim | 4 +- benchmarks/bench_summary_pasta.nim | 4 +- .../limbs_asm_mul_mont_x86_adx_bmi2.nim | 2 +- .../math/arithmetic/limbs_montgomery.nim | 2 - constantine/math/io/io_bigints.nim | 25 +++++----- constantine/platforms/gpu/ir.nim | 11 ++--- tests/gpu/hello_world_nvidia.nim | 4 +- tests/math/t_bigints_mod_vs_gmp.nim | 2 +- .../math/t_bigints_mul_high_words_vs_gmp.nim | 2 +- tests/math/t_bigints_mul_vs_gmp.nim | 2 +- tests/math/t_ec_shortw_prj_edge_cases.nim | 9 ++-- tests/math/t_ec_shortw_prj_g1_mul_sanity.nim | 5 +- tests/math/t_finite_fields_vs_gmp.nim | 4 +- .../t_pairing_bls12_377_line_functions.nim | 4 +- .../t_pairing_bls12_381_line_functions.nim | 7 ++- tests/math/t_pairing_cyclotomic_subgroup.nim | 2 +- tests/t_blssig_pop_on_bls12381_g2.nim | 4 +- tests/t_ethereum_evm_precompiles.nim | 2 - tests/t_hash_sha256_vs_openssl.nim | 9 ++-- tests/t_hash_to_curve.nim | 2 +- tests/t_hash_to_curve_random.nim | 2 +- tests/t_hash_to_field.nim | 25 +++++----- tests/t_kdf_hkdf.nim | 12 ++++- 41 files changed, 128 insertions(+), 136 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e45a0ff..dcea539 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -188,14 +188,14 @@ jobs: run: | pacman -S --needed --noconfirm mingw-w64-x86_64-gmp nimble refresh --verbose -y - nimble install --verbose -y gmp stew jsony asynctools + nimble install --verbose -y gmp jsony asynctools - name: Install test dependencies if: runner.os != 'Windows' shell: bash run: | nimble refresh --verbose -y - nimble install --verbose -y gmp stew jsony asynctools + nimble install --verbose -y gmp jsony asynctools - name: Run Constantine tests (UNIX with Assembly) if: runner.os != 'Windows' && matrix.target.BACKEND == 'ASM' diff --git a/.travis.yml b/.travis.yml index bc6670d..eeab39b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -81,17 +81,11 @@ addons: apt: packages: - libgmp-dev - - parallel homebrew: packages: - gmp - # Travis `bundle` bug: https://travis-ci.community/t/macos-build-fails-because-of-homebrew-bundle-unknown-command/7296/28 - # - parallel before_install: - - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then - HOMEBREW_NO_AUTO_UPDATE=1 brew install parallel; - fi - | if [ "${CHANNEL}" = stable ]; then BRANCH="v$(curl https://nim-lang.org/channels/stable)" @@ -127,7 +121,7 @@ before_script: - export PATH="$PWD/nim-${CHANNEL}/bin${PATH:+:$PATH}" script: - nimble refresh - - nimble install -y gmp stew jsony + - nimble install -y gmp jsony # Installing Clang9.0 or later is a pain in Travis # for inline assembly "flag output constraint" @@ -137,9 +131,6 @@ script: nimble test_parallel_no_assembler else nimble test_parallel - if [[ "$ARCH" == "amd64" ]]; then - nimble test_parallel_no_assembler - fi fi branches: except: diff --git a/README.md b/README.md index e397781..a665221 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,34 @@ with a particular focus on pairing-based cryptography as used in blockchains and The implementations are accompanied with SAGE code used as reference implementation and test vectors generators before writing highly optimized routines implemented in the [Nim language](https://nim-lang.org/) -> The library is in development state and high-level wrappers or example protocols are not available yet. +> The library is in development state and high-level wrappers or example protocols are work-in-progress. + +## Table of Contents + + + +- [Constantine - Fast, compact, hardened Pairing-Based Cryptography](#constantine---fast-compact-hardened-pairing-based-cryptography) + - [Table of Contents](#table-of-contents) + - [Target audience](#target-audience) + - [Protocols](#protocols) + - [Curves supported in the backend](#curves-supported-in-the-backend) + - [Installation](#installation) + - [Dependencies](#dependencies) + - [Security](#security) + - [Disclaimer](#disclaimer) + - [Security disclosure](#security-disclosure) + - [Performance](#performance) + - [In blockchain](#in-blockchain) + - [In zero-knowledge proofs](#in-zero-knowledge-proofs) + - [Measuring performance](#measuring-performance) + - [BLS12_381 Clang + inline Assembly](#bls12_381-clang--inline-assembly) + - [Why Nim](#why-nim) + - [Compiler caveats](#compiler-caveats) + - [Inline assembly](#inline-assembly) + - [Sizes: code size, stack usage](#sizes-code-size-stack-usage) + - [License](#license) + + ## Target audience @@ -102,6 +129,21 @@ generated incorrect add-with-carry code. On x86-64, inline assembly is used to workaround compilers having issues optimizing large integer arithmetic, and also ensure constant-time code. +## Dependencies + +Constantine has no dependencies, even on Nim standard library except: +- for testing + - jsony for parsing json test vectors + - the Nim standard library for unittesting, formatting and datetime. + - GMP for testing against GMP +- for benchmarking + - The Nim standard libreary for timing and formatting +- for Nvidia GPU backend: + - the LLVM runtime ("dev" version with headers is not needed) + - the CUDA runtime ("dev" version with headers is not needed) +- at compile-time + - we need the std/macros library to generate Nim code. + ## Security Hardening an implementation against all existing and upcoming attack vectors is an extremely complex task. @@ -111,6 +153,8 @@ The library is provided as is, without any guarantees at least until: - formal verification of constant-time implementation is possible Defense against common attack vectors are provided on a best effort basis. +Do note that Constantine has no external package dependencies hence it is not vulnerable to +supply chain attacks (unless they affect a compiler or the OS). Attackers may go to great lengths to retrieve secret data including: - Timing the time taken to multiply on an elliptic curve @@ -231,7 +275,7 @@ The Nim language offers the following benefits for cryptography: - derive constants - write a size-independent inline assembly code generator - Upcoming proof system for formal verification via Z3 ([DrNim](https://nim-lang.org/docs/drnim.html), [Correct-by-Construction RFC](https://github.com/nim-lang/RFCs/issues/222)) -### Compiler caveats +## Compiler caveats Unfortunately compilers and in particular GCC are not very good at optimizing big integers and/or cryptographic code even when using intrinsics like `addcarry_u64`. diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 785589c..0815103 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -268,7 +268,7 @@ steps: - bash: | echo "PATH=${PATH}" nimble refresh - nimble install -y gmp stew jsony + nimble install -y gmp jsony displayName: 'Installing package and testing dependencies' - bash: | diff --git a/benchmarks/bench_ec_g1.nim b/benchmarks/bench_ec_g1.nim index b9191ef..1419b49 100644 --- a/benchmarks/bench_ec_g1.nim +++ b/benchmarks/bench_ec_g1.nim @@ -15,9 +15,7 @@ import ec_shortweierstrass_jacobian], # Helpers ../helpers/static_for, - ./bench_elliptic_template, - # Standard library - std/strutils + ./bench_elliptic_template # ############################################################ # diff --git a/benchmarks/bench_ec_g1_batch.nim b/benchmarks/bench_ec_g1_batch.nim index 8e1ebbd..6d61a4d 100644 --- a/benchmarks/bench_ec_g1_batch.nim +++ b/benchmarks/bench_ec_g1_batch.nim @@ -16,9 +16,7 @@ import ec_shortweierstrass_jacobian], # Helpers ../helpers/static_for, - ./bench_elliptic_template, - # Standard library - std/strutils + ./bench_elliptic_template # ############################################################ # diff --git a/benchmarks/bench_fp12.nim b/benchmarks/bench_fp12.nim index 381e20d..9c636db 100644 --- a/benchmarks/bench_fp12.nim +++ b/benchmarks/bench_fp12.nim @@ -12,9 +12,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_fields_template, - # Standard library - std/strutils + ./bench_fields_template # ############################################################ # diff --git a/benchmarks/bench_fp2.nim b/benchmarks/bench_fp2.nim index e4503df..c75a0f2 100644 --- a/benchmarks/bench_fp2.nim +++ b/benchmarks/bench_fp2.nim @@ -12,9 +12,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_fields_template, - # Standard library - std/strutils + ./bench_fields_template # ############################################################ # diff --git a/benchmarks/bench_fp4.nim b/benchmarks/bench_fp4.nim index 1f14c8b..99eec03 100644 --- a/benchmarks/bench_fp4.nim +++ b/benchmarks/bench_fp4.nim @@ -12,9 +12,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_fields_template, - # Standard library - std/strutils + ./bench_fields_template # ############################################################ # diff --git a/benchmarks/bench_fp6.nim b/benchmarks/bench_fp6.nim index b167121..1fb440b 100644 --- a/benchmarks/bench_fp6.nim +++ b/benchmarks/bench_fp6.nim @@ -12,9 +12,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_fields_template, - # Standard library - std/strutils + ./bench_fields_template # ############################################################ # diff --git a/benchmarks/bench_fp_double_precision.nim b/benchmarks/bench_fp_double_precision.nim index 49d1167..cb5dd91 100644 --- a/benchmarks/bench_fp_double_precision.nim +++ b/benchmarks/bench_fp_double_precision.nim @@ -22,7 +22,7 @@ import ../helpers/[prng_unsafe, static_for], ./platforms, # Standard library - std/[monotimes, times, strformat, strutils, macros] + std/[monotimes, times, strformat, strutils] var rng: RngState let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32 diff --git a/benchmarks/bench_pairing_bls12_377.nim b/benchmarks/bench_pairing_bls12_377.nim index ac23eee..7ce8844 100644 --- a/benchmarks/bench_pairing_bls12_377.nim +++ b/benchmarks/bench_pairing_bls12_377.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_pairing_template, - # Standard library - std/strutils + ./bench_pairing_template # ############################################################ # diff --git a/benchmarks/bench_pairing_bls12_381.nim b/benchmarks/bench_pairing_bls12_381.nim index 3f6c25c..4e868c5 100644 --- a/benchmarks/bench_pairing_bls12_381.nim +++ b/benchmarks/bench_pairing_bls12_381.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_pairing_template, - # Standard library - std/strutils + ./bench_pairing_template # ############################################################ # diff --git a/benchmarks/bench_pairing_bn254_nogami.nim b/benchmarks/bench_pairing_bn254_nogami.nim index ac776b7..7ec159b 100644 --- a/benchmarks/bench_pairing_bn254_nogami.nim +++ b/benchmarks/bench_pairing_bn254_nogami.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_pairing_template, - # Standard library - std/strutils + ./bench_pairing_template # ############################################################ # diff --git a/benchmarks/bench_pairing_bn254_snarks.nim b/benchmarks/bench_pairing_bn254_snarks.nim index b637a69..f6e0461 100644 --- a/benchmarks/bench_pairing_bn254_snarks.nim +++ b/benchmarks/bench_pairing_bn254_snarks.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_pairing_template, - # Standard library - std/strutils + ./bench_pairing_template # ############################################################ # diff --git a/benchmarks/bench_summary_bls12_377.nim b/benchmarks/bench_summary_bls12_377.nim index 107e01b..9783d5c 100644 --- a/benchmarks/bench_summary_bls12_377.nim +++ b/benchmarks/bench_summary_bls12_377.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_summary_template, - # Standard library - std/strutils + ./bench_summary_template # ############################################################ # diff --git a/benchmarks/bench_summary_bls12_381.nim b/benchmarks/bench_summary_bls12_381.nim index 4362771..f7ebe11 100644 --- a/benchmarks/bench_summary_bls12_381.nim +++ b/benchmarks/bench_summary_bls12_381.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_summary_template, - # Standard library - std/strutils + ./bench_summary_template # ############################################################ # diff --git a/benchmarks/bench_summary_bn254_nogami.nim b/benchmarks/bench_summary_bn254_nogami.nim index 2703b92..4e23c79 100644 --- a/benchmarks/bench_summary_bn254_nogami.nim +++ b/benchmarks/bench_summary_bn254_nogami.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_summary_template, - # Standard library - std/strutils + ./bench_summary_template # ############################################################ # diff --git a/benchmarks/bench_summary_bn254_snarks.nim b/benchmarks/bench_summary_bn254_snarks.nim index 86f307a..fdb182d 100644 --- a/benchmarks/bench_summary_bn254_snarks.nim +++ b/benchmarks/bench_summary_bn254_snarks.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_summary_template, - # Standard library - std/strutils + ./bench_summary_template # ############################################################ # diff --git a/benchmarks/bench_summary_pasta.nim b/benchmarks/bench_summary_pasta.nim index c0c5b8b..56d4256 100644 --- a/benchmarks/bench_summary_pasta.nim +++ b/benchmarks/bench_summary_pasta.nim @@ -13,9 +13,7 @@ import ../constantine/math/extension_fields, # Helpers ../helpers/static_for, - ./bench_summary_template, - # Standard library - std/strutils + ./bench_summary_template # ############################################################ # diff --git a/constantine/math/arithmetic/assembly/limbs_asm_mul_mont_x86_adx_bmi2.nim b/constantine/math/arithmetic/assembly/limbs_asm_mul_mont_x86_adx_bmi2.nim index 02909b3..d602bf6 100644 --- a/constantine/math/arithmetic/assembly/limbs_asm_mul_mont_x86_adx_bmi2.nim +++ b/constantine/math/arithmetic/assembly/limbs_asm_mul_mont_x86_adx_bmi2.nim @@ -8,7 +8,7 @@ import # Standard library - std/[macros, algorithm], + std/macros, # Internal ../../../platforms/abstractions, ./limbs_asm_modular_x86, diff --git a/constantine/math/arithmetic/limbs_montgomery.nim b/constantine/math/arithmetic/limbs_montgomery.nim index 2c01f2e..b3596a8 100644 --- a/constantine/math/arithmetic/limbs_montgomery.nim +++ b/constantine/math/arithmetic/limbs_montgomery.nim @@ -7,8 +7,6 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - # Standard library - std/macros, # Internal ../../platforms/abstractions, ./limbs, ./limbs_extmul diff --git a/constantine/math/io/io_bigints.nim b/constantine/math/io/io_bigints.nim index aa54437..b24d65b 100644 --- a/constantine/math/io/io_bigints.nim +++ b/constantine/math/io/io_bigints.nim @@ -291,7 +291,7 @@ func marshalBE[T]( var tail = dst.len while tail > 0: - let w = if src_idx < src.len: BaseType(src[src_idx]) + let w = if src_idx < src.len: BT(src[src_idx]) else: 0 inc src_idx @@ -439,23 +439,20 @@ func hexToPaddedByteArray*(hexStr: string, output: var openArray[byte], order: s shift = (shift + 4) and 4 dstIdx += shift shr 2 -func nativeEndianToHex*(bytes: openarray[byte], order: static[Endianness]): string = +func toHex*(bytes: openarray[byte]): string = ## Convert a byte-array to its hex representation - ## Output is in lowercase and not prefixed. - ## This assumes that input is in platform native endianness + ## Output is in lowercase and prefixed with 0x const hexChars = "0123456789abcdef" result = newString(2 + 2 * bytes.len) result[0] = '0' result[1] = 'x' for i in 0 ..< bytes.len: - when order == system.cpuEndian: - let bi = bytes[i] - result[2 + 2*i] = hexChars.secretLookup(SecretWord bi shr 4 and 0xF) - result[2 + 2*i+1] = hexChars.secretLookup(SecretWord bi and 0xF) - else: - let bmi = bytes[bytes.high - i] - result[2 + 2*i] = hexChars.secretLookup(SecretWord bmi shr 4 and 0xF) - result[2 + 2*i+1] = hexChars.secretLookup(SecretWord bmi and 0xF) + let bi = bytes[i] + result[2 + 2*i] = hexChars.secretLookup(SecretWord bi shr 4 and 0xF) + result[2 + 2*i+1] = hexChars.secretLookup(SecretWord bi and 0xF) + +func fromHex*[N: static int](T: type array[N, byte], hex: string): T = + hexToPaddedByteArray(hex, result, bigEndian) # ############################################################ # @@ -520,10 +517,10 @@ func appendHex*(dst: var string, big: BigInt, order: static Endianness = bigEndi # 1. Convert Big Int to canonical uint const canonLen = (big.bits + 8 - 1) div 8 var bytes: array[canonLen, byte] - marshal(bytes, big, cpuEndian) + marshal(bytes, big, order) # 2 Convert canonical uint to hex - dst.add bytes.nativeEndianToHex(order) + dst.add bytes.toHex() func toHex*(big: BigInt, order: static Endianness = bigEndian): string = ## Stringify an int to hex. diff --git a/constantine/platforms/gpu/ir.nim b/constantine/platforms/gpu/ir.nim index 51046c6..913121f 100644 --- a/constantine/platforms/gpu/ir.nim +++ b/constantine/platforms/gpu/ir.nim @@ -10,8 +10,7 @@ import ../../math/config/[curves, precompute], ../../math/io/io_bigints, ../primitives, ../bithacks, ../endians, - ./llvm, - std/hashes + ./llvm # ############################################################ # @@ -129,10 +128,10 @@ func toHex[T](a: BigNum[T]): string = # 1. Convert BigInt to canonical uint const wordBitwidth = sizeof(T) * 8 var bytes = newSeq[byte](byteLen(a.bits)) - bytes.marshal(a.limbs, wordBitwidth, cpuEndian) + bytes.marshal(a.limbs, wordBitwidth, bigEndian) # 2 Convert canonical uint to hex - return bytes.nativeEndianToHex(bigEndian) + return bytes.toHex() # Checks # ------------------------------------------------ @@ -245,10 +244,6 @@ proc init*( result.fp.setFieldConst(ctx, wordSize, fpBits, fpMod) result.fr.setFieldConst(ctx, wordSize, frBits, frMod) -proc hash*(curveOp: tuple[cm: CurveMetadata, op: Opcode]): Hash {.inline.} = - result = hash(curveOp.cm.curve) !& int(hash(curveOp.op)) - result = !$result - proc genSymbol*(cm: CurveMetadata, opcode: Opcode): string {.inline.} = cm.prefix & (if cm.wordSize == size32: "32b_" else: "64b_") & diff --git a/tests/gpu/hello_world_nvidia.nim b/tests/gpu/hello_world_nvidia.nim index 793baaa..4b4bfc1 100644 --- a/tests/gpu/hello_world_nvidia.nim +++ b/tests/gpu/hello_world_nvidia.nim @@ -91,7 +91,7 @@ proc getNvvmLog(prog: NvvmProgram): string {.used.} = result = newString(logSize) check nvvmGetProgramLog(prog, result[0].addr) -proc ptxCodegenViaNvidiaNvvm(module: ModuleRef, sm: tuple[major, minor: int32]): string {.used.} = +proc ptxCodegenViaNvidiaNvvm(module: ModuleRef, sm: tuple[major, minor: int32]): string = ## PTX codegen via Nvidia NVVM # ###################################### @@ -118,7 +118,7 @@ proc ptxCodegenViaNvidiaNvvm(module: ModuleRef, sm: tuple[major, minor: int32]): check nvvmDestroyProgram(prog) -proc ptxCodegenViaLlvmNvptx(module: ModuleRef, sm: tuple[major, minor: int32]): string {.used.} = +proc ptxCodegenViaLlvmNvptx(module: ModuleRef, sm: tuple[major, minor: int32]): string = ## PTX codegen via LLVM NVPTX module.verify(AbortProcessAction) diff --git a/tests/math/t_bigints_mod_vs_gmp.nim b/tests/math/t_bigints_mod_vs_gmp.nim index d46ffe9..b9578e9 100644 --- a/tests/math/t_bigints_mod_vs_gmp.nim +++ b/tests/math/t_bigints_mod_vs_gmp.nim @@ -10,7 +10,7 @@ import # Standard library std/[random, macros, times, strutils], # Third-party - gmp, stew/byteutils, + gmp, # Internal ../../constantine/math/io/io_bigints, ../../constantine/math/arithmetic, diff --git a/tests/math/t_bigints_mul_high_words_vs_gmp.nim b/tests/math/t_bigints_mul_high_words_vs_gmp.nim index ffd6f5a..44a2cbc 100644 --- a/tests/math/t_bigints_mul_high_words_vs_gmp.nim +++ b/tests/math/t_bigints_mul_high_words_vs_gmp.nim @@ -10,7 +10,7 @@ import # Standard library std/[random, macros, times, strutils], # Third-party - gmp, stew/byteutils, + gmp, # Internal ../../constantine/math/io/io_bigints, ../../constantine/math/arithmetic, diff --git a/tests/math/t_bigints_mul_vs_gmp.nim b/tests/math/t_bigints_mul_vs_gmp.nim index 2a7962a..c2fd1ae 100644 --- a/tests/math/t_bigints_mul_vs_gmp.nim +++ b/tests/math/t_bigints_mul_vs_gmp.nim @@ -10,7 +10,7 @@ import # Standard library std/[random, macros, times, strutils], # Third-party - gmp, stew/byteutils, + gmp, # Internal ../../constantine/math/io/io_bigints, ../../constantine/math/arithmetic, diff --git a/tests/math/t_ec_shortw_prj_edge_cases.nim b/tests/math/t_ec_shortw_prj_edge_cases.nim index 012a28b..1fb7efb 100644 --- a/tests/math/t_ec_shortw_prj_edge_cases.nim +++ b/tests/math/t_ec_shortw_prj_edge_cases.nim @@ -14,17 +14,14 @@ import # Standard library - std/[unittest, times], + std/unittest, # Internals ../../constantine/platforms/abstractions, ../../constantine/math/config/curves, ../../constantine/math/arithmetic, ../../constantine/math/extension_fields, - ../../constantine/math/io/[io_bigints, io_fields, io_extfields, io_ec], - ../../constantine/math/elliptic/[ec_shortweierstrass_projective, ec_scalar_mul], - # Test utilities - ../../helpers/prng_unsafe, - ./support/ec_reference_scalar_mult + ../../constantine/math/io/[io_bigints, io_fields, io_extfields], + ../../constantine/math/elliptic/ec_shortweierstrass_projective func testAddAssociativity[EC](a, b, c: EC) = var tmp1{.noInit.}, tmp2{.noInit.}: ECP_ShortW_Prj[Fp2[BLS12_381], G2] diff --git a/tests/math/t_ec_shortw_prj_g1_mul_sanity.nim b/tests/math/t_ec_shortw_prj_g1_mul_sanity.nim index 88e9123..821548d 100644 --- a/tests/math/t_ec_shortw_prj_g1_mul_sanity.nim +++ b/tests/math/t_ec_shortw_prj_g1_mul_sanity.nim @@ -10,9 +10,8 @@ import # Standard library std/[unittest, times], # Internals - ../../constantine/math/config/[common, curves], - ../../constantine/math/[arithmetic, primitives], - ../../constantine/math/io/[io_bigints, io_fields, io_ec], + ../../constantine/math/config/curves, + ../../constantine/math/io/io_fields, ../../constantine/math/elliptic/[ec_shortweierstrass_affine, ec_shortweierstrass_projective, ec_scalar_mul], # Test utilities ../../helpers/prng_unsafe, diff --git a/tests/math/t_finite_fields_vs_gmp.nim b/tests/math/t_finite_fields_vs_gmp.nim index a8b4484..6776e34 100644 --- a/tests/math/t_finite_fields_vs_gmp.nim +++ b/tests/math/t_finite_fields_vs_gmp.nim @@ -8,9 +8,9 @@ import # Standard library - std/[random, macros, times, strutils], + std/[random, macros, times], # Third-party - gmp, stew/byteutils, + gmp, # Internal ../../constantine/platforms/abstractions, ../../constantine/math/io/[io_bigints, io_fields], diff --git a/tests/math/t_pairing_bls12_377_line_functions.nim b/tests/math/t_pairing_bls12_377_line_functions.nim index 3b413de..8af6c65 100644 --- a/tests/math/t_pairing_bls12_377_line_functions.nim +++ b/tests/math/t_pairing_bls12_377_line_functions.nim @@ -8,7 +8,7 @@ import # Standard library - std/[tables, unittest, times], + std/[unittest, times], # Internals ../../constantine/platforms/abstractions, ../../constantine/math/arithmetic, @@ -21,7 +21,7 @@ import ec_scalar_mul], ../../constantine/math/pairing/lines_eval, # Test utilities - ../helpers/[prng_unsafe, static_for] + ../../helpers/[prng_unsafe, static_for] const Iters = 4 diff --git a/tests/math/t_pairing_bls12_381_line_functions.nim b/tests/math/t_pairing_bls12_381_line_functions.nim index b6441f1..318ed85 100644 --- a/tests/math/t_pairing_bls12_381_line_functions.nim +++ b/tests/math/t_pairing_bls12_381_line_functions.nim @@ -8,7 +8,7 @@ import # Standard library - std/[tables, unittest, times], + std/[unittest, times], # Internals ../../constantine/platforms/abstractions, ../../constantine/math/arithmetic, @@ -17,11 +17,10 @@ import ../../constantine/math/io/io_extfields, ../../constantine/math/elliptic/[ ec_shortweierstrass_affine, - ec_shortweierstrass_projective, - ec_scalar_mul], + ec_shortweierstrass_projective], ../../constantine/math/pairing/lines_eval, # Test utilities - ../helpers/[prng_unsafe, static_for] + ../../helpers/[prng_unsafe, static_for] const Iters = 4 diff --git a/tests/math/t_pairing_cyclotomic_subgroup.nim b/tests/math/t_pairing_cyclotomic_subgroup.nim index 30f1b7a..c0d1aa4 100644 --- a/tests/math/t_pairing_cyclotomic_subgroup.nim +++ b/tests/math/t_pairing_cyclotomic_subgroup.nim @@ -8,7 +8,7 @@ import # Standard library - std/[tables, unittest, times], + std/[unittest, times], # Internals ../../constantine/platforms/abstractions, ../../constantine/math/arithmetic, diff --git a/tests/t_blssig_pop_on_bls12381_g2.nim b/tests/t_blssig_pop_on_bls12381_g2.nim index f3ce52e..c7cdf37 100644 --- a/tests/t_blssig_pop_on_bls12381_g2.nim +++ b/tests/t_blssig_pop_on_bls12381_g2.nim @@ -7,8 +7,8 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - std/[json, os, unittest, strutils], - pkg/[jsony, stew/byteutils], + std/[os, unittest, strutils], + pkg/jsony, ../constantine/blssig_pop_on_bls12381_g2, ../constantine/math/io/io_bigints diff --git a/tests/t_ethereum_evm_precompiles.nim b/tests/t_ethereum_evm_precompiles.nim index 76b679e..ebffd66 100644 --- a/tests/t_ethereum_evm_precompiles.nim +++ b/tests/t_ethereum_evm_precompiles.nim @@ -9,8 +9,6 @@ import # Standard library std/[times, os, strutils, macros], - # Status - pkg/stew/byteutils, # 3rd party pkg/jsony, # Internals diff --git a/tests/t_hash_sha256_vs_openssl.nim b/tests/t_hash_sha256_vs_openssl.nim index 463b0ac..38c7af7 100644 --- a/tests/t_hash_sha256_vs_openssl.nim +++ b/tests/t_hash_sha256_vs_openssl.nim @@ -1,10 +1,9 @@ import # Internals ../constantine/hashes, + ../constantine/math/io/io_bigints, # Helpers - ../helpers/prng_unsafe, - # Third-party - stew/byteutils + ../helpers/prng_unsafe # Deal with platform mess # -------------------------------------------------------------------- @@ -65,7 +64,7 @@ proc sanityABC = var bufCt: array[32, byte] let msg = "abc" - let hashed = hexToByteArray[32]( + let hashed = array[32, byte].fromHex( "BA7816BF8F01CFEA414140DE5DAE2223" & "B00361A396177A9CB410FF61F20015AD") @@ -77,7 +76,7 @@ proc sanityABC2 = var bufCt: array[32, byte] let msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - let hashed = hexToByteArray[32]( + let hashed = array[32, byte].fromHex( "248D6A61D20638B8E5C026930C3E6039" & "A33CE45964FF2167F6ECEDD419DB06C1") diff --git a/tests/t_hash_to_curve.nim b/tests/t_hash_to_curve.nim index 16cb0f2..0ef790d 100644 --- a/tests/t_hash_to_curve.nim +++ b/tests/t_hash_to_curve.nim @@ -8,7 +8,7 @@ import # Standard library - std/[unittest, times, os, strutils, macros], + std/[unittest, times, os, strutils], # 3rd party pkg/jsony, # Internals diff --git a/tests/t_hash_to_curve_random.nim b/tests/t_hash_to_curve_random.nim index cc13bb1..09d0859 100644 --- a/tests/t_hash_to_curve_random.nim +++ b/tests/t_hash_to_curve_random.nim @@ -8,7 +8,7 @@ import # Standard library - std/[unittest, times, os, strutils], + std/[unittest, times], # Internals ../constantine/math/config/curves, ../constantine/math/extension_fields, diff --git a/tests/t_hash_to_field.nim b/tests/t_hash_to_field.nim index dfd33e1..5976455 100644 --- a/tests/t_hash_to_field.nim +++ b/tests/t_hash_to_field.nim @@ -11,10 +11,7 @@ import ../constantine/hash_to_curve/h2c_hash_to_field, ../constantine/math/config/[curves_declaration, type_ff], ../constantine/math/extension_fields/towers, - ../constantine/math/io/[io_fields, io_extfields], - - # Third-party - stew/byteutils + ../constantine/math/io/[io_bigints, io_fields, io_extfields] # Test vectors for expandMessageXMD # ---------------------------------------------------------------------- @@ -47,19 +44,19 @@ testExpandMessageXMD(1): let msg = "" const expected = "f659819a6473c1835b25ea59e3d38914c98b374f0970b7e4c92181df928fca88" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(2): let msg = "abc" const expected = "1c38f7c211ef233367b2420d04798fa4698080a8901021a795a1151775fe4da7" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(3): let msg = "abcdef0123456789" const expected = "8f7e7b66791f0da0dbb5ec7c22ec637f79758c0a48170bfb7c4611bd304ece89" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(4): let msg = "q128_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" & @@ -67,7 +64,7 @@ testExpandMessageXMD(4): "qqqqqqqqqqqqqqqqqqqqqqqqq" const expected = "72d5aa5ec810370d1f0013c0df2f1d65699494ee2a39f72e1716b1b964e1c642" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(5): let msg = "a512_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & @@ -82,7 +79,7 @@ testExpandMessageXMD(5): "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" const expected = "3b8e704fc48336aca4c2a12195b720882f2162a4b7b13a9c350db46f429b771b" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(6): let msg = "" @@ -92,7 +89,7 @@ testExpandMessageXMD(6): "fc5d9d8d77e2071b86ab114a9f34150954a7531da568a1ea8c7608" & "61c0cde2005afc2c114042ee7b5848f5303f0611cf297f" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(7): let msg = "abc" @@ -102,7 +99,7 @@ testExpandMessageXMD(7): "98619c0aa0c6c51fca15520789925e813dcfd318b542f879944127" & "1f4db9ee3b8092a7a2e8d5b75b73e28fb1ab6b4573c192" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(8): let msg = "abcdef0123456789" @@ -112,7 +109,7 @@ testExpandMessageXMD(8): "4b9535a819b445814890b7029b5de805bf62b33a4dc7e24acdf2c9" & "24e9fe50d55a6b832c8c84c7f82474b34e48c6d43867be" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(9): let msg = "q128_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" & @@ -124,7 +121,7 @@ testExpandMessageXMD(9): "720fe96ba53db947842120a068816ac05c159bb5266c63658b4f00" & "0cbf87b1209a225def8ef1dca917bcda79a1e42acd8069" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) testExpandMessageXMD(10): let msg = "a512_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & @@ -143,7 +140,7 @@ testExpandMessageXMD(10): "42a0807bb148b77c2ef82ed4b6c9f7fcb732e7f94466c8b51e52bf" & "378fba044a31f5cb44583a892f5969dcd73b3fa128816e" const len_in_bytes = expected.len div 2 - const expectedBytes = hexToByteArray[len_in_bytes](expected) + const expectedBytes = array[len_in_bytes, byte].fromHex(expected) template testHashToField(id, constants: untyped) = # Section "Expand test vectors {#expand-testvectors}" diff --git a/tests/t_kdf_hkdf.nim b/tests/t_kdf_hkdf.nim index 3691f54..90d4998 100644 --- a/tests/t_kdf_hkdf.nim +++ b/tests/t_kdf_hkdf.nim @@ -7,11 +7,19 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import - stew/byteutils, + ../constantine/math/io/io_bigints, ../constantine/[hashes, mac/mac_hmac, kdf/kdf_hkdf] proc hexToBytes(s: string): seq[byte] = - if s.len != 0: return hexToSeqByte(s) + if s.len > 0: + var skip = 0 + if s.len >= 2: + skip = 2*( + int(s[0] == '0') and + (int(s[1] == 'x') or int(s[1] == 'X')) + ) + result.setLen((s.len - skip) div 2) + s.hexToPaddedByteArray(result, bigEndian) template test(id, constants: untyped) = proc `test _ id`() =