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

Implemented and Tested Starkware Poseidon #3

Merged
merged 6 commits into from
Jul 22, 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
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[package]
name = "pumice"
version = "0.1.0"
edition = "2021"
[workspace]
members = ["felt", "merkle", "poseidon"]
resolver = "2"

[dependencies]
[workspace.dependencies]
anyhow = "1.0.86"
ark-ff = "0.4.2"
ark-poly = "0.4.2"
7 changes: 7 additions & 0 deletions felt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "felt"
version = "0.1.0"
edition = "2021"

[dependencies]
ark-ff.workspace = true
20 changes: 20 additions & 0 deletions src/felt252.rs → felt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ark_ff::Zero;

pub use container::Felt252;

#[allow(non_local_definitions)]
Expand All @@ -16,6 +18,24 @@ mod container {
pub type Felt252 = Fp256<MontBackend<Felt252Config, 4>>;
}

/// This method is used for testing / debugging purposes.
/// It provides a convenient way to create a `Felt252` from a hex string.
///
/// Warning: this method is slow and will panic if the input is not a valid hex string.
pub fn hex(hex: &str) -> Felt252 {
let mut chars = hex.chars();
assert!(chars.next().unwrap() == '0');
assert!(chars.next().unwrap() == 'x');

let mut res = Felt252::zero();
for digit in chars {
let val = u8::from_str_radix(&digit.to_string(), 0x10).unwrap();
res *= Felt252::from(0x10u64);
res += Felt252::from(val as u64);
}
res
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
7 changes: 7 additions & 0 deletions merkle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "merkle"
version = "0.1.0"
edition = "2021"

[dependencies]
ark-ff.workspace = true
1 change: 1 addition & 0 deletions src/merkle/mod.rs → merkle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl<F: Field, H: Hasher<F>> MerkleTree<F, H> {
///
/// Returns an error type for easier "monadic style" chaining.
#[allow(dead_code)]
#[allow(clippy::result_unit_err)]
pub fn verify_decommitment(
&self,
comm: H::Output,
Expand Down
15 changes: 15 additions & 0 deletions poseidon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "poseidon"
version = "0.1.0"
edition = "2021"

[dependencies]
ark-ff.workspace = true
felt = { path = "../felt" }

[dev-dependencies]
criterion = "0.5.1"

[[bench]]
name = "poseidon"
harness = false
1 change: 1 addition & 0 deletions poseidon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# StarkWare Poseidon
45 changes: 45 additions & 0 deletions poseidon/benches/poseidon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use ark_ff::BigInt;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use felt::Felt252;
use poseidon::{FieldHasher, Poseidon3};

fn criterion_benchmark(c: &mut Criterion) {
let left = Felt252::new(BigInt([
0xdeadbeefdeadbeef,
0xdeadbeefdeadbeef,
0xdeadbeefdeadbeef,
0xdeadbeefdeadbeef,
]));

let right = Felt252::new(BigInt([
0xcafebabecafebabe,
0xcafebabecafebabe,
0xcafebabecafebabe,
0xcafebabecafebabe,
]));

let elems = [left, right].into_iter().cycle();
let elems_4 = elems.clone().take(4).collect::<Vec<_>>();
let elems_8 = elems.clone().take(8).collect::<Vec<_>>();
let elems_16 = elems.clone().take(16).collect::<Vec<_>>();

c.bench_function("Poseidon3::pair", |b| {
b.iter(|| black_box(Poseidon3::pair(left, right)))
});

c.bench_function("Poseidon3::hash(|4|)", |b| {
b.iter(|| black_box(Poseidon3::hash(&elems_4)))
});

c.bench_function("Poseidon3::hash(|8|)", |b| {
b.iter(|| black_box(Poseidon3::hash(&elems_8)))
});

c.bench_function("Poseidon3::hash(|16|)", |b| {
b.iter(|| black_box(Poseidon3::hash(&elems_16)))
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading