-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hanting Zhang
committed
Dec 11, 2023
1 parent
95d66b5
commit 8ccc92d
Showing
10 changed files
with
712 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
[package] | ||
name = "grumpkin-msm" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
description = "Optimized multiscalar multiplicaton for the Grumpkin curve cycle" | ||
repository = "https://github.com/lurk-lab/grumpkin-msm" | ||
readme = "README.md" | ||
include = [ | ||
"/benches/**", | ||
"/cuda/**", | ||
"/src/**", | ||
"/Cargo.toml", | ||
"/build.rs", | ||
"/README.md", | ||
] | ||
|
||
[features] | ||
# By default, compile with ADX extension if the host supports it. | ||
# Binary can be executed on systems similar to the host. | ||
default = [] | ||
# Compile in portable mode, without ISA extensions. | ||
# Binary can be executed on all systems. | ||
portable = [ "blst/portable" ] | ||
# Enable ADX even if the host CPU doesn't support it. | ||
# Binary can be executed on Broadwell+ and Ryzen+ systems. | ||
force-adx = [ "blst/force-adx" ] | ||
cuda-mobile = [] | ||
|
||
[dependencies] | ||
blst = "~0.3.11" | ||
sppark = "~0.1.2" | ||
halo2curves = { version = "0.4.0" } | ||
|
||
[build-dependencies] | ||
cc = "^1.0.70" | ||
which = "^4.0" | ||
|
||
[dev-dependencies] | ||
criterion = { version = "0.3", features = [ "html_reports" ] } | ||
rand = "^0" | ||
rand_chacha = "^0" | ||
rayon = "1.5" | ||
|
||
[[bench]] | ||
name = "msm" | ||
harness = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright Supranational LLC | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![allow(dead_code)] | ||
#![allow(unused_imports)] | ||
#![allow(unused_mut)] | ||
|
||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
|
||
use grumpkin_msm; | ||
|
||
#[cfg(feature = "cuda")] | ||
extern "C" { | ||
fn cuda_available() -> bool; | ||
} | ||
|
||
include!("../src/tests.rs"); | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let bench_npow: usize = std::env::var("BENCH_NPOW") | ||
.unwrap_or("17".to_string()) | ||
.parse() | ||
.unwrap(); | ||
let npoints: usize = 1 << bench_npow; | ||
|
||
//println!("generating {} random points, just hang on...", npoints); | ||
let mut points = crate::tests::gen_points(npoints); | ||
let mut scalars = crate::tests::gen_scalars(npoints); | ||
|
||
#[cfg(feature = "cuda")] | ||
{ | ||
unsafe { grumpkin_msm::CUDA_OFF = true }; | ||
} | ||
|
||
let mut group = c.benchmark_group("CPU"); | ||
group.sample_size(10); | ||
|
||
group.bench_function(format!("2**{} points", bench_npow), |b| { | ||
b.iter(|| { | ||
let _ = grumpkin_msm::bn256(&points, &scalars); | ||
}) | ||
}); | ||
|
||
group.finish(); | ||
|
||
#[cfg(feature = "cuda")] | ||
if unsafe { cuda_available() } { | ||
unsafe { grumpkin_msm::CUDA_OFF = false }; | ||
|
||
const EXTRA: usize = 5; | ||
let bench_npow = bench_npow + EXTRA; | ||
let npoints: usize = 1 << bench_npow; | ||
|
||
while points.len() < npoints { | ||
points.append(&mut points.clone()); | ||
} | ||
scalars.append(&mut crate::tests::gen_scalars(npoints - scalars.len())); | ||
|
||
let mut group = c.benchmark_group("GPU"); | ||
group.sample_size(20); | ||
|
||
group.bench_function(format!("2**{} points", bench_npow), |b| { | ||
b.iter(|| { | ||
let _ = grumpkin_msm::bn256(&points, &scalars); | ||
}) | ||
}); | ||
|
||
group.finish(); | ||
} | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
// account for cross-compilation [by examining environment variable] | ||
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); | ||
|
||
// Set CXX environment variable to choose alternative C compiler. | ||
// Optimization level depends on whether or not --release is passed | ||
// or implied. | ||
let mut cc = cc::Build::new(); | ||
cc.cpp(true); | ||
|
||
let c_src_dir = PathBuf::from("src"); | ||
let files = vec![c_src_dir.join("pippenger.cpp")]; | ||
let mut cc_def = None; | ||
|
||
match (cfg!(feature = "portable"), cfg!(feature = "force-adx")) { | ||
(true, false) => { | ||
println!("Compiling in portable mode without ISA extensions"); | ||
cc_def = Some("__PASTA_PORTABLE__"); | ||
} | ||
(false, true) => { | ||
if target_arch.eq("x86_64") { | ||
println!("Enabling ADX support via `force-adx` feature"); | ||
cc_def = Some("__ADX__"); | ||
} else { | ||
println!("`force-adx` is ignored for non-x86_64 targets"); | ||
} | ||
} | ||
(false, false) => { | ||
#[cfg(target_arch = "x86_64")] | ||
if target_arch.eq("x86_64") && std::is_x86_feature_detected!("adx") | ||
{ | ||
println!("Enabling ADX because it was detected on the host"); | ||
cc_def = Some("__ADX__"); | ||
} | ||
} | ||
(true, true) => panic!( | ||
"Cannot compile with both `portable` and `force-adx` features" | ||
), | ||
} | ||
|
||
cc.flag_if_supported("-mno-avx") // avoid costly transitions | ||
.flag_if_supported("-fno-builtin") | ||
.flag_if_supported("-std=c++11") | ||
.flag_if_supported("-Wno-unused-command-line-argument"); | ||
if !cfg!(debug_assertions) { | ||
cc.define("NDEBUG", None); | ||
} | ||
if let Some(def) = cc_def { | ||
cc.define(def, None); | ||
} | ||
if let Some(include) = env::var_os("DEP_BLST_C_SRC") { | ||
cc.include(include); | ||
} | ||
if let Some(include) = env::var_os("DEP_SPPARK_ROOT") { | ||
cc.include(include); | ||
} | ||
cc.files(&files).compile("pasta_msm"); | ||
|
||
if cfg!(target_os = "windows") && !cfg!(target_env = "msvc") { | ||
return; | ||
} | ||
// Detect if there is CUDA compiler and engage "cuda" feature accordingly | ||
let nvcc = match env::var("NVCC") { | ||
Ok(var) => which::which(var), | ||
Err(_) => which::which("nvcc"), | ||
}; | ||
if nvcc.is_ok() { | ||
let mut nvcc = cc::Build::new(); | ||
nvcc.cuda(true); | ||
nvcc.flag("-arch=sm_80"); | ||
nvcc.flag("-gencode").flag("arch=compute_70,code=sm_70"); | ||
nvcc.flag("-t0"); | ||
#[cfg(not(target_env = "msvc"))] | ||
nvcc.flag("-Xcompiler").flag("-Wno-unused-function"); | ||
nvcc.define("TAKE_RESPONSIBILITY_FOR_ERROR_MESSAGE", None); | ||
#[cfg(feature = "cuda-mobile")] | ||
nvcc.define("NTHREADS", "128"); | ||
if let Some(def) = cc_def { | ||
nvcc.define(def, None); | ||
} | ||
if let Some(include) = env::var_os("DEP_BLST_C_SRC") { | ||
nvcc.include(include); | ||
} | ||
if let Some(include) = env::var_os("DEP_SPPARK_ROOT") { | ||
nvcc.include(include); | ||
} | ||
nvcc.clone() | ||
.file("cuda/bn254.cu") | ||
.compile("pallas_msm_cuda"); | ||
nvcc.define("__MSM_SORT_DONT_IMPLEMENT__", None) | ||
.file("cuda/grumpkin.cu") | ||
.compile("vesta_msm_cuda"); | ||
|
||
println!("cargo:rerun-if-changed=cuda"); | ||
println!("cargo:rerun-if-env-changed=CXXFLAGS"); | ||
println!("cargo:rustc-cfg=feature=\"cuda\""); | ||
} | ||
println!("cargo:rerun-if-env-changed=NVCC"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright Supranational LLC | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <cuda.h> | ||
|
||
#include <ec/jacobian_t.hpp> | ||
#include <ec/xyzz_t.hpp> | ||
|
||
#include <ff/alt_bn128.hpp> | ||
|
||
typedef jacobian_t<fp_t> point_t; | ||
typedef xyzz_t<fp_t> bucket_t; | ||
typedef bucket_t::affine_t affine_t; | ||
typedef fr_t scalar_t; | ||
|
||
#include <msm/pippenger.cuh> | ||
|
||
#ifndef __CUDA_ARCH__ | ||
extern "C" | ||
RustError cuda_pippenger_bn254(point_t *out, const affine_t points[], size_t npoints, | ||
const scalar_t scalars[]) | ||
{ return mult_pippenger<bucket_t>(out, points, npoints, scalars); } | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright Supranational LLC | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <cuda.h> | ||
|
||
#include <ec/jacobian_t.hpp> | ||
#include <ec/xyzz_t.hpp> | ||
|
||
#include <ff/alt_bn128.hpp> | ||
|
||
typedef jacobian_t<fr_t> point_t; | ||
typedef xyzz_t<fr_t> bucket_t; | ||
typedef bucket_t::affine_t affine_t; | ||
typedef fp_t scalar_t; | ||
|
||
#include <msm/pippenger.cuh> | ||
|
||
#ifndef __CUDA_ARCH__ | ||
extern "C" | ||
RustError cuda_pippenger_grumpkin(point_t *out, const affine_t points[], size_t npoints, | ||
const scalar_t scalars[]) | ||
{ return mult_pippenger<bucket_t>(out, points, npoints, scalars); } | ||
#endif |
Oops, something went wrong.