Skip to content

Commit

Permalink
Merge branch 'master' into zkalc
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jul 12, 2024
2 parents 7e6ce46 + 405ec70 commit 623e05f
Show file tree
Hide file tree
Showing 10 changed files with 658 additions and 14 deletions.
75 changes: 75 additions & 0 deletions benchmarks/bench_multilinear_extensions.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# 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.

import
constantine/boolean_hypercube/multilinear_extensions,
constantine/named/algebras,
constantine/math/arithmetic,
constantine/platforms/static_for,
# Helpers
helpers/prng_unsafe,
benchmarks/bench_blueprint,
std/macros

var rng*: RngState
let seed = 1234
rng.seed(seed)
echo "bench multilinear_extensions xoshiro512** seed: ", seed

proc separator*() = separator(155)

proc report(op, field: string, start, stop: MonoTime, startClk, stopClk: int64, iters: int) =
let ns = inNanoseconds((stop-start) div iters)
let throughput = 1e9 / float64(ns)
when SupportsGetTicks:
echo &"{op:<60} {field:<18} {throughput:>15.3f} ops/s {ns:>9} ns/op {(stopClk - startClk) div iters:>9} CPU cycles (approx)"
else:
echo &"{op:<60} {field:<18} {throughput:>15.3f} ops/s {ns:>9} ns/op"

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()
var name = $instantiated[1][0] # 𝔽p
name.add "[" & $Algebra(instantiated[1][1].intVal) & "]"
result = newLit name

template bench(op: string, T: typedesc, iters: int, body: untyped): untyped =
measure(iters, startTime, stopTime, startClk, stopClk, body)
report(op, fixFieldDisplay(T), startTime, stopTime, startClk, stopClk, iters)

proc bench_mle(F: typedesc, num_vars: int) =
var evals = newSeq[F](1 shl num_vars)
for eval in evals.mitems():
eval = rng.random_unsafe(F)

let mle = MultilinearExtension[F].new(num_vars, evals)

var coords = newSeq[F](num_vars)
for coord in coords.mitems():
coord = rng.random_unsafe(F)

var r: F
if num_vars <= 13:
bench("Multilinear Extension " & $num_vars & " variables: Reference EvaluateAt", F, 100):
r.evalMultilinearExtensionAt_reference(mle, coords)

block:
bench("Multilinear Extension " & $num_vars & " variables: Optimized EvaluateAt", F, 100):
r.evalMultilinearExtensionAt(mle, coords)

const Curves = [BN254_Snarks, BLS12_381]

separator()
separator()
staticFor i, 0, Curves.len:
const curve = Curves[i]
for num_vars in countup(9, 19, 2):
bench_mle(Fr[curve], num_vars)
separator()
separator()
12 changes: 7 additions & 5 deletions constantine.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,10 @@ const testDesc: seq[tuple[path: string, useGMP: bool]] = @[
# ("tests/math_pairings/t_pairing_bls12_381_line_functions.nim", false),
# ("tests/math_pairings/t_pairing_mul_fp12_by_lines.nim", false),
("tests/math_pairings/t_pairing_cyclotomic_subgroup.nim", false),
("tests/math_pairings/t_pairing_bn254_nogami_optate.nim", false),
("tests/math_pairings/t_pairing_bn254_snarks_optate.nim", false),
("tests/math_pairings/t_pairing_bls12_377_optate.nim", false),
("tests/math_pairings/t_pairing_bls12_381_optate.nim", false),
# ("tests/math_pairings/t_pairing_bn254_nogami_optate.nim", false),
# ("tests/math_pairings/t_pairing_bn254_snarks_optate.nim", false),
# ("tests/math_pairings/t_pairing_bls12_377_optate.nim", false),
# ("tests/math_pairings/t_pairing_bls12_381_optate.nim", false),

# Multi-Pairing
# ----------------------------------------------------------
Expand All @@ -575,7 +575,7 @@ const testDesc: seq[tuple[path: string, useGMP: bool]] = @[

# Hashing to elliptic curves
# ----------------------------------------------------------
("tests/t_hash_to_field.nim", false),
# ("tests/t_hash_to_field.nim", false),
("tests/t_hash_to_curve_random.nim", false),
("tests/t_hash_to_curve.nim", false),

Expand All @@ -597,6 +597,7 @@ const testDesc: seq[tuple[path: string, useGMP: bool]] = @[
# Proof systems
# ----------------------------------------------------------
("tests/proof_systems/t_r1cs_parser.nim", false),
("tests/interactive_proofs/t_multilinear_extensions.nim", false),
]

const testDescNvidia: seq[string] = @[
Expand Down Expand Up @@ -665,6 +666,7 @@ const benchDesc = [
"bench_eth_eip2537_subgroup_checks_impact",
"bench_verkle_primitives",
"bench_eth_evm_precompiles",
"bench_multilinear_extensions",
# "zkalc", # Already tested through make_zkalc
]

Expand Down
7 changes: 7 additions & 0 deletions constantine/boolean_hypercube/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Boolean Hypercube

This folder holds utilities to work with the Boolean Hypercube,
via MLE, Multilinear Extension of polynomials evaluated at {0, 1}ⁿ.

This is a heavy area of experimentation to nail down useful software architecture.
Expect many refactorings.
Loading

0 comments on commit 623e05f

Please sign in to comment.