Skip to content

Commit

Permalink
Add pedersen hash for Noir
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszmlc committed Sep 26, 2023
1 parent 1c83af3 commit 7d49aaf
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 2 deletions.
45 changes: 43 additions & 2 deletions noir/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate noir;
extern crate rand;

use benchy::{benchmark, BenchmarkRun};
use noir::{InputMap, InputValue, Proof};
use noir::{backends::FIELD_BITS, InputMap, InputValue, Proof};
use rand::Rng;

#[benchmark]
Expand Down Expand Up @@ -112,6 +112,41 @@ fn sha256(b: &mut BenchmarkRun, p: usize) {
);
}

#[benchmark("pedersen", [
("1k bytes", 1000),
("10k bytes", 10000),
("100k bytes", 100000),
])]
fn pedersen(b: &mut BenchmarkRun, n_bytes: usize) {
let bytes_per_field = (FIELD_BITS as f64 / 8.).floor();
let n_fields = (n_bytes as f64 / bytes_per_field).ceil() as usize;

let backend = noir::backends::ConcreteBackend::default();
let dir = std::env::current_dir().expect("current dir to exist");

let mut inputs = InputMap::new();

// Generate random bytes
let bytes = generate_random_u8_slice(n_fields)
.iter()
.map(|b| InputValue::Field((*b as u128).into()))
.collect::<Vec<_>>();

inputs.insert("x".to_string(), InputValue::Vec(bytes));

let proof = Proof::new(
&backend,
"pedersen",
dir.join(format!("pkgs/pedersen/{}", n_fields)),
);
let proof_bytes = b.run(|| proof.run_and_prove(&inputs));
b.log("proof_size_bytes", proof_bytes.len());
b.log(
"compressed_proof_size_bytes",
zstd::encode_all(&proof_bytes[..], 21).unwrap().len(),
);
}

fn generate_random_u8_slice(len: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
let mut vec = Vec::with_capacity(len);
Expand All @@ -121,4 +156,10 @@ fn generate_random_u8_slice(len: usize) -> Vec<u8> {
vec
}

benchy::main!("noir", assert, fibonacci, sha256, merkle_membership);
benchy::main!(
"noir", // assert,
// fibonacci,
// sha256,
pedersen,
// merkle_membership
);
9 changes: 9 additions & 0 deletions noir/pkgs/pedersen/3226/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

[package]
name = pedersen_3226
type = bin
authors = []
compiler_version = 0.10.3

[dependencies]

6 changes: 6 additions & 0 deletions noir/pkgs/pedersen/3226/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

use dep::std;

fn main(x: [Field; 3226]) -> pub [Field; 2] {
std::hash::pedersen(x)
}
9 changes: 9 additions & 0 deletions noir/pkgs/pedersen/323/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

[package]
name = pedersen_323
type = bin
authors = []
compiler_version = 0.10.3

[dependencies]

6 changes: 6 additions & 0 deletions noir/pkgs/pedersen/323/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

use dep::std;

fn main(x: [Field; 323]) -> pub [Field; 2] {
std::hash::pedersen(x)
}
9 changes: 9 additions & 0 deletions noir/pkgs/pedersen/33/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

[package]
name = pedersen_33
type = bin
authors = []
compiler_version = 0.10.3

[dependencies]

6 changes: 6 additions & 0 deletions noir/pkgs/pedersen/33/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

use dep::std;

fn main(x: [Field; 33]) -> pub [Field; 2] {
std::hash::pedersen(x)
}
35 changes: 35 additions & 0 deletions noir/pkgs/pedersen/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

set -eo pipefail

field_bits=254

function generate() {
n_bytes="$1"
n_fields="$(python3 -c "import math; print(math.ceil($n_bytes / math.floor($field_bits / 8)))")"
n="$n_fields"

code="
use dep::std;
fn main(x: [Field; $n]) -> pub [Field; 2] {
std::hash::pedersen(x)
}"
toml="
[package]
name = "pedersen_$n"
type = "bin"
authors = [""]
compiler_version = "0.10.3"
[dependencies]
"

mkdir -p "$n/src"
echo "$code" > "$n/src/main.nr"
echo "$toml" > "$n/Nargo.toml"
}

generate 1000
generate 10000
generate 100000
2 changes: 2 additions & 0 deletions noir/src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ compile_error!("please specify a backend to compile with");
compile_error!(
"feature \"plonk_bn254\" and feature \"plonk_bn254_wasm\" cannot be enabled at the same time"
);

pub const FIELD_BITS: usize = 254;

0 comments on commit 7d49aaf

Please sign in to comment.