From 472823b7499c414eff287dd92e3265dc17a11439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Fri, 6 Mar 2020 17:44:30 +0100 Subject: [PATCH] more comprehensive benchmark of Fp --- benchmarks/bls12_381_fp.nim | 80 +++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/benchmarks/bls12_381_fp.nim b/benchmarks/bls12_381_fp.nim index b96cec2..0811631 100644 --- a/benchmarks/bls12_381_fp.nim +++ b/benchmarks/bls12_381_fp.nim @@ -29,14 +29,18 @@ import ./timers const Iters = 1_000_000 +const InvIters = 1000 randomize(1234) echo "\n⚠️ Measurements are approximate and use the CPU nominal clock: Turbo-Boost and overclocking will skew them." 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() = - var r, x, y: Fp[BLS12_381] + var x, y: Fp[BLS12_381] # BN254 field modulus x.fromHex("0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47") # BLS12-381 prime - 2 @@ -48,13 +52,43 @@ proc addBench() = x += y let stopClk = getTicks() 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() +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() = var r, x, y: Fp[BLS12_381] # BN254 field modulus @@ -68,9 +102,39 @@ proc mulBench() = r.prod(x, y) let stopClk = getTicks() let stop = getMonotime() - - 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" + report("Multiplication", "Fp[BLS12_381]", start, stop, startClk, stopClk, Iters) 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()