Skip to content

Commit a6c3d26

Browse files
committed
GT-multiexp: Add detailed metering
1 parent 71f790a commit a6c3d26

File tree

8 files changed

+1003
-4
lines changed

8 files changed

+1003
-4
lines changed

constantine/math_compiler/assembly/llvm_assembler_arm64.nim

Lines changed: 716 additions & 0 deletions
Large diffs are not rendered by default.

metering/eip2537.md renamed to metering/m_eip2537.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> CC=clang nimble bench_summary_bls12_381
44
5-
![eip2537_bench_ryzen_7840U.png](eip2537_bench_ryzen_7840U.png)
5+
![eip2537_bench_ryzen_7840U.png](m_eip2537_bench_ryzen_7840U.png)
66

77
| | | | | |
88
| ------------------------------------ | ---------------------------------------- | ------------------------------- | ---------------------- | ------------------------------ |

metering/m_eip2537.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import
2929
var rng*: RngState
3030
let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
3131
rng.seed(seed)
32-
echo "bench xoshiro512** seed: ", seed
32+
echo "metering xoshiro512** seed: ", seed
3333

3434
func random_point*(rng: var RngState, EC: typedesc[EC_ShortW_Aff]): EC {.noInit.} =
3535
var jac = rng.random_unsafe(EC_ShortW_Jac[EC.F, EC.G])

metering/m_gt_multi_exp.md

Lines changed: 218 additions & 0 deletions
Large diffs are not rendered by default.

metering/m_gt_multiexp.nim

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Constantine
2+
# Copyright (c) 2018-2019 Status Research & Development GmbH
3+
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy
4+
# Licensed and distributed under either of
5+
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
6+
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
7+
# at your option. This file may not be copied, modified, or distributed except according to those terms.
8+
9+
import
10+
std/times,
11+
constantine/platforms/abstractions,
12+
constantine/platforms/metering/[reports, tracer],
13+
constantine/named/algebras,
14+
constantine/named/zoo_subgroups,
15+
constantine/math/[arithmetic, extension_fields],
16+
constantine/math/pairings/[gt_multiexp, pairings_generic],
17+
# Helpers
18+
helpers/prng_unsafe
19+
20+
var rng*: RngState
21+
let seed = 777
22+
rng.seed(seed)
23+
echo "metering xoshiro512** seed: ", seed
24+
25+
func random_gt(rng: var RngState, F: typedesc): F {.noInit.} =
26+
result = rng.random_unsafe(F)
27+
result.finalExp()
28+
debug: doAssert bool result.isInPairingSubgroup()
29+
30+
proc genBatch(rng: var RngState, GT: typedesc, numPoints: int): (seq[GT], seq[Fr[GT.Name].getBigInt()]) =
31+
var elems = newSeq[GT](numPoints)
32+
var exponents = newSeq[Fr[GT.Name]](numPoints)
33+
34+
for i in 0 ..< numPoints:
35+
elems[i] = rng.random_gt(GT)
36+
exponents[i] = rng.random_unsafe(Fr[GT.Name])
37+
38+
var exponents_big = newSeq[Fr[GT.Name].getBigInt()](numPoints)
39+
exponents_big.asUnchecked().batchFromField(exponents.asUnchecked(), numPoints)
40+
41+
return (elems, exponents_big)
42+
43+
proc mexpMeter[bits: static int](elems: openArray[AnyFp12], exponents: openArray[BigInt[bits]], useTorus: static bool) =
44+
var r{.noInit.}: AnyFp12
45+
r.setZero()
46+
resetMetering()
47+
r.multiExp_vartime(elems, exponents, useTorus)
48+
49+
type GT_12o6 = QuadraticExt[Fp6[BLS12_381]]
50+
type GT_12o4 = CubicExt[Fp4[BLS12_381]]
51+
52+
type GT = GT_12o6
53+
const N = 256
54+
const useTorus = true
55+
56+
echo "Config: GT = ", $GT, ", N = ", N, ", use-torus = ", useTorus
57+
58+
resetMetering()
59+
let (elems, exponents) = rng.genBatch(GT, N)
60+
resetMetering()
61+
62+
mexpMeter(elems, exponents, useTorus)
63+
const flags = if UseASM_X86_64 or UseASM_X86_32: "UseAssembly" else: "NoAssembly"
64+
65+
reportCli(Metrics, flags)

metering/m_msm.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import
2020
var rng*: RngState
2121
let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
2222
rng.seed(seed)
23-
echo "bench xoshiro512** seed: ", seed
23+
echo "metering xoshiro512** seed: ", seed
2424

2525
proc msmMeter*(EC: typedesc, numPoints: int) =
2626
const bits = EC.getScalarField().bits()

metering/m_pairings.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import
2020
var rng*: RngState
2121
let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
2222
rng.seed(seed)
23-
echo "bench xoshiro512** seed: ", seed
23+
echo "metering xoshiro512** seed: ", seed
2424

2525
func random_point*(rng: var RngState, EC: typedesc[EC_ShortW_Aff]): EC {.noInit.} =
2626
var jac = rng.random_unsafe(EC_ShortW_Jac[EC.F, EC.G])

0 commit comments

Comments
 (0)