Skip to content
Draft
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
50 changes: 43 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ endif
[ -s "$$NVM_DIR/nvm.sh" ] && \. "$$NVM_DIR/nvm.sh" && \
nvm install 22.14.0 && \
nvm use 22.14.0'
@curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
@cargo install wasm-pack
@echo "\033[1;32m>>> Now run this command to activate Node.js 22.14.0: \033[1;33msource $$HOME/.nvm/nvm.sh && nvm use 22.14.0\033[0m"

build: .pre-build
Expand Down
23 changes: 22 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
inputs = {
# Version 24.11
nixpkgs.url = "github:NixOS/nixpkgs?rev=f44bd8ca21e026135061a0a57dcf3d0775b67a49";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs }:
outputs = { self, nixpkgs, rust-overlay }:
let
stableSystems = [
"x86_64-linux" "aarch64-linux"
Expand All @@ -15,7 +19,8 @@
"i686-windows"
];
forAllSystems = nixpkgs.lib.genAttrs stableSystems;
pkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
overlays = [ (import rust-overlay) ];
pkgsFor = forAllSystems (system: import nixpkgs { inherit system overlays; });
in rec
{
packages = forAllSystems (system: let
Expand All @@ -29,9 +34,21 @@
pkgs = pkgsFor.${system};
in {
default = pkgs.mkShell {
inputsFrom = [
packages.${system}.default
buildInputs = with pkgs; [
git
cmake
rustup
cargo-make
gnuplot
xz
wasm-pack
rust-bin.stable.latest.default
];
# Shared library liblzma.so.5 used by wasm-pack
shellHook = ''
xz_lib=$(nix-store -q --references $(which xz) | grep xz)
export LD_LIBRARY_PATH=$xz_lib/lib:$LD_LIBRARY_PATH
'';
};
});
};
Expand Down
5 changes: 5 additions & 0 deletions utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ sled = "0.34.7"
serde = "1.0"
lazy_static = "1.5.0"
hex = "0.4"
light-poseidon = "0.3.0"

[dev-dependencies]
ark-bn254 = { version = "0.5.0", features = ["std"] }
num-traits = "0.2.19"
hex-literal = "1.0.0"
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
criterion = { version = "0.4.0", features = ["html_reports"] }
rand = "0.9.1"
rand_chacha = "0.9.0"
rand_core = "0.9.3"
rln = { path = "../rln", default-features = false }

[features]
default = []
Expand Down
90 changes: 68 additions & 22 deletions utils/benches/poseidon_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ use ark_bn254::Fr;
use criterion::{
black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput,
};
use zerokit_utils::Poseidon;
use light_poseidon::{
PoseidonHasher as LPoseidonHasher, PoseidonParameters as LPoseidonParameters,
};
use rand::RngCore;
use rand_chacha::ChaCha8Rng;
use rand_core::SeedableRng;
use rln::utils::bytes_le_to_fr;
use zerokit_utils::{Poseidon, RoundParameters};

const ROUND_PARAMS: [(usize, usize, usize, usize); 8] = [
(2, 8, 56, 0),
Expand All @@ -15,42 +22,81 @@ const ROUND_PARAMS: [(usize, usize, usize, usize); 8] = [
(9, 8, 63, 0),
];

struct U256Stream {
rng: ChaCha8Rng,
}
impl U256Stream {
fn seeded_stream(seed: u64) -> Self {
let rng = ChaCha8Rng::seed_from_u64(seed);
Self { rng }
}
}

impl Iterator for U256Stream {
type Item = [u8; 32];

fn next(&mut self) -> Option<Self::Item> {
let mut res = [0; 32];
self.rng.fill_bytes(&mut res);
Some(res)
}
}

pub fn poseidon_benchmark(c: &mut Criterion) {
let hasher = Poseidon::<Fr>::from(&ROUND_PARAMS);
let mut group = c.benchmark_group("poseidon Fr");

for size in [10u32, 100, 1000].iter() {
// group.measurement_time(std::time::Duration::from_secs(30));
for size in [1u32, 2].iter() {
group.throughput(Throughput::Elements(*size as u64));
let vals = U256Stream::seeded_stream(*size as u64)
.take(*size as usize)
.map(|b| bytes_le_to_fr(&b).0)
.collect::<Vec<_>>();
let RoundParameters {
t,
n_rounds_full,
n_rounds_partial,
skip_matrices: _,
ark_consts,
mds,
} = hasher.select_params(&vals).unwrap();

group.bench_with_input(BenchmarkId::new("Array hash", size), size, |b, &size| {
group.bench_function(BenchmarkId::new("Array hash light", size), |b| {
b.iter_batched(
// Setup: create values for each benchmark iteration
// setup
|| {
let mut values = Vec::with_capacity(size as usize);
for i in 0..size {
values.push([Fr::from(i)]);
}
values
// this needs to be done here due to move/copy/etc issues.
let l_params = LPoseidonParameters {
ark: ark_consts.clone(),
mds: mds.clone(),
full_rounds: *n_rounds_full,
partial_rounds: *n_rounds_partial,
width: *t,
alpha: 5,
};

light_poseidon::Poseidon::<Fr>::new(l_params)
},
// Actual benchmark
|values| {
for v in values.iter() {
let _ = hasher.hash(black_box(&v[..]));
}
},
|mut light_hasher| black_box(light_hasher.hash(&vals)),
BatchSize::SmallInput,
)
});
group.bench_function(BenchmarkId::new("Array hash ift", size), |b| {
b.iter(|| black_box(hasher.hash(&vals)))
});
group.bench_function(BenchmarkId::new("Array hash light_circom", size), |b| {
b.iter_batched(
// setup
|| light_poseidon::Poseidon::<Fr>::new_circom(*size as usize).unwrap(),
// Actual benchmark
|mut light_hasher_circom| black_box(light_hasher_circom.hash(&vals)),
BatchSize::SmallInput,
)
});
}

// Benchmark single hash operation separately
group.bench_function("Single hash", |b| {
let input = [Fr::from(u64::MAX)];
b.iter(|| {
let _ = hasher.hash(black_box(&input[..]));
})
});

group.finish();
}

Expand Down
Loading