mirror of
https://github.com/codex-storage/constantine.git
synced 2025-01-13 20:44:49 +00:00
Benchmark template for 𝔽p, 𝔽p2, 𝔽p6
This commit is contained in:
parent
03898b2292
commit
9e78cd5d6d
@ -18,25 +18,16 @@ import
|
||||
../constantine/arithmetic,
|
||||
../constantine/io/[io_bigints, io_fields],
|
||||
../constantine/primitives,
|
||||
../constantine/tower_field_extensions/[abelian_groups, fp2_complex, fp6_1_plus_i],
|
||||
# Helpers
|
||||
../helpers/[timers, prng, static_for],
|
||||
# Standard library
|
||||
std/[monotimes, times, strformat, strutils, macros]
|
||||
|
||||
const Iters = 1_000_000
|
||||
const InvIters = 1000
|
||||
const AvailableCurves = [
|
||||
P224,
|
||||
BN254,
|
||||
P256,
|
||||
Secp256k1,
|
||||
BLS12_381
|
||||
]
|
||||
|
||||
var rng: RngState
|
||||
let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
|
||||
rng.seed(seed)
|
||||
echo "bench_finite_field xoshiro512** seed: ", seed
|
||||
echo "bench xoshiro512** seed: ", seed
|
||||
|
||||
# warmup
|
||||
proc warmup*() =
|
||||
@ -82,9 +73,6 @@ macro fixFieldDisplay(T: typedesc): untyped =
|
||||
name.add "[" & $Curve(instantiated[1][1].intVal) & "]"
|
||||
result = newLit name
|
||||
|
||||
# Compilers are smart with dead code (but not with multiprecision arithmetic :/)
|
||||
var globalsAreNotOptimizedAway: Word
|
||||
|
||||
template bench(op: string, T: typedesc, iters: int, body: untyped): untyped =
|
||||
let start = getMonotime()
|
||||
let startClk = getTicks()
|
||||
@ -95,63 +83,43 @@ template bench(op: string, T: typedesc, iters: int, body: untyped): untyped =
|
||||
|
||||
report(op, fixFieldDisplay(T), start, stop, startClk, stopClk, iters)
|
||||
|
||||
proc addBench(T: typedesc) =
|
||||
proc addBench*(T: typedesc, iters: int) =
|
||||
var x = rng.random(T)
|
||||
let y = rng.random(T)
|
||||
bench("Addition", T, Iters):
|
||||
bench("Addition", T, iters):
|
||||
x += y
|
||||
globalsAreNotOptimizedAway += x.mres.limbs[^1]
|
||||
|
||||
proc subBench(T: typedesc) =
|
||||
proc subBench*(T: typedesc, iters: int) =
|
||||
var x = rng.random(T)
|
||||
let y = rng.random(T)
|
||||
preventOptimAway(x)
|
||||
bench("Substraction", T, Iters):
|
||||
bench("Substraction", T, iters):
|
||||
x -= y
|
||||
globalsAreNotOptimizedAway += x.mres.limbs[^1]
|
||||
|
||||
proc negBench(T: typedesc) =
|
||||
proc negBench*(T: typedesc, iters: int) =
|
||||
var r: T
|
||||
let x = rng.random(T)
|
||||
bench("Negation", T, Iters):
|
||||
bench("Negation", T, iters):
|
||||
r.neg(x)
|
||||
globalsAreNotOptimizedAway += r.mres.limbs[^1]
|
||||
|
||||
proc mulBench(T: typedesc) =
|
||||
proc mulBench*(T: typedesc, iters: int) =
|
||||
var r: T
|
||||
let x = rng.random(T)
|
||||
let y = rng.random(T)
|
||||
preventOptimAway(r)
|
||||
bench("Multiplication", T, Iters):
|
||||
bench("Multiplication", T, iters):
|
||||
r.prod(x, y)
|
||||
|
||||
proc sqrBench(T: typedesc) =
|
||||
proc sqrBench*(T: typedesc, iters: int) =
|
||||
var r: T
|
||||
let x = rng.random(T)
|
||||
preventOptimAway(r)
|
||||
bench("Squaring", T, Iters):
|
||||
bench("Squaring", T, iters):
|
||||
r.square(x)
|
||||
|
||||
proc invBench(T: typedesc) =
|
||||
proc invBench*(T: typedesc, iters: int) =
|
||||
var r: T
|
||||
let x = rng.random(T)
|
||||
preventOptimAway(r)
|
||||
bench("Inversion", T, InvIters):
|
||||
bench("Inversion", T, iters):
|
||||
r.inv(x)
|
||||
|
||||
proc main() =
|
||||
echo "-".repeat(80)
|
||||
staticFor i, 0, AvailableCurves.len:
|
||||
const curve = AvailableCurves[i]
|
||||
addBench(Fp[curve])
|
||||
subBench(Fp[curve])
|
||||
negBench(Fp[curve])
|
||||
mulBench(Fp[curve])
|
||||
sqrBench(Fp[curve])
|
||||
invBench(Fp[curve])
|
||||
echo "-".repeat(80)
|
||||
|
||||
main()
|
||||
|
||||
echo "Notes:"
|
||||
echo " GCC is significantly slower than Clang on multiprecision arithmetic."
|
51
benchmarks/bench_fp.nim
Normal file
51
benchmarks/bench_fp.nim
Normal file
@ -0,0 +1,51 @@
|
||||
# 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.
|
||||
|
||||
import
|
||||
# Internals
|
||||
../constantine/config/curves,
|
||||
# Helpers
|
||||
../helpers/static_for,
|
||||
./bench_fields_template,
|
||||
# Standard library
|
||||
std/strutils
|
||||
|
||||
# ############################################################
|
||||
#
|
||||
# Benchmark of 𝔽p
|
||||
#
|
||||
# ############################################################
|
||||
|
||||
|
||||
const Iters = 1_000_000
|
||||
const InvIters = 1000
|
||||
const AvailableCurves = [
|
||||
P224,
|
||||
BN254,
|
||||
P256,
|
||||
Secp256k1,
|
||||
BLS12_381
|
||||
]
|
||||
|
||||
proc main() =
|
||||
echo "-".repeat(80)
|
||||
staticFor i, 0, AvailableCurves.len:
|
||||
const curve = AvailableCurves[i]
|
||||
addBench(Fp[curve], Iters)
|
||||
subBench(Fp[curve], Iters)
|
||||
negBench(Fp[curve], Iters)
|
||||
mulBench(Fp[curve], Iters)
|
||||
sqrBench(Fp[curve], Iters)
|
||||
invBench(Fp[curve], InvIters)
|
||||
echo "-".repeat(80)
|
||||
|
||||
main()
|
||||
|
||||
echo "Notes:"
|
||||
echo " GCC is significantly slower than Clang on multiprecision arithmetic."
|
||||
echo " The simplest operations might be optimized away by the compiler."
|
48
benchmarks/bench_fp2.nim
Normal file
48
benchmarks/bench_fp2.nim
Normal file
@ -0,0 +1,48 @@
|
||||
# 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.
|
||||
|
||||
import
|
||||
# Internals
|
||||
../constantine/config/curves,
|
||||
../constantine/tower_field_extensions/[abelian_groups, fp2_complex],
|
||||
# Helpers
|
||||
../helpers/static_for,
|
||||
./bench_fields_template,
|
||||
# Standard library
|
||||
std/strutils
|
||||
|
||||
# ############################################################
|
||||
#
|
||||
# Benchmark of 𝔽p2 = 𝔽p[𝑖]
|
||||
#
|
||||
# ############################################################
|
||||
|
||||
|
||||
const Iters = 1_000_000
|
||||
const InvIters = 1000
|
||||
const AvailableCurves = [
|
||||
BN254,
|
||||
BLS12_381
|
||||
]
|
||||
|
||||
proc main() =
|
||||
echo "-".repeat(80)
|
||||
staticFor i, 0, AvailableCurves.len:
|
||||
const curve = AvailableCurves[i]
|
||||
addBench(Fp2[curve], Iters)
|
||||
subBench(Fp2[curve], Iters)
|
||||
negBench(Fp2[curve], Iters)
|
||||
mulBench(Fp2[curve], Iters)
|
||||
sqrBench(Fp2[curve], Iters)
|
||||
invBench(Fp2[curve], InvIters)
|
||||
echo "-".repeat(80)
|
||||
|
||||
main()
|
||||
|
||||
echo "Notes:"
|
||||
echo " GCC is significantly slower than Clang on multiprecision arithmetic."
|
48
benchmarks/bench_fp6.nim
Normal file
48
benchmarks/bench_fp6.nim
Normal file
@ -0,0 +1,48 @@
|
||||
# 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.
|
||||
|
||||
import
|
||||
# Internals
|
||||
../constantine/config/curves,
|
||||
../constantine/tower_field_extensions/[abelian_groups, fp6_1_plus_i],
|
||||
# Helpers
|
||||
../helpers/static_for,
|
||||
./bench_fields_template,
|
||||
# Standard library
|
||||
std/strutils
|
||||
|
||||
# ############################################################
|
||||
#
|
||||
# Benchmark of 𝔽p2 = 𝔽p[𝑖]
|
||||
#
|
||||
# ############################################################
|
||||
|
||||
|
||||
const Iters = 1_000_000
|
||||
const InvIters = 1000
|
||||
const AvailableCurves = [
|
||||
BN254,
|
||||
BLS12_381
|
||||
]
|
||||
|
||||
proc main() =
|
||||
echo "-".repeat(80)
|
||||
staticFor i, 0, AvailableCurves.len:
|
||||
const curve = AvailableCurves[i]
|
||||
# addBench(Fp6[curve], Iters)
|
||||
# subBench(Fp6[curve], Iters)
|
||||
# negBench(Fp6[curve], Iters)
|
||||
# mulBench(Fp6[curve], Iters)
|
||||
sqrBench(Fp6[curve], Iters)
|
||||
# invBench(Fp6[curve], InvIters)
|
||||
echo "-".repeat(80)
|
||||
|
||||
main()
|
||||
|
||||
echo "Notes:"
|
||||
echo " GCC is significantly slower than Clang on multiprecision arithmetic."
|
@ -111,17 +111,40 @@ task test_no_gmp, "Run tests that don't require GMP":
|
||||
# 𝔽p2
|
||||
test "", "tests/test_fp2.nim"
|
||||
|
||||
task bench, "Run benchmark with your default compiler":
|
||||
proc runBench(benchName: string, compiler = "") =
|
||||
if not dirExists "build":
|
||||
mkDir "build"
|
||||
exec "nim c -d:danger --verbosity:0 -o:build/bench_finite_fields_default -r --hints:off --warnings:off benchmarks/bench_finite_fields"
|
||||
|
||||
task bench_gcc, "Run benchmark with gcc":
|
||||
if not dirExists "build":
|
||||
mkDir "build"
|
||||
exec "nim c --cc:gcc -d:danger --verbosity:0 -o:build/bench_finite_fields_gcc -r --hints:off --warnings:off benchmarks/bench_finite_fields"
|
||||
var cc = ""
|
||||
if compiler != "":
|
||||
cc = "--cc:" & compiler
|
||||
exec "nim c " & cc &
|
||||
" -d:danger --verbosity:0 -o:build/" & benchName & "_" & compiler &
|
||||
" -r --hints:off --warnings:off benchmarks/" & benchName & ".nim"
|
||||
|
||||
task bench_clang, "Run benchmark with clang":
|
||||
if not dirExists "build":
|
||||
mkDir "build"
|
||||
exec "nim c --cc:clang -d:danger --verbosity:0 -o:build/bench_finite_fields_clang -r --hints:off --warnings:off benchmarks/bench_finite_fields"
|
||||
task bench_fp, "Run benchmark 𝔽p with your default compiler":
|
||||
runBench("bench_fp")
|
||||
|
||||
task bench_fp_gcc, "Run benchmark 𝔽p with gcc":
|
||||
runBench("bench_fp", "gcc")
|
||||
|
||||
task bench_fp_clang, "Run benchmark 𝔽p with clang":
|
||||
runBench("bench_fp", "clang")
|
||||
|
||||
task bench_fp2, "Run benchmark with 𝔽p2 your default compiler":
|
||||
runBench("bench_fp2")
|
||||
|
||||
task bench_fp2_gcc, "Run benchmark 𝔽p2 with gcc":
|
||||
runBench("bench_fp2", "gcc")
|
||||
|
||||
task bench_fp2_clang, "Run benchmark 𝔽p2 with clang":
|
||||
runBench("bench_fp2", "clang")
|
||||
|
||||
task bench_fp6, "Run benchmark with 𝔽p6 your default compiler":
|
||||
runBench("bench_fp6")
|
||||
|
||||
task bench_fp6_gcc, "Run benchmark 𝔽p6 with gcc":
|
||||
runBench("bench_fp6", "gcc")
|
||||
|
||||
task bench_fp6_clang, "Run benchmark 𝔽p6 with clang":
|
||||
runBench("bench_fp6", "clang")
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
import
|
||||
../config/[common, curves],
|
||||
./bigints,
|
||||
./finite_fields
|
||||
|
||||
# ############################################################
|
||||
|
Loading…
x
Reference in New Issue
Block a user