Skip to content

Commit

Permalink
Merge pull request #5 from okx/dev-cliff-support-bn128
Browse files Browse the repository at this point in the history
Dev cliff support bn128
  • Loading branch information
cliff0412 authored Feb 27, 2024
2 parents 122d3f0 + 01844bd commit b99ff7e
Show file tree
Hide file tree
Showing 40 changed files with 1,506 additions and 24 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ pgo-data.profdata
.DS_Store

**/generated
docs/**bench**
docs/**bench**

**/*/libposeidon-permute-c-mac.a
**/*/go-iden3-crypto/
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "cryptography_cuda"]
path = cryptography_cuda
url = [email protected]:okx/cryptography_cuda.git
[submodule "depends/cryptography_cuda"]
path = depends/cryptography_cuda
url = [email protected]:okx/cryptography_cuda.git
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ incremental = true
#codegen-units = 1

[profile.bench]
opt-level = 3

opt-level = 3
1 change: 0 additions & 1 deletion cryptography_cuda
Submodule cryptography_cuda deleted from 4a900e
1 change: 1 addition & 0 deletions depends/cryptography_cuda
Submodule cryptography_cuda added at f22980
9 changes: 9 additions & 0 deletions docs/rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rustc 1.78.0-nightly will throw below error
```
error[E0635]: unknown feature `stdsimd`
```

```
rustup toolchian install nightly-2024-02-04
rustup override set nightly-2024-02-04
```
2 changes: 1 addition & 1 deletion evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ hashbrown = { version = "0.14.0" }
tiny-keccak = "2.0.2"
serde_json = "1.0"

[target.'cfg(not(target_env = "msvc"))'.dependencies]
[target.'cfg(not(target_os = "macos"))'.dependencies]
jemallocator = "0.5.0"

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub mod witness;

use eth_trie_utils::partial_trie::HashedPartialTrie;
// Set up Jemalloc
#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_os = "macos"))]
use jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_os = "macos"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

Expand Down
8 changes: 5 additions & 3 deletions field/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ edition = "2021"
name = "fft"

[dependencies]
cryptography_cuda = {path="../cryptography_cuda", optional = true}
anyhow = { version = "1.0.40", default-features = false }
itertools = { version = "0.11.0", default-features = false, features = ["use_alloc"] }
num = { version = "0.4", default-features = false, features = ["alloc", "rand"] }
Expand All @@ -20,6 +19,7 @@ serde = { version = "1.0", default-features = false, features = ["alloc", "deriv
static_assertions = { version = "1.1.0", default-features = false }
unroll = { version = "0.1.5", default-features = false }
lazy_static = "1.4.0"
cryptography_cuda ={path="../depends/cryptography_cuda", optional=true}

[dev-dependencies]
rand = "*"
Expand All @@ -32,5 +32,7 @@ proc-macro2 = "1"
quote = "1"

[features]
default = []
cuda = ["cryptography_cuda"]
default = ["no_cuda"]
cuda = ["cryptography_cuda"]
precompile = []
no_cuda = ["cryptography_cuda/no_cuda"]
17 changes: 15 additions & 2 deletions field/build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
use std::env;
use std::fs::write;
use std::path::{Path, PathBuf};
use std::path::{Path};
#[cfg(feature = "precompile")]
use std::Path::{PathBuf};
use std::process::Command;
#[cfg(feature = "precompile")]
use std::str::FromStr;

use anyhow::Context;
#[cfg(feature = "precompile")]
use plonky2_util::pre_compute::{get_pre_compute_size, PRE_COMPUTE_END, PRE_COMPUTE_START};
use proc_macro2::TokenStream;
#[cfg(feature = "precompile")]
use quote::quote;
#[cfg(feature = "precompile")]
use syn::Lit;

fn main() {
#[cfg(feature = "precompile")]
fn build_precompile() {
println!("cargo:rerun-if-changed=generated");
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

Expand All @@ -21,7 +28,13 @@ fn main() {
let token_stream = build_token_stream(&path).expect("build token stream error");
_ = write_generated_file(token_stream, "goldilock_root_of_unity.rs");
}
fn main() {

#[cfg(feature = "precompile")]
build_precompile();
}

#[cfg(feature = "precompile")]
fn build_token_stream(path: &PathBuf) -> anyhow::Result<TokenStream> {
let size = get_pre_compute_size(PRE_COMPUTE_START, PRE_COMPUTE_END);
let token = syn::parse_str::<Lit>(&format!("{}", size)).unwrap();
Expand Down
8 changes: 7 additions & 1 deletion field/src/goldilocks_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use num::{BigUint, Integer};
use plonky2_util::{assume, branch_hint, log2_strict};
use plonky2_util::{assume, branch_hint};
use serde::{Deserialize, Serialize};
#[cfg(feature = "precompile")]
use plonky2_util::{log2_strict};

#[cfg(feature = "precompile")]
use crate::fft::FftRootTable;
use crate::ops::Square;
use crate::types::{Field, Field64, PrimeField, PrimeField64, Sample};

#[cfg(feature = "precompile")]
use crate::PRE_COMPUTE_ROOT_TABLES;

const EPSILON: u64 = (1 << 32) - 1;
Expand Down Expand Up @@ -98,6 +103,7 @@ impl Field for GoldilocksField {
Self::order()
}

#[cfg(feature = "precompile")]
fn pre_compute_fft_root_table(input_len: usize) -> Option<&'static FftRootTable<Self>> {
let lg_n = log2_strict(input_len);
PRE_COMPUTE_ROOT_TABLES.get(&lg_n)
Expand Down
10 changes: 9 additions & 1 deletion field/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@ mod field_testing;
#[cfg(test)]
mod prime_field_testing;

#[cfg(feature = "precompile")]
include!(concat!(env!("OUT_DIR"), "/goldilock_root_of_unity.rs"));

#[cfg(feature = "precompile")]
use std::collections::HashMap;

#[cfg(feature = "precompile")]
use fft::FftRootTable;
#[cfg(feature = "precompile")]
use goldilocks_field::GoldilocksField;
#[cfg(feature = "precompile")]
use lazy_static::lazy_static;
#[cfg(feature = "precompile")]
use plonky2_util::pre_compute::{PRE_COMPUTE_END, PRE_COMPUTE_START};

#[cfg(feature = "precompile")]
lazy_static! {
pub static ref PRE_COMPUTE_ROOT_TABLES: HashMap<usize, FftRootTable<GoldilocksField>> = {
let mut map = HashMap::new();
Expand Down Expand Up @@ -75,6 +81,8 @@ lazy_static! {
#[cfg(test)]
mod test {
use super::*;

#[cfg(feature = "precompile")]
#[test]
fn test_pre_compute() {
for lgn_size in (PRE_COMPUTE_START..=PRE_COMPUTE_END) {
Expand Down
2 changes: 2 additions & 0 deletions gen/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(incomplete_features)]

#![feature(generic_const_exprs)]

use std::fs::File;
Expand Down
8 changes: 6 additions & 2 deletions plonky2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parallel = ["hashbrown/rayon", "plonky2_maybe_rayon/parallel"]
std = ["anyhow/std", "rand/std", "itertools/use_std"]
timing = ["std"]
cuda =["cryptography_cuda"]
no_cuda = ["cryptography_cuda/no_cuda"]
batch =[]

[dependencies]
Expand All @@ -36,7 +37,7 @@ serde = { version = "1.0", default-features = false, features = ["derive", "rc"]
serde_json = "1.0"
static_assertions = { version = "1.1.0", default-features = false }
unroll = { version = "0.1.5", default-features = false }
cryptography_cuda ={path="../cryptography_cuda", optional=true}
cryptography_cuda ={path="../depends/cryptography_cuda", optional=true}

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
getrandom = { version = "0.2", default-features = false, features = ["js"] }
Expand All @@ -51,9 +52,12 @@ serde_cbor = { version = "0.11.2" }
structopt = { version = "0.3.26", default-features = false }
tynm = { version = "0.1.6", default-features = false }

[target.'cfg(not(target_env = "msvc"))'.dev-dependencies]
[target.'cfg(not(target_os = "macos"))'.dev-dependencies]
jemallocator = "0.5.0"

[build-dependencies]
bindgen = "*"

[[bin]]
name = "generate_constants"
required-features = ["rand_chacha"]
Expand Down
4 changes: 2 additions & 2 deletions plonky2/benches/allocator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Set up Jemalloc
#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_os = "macos"))]
use jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_os = "macos"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
84 changes: 84 additions & 0 deletions plonky2/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
extern crate bindgen;

use std::env;
use std::path::{Path, PathBuf};

fn main() {
let dir = env::var("CARGO_MANIFEST_DIR").unwrap();

let poseidon_dir = Path::new(&dir).join("poseidon_bn128");

if cfg!(target_os = "macos") {
println!("target os is macos");
let output = std::process::Command::new("go")
.arg("version")
.output()
.expect("Failed to run command");

// Check the exit status
if output.status.success() {
// Go is installed
println!(
"Go is installed: {:?}",
String::from_utf8_lossy(&output.stdout)
);
} else {
// Go is not installed
panic!("Go is not installed");
}
let poseidon_c_dir = Path::new(&dir).join("go-iden3-crypto");
println!("poseidon_c_dir: {:?}", poseidon_c_dir);
if poseidon_c_dir.exists() {
std::process::Command::new("sh")
.arg("-c")
.arg("rm")
.arg("-rf")
.arg(poseidon_c_dir.clone())
.output()
.expect("rm go-iden3-crypto failure");
}
println!("start clone go iden3");
std::process::Command::new("git")
.arg("clone")
.arg("https://github.com/polymerdao/go-iden3-crypto.git")
.arg(poseidon_c_dir.clone())
.output()
.expect("clone go iden3 crypto failure");
println!("end clone go iden3");

let ret = std::process::Command::new("sh")
.arg("-c")
.arg("./compile.sh")
.current_dir(poseidon_c_dir.clone().join("poseidon-permute-c"))
.output()
.expect("compile poseidon permute c failure");
println!("compile lib ret: {:?}", ret);

std::process::Command::new("mv")
.arg("libposeidon-permute-c.a")
.arg(poseidon_dir.join("libposeidon-permute-c-mac.a"))
.current_dir(poseidon_c_dir.clone().join("poseidon-permute-c"))
.output()
.expect("mv failure");
}
println!("cargo:rustc-link-search=native={}", poseidon_dir.display());

if cfg!(target_os = "macos") {
println!("link to mac lib");
println!("cargo:rustc-link-lib=static=poseidon-permute-c-mac");
} else {
println!("link to linux lib");
println!("cargo:rustc-link-lib=static=poseidon-permute-c");
}

let bindings = bindgen::Builder::default()
.header(poseidon_dir.join("wrapper.h").display().to_string())
.generate()
.expect("Unable to generate bindings");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("{}", out_path.to_str().unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
Binary file added plonky2/poseidon_bn128/libposeidon-permute-c.a
Binary file not shown.
Loading

0 comments on commit b99ff7e

Please sign in to comment.