Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multilinear extensions of polynomials #423

Merged
merged 3 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -531,10 +531,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 @@ -549,7 +549,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 @@ -571,6 +571,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 @@ -639,6 +640,7 @@ const benchDesc = [
"bench_eth_eip2537_subgroup_checks_impact",
"bench_verkle_primitives",
"bench_eth_evm_precompiles",
"bench_multilinear_extensions"
]

# For temporary (hopefully) investigation that can only be reproduced in CI
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
Loading