2020-04-15 17:38:02 +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 elliptic curves
|
|
|
|
#
|
|
|
|
# ############################################################
|
|
|
|
|
|
|
|
import
|
|
|
|
# Internals
|
2022-02-27 00:49:08 +00:00
|
|
|
../constantine/platforms/abstractions,
|
|
|
|
../constantine/math/config/curves,
|
|
|
|
../constantine/math/arithmetic,
|
|
|
|
../constantine/math/io/io_bigints,
|
|
|
|
../constantine/math/elliptic/[
|
2021-01-30 13:21:55 +00:00
|
|
|
ec_shortweierstrass_affine,
|
|
|
|
ec_shortweierstrass_projective,
|
|
|
|
ec_shortweierstrass_jacobian,
|
2022-10-29 20:43:40 +00:00
|
|
|
ec_shortweierstrass_batch_ops,
|
2021-01-30 13:21:55 +00:00
|
|
|
ec_scalar_mul, ec_endomorphism_accel],
|
2020-04-15 17:38:02 +00:00
|
|
|
# Helpers
|
2020-04-15 19:24:18 +00:00
|
|
|
../helpers/[prng_unsafe, static_for],
|
|
|
|
./platforms,
|
2020-12-15 18:18:36 +00:00
|
|
|
./bench_blueprint,
|
2020-06-14 13:39:06 +00:00
|
|
|
# Reference unsafe scalar multiplication
|
2022-02-27 00:49:08 +00:00
|
|
|
../tests/math/support/ec_reference_scalar_mult
|
2020-04-15 17:38:02 +00:00
|
|
|
|
2020-12-15 18:18:36 +00:00
|
|
|
export notes
|
2022-08-06 17:05:54 +00:00
|
|
|
export abstractions # generic sandwich on SecretBool and SecretBool in Jacobian sum
|
|
|
|
|
2020-12-15 18:18:36 +00:00
|
|
|
proc separator*() = separator(177)
|
2020-07-24 20:02:30 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
macro fixEllipticDisplay(EC: typedesc): untyped =
|
2020-04-15 17:38:02 +00:00
|
|
|
# At compile-time, enums are integers and their display is buggy
|
|
|
|
# we get the Curve ID instead of the curve name.
|
2022-10-29 20:43:40 +00:00
|
|
|
let instantiated = EC.getTypeInst()
|
2020-04-15 17:38:02 +00:00
|
|
|
var name = $instantiated[1][0] # EllipticEquationFormCoordinates
|
|
|
|
let fieldName = $instantiated[1][1][0]
|
|
|
|
let curveName = $Curve(instantiated[1][1][1].intVal)
|
|
|
|
name.add "[" & fieldName & "[" & curveName & "]]"
|
|
|
|
result = newLit name
|
|
|
|
|
2020-12-15 18:18:36 +00:00
|
|
|
proc report(op, elliptic: string, start, stop: MonoTime, startClk, stopClk: int64, iters: int) =
|
|
|
|
let ns = inNanoseconds((stop-start) div iters)
|
|
|
|
let throughput = 1e9 / float64(ns)
|
2020-06-04 20:09:30 +00:00
|
|
|
when SupportsGetTicks:
|
2020-12-15 18:18:36 +00:00
|
|
|
echo &"{op:<60} {elliptic:<40} {throughput:>15.3f} ops/s {ns:>9} ns/op {(stopClk - startClk) div iters:>9} CPU cycles (approx)"
|
|
|
|
else:
|
|
|
|
echo &"{op:<60} {elliptic:<40} {throughput:>15.3f} ops/s {ns:>9} ns/op"
|
2020-06-04 20:09:30 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
template bench(op: string, EC: typedesc, iters: int, body: untyped): untyped =
|
2020-12-15 18:18:36 +00:00
|
|
|
measure(iters, startTime, stopTime, startClk, stopClk, body)
|
2022-10-29 20:43:40 +00:00
|
|
|
report(op, fixEllipticDisplay(EC), startTime, stopTime, startClk, stopClk, iters)
|
|
|
|
|
|
|
|
proc addBench*(EC: typedesc, iters: int) =
|
|
|
|
var r {.noInit.}: EC
|
|
|
|
let P = rng.random_unsafe(EC)
|
|
|
|
let Q = rng.random_unsafe(EC)
|
|
|
|
bench("EC Add " & $EC.G, EC, iters):
|
2020-04-15 20:23:46 +00:00
|
|
|
r.sum(P, Q)
|
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
proc mixedAddBench*(EC: typedesc, iters: int) =
|
|
|
|
var r {.noInit.}: EC
|
|
|
|
let P = rng.random_unsafe(EC)
|
|
|
|
let Q = rng.random_unsafe(EC)
|
|
|
|
var Qaff: ECP_ShortW_Aff[EC.F, EC.G]
|
2022-02-10 13:05:07 +00:00
|
|
|
Qaff.affine(Q)
|
2022-10-29 20:43:40 +00:00
|
|
|
bench("EC Mixed Addition " & $EC.G, EC, iters):
|
2020-09-26 07:16:29 +00:00
|
|
|
r.madd(P, Qaff)
|
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
proc doublingBench*(EC: typedesc, iters: int) =
|
|
|
|
var r {.noInit.}: EC
|
|
|
|
let P = rng.random_unsafe(EC)
|
|
|
|
bench("EC Double " & $EC.G, EC, iters):
|
2020-04-15 20:23:46 +00:00
|
|
|
r.double(P)
|
2020-06-04 18:37:29 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
proc affFromProjBench*(EC: typedesc, iters: int) =
|
|
|
|
var r {.noInit.}: ECP_ShortW_Aff[EC.F, EC.G]
|
|
|
|
let P = rng.random_unsafe(EC)
|
|
|
|
bench("EC Projective to Affine " & $EC.G, EC, iters):
|
2022-02-10 13:05:07 +00:00
|
|
|
r.affine(P)
|
2020-10-01 22:01:09 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
proc affFromJacBench*(EC: typedesc, iters: int) =
|
|
|
|
var r {.noInit.}: ECP_ShortW_Aff[EC.F, EC.G]
|
|
|
|
let P = rng.random_unsafe(EC)
|
|
|
|
bench("EC Jacobian to Affine " & $EC.G, EC, iters):
|
2022-02-10 13:05:07 +00:00
|
|
|
r.affine(P)
|
2020-10-01 22:01:09 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
proc scalarMulGenericBench*(EC: typedesc, window: static int, iters: int) =
|
|
|
|
const bits = EC.F.C.getCurveOrderBitwidth()
|
2020-06-04 18:37:29 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
var r {.noInit.}: EC
|
|
|
|
let P = rng.random_unsafe(EC) # TODO: clear cofactor
|
2020-06-04 18:37:29 +00:00
|
|
|
|
|
|
|
let exponent = rng.random_unsafe(BigInt[bits])
|
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
bench("EC ScalarMul " & $bits & "-bit " & $EC.G & " (window-" & $window & ", generic)", EC, iters):
|
2020-06-04 18:37:29 +00:00
|
|
|
r = P
|
2020-09-03 21:10:48 +00:00
|
|
|
r.scalarMulGeneric(exponent, window)
|
2020-06-04 18:37:29 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
proc scalarMulEndo*(EC: typedesc, iters: int) =
|
|
|
|
const bits = EC.F.C.getCurveOrderBitwidth()
|
2020-06-14 13:39:06 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
var r {.noInit.}: EC
|
|
|
|
let P = rng.random_unsafe(EC) # TODO: clear cofactor
|
2020-06-14 13:39:06 +00:00
|
|
|
|
|
|
|
let exponent = rng.random_unsafe(BigInt[bits])
|
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
bench("EC ScalarMul " & $bits & "-bit " & $EC.G & " (endomorphism accelerated)", EC, iters):
|
2020-06-14 13:39:06 +00:00
|
|
|
r = P
|
2020-09-03 21:10:48 +00:00
|
|
|
r.scalarMulEndo(exponent)
|
2020-06-14 13:39:06 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
proc scalarMulEndoWindow*(EC: typedesc, iters: int) =
|
|
|
|
const bits = EC.F.C.getCurveOrderBitwidth()
|
2020-08-24 22:02:30 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
var r {.noInit.}: EC
|
|
|
|
let P = rng.random_unsafe(EC) # TODO: clear cofactor
|
2020-08-24 22:02:30 +00:00
|
|
|
|
|
|
|
let exponent = rng.random_unsafe(BigInt[bits])
|
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
bench("EC ScalarMul " & $bits & "-bit " & $EC.G & " (window-2, endomorphism accelerated)", EC, iters):
|
2020-08-24 22:02:30 +00:00
|
|
|
r = P
|
2022-10-29 20:43:40 +00:00
|
|
|
when EC.F is Fp:
|
2020-08-24 22:02:30 +00:00
|
|
|
r.scalarMulGLV_m2w2(exponent)
|
|
|
|
else:
|
|
|
|
{.error: "Not implemented".}
|
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
proc scalarMulUnsafeDoubleAddBench*(EC: typedesc, iters: int) =
|
|
|
|
const bits = EC.F.C.getCurveOrderBitwidth()
|
2020-06-14 13:39:06 +00:00
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
var r {.noInit.}: EC
|
|
|
|
let P = rng.random_unsafe(EC) # TODO: clear cofactor
|
2020-06-14 13:39:06 +00:00
|
|
|
|
|
|
|
let exponent = rng.random_unsafe(BigInt[bits])
|
|
|
|
|
2022-10-29 20:43:40 +00:00
|
|
|
bench("EC ScalarMul " & $bits & "-bit " & $EC.G & " (unsafe reference DoubleAdd)", EC, iters):
|
2020-06-14 13:39:06 +00:00
|
|
|
r = P
|
2020-09-03 21:10:48 +00:00
|
|
|
r.unsafe_ECmul_double_add(exponent)
|
2022-10-29 20:43:40 +00:00
|
|
|
|
|
|
|
proc multiAddBench*(EC: typedesc, numPoints: int, useBatching: bool, iters: int) =
|
|
|
|
var points = newSeq[ECP_ShortW_Aff[EC.F, EC.G]](numPoints)
|
|
|
|
|
|
|
|
for i in 0 ..< numPoints:
|
|
|
|
points[i] = rng.random_unsafe(ECP_ShortW_Aff[EC.F, EC.G])
|
|
|
|
|
|
|
|
var r{.noInit.}: EC
|
|
|
|
|
|
|
|
if useBatching:
|
|
|
|
bench("EC Multi-Addition batched " & $EC.G & " (" & $numPoints & " points)", EC, iters):
|
|
|
|
r.sum_batch_vartime(points)
|
|
|
|
else:
|
|
|
|
bench("EC Multi-Addition unbatched mixed add " & $EC.G & " (" & $numPoints & " points)", EC, iters):
|
|
|
|
r.setInf()
|
|
|
|
for i in 0 ..< numPoints:
|
|
|
|
r += points[i]
|