2020-02-28 22:46:20 +01:00
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
# ############################################################
|
|
|
|
|
#
|
|
|
|
|
# Benchmark of modular exponentiation
|
|
|
|
|
#
|
|
|
|
|
# ############################################################
|
|
|
|
|
|
|
|
|
|
# 2 implementations are available
|
|
|
|
|
# - 1 is constant time
|
|
|
|
|
# - 1 exposes the exponent bits to:
|
|
|
|
|
# timing attack,
|
|
|
|
|
# memory access analysis,
|
|
|
|
|
# power analysis (i.e. oscilloscopes on embedded)
|
|
|
|
|
# It is suitable for public exponents for example
|
|
|
|
|
# to compute modular inversion via the Fermat method
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
../constantine/config/[common, curves],
|
|
|
|
|
../constantine/arithmetic/[bigints_checked, finite_fields],
|
|
|
|
|
../constantine/io/[io_bigints, io_fields],
|
2020-02-29 19:36:35 +01:00
|
|
|
|
random, std/monotimes, times, strformat,
|
|
|
|
|
./timers
|
2020-02-28 22:46:20 +01:00
|
|
|
|
|
|
|
|
|
const Iters = 1_000_000
|
|
|
|
|
|
|
|
|
|
randomize(1234)
|
|
|
|
|
|
2020-02-29 19:36:35 +01:00
|
|
|
|
echo "\n⚠️ Measurements are approximate and use the CPU nominal clock: Turbo-Boost and overclocking will skew them."
|
|
|
|
|
echo "==========================================================================================================\n"
|
|
|
|
|
|
2020-02-28 22:46:20 +01:00
|
|
|
|
proc addBench() =
|
|
|
|
|
var r, x, y: Fp[BLS12_381]
|
|
|
|
|
# BN254 field modulus
|
|
|
|
|
x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
|
|
|
|
|
# BLS12-381 prime - 2
|
|
|
|
|
y.fromHex("0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaa9")
|
|
|
|
|
|
|
|
|
|
let start = getMonotime()
|
2020-02-29 19:36:35 +01:00
|
|
|
|
let startClk = getTicks()
|
2020-02-28 22:46:20 +01:00
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
x += y
|
2020-02-29 19:36:35 +01:00
|
|
|
|
let stopClk = getTicks()
|
2020-02-28 22:46:20 +01:00
|
|
|
|
let stop = getMonotime()
|
|
|
|
|
|
2020-02-29 18:27:20 +01:00
|
|
|
|
echo &"Time for {Iters} additions in 𝔽p (constant-time 381-bit): {inMilliseconds(stop-start)} ms"
|
|
|
|
|
echo &"Time for 1 addition in 𝔽p ==> {inNanoseconds((stop-start) div Iters)} ns"
|
2020-02-29 19:36:35 +01:00
|
|
|
|
echo &"Cycles per addition 𝔽p ==> {(stopClk - startClk) div Iters} cycles"
|
2020-02-28 22:46:20 +01:00
|
|
|
|
|
|
|
|
|
addBench()
|
|
|
|
|
|
|
|
|
|
proc mulBench() =
|
|
|
|
|
var r, x, y: Fp[BLS12_381]
|
|
|
|
|
# BN254 field modulus
|
|
|
|
|
x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
|
|
|
|
|
# BLS12-381 prime - 2
|
|
|
|
|
y.fromHex("0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaa9")
|
|
|
|
|
|
|
|
|
|
let start = getMonotime()
|
2020-02-29 19:36:35 +01:00
|
|
|
|
let startClk = getTicks()
|
2020-02-28 22:46:20 +01:00
|
|
|
|
for _ in 0 ..< Iters:
|
|
|
|
|
r.prod(x, y)
|
2020-02-29 19:36:35 +01:00
|
|
|
|
let stopClk = getTicks()
|
2020-02-28 22:46:20 +01:00
|
|
|
|
let stop = getMonotime()
|
|
|
|
|
|
2020-02-29 18:27:20 +01:00
|
|
|
|
echo &"Time for {Iters} multiplications 𝔽p (constant-time 381-bit): {inMilliseconds(stop-start)} ms"
|
|
|
|
|
echo &"Time for 1 multiplication 𝔽p ==> {inNanoseconds((stop-start) div Iters)} ns"
|
2020-02-29 19:36:35 +01:00
|
|
|
|
echo &"Cycles per multiplication 𝔽p ==> {(stopClk - startClk) div Iters} cycles"
|
2020-02-28 22:46:20 +01:00
|
|
|
|
|
|
|
|
|
mulBench()
|