more comprehensive benchmark of Fp

This commit is contained in:
Mamy André-Ratsimbazafy 2020-03-06 17:44:30 +01:00
parent adc630f3af
commit 472823b749
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
1 changed files with 72 additions and 8 deletions

View File

@ -29,14 +29,18 @@ import
./timers ./timers
const Iters = 1_000_000 const Iters = 1_000_000
const InvIters = 1000
randomize(1234) randomize(1234)
echo "\n⚠️ Measurements are approximate and use the CPU nominal clock: Turbo-Boost and overclocking will skew them." echo "\n⚠️ Measurements are approximate and use the CPU nominal clock: Turbo-Boost and overclocking will skew them."
echo "==========================================================================================================\n" echo "==========================================================================================================\n"
proc report(op, field: string, start, stop: MonoTime, startClk, stopClk: int64, iters: int) =
echo &"{op:<15} {field:<15} {inNanoseconds((stop-start) div iters):>9} ns {(stopClk - startClk) div iters:>9} cycles"
proc addBench() = proc addBench() =
var r, x, y: Fp[BLS12_381] var x, y: Fp[BLS12_381]
# BN254 field modulus # BN254 field modulus
x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47") x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
# BLS12-381 prime - 2 # BLS12-381 prime - 2
@ -48,13 +52,43 @@ proc addBench() =
x += y x += y
let stopClk = getTicks() let stopClk = getTicks()
let stop = getMonotime() let stop = getMonotime()
report("Addition", "Fp[BLS12_381]", start, stop, startClk, stopClk, Iters)
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"
echo &"Cycles per addition 𝔽p ==> {(stopClk - startClk) div Iters} cycles"
addBench() addBench()
proc subBench() =
var x, y: Fp[BLS12_381]
# BN254 field modulus
x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
# BLS12-381 prime - 2
y.fromHex("0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaa9")
let start = getMonotime()
let startClk = getTicks()
for _ in 0 ..< Iters:
x -= y
let stopClk = getTicks()
let stop = getMonotime()
report("Substraction", "Fp[BLS12_381]", start, stop, startClk, stopClk, Iters)
# subBench()
proc negBench() =
var r, x: Fp[BLS12_381]
# BN254 field modulus
x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
let start = getMonotime()
let startClk = getTicks()
for _ in 0 ..< Iters:
r.neg(x)
let stopClk = getTicks()
let stop = getMonotime()
report("Negation", "Fp[BLS12_381]", start, stop, startClk, stopClk, Iters)
negBench()
proc mulBench() = proc mulBench() =
var r, x, y: Fp[BLS12_381] var r, x, y: Fp[BLS12_381]
# BN254 field modulus # BN254 field modulus
@ -68,9 +102,39 @@ proc mulBench() =
r.prod(x, y) r.prod(x, y)
let stopClk = getTicks() let stopClk = getTicks()
let stop = getMonotime() let stop = getMonotime()
report("Multiplication", "Fp[BLS12_381]", start, stop, startClk, stopClk, Iters)
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"
echo &"Cycles per multiplication 𝔽p ==> {(stopClk - startClk) div Iters} cycles"
mulBench() mulBench()
proc sqrBench() =
var r, x: Fp[BLS12_381]
# BN254 field modulus
x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
let start = getMonotime()
let startClk = getTicks()
for _ in 0 ..< Iters:
r.square(x)
let stopClk = getTicks()
let stop = getMonotime()
report("Squaring", "Fp[BLS12_381]", start, stop, startClk, stopClk, Iters)
sqrBench()
proc invBench() =
# TODO: having x on the stack triggers stack smashing detection. To be investigated
var x: ref Fp[BLS12_381]
new x
# BN254 field modulus
x[].fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47")
let start = getMonotime()
let startClk = getTicks()
for _ in 0 ..< InvIters:
# Note: we don't copy the original x so x is alterning between x and x^-1
inv(x[])
let stopClk = getTicks()
let stop = getMonotime()
report("Inversion", "Fp[BLS12_381]", start, stop, startClk, stopClk, InvIters)
invBench()