2020-03-20 22:03:52 +00: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 finite fields
|
|
|
|
|
#
|
|
|
|
|
# ############################################################
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
# Internals
|
2022-02-27 00:49:08 +00:00
|
|
|
|
../constantine/platforms/abstractions,
|
|
|
|
|
../constantine/math/config/curves,
|
|
|
|
|
../constantine/math/arithmetic,
|
|
|
|
|
../constantine/math/extension_fields,
|
2022-08-14 07:48:10 +00:00
|
|
|
|
../constantine/math/constants/zoo_square_roots,
|
2020-03-20 22:03:52 +00:00
|
|
|
|
# Helpers
|
2022-01-01 15:19:35 +00:00
|
|
|
|
../helpers/prng_unsafe,
|
2020-12-15 18:18:36 +00:00
|
|
|
|
./bench_blueprint
|
2020-03-20 22:03:52 +00:00
|
|
|
|
|
2023-02-07 12:11:44 +00:00
|
|
|
|
export notes, abstractions
|
2021-01-23 19:55:40 +00:00
|
|
|
|
proc separator*() = separator(165)
|
2022-01-01 15:19:35 +00:00
|
|
|
|
proc smallSeparator*() = separator(8)
|
2020-04-15 17:38:02 +00:00
|
|
|
|
|
2020-03-20 22:03:52 +00:00
|
|
|
|
proc report(op, field: string, start, stop: MonoTime, startClk, stopClk: int64, iters: int) =
|
2020-04-15 17:38:02 +00:00
|
|
|
|
let ns = inNanoseconds((stop-start) div iters)
|
|
|
|
|
let throughput = 1e9 / float64(ns)
|
2020-06-04 20:09:30 +00:00
|
|
|
|
when SupportsGetTicks:
|
2021-01-23 19:55:40 +00:00
|
|
|
|
echo &"{op:<70} {field:<18} {throughput:>15.3f} ops/s {ns:>9} ns/op {(stopClk - startClk) div iters:>9} CPU cycles (approx)"
|
2020-06-04 20:09:30 +00:00
|
|
|
|
else:
|
2021-01-23 19:55:40 +00:00
|
|
|
|
echo &"{op:<70} {field:<18} {throughput:>15.3f} ops/s {ns:>9} ns/op"
|
2020-03-20 22:03:52 +00:00
|
|
|
|
|
|
|
|
|
macro fixFieldDisplay(T: typedesc): untyped =
|
|
|
|
|
# At compile-time, enums are integers and their display is buggy
|
|
|
|
|
# we get the Curve ID instead of the curve name.
|
|
|
|
|
let instantiated = T.getTypeInst()
|
2022-02-26 20:22:34 +00:00
|
|
|
|
var name = $instantiated[1][0] # 𝔽p
|
2020-03-20 22:03:52 +00:00
|
|
|
|
name.add "[" & $Curve(instantiated[1][1].intVal) & "]"
|
|
|
|
|
result = newLit name
|
|
|
|
|
|
|
|
|
|
template bench(op: string, T: typedesc, iters: int, body: untyped): untyped =
|
2020-12-15 18:18:36 +00:00
|
|
|
|
measure(iters, startTime, stopTime, startClk, stopClk, body)
|
|
|
|
|
report(op, fixFieldDisplay(T), startTime, stopTime, startClk, stopClk, iters)
|
2020-03-20 22:03:52 +00:00
|
|
|
|
|
2022-08-14 07:48:10 +00:00
|
|
|
|
func random_unsafe(rng: var RngState, a: var FpDbl) =
|
|
|
|
|
## Initialize a standalone Double-Width field element
|
|
|
|
|
## we don't reduce it modulo p², this is only used for benchmark
|
|
|
|
|
let aHi = rng.random_unsafe(Fp[FpDbl.C])
|
|
|
|
|
let aLo = rng.random_unsafe(Fp[FpDbl.C])
|
|
|
|
|
for i in 0 ..< aLo.mres.limbs.len:
|
|
|
|
|
a.limbs2x[i] = aLo.mres.limbs[i]
|
|
|
|
|
for i in 0 ..< aHi.mres.limbs.len:
|
|
|
|
|
a.limbs2x[aLo.mres.limbs.len+i] = aHi.mres.limbs[i]
|
|
|
|
|
|
|
|
|
|
func random_unsafe(rng: var RngState, a: var ExtensionField2x) =
|
|
|
|
|
for i in 0 ..< a.coords.len:
|
|
|
|
|
rng.random_unsafe(a.coords[i])
|
|
|
|
|
|
2020-03-21 01:31:31 +00:00
|
|
|
|
proc addBench*(T: typedesc, iters: int) =
|
2020-04-15 00:12:45 +00:00
|
|
|
|
var x = rng.random_unsafe(T)
|
|
|
|
|
let y = rng.random_unsafe(T)
|
2020-03-21 01:31:31 +00:00
|
|
|
|
bench("Addition", T, iters):
|
2020-03-20 22:03:52 +00:00
|
|
|
|
x += y
|
|
|
|
|
|
2020-03-21 01:31:31 +00:00
|
|
|
|
proc subBench*(T: typedesc, iters: int) =
|
2020-04-15 00:12:45 +00:00
|
|
|
|
var x = rng.random_unsafe(T)
|
|
|
|
|
let y = rng.random_unsafe(T)
|
2020-03-20 22:03:52 +00:00
|
|
|
|
preventOptimAway(x)
|
2020-03-21 01:31:31 +00:00
|
|
|
|
bench("Substraction", T, iters):
|
2020-03-20 22:03:52 +00:00
|
|
|
|
x -= y
|
|
|
|
|
|
2020-03-21 01:31:31 +00:00
|
|
|
|
proc negBench*(T: typedesc, iters: int) =
|
2020-03-20 22:03:52 +00:00
|
|
|
|
var r: T
|
2020-04-15 00:12:45 +00:00
|
|
|
|
let x = rng.random_unsafe(T)
|
2020-03-21 01:31:31 +00:00
|
|
|
|
bench("Negation", T, iters):
|
2020-03-20 22:03:52 +00:00
|
|
|
|
r.neg(x)
|
|
|
|
|
|
2020-10-01 22:01:09 +00:00
|
|
|
|
proc ccopyBench*(T: typedesc, iters: int) =
|
|
|
|
|
var r: T
|
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
bench("Conditional Copy", T, iters):
|
|
|
|
|
r.ccopy(x, CtFalse)
|
|
|
|
|
|
|
|
|
|
proc div2Bench*(T: typedesc, iters: int) =
|
|
|
|
|
var x = rng.random_unsafe(T)
|
|
|
|
|
bench("Division by 2", T, iters):
|
|
|
|
|
x.div2()
|
|
|
|
|
|
2020-03-21 01:31:31 +00:00
|
|
|
|
proc mulBench*(T: typedesc, iters: int) =
|
2020-03-20 22:03:52 +00:00
|
|
|
|
var r: T
|
2020-04-15 00:12:45 +00:00
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
let y = rng.random_unsafe(T)
|
2020-03-20 22:03:52 +00:00
|
|
|
|
preventOptimAway(r)
|
2020-03-21 01:31:31 +00:00
|
|
|
|
bench("Multiplication", T, iters):
|
2020-03-20 22:03:52 +00:00
|
|
|
|
r.prod(x, y)
|
|
|
|
|
|
2020-03-21 01:31:31 +00:00
|
|
|
|
proc sqrBench*(T: typedesc, iters: int) =
|
2020-03-20 22:03:52 +00:00
|
|
|
|
var r: T
|
2020-04-15 00:12:45 +00:00
|
|
|
|
let x = rng.random_unsafe(T)
|
2020-03-20 22:03:52 +00:00
|
|
|
|
preventOptimAway(r)
|
2020-03-21 01:31:31 +00:00
|
|
|
|
bench("Squaring", T, iters):
|
2020-03-20 22:03:52 +00:00
|
|
|
|
r.square(x)
|
|
|
|
|
|
2022-08-14 07:48:10 +00:00
|
|
|
|
proc mul2xUnrBench*(T: typedesc, iters: int) =
|
2022-02-20 19:15:20 +00:00
|
|
|
|
var r: doublePrec(T)
|
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
let y = rng.random_unsafe(T)
|
|
|
|
|
preventOptimAway(r)
|
2022-08-14 07:48:10 +00:00
|
|
|
|
bench("Multiplication 2x unreduced", T, iters):
|
2022-02-20 19:15:20 +00:00
|
|
|
|
r.prod2x(x, y)
|
|
|
|
|
|
2022-08-14 07:48:10 +00:00
|
|
|
|
proc sqr2xUnrBench*(T: typedesc, iters: int) =
|
2022-02-20 19:15:20 +00:00
|
|
|
|
var r: doublePrec(T)
|
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
preventOptimAway(r)
|
2022-08-14 07:48:10 +00:00
|
|
|
|
bench("Squaring 2x unreduced", T, iters):
|
2022-02-20 19:15:20 +00:00
|
|
|
|
r.square2x(x)
|
|
|
|
|
|
2022-08-14 07:48:10 +00:00
|
|
|
|
proc rdc2xBench*(T: typedesc, iters: int) =
|
|
|
|
|
var r: T
|
|
|
|
|
var t: doublePrec(T)
|
|
|
|
|
rng.random_unsafe(t)
|
|
|
|
|
preventOptimAway(r)
|
|
|
|
|
bench("Redc 2x", T, iters):
|
|
|
|
|
r.redc2x(t)
|
|
|
|
|
|
|
|
|
|
proc sumprodBench*(T: typedesc, iters: int) =
|
|
|
|
|
var r: T
|
|
|
|
|
let a = rng.random_unsafe(T)
|
|
|
|
|
let b = rng.random_unsafe(T)
|
|
|
|
|
let u = rng.random_unsafe(T)
|
|
|
|
|
let v = rng.random_unsafe(T)
|
|
|
|
|
preventOptimAway(r)
|
|
|
|
|
bench("Linear combination", T, iters):
|
|
|
|
|
r.sumprod([a, b], [u, v])
|
|
|
|
|
|
2022-02-13 23:16:55 +00:00
|
|
|
|
proc toBigBench*(T: typedesc, iters: int) =
|
|
|
|
|
var r: matchingBigInt(T.C)
|
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
preventOptimAway(r)
|
|
|
|
|
bench("BigInt <- field conversion", T, iters):
|
|
|
|
|
r.fromField(x)
|
|
|
|
|
|
|
|
|
|
proc toFieldBench*(T: typedesc, iters: int) =
|
|
|
|
|
var r: T
|
|
|
|
|
let x = rng.random_unsafe(matchingBigInt(T.C))
|
|
|
|
|
preventOptimAway(r)
|
|
|
|
|
bench("BigInt -> field conversion", T, iters):
|
|
|
|
|
r.fromBig(x)
|
|
|
|
|
|
2020-03-21 01:31:31 +00:00
|
|
|
|
proc invBench*(T: typedesc, iters: int) =
|
2020-03-20 22:03:52 +00:00
|
|
|
|
var r: T
|
2020-04-15 00:12:45 +00:00
|
|
|
|
let x = rng.random_unsafe(T)
|
2020-03-20 22:03:52 +00:00
|
|
|
|
preventOptimAway(r)
|
2022-02-10 13:05:07 +00:00
|
|
|
|
bench("Inversion (constant-time)", T, iters):
|
2020-03-20 22:03:52 +00:00
|
|
|
|
r.inv(x)
|
2020-06-17 20:44:52 +00:00
|
|
|
|
|
2023-02-16 11:45:05 +00:00
|
|
|
|
proc invVartimeBench*(T: typedesc, iters: int) =
|
|
|
|
|
var r: T
|
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
preventOptimAway(r)
|
|
|
|
|
bench("Inversion (variable-time)", T, iters):
|
|
|
|
|
r.inv_vartime(x)
|
|
|
|
|
|
2022-08-07 17:50:28 +00:00
|
|
|
|
proc isSquareBench*(T: typedesc, iters: int) =
|
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
bench("isSquare (constant-time)", T, iters):
|
|
|
|
|
let qrt = x.isSquare()
|
|
|
|
|
|
2020-06-17 20:44:52 +00:00
|
|
|
|
proc sqrtBench*(T: typedesc, iters: int) =
|
|
|
|
|
let x = rng.random_unsafe(T)
|
2022-01-01 15:19:35 +00:00
|
|
|
|
|
|
|
|
|
const algoType = block:
|
2022-04-12 21:40:07 +00:00
|
|
|
|
when T.C.has_P_3mod4_primeModulus():
|
2022-01-01 15:19:35 +00:00
|
|
|
|
"p ≡ 3 (mod 4)"
|
2022-04-12 21:40:07 +00:00
|
|
|
|
elif T.C.has_P_5mod8_primeModulus():
|
2022-01-01 15:19:35 +00:00
|
|
|
|
"p ≡ 5 (mod 8)"
|
|
|
|
|
else:
|
|
|
|
|
"Tonelli-Shanks"
|
|
|
|
|
const addchain = block:
|
|
|
|
|
when T.C.hasSqrtAddchain() or T.C.hasTonelliShanksAddchain():
|
|
|
|
|
"with addition chain"
|
|
|
|
|
else:
|
|
|
|
|
"without addition chain"
|
|
|
|
|
const desc = "Square Root (constant-time " & algoType & " " & addchain & ")"
|
|
|
|
|
bench(desc, T, iters):
|
2020-06-17 20:44:52 +00:00
|
|
|
|
var r = x
|
|
|
|
|
discard r.sqrt_if_square()
|
|
|
|
|
|
2022-01-01 15:19:35 +00:00
|
|
|
|
proc sqrtRatioBench*(T: typedesc, iters: int) =
|
2021-08-14 19:01:50 +00:00
|
|
|
|
var r: T
|
2022-01-01 15:19:35 +00:00
|
|
|
|
let u = rng.random_unsafe(T)
|
|
|
|
|
let v = rng.random_unsafe(T)
|
|
|
|
|
bench("Fused SquareRoot+Division+isSquare sqrt(u/v)", T, iters):
|
|
|
|
|
let isSquare = r.sqrt_ratio_if_square(u, v)
|
2021-01-23 19:55:40 +00:00
|
|
|
|
|
2020-06-17 20:44:52 +00:00
|
|
|
|
proc powBench*(T: typedesc, iters: int) =
|
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
let exponent = rng.random_unsafe(BigInt[T.C.getCurveOrderBitwidth()])
|
|
|
|
|
bench("Exp curve order (constant-time) - " & $exponent.bits & "-bit", T, iters):
|
|
|
|
|
var r = x
|
|
|
|
|
r.pow(exponent)
|
|
|
|
|
|
|
|
|
|
proc powUnsafeBench*(T: typedesc, iters: int) =
|
|
|
|
|
let x = rng.random_unsafe(T)
|
|
|
|
|
let exponent = rng.random_unsafe(BigInt[T.C.getCurveOrderBitwidth()])
|
|
|
|
|
bench("Exp curve order (Leak exponent bits) - " & $exponent.bits & "-bit", T, iters):
|
|
|
|
|
var r = x
|
2023-06-01 21:38:41 +00:00
|
|
|
|
r.pow_vartime(exponent)
|