chore: remove stew/byteutils dependencies and unneeded imports

This commit is contained in:
Mamy Ratsimbazafy 2023-01-12 20:25:57 +01:00
parent 4052a07611
commit 4be89d309f
No known key found for this signature in database
GPG Key ID: 6227262F49BE273A
41 changed files with 128 additions and 136 deletions

View File

@ -188,14 +188,14 @@ jobs:
run: | run: |
pacman -S --needed --noconfirm mingw-w64-x86_64-gmp pacman -S --needed --noconfirm mingw-w64-x86_64-gmp
nimble refresh --verbose -y nimble refresh --verbose -y
nimble install --verbose -y gmp stew jsony asynctools nimble install --verbose -y gmp jsony asynctools
- name: Install test dependencies - name: Install test dependencies
if: runner.os != 'Windows' if: runner.os != 'Windows'
shell: bash shell: bash
run: | run: |
nimble refresh --verbose -y 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) - name: Run Constantine tests (UNIX with Assembly)
if: runner.os != 'Windows' && matrix.target.BACKEND == 'ASM' if: runner.os != 'Windows' && matrix.target.BACKEND == 'ASM'

View File

@ -81,17 +81,11 @@ addons:
apt: apt:
packages: packages:
- libgmp-dev - libgmp-dev
- parallel
homebrew: homebrew:
packages: packages:
- gmp - gmp
# Travis `bundle` bug: https://travis-ci.community/t/macos-build-fails-because-of-homebrew-bundle-unknown-command/7296/28
# - parallel
before_install: before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
HOMEBREW_NO_AUTO_UPDATE=1 brew install parallel;
fi
- | - |
if [ "${CHANNEL}" = stable ]; then if [ "${CHANNEL}" = stable ]; then
BRANCH="v$(curl https://nim-lang.org/channels/stable)" BRANCH="v$(curl https://nim-lang.org/channels/stable)"
@ -127,7 +121,7 @@ before_script:
- export PATH="$PWD/nim-${CHANNEL}/bin${PATH:+:$PATH}" - export PATH="$PWD/nim-${CHANNEL}/bin${PATH:+:$PATH}"
script: script:
- nimble refresh - nimble refresh
- nimble install -y gmp stew jsony - nimble install -y gmp jsony
# Installing Clang9.0 or later is a pain in Travis # Installing Clang9.0 or later is a pain in Travis
# for inline assembly "flag output constraint" # for inline assembly "flag output constraint"
@ -137,9 +131,6 @@ script:
nimble test_parallel_no_assembler nimble test_parallel_no_assembler
else else
nimble test_parallel nimble test_parallel
if [[ "$ARCH" == "amd64" ]]; then
nimble test_parallel_no_assembler
fi
fi fi
branches: branches:
except: except:

View File

@ -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 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
<!-- TOC -->
- [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)
<!-- /TOC -->
## Target audience ## 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, On x86-64, inline assembly is used to workaround compilers having issues optimizing large integer arithmetic,
and also ensure constant-time code. 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 ## Security
Hardening an implementation against all existing and upcoming attack vectors is an extremely complex task. 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 - formal verification of constant-time implementation is possible
Defense against common attack vectors are provided on a best effort basis. 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: Attackers may go to great lengths to retrieve secret data including:
- Timing the time taken to multiply on an elliptic curve - 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 - derive constants
- write a size-independent inline assembly code generator - 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)) - 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`. 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`.

View File

@ -268,7 +268,7 @@ steps:
- bash: | - bash: |
echo "PATH=${PATH}" echo "PATH=${PATH}"
nimble refresh nimble refresh
nimble install -y gmp stew jsony nimble install -y gmp jsony
displayName: 'Installing package and testing dependencies' displayName: 'Installing package and testing dependencies'
- bash: | - bash: |

View File

@ -15,9 +15,7 @@ import
ec_shortweierstrass_jacobian], ec_shortweierstrass_jacobian],
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_elliptic_template, ./bench_elliptic_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -16,9 +16,7 @@ import
ec_shortweierstrass_jacobian], ec_shortweierstrass_jacobian],
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_elliptic_template, ./bench_elliptic_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -12,9 +12,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_fields_template, ./bench_fields_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -12,9 +12,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_fields_template, ./bench_fields_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -12,9 +12,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_fields_template, ./bench_fields_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -12,9 +12,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_fields_template, ./bench_fields_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -22,7 +22,7 @@ import
../helpers/[prng_unsafe, static_for], ../helpers/[prng_unsafe, static_for],
./platforms, ./platforms,
# Standard library # Standard library
std/[monotimes, times, strformat, strutils, macros] std/[monotimes, times, strformat, strutils]
var rng: RngState var rng: RngState
let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32 let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_pairing_template, ./bench_pairing_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_pairing_template, ./bench_pairing_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_pairing_template, ./bench_pairing_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_pairing_template, ./bench_pairing_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_summary_template, ./bench_summary_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_summary_template, ./bench_summary_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_summary_template, ./bench_summary_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_summary_template, ./bench_summary_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -13,9 +13,7 @@ import
../constantine/math/extension_fields, ../constantine/math/extension_fields,
# Helpers # Helpers
../helpers/static_for, ../helpers/static_for,
./bench_summary_template, ./bench_summary_template
# Standard library
std/strutils
# ############################################################ # ############################################################
# #

View File

@ -8,7 +8,7 @@
import import
# Standard library # Standard library
std/[macros, algorithm], std/macros,
# Internal # Internal
../../../platforms/abstractions, ../../../platforms/abstractions,
./limbs_asm_modular_x86, ./limbs_asm_modular_x86,

View File

@ -7,8 +7,6 @@
# 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
# Standard library
std/macros,
# Internal # Internal
../../platforms/abstractions, ../../platforms/abstractions,
./limbs, ./limbs_extmul ./limbs, ./limbs_extmul

View File

@ -291,7 +291,7 @@ func marshalBE[T](
var tail = dst.len var tail = dst.len
while tail > 0: 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 else: 0
inc src_idx inc src_idx
@ -439,23 +439,20 @@ func hexToPaddedByteArray*(hexStr: string, output: var openArray[byte], order: s
shift = (shift + 4) and 4 shift = (shift + 4) and 4
dstIdx += shift shr 2 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 ## Convert a byte-array to its hex representation
## Output is in lowercase and not prefixed. ## Output is in lowercase and prefixed with 0x
## This assumes that input is in platform native endianness
const hexChars = "0123456789abcdef" const hexChars = "0123456789abcdef"
result = newString(2 + 2 * bytes.len) result = newString(2 + 2 * bytes.len)
result[0] = '0' result[0] = '0'
result[1] = 'x' result[1] = 'x'
for i in 0 ..< bytes.len: for i in 0 ..< bytes.len:
when order == system.cpuEndian:
let bi = bytes[i] let bi = bytes[i]
result[2 + 2*i] = hexChars.secretLookup(SecretWord bi shr 4 and 0xF) result[2 + 2*i] = hexChars.secretLookup(SecretWord bi shr 4 and 0xF)
result[2 + 2*i+1] = hexChars.secretLookup(SecretWord bi and 0xF) result[2 + 2*i+1] = hexChars.secretLookup(SecretWord bi and 0xF)
else:
let bmi = bytes[bytes.high - i] func fromHex*[N: static int](T: type array[N, byte], hex: string): T =
result[2 + 2*i] = hexChars.secretLookup(SecretWord bmi shr 4 and 0xF) hexToPaddedByteArray(hex, result, bigEndian)
result[2 + 2*i+1] = hexChars.secretLookup(SecretWord bmi and 0xF)
# ############################################################ # ############################################################
# #
@ -520,10 +517,10 @@ func appendHex*(dst: var string, big: BigInt, order: static Endianness = bigEndi
# 1. Convert Big Int to canonical uint # 1. Convert Big Int to canonical uint
const canonLen = (big.bits + 8 - 1) div 8 const canonLen = (big.bits + 8 - 1) div 8
var bytes: array[canonLen, byte] var bytes: array[canonLen, byte]
marshal(bytes, big, cpuEndian) marshal(bytes, big, order)
# 2 Convert canonical uint to hex # 2 Convert canonical uint to hex
dst.add bytes.nativeEndianToHex(order) dst.add bytes.toHex()
func toHex*(big: BigInt, order: static Endianness = bigEndian): string = func toHex*(big: BigInt, order: static Endianness = bigEndian): string =
## Stringify an int to hex. ## Stringify an int to hex.

View File

@ -10,8 +10,7 @@ import
../../math/config/[curves, precompute], ../../math/config/[curves, precompute],
../../math/io/io_bigints, ../../math/io/io_bigints,
../primitives, ../bithacks, ../endians, ../primitives, ../bithacks, ../endians,
./llvm, ./llvm
std/hashes
# ############################################################ # ############################################################
# #
@ -129,10 +128,10 @@ func toHex[T](a: BigNum[T]): string =
# 1. Convert BigInt to canonical uint # 1. Convert BigInt to canonical uint
const wordBitwidth = sizeof(T) * 8 const wordBitwidth = sizeof(T) * 8
var bytes = newSeq[byte](byteLen(a.bits)) 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 # 2 Convert canonical uint to hex
return bytes.nativeEndianToHex(bigEndian) return bytes.toHex()
# Checks # Checks
# ------------------------------------------------ # ------------------------------------------------
@ -245,10 +244,6 @@ proc init*(
result.fp.setFieldConst(ctx, wordSize, fpBits, fpMod) result.fp.setFieldConst(ctx, wordSize, fpBits, fpMod)
result.fr.setFieldConst(ctx, wordSize, frBits, frMod) 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.} = proc genSymbol*(cm: CurveMetadata, opcode: Opcode): string {.inline.} =
cm.prefix & cm.prefix &
(if cm.wordSize == size32: "32b_" else: "64b_") & (if cm.wordSize == size32: "32b_" else: "64b_") &

View File

@ -91,7 +91,7 @@ proc getNvvmLog(prog: NvvmProgram): string {.used.} =
result = newString(logSize) result = newString(logSize)
check nvvmGetProgramLog(prog, result[0].addr) 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 ## PTX codegen via Nvidia NVVM
# ###################################### # ######################################
@ -118,7 +118,7 @@ proc ptxCodegenViaNvidiaNvvm(module: ModuleRef, sm: tuple[major, minor: int32]):
check nvvmDestroyProgram(prog) 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 ## PTX codegen via LLVM NVPTX
module.verify(AbortProcessAction) module.verify(AbortProcessAction)

View File

@ -10,7 +10,7 @@ import
# Standard library # Standard library
std/[random, macros, times, strutils], std/[random, macros, times, strutils],
# Third-party # Third-party
gmp, stew/byteutils, gmp,
# Internal # Internal
../../constantine/math/io/io_bigints, ../../constantine/math/io/io_bigints,
../../constantine/math/arithmetic, ../../constantine/math/arithmetic,

View File

@ -10,7 +10,7 @@ import
# Standard library # Standard library
std/[random, macros, times, strutils], std/[random, macros, times, strutils],
# Third-party # Third-party
gmp, stew/byteutils, gmp,
# Internal # Internal
../../constantine/math/io/io_bigints, ../../constantine/math/io/io_bigints,
../../constantine/math/arithmetic, ../../constantine/math/arithmetic,

View File

@ -10,7 +10,7 @@ import
# Standard library # Standard library
std/[random, macros, times, strutils], std/[random, macros, times, strutils],
# Third-party # Third-party
gmp, stew/byteutils, gmp,
# Internal # Internal
../../constantine/math/io/io_bigints, ../../constantine/math/io/io_bigints,
../../constantine/math/arithmetic, ../../constantine/math/arithmetic,

View File

@ -14,17 +14,14 @@
import import
# Standard library # Standard library
std/[unittest, times], std/unittest,
# Internals # Internals
../../constantine/platforms/abstractions, ../../constantine/platforms/abstractions,
../../constantine/math/config/curves, ../../constantine/math/config/curves,
../../constantine/math/arithmetic, ../../constantine/math/arithmetic,
../../constantine/math/extension_fields, ../../constantine/math/extension_fields,
../../constantine/math/io/[io_bigints, io_fields, io_extfields, io_ec], ../../constantine/math/io/[io_bigints, io_fields, io_extfields],
../../constantine/math/elliptic/[ec_shortweierstrass_projective, ec_scalar_mul], ../../constantine/math/elliptic/ec_shortweierstrass_projective
# Test utilities
../../helpers/prng_unsafe,
./support/ec_reference_scalar_mult
func testAddAssociativity[EC](a, b, c: EC) = func testAddAssociativity[EC](a, b, c: EC) =
var tmp1{.noInit.}, tmp2{.noInit.}: ECP_ShortW_Prj[Fp2[BLS12_381], G2] var tmp1{.noInit.}, tmp2{.noInit.}: ECP_ShortW_Prj[Fp2[BLS12_381], G2]

View File

@ -10,9 +10,8 @@ import
# Standard library # Standard library
std/[unittest, times], std/[unittest, times],
# Internals # Internals
../../constantine/math/config/[common, curves], ../../constantine/math/config/curves,
../../constantine/math/[arithmetic, primitives], ../../constantine/math/io/io_fields,
../../constantine/math/io/[io_bigints, io_fields, io_ec],
../../constantine/math/elliptic/[ec_shortweierstrass_affine, ec_shortweierstrass_projective, ec_scalar_mul], ../../constantine/math/elliptic/[ec_shortweierstrass_affine, ec_shortweierstrass_projective, ec_scalar_mul],
# Test utilities # Test utilities
../../helpers/prng_unsafe, ../../helpers/prng_unsafe,

View File

@ -8,9 +8,9 @@
import import
# Standard library # Standard library
std/[random, macros, times, strutils], std/[random, macros, times],
# Third-party # Third-party
gmp, stew/byteutils, gmp,
# Internal # Internal
../../constantine/platforms/abstractions, ../../constantine/platforms/abstractions,
../../constantine/math/io/[io_bigints, io_fields], ../../constantine/math/io/[io_bigints, io_fields],

View File

@ -8,7 +8,7 @@
import import
# Standard library # Standard library
std/[tables, unittest, times], std/[unittest, times],
# Internals # Internals
../../constantine/platforms/abstractions, ../../constantine/platforms/abstractions,
../../constantine/math/arithmetic, ../../constantine/math/arithmetic,
@ -21,7 +21,7 @@ import
ec_scalar_mul], ec_scalar_mul],
../../constantine/math/pairing/lines_eval, ../../constantine/math/pairing/lines_eval,
# Test utilities # Test utilities
../helpers/[prng_unsafe, static_for] ../../helpers/[prng_unsafe, static_for]
const const
Iters = 4 Iters = 4

View File

@ -8,7 +8,7 @@
import import
# Standard library # Standard library
std/[tables, unittest, times], std/[unittest, times],
# Internals # Internals
../../constantine/platforms/abstractions, ../../constantine/platforms/abstractions,
../../constantine/math/arithmetic, ../../constantine/math/arithmetic,
@ -17,11 +17,10 @@ import
../../constantine/math/io/io_extfields, ../../constantine/math/io/io_extfields,
../../constantine/math/elliptic/[ ../../constantine/math/elliptic/[
ec_shortweierstrass_affine, ec_shortweierstrass_affine,
ec_shortweierstrass_projective, ec_shortweierstrass_projective],
ec_scalar_mul],
../../constantine/math/pairing/lines_eval, ../../constantine/math/pairing/lines_eval,
# Test utilities # Test utilities
../helpers/[prng_unsafe, static_for] ../../helpers/[prng_unsafe, static_for]
const const
Iters = 4 Iters = 4

View File

@ -8,7 +8,7 @@
import import
# Standard library # Standard library
std/[tables, unittest, times], std/[unittest, times],
# Internals # Internals
../../constantine/platforms/abstractions, ../../constantine/platforms/abstractions,
../../constantine/math/arithmetic, ../../constantine/math/arithmetic,

View File

@ -7,8 +7,8 @@
# 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
std/[json, os, unittest, strutils], std/[os, unittest, strutils],
pkg/[jsony, stew/byteutils], pkg/jsony,
../constantine/blssig_pop_on_bls12381_g2, ../constantine/blssig_pop_on_bls12381_g2,
../constantine/math/io/io_bigints ../constantine/math/io/io_bigints

View File

@ -9,8 +9,6 @@
import import
# Standard library # Standard library
std/[times, os, strutils, macros], std/[times, os, strutils, macros],
# Status
pkg/stew/byteutils,
# 3rd party # 3rd party
pkg/jsony, pkg/jsony,
# Internals # Internals

View File

@ -1,10 +1,9 @@
import import
# Internals # Internals
../constantine/hashes, ../constantine/hashes,
../constantine/math/io/io_bigints,
# Helpers # Helpers
../helpers/prng_unsafe, ../helpers/prng_unsafe
# Third-party
stew/byteutils
# Deal with platform mess # Deal with platform mess
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -65,7 +64,7 @@ proc sanityABC =
var bufCt: array[32, byte] var bufCt: array[32, byte]
let msg = "abc" let msg = "abc"
let hashed = hexToByteArray[32]( let hashed = array[32, byte].fromHex(
"BA7816BF8F01CFEA414140DE5DAE2223" & "BA7816BF8F01CFEA414140DE5DAE2223" &
"B00361A396177A9CB410FF61F20015AD") "B00361A396177A9CB410FF61F20015AD")
@ -77,7 +76,7 @@ proc sanityABC2 =
var bufCt: array[32, byte] var bufCt: array[32, byte]
let msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" let msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
let hashed = hexToByteArray[32]( let hashed = array[32, byte].fromHex(
"248D6A61D20638B8E5C026930C3E6039" & "248D6A61D20638B8E5C026930C3E6039" &
"A33CE45964FF2167F6ECEDD419DB06C1") "A33CE45964FF2167F6ECEDD419DB06C1")

View File

@ -8,7 +8,7 @@
import import
# Standard library # Standard library
std/[unittest, times, os, strutils, macros], std/[unittest, times, os, strutils],
# 3rd party # 3rd party
pkg/jsony, pkg/jsony,
# Internals # Internals

View File

@ -8,7 +8,7 @@
import import
# Standard library # Standard library
std/[unittest, times, os, strutils], std/[unittest, times],
# Internals # Internals
../constantine/math/config/curves, ../constantine/math/config/curves,
../constantine/math/extension_fields, ../constantine/math/extension_fields,

View File

@ -11,10 +11,7 @@ import
../constantine/hash_to_curve/h2c_hash_to_field, ../constantine/hash_to_curve/h2c_hash_to_field,
../constantine/math/config/[curves_declaration, type_ff], ../constantine/math/config/[curves_declaration, type_ff],
../constantine/math/extension_fields/towers, ../constantine/math/extension_fields/towers,
../constantine/math/io/[io_fields, io_extfields], ../constantine/math/io/[io_bigints, io_fields, io_extfields]
# Third-party
stew/byteutils
# Test vectors for expandMessageXMD # Test vectors for expandMessageXMD
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
@ -47,19 +44,19 @@ testExpandMessageXMD(1):
let msg = "" let msg = ""
const expected = "f659819a6473c1835b25ea59e3d38914c98b374f0970b7e4c92181df928fca88" const expected = "f659819a6473c1835b25ea59e3d38914c98b374f0970b7e4c92181df928fca88"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(2):
let msg = "abc" let msg = "abc"
const expected = "1c38f7c211ef233367b2420d04798fa4698080a8901021a795a1151775fe4da7" const expected = "1c38f7c211ef233367b2420d04798fa4698080a8901021a795a1151775fe4da7"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(3):
let msg = "abcdef0123456789" let msg = "abcdef0123456789"
const expected = "8f7e7b66791f0da0dbb5ec7c22ec637f79758c0a48170bfb7c4611bd304ece89" const expected = "8f7e7b66791f0da0dbb5ec7c22ec637f79758c0a48170bfb7c4611bd304ece89"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(4):
let msg = "q128_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" & let msg = "q128_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" &
@ -67,7 +64,7 @@ testExpandMessageXMD(4):
"qqqqqqqqqqqqqqqqqqqqqqqqq" "qqqqqqqqqqqqqqqqqqqqqqqqq"
const expected = "72d5aa5ec810370d1f0013c0df2f1d65699494ee2a39f72e1716b1b964e1c642" const expected = "72d5aa5ec810370d1f0013c0df2f1d65699494ee2a39f72e1716b1b964e1c642"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(5):
let msg = "a512_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & let msg = "a512_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" &
@ -82,7 +79,7 @@ testExpandMessageXMD(5):
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
const expected = "3b8e704fc48336aca4c2a12195b720882f2162a4b7b13a9c350db46f429b771b" const expected = "3b8e704fc48336aca4c2a12195b720882f2162a4b7b13a9c350db46f429b771b"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(6):
let msg = "" let msg = ""
@ -92,7 +89,7 @@ testExpandMessageXMD(6):
"fc5d9d8d77e2071b86ab114a9f34150954a7531da568a1ea8c7608" & "fc5d9d8d77e2071b86ab114a9f34150954a7531da568a1ea8c7608" &
"61c0cde2005afc2c114042ee7b5848f5303f0611cf297f" "61c0cde2005afc2c114042ee7b5848f5303f0611cf297f"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(7):
let msg = "abc" let msg = "abc"
@ -102,7 +99,7 @@ testExpandMessageXMD(7):
"98619c0aa0c6c51fca15520789925e813dcfd318b542f879944127" & "98619c0aa0c6c51fca15520789925e813dcfd318b542f879944127" &
"1f4db9ee3b8092a7a2e8d5b75b73e28fb1ab6b4573c192" "1f4db9ee3b8092a7a2e8d5b75b73e28fb1ab6b4573c192"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(8):
let msg = "abcdef0123456789" let msg = "abcdef0123456789"
@ -112,7 +109,7 @@ testExpandMessageXMD(8):
"4b9535a819b445814890b7029b5de805bf62b33a4dc7e24acdf2c9" & "4b9535a819b445814890b7029b5de805bf62b33a4dc7e24acdf2c9" &
"24e9fe50d55a6b832c8c84c7f82474b34e48c6d43867be" "24e9fe50d55a6b832c8c84c7f82474b34e48c6d43867be"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(9):
let msg = "q128_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" & let msg = "q128_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" &
@ -124,7 +121,7 @@ testExpandMessageXMD(9):
"720fe96ba53db947842120a068816ac05c159bb5266c63658b4f00" & "720fe96ba53db947842120a068816ac05c159bb5266c63658b4f00" &
"0cbf87b1209a225def8ef1dca917bcda79a1e42acd8069" "0cbf87b1209a225def8ef1dca917bcda79a1e42acd8069"
const len_in_bytes = expected.len div 2 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): testExpandMessageXMD(10):
let msg = "a512_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & let msg = "a512_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" &
@ -143,7 +140,7 @@ testExpandMessageXMD(10):
"42a0807bb148b77c2ef82ed4b6c9f7fcb732e7f94466c8b51e52bf" & "42a0807bb148b77c2ef82ed4b6c9f7fcb732e7f94466c8b51e52bf" &
"378fba044a31f5cb44583a892f5969dcd73b3fa128816e" "378fba044a31f5cb44583a892f5969dcd73b3fa128816e"
const len_in_bytes = expected.len div 2 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) = template testHashToField(id, constants: untyped) =
# Section "Expand test vectors {#expand-testvectors}" # Section "Expand test vectors {#expand-testvectors}"

View File

@ -7,11 +7,19 @@
# 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
stew/byteutils, ../constantine/math/io/io_bigints,
../constantine/[hashes, mac/mac_hmac, kdf/kdf_hkdf] ../constantine/[hashes, mac/mac_hmac, kdf/kdf_hkdf]
proc hexToBytes(s: string): seq[byte] = 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) = template test(id, constants: untyped) =
proc `test _ id`() = proc `test _ id`() =