Skip to content

Commit

Permalink
merge with dev
Browse files Browse the repository at this point in the history
  • Loading branch information
cliff0412 committed Apr 15, 2024
2 parents 57b323e + 4b429c2 commit 55b669e
Show file tree
Hide file tree
Showing 29 changed files with 3,183 additions and 130 deletions.
2 changes: 1 addition & 1 deletion field/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ proc-macro2 = "1"
quote = "1"

[features]
default = ["cryptography_cuda/no_cuda"]
default = []
cuda = ["cryptography_cuda/cuda"]
precompile = []
no_cuda = ["cryptography_cuda/no_cuda"]
Expand Down
15 changes: 9 additions & 6 deletions plonky2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ keywords.workspace = true
categories.workspace = true

[features]
default = ["gate_testing", "parallel", "rand_chacha", "std", "cryptography_cuda/no_cuda"]
default = ["gate_testing", "parallel", "rand_chacha", "std"]
gate_testing = []
parallel = ["hashbrown/rayon", "plonky2_maybe_rayon/parallel"]
std = ["anyhow/std", "rand/std", "itertools/use_std"]
timing = ["std", "dep:web-time"]
cuda =["cryptography_cuda/cuda"]
cuda = ["cryptography_cuda/cuda"]
no_cuda = ["cryptography_cuda/no_cuda"]
batch =[]
batch = []
cuda_timing = []

[dependencies]
ahash = { workspace = true }
Expand All @@ -35,6 +36,7 @@ serde = { workspace = true, features = ["rc"] }
static_assertions = { workspace = true }
unroll = { workspace = true }
web-time = { version = "1.0.0", optional = true }
once_cell = { version = "1.18.0" }

# Local dependencies
plonky2_field = { version = "0.2.0", path = "../field", default-features = false }
Expand All @@ -45,6 +47,9 @@ 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"] }

[build-dependencies]
bindgen = { version = "0.68.1" }

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false }
env_logger = { version = "0.9.0", default-features = false }
Expand All @@ -55,13 +60,11 @@ serde_cbor = { version = "0.11.2" }
serde_json = { version = "1.0" }
structopt = { version = "0.3.26", default-features = false }
tynm = { version = "0.1.6", default-features = false }
zkhash = { git = "https://github.com/HorizenLabs/poseidon2" }

[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
44 changes: 44 additions & 0 deletions plonky2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,50 @@ Plonky2 is a SNARK implementation based on techniques from PLONK and FRI. It is

Plonky2 is built for speed, and features a highly efficient recursive circuit. On a Macbook Pro, recursive proofs can be generated in about 170 ms.

# Rust

To use a nightly toolchain for Plonky2 by default, you can run

```
rustup override set nightly
```

# Plonky2 on GPU

## Poseidon Hash on GPU (CUDA)

Build the shared library

```
cd cryptography_cuda/cuda/merkle
make lib
make libgpu
```

Run tests (in plonky2 folder)

```
export LD_LIBRARY_PATH=<path-to cryptography_cuda/cuda/merkle>
# CPU-only
cargo test -- --nocapture merkle_trees
# GPU
cargo test --features=cuda -- --nocapture merkle_trees
```

Run benchmarks
```
# CPU
cargo bench merkle
# GPU
cargo bench --features=cuda merkle
```

Run microbenchmarks

```
cd cryptography_cuda/cuda/merkle
./run-benchmark.sh
```

## License

Expand Down
13 changes: 11 additions & 2 deletions plonky2/benches/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use plonky2::hash::hash_types::RichField;
use plonky2::hash::keccak::KeccakHash;
use plonky2::hash::merkle_tree::MerkleTree;
use plonky2::hash::poseidon::PoseidonHash;
use plonky2::hash::poseidon2::Poseidon2Hash;
use plonky2::hash::poseidon_bn128::PoseidonBN128Hash;
use plonky2::plonk::config::Hasher;
use tynm::type_name;

Expand All @@ -22,15 +24,22 @@ pub(crate) fn bench_merkle_tree<F: RichField, H: Hasher<F>>(c: &mut Criterion) {
for size_log in [13, 14, 15] {
let size = 1 << size_log;
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| {
let leaves = vec![F::rand_vec(ELEMS_PER_LEAF); size];
b.iter(|| MerkleTree::<F, H>::new(leaves.clone(), 0));
// let leaves = vec![F::rand_vec(ELEMS_PER_LEAF); size];
// b.iter(|| MerkleTree::<F, H>::new_from_2d(leaves.clone(), 0));
let mut leaves = Vec::with_capacity(size * ELEMS_PER_LEAF);
for _ in 0..size {
leaves.append(&mut F::rand_vec(ELEMS_PER_LEAF));
}
b.iter(|| MerkleTree::<F, H>::new_from_1d(leaves.clone(), ELEMS_PER_LEAF, 2));
});
}
}

fn criterion_benchmark(c: &mut Criterion) {
bench_merkle_tree::<GoldilocksField, PoseidonHash>(c);
bench_merkle_tree::<GoldilocksField, Poseidon2Hash>(c);
bench_merkle_tree::<GoldilocksField, KeccakHash<25>>(c);
bench_merkle_tree::<GoldilocksField, PoseidonBN128Hash>(c);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
3 changes: 2 additions & 1 deletion plonky2/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ fn main() {

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!");
.expect("Couldn't write bindings!");
}
2 changes: 0 additions & 2 deletions plonky2/examples/bench_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use alloc::sync::Arc;
use core::num::ParseIntError;
use core::ops::RangeInclusive;
use core::str::FromStr;
#[cfg(feature = "std")]
use std::sync::Arc;

use anyhow::{anyhow, Context as _, Result};
use log::{info, Level, LevelFilter};
Expand Down
Loading

0 comments on commit 55b669e

Please sign in to comment.