Skip to content

Commit

Permalink
Add Pedersen hash for Noir (#53)
Browse files Browse the repository at this point in the history
* Add pedersen hash for Noir

* Fix strings in generated toml files
  • Loading branch information
mateuszmlc committed Sep 27, 2023
1 parent 4aff561 commit c4fe044
Show file tree
Hide file tree
Showing 24 changed files with 157 additions and 55 deletions.
46 changes: 44 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,11 @@ 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
);
10 changes: 4 additions & 6 deletions noir/pkgs/fib/1/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

[package]
name = fib_1
type = bin
authors = []
compiler_version = 0.10.3
name = "fib_1"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]

1 change: 0 additions & 1 deletion noir/pkgs/fib/1/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

fn main(a_start: Field, b_start: Field) -> pub Field {
let mut a = a_start;
let mut b = b_start;
Expand Down
10 changes: 4 additions & 6 deletions noir/pkgs/fib/10/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

[package]
name = fib_10
type = bin
authors = []
compiler_version = 0.10.3
name = "fib_10"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]

1 change: 0 additions & 1 deletion noir/pkgs/fib/10/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

fn main(a_start: Field, b_start: Field) -> pub Field {
let mut a = a_start;
let mut b = b_start;
Expand Down
10 changes: 4 additions & 6 deletions noir/pkgs/fib/100/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

[package]
name = fib_100
type = bin
authors = []
compiler_version = 0.10.3
name = "fib_100"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]

1 change: 0 additions & 1 deletion noir/pkgs/fib/100/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

fn main(a_start: Field, b_start: Field) -> pub Field {
let mut a = a_start;
let mut b = b_start;
Expand Down
10 changes: 4 additions & 6 deletions noir/pkgs/fib/1000/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

[package]
name = fib_1000
type = bin
authors = []
compiler_version = 0.10.3
name = "fib_1000"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]

1 change: 0 additions & 1 deletion noir/pkgs/fib/1000/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

fn main(a_start: Field, b_start: Field) -> pub Field {
let mut a = a_start;
let mut b = b_start;
Expand Down
10 changes: 4 additions & 6 deletions noir/pkgs/fib/10000/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

[package]
name = fib_10000
type = bin
authors = []
compiler_version = 0.10.3
name = "fib_10000"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]

1 change: 0 additions & 1 deletion noir/pkgs/fib/10000/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

fn main(a_start: Field, b_start: Field) -> pub Field {
let mut a = a_start;
let mut b = b_start;
Expand Down
10 changes: 4 additions & 6 deletions noir/pkgs/fib/100000/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

[package]
name = fib_100000
type = bin
authors = []
compiler_version = 0.10.3
name = "fib_100000"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]

1 change: 0 additions & 1 deletion noir/pkgs/fib/100000/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

fn main(a_start: Field, b_start: Field) -> pub Field {
let mut a = a_start;
let mut b = b_start;
Expand Down
10 changes: 4 additions & 6 deletions noir/pkgs/fib/1000000/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

[package]
name = fib_1000000
type = bin
authors = []
compiler_version = 0.10.3
name = "fib_1000000"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]

1 change: 0 additions & 1 deletion noir/pkgs/fib/1000000/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

fn main(a_start: Field, b_start: Field) -> pub Field {
let mut a = a_start;
let mut b = b_start;
Expand Down
12 changes: 8 additions & 4 deletions noir/pkgs/fib/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -eo pipefail

function generate() {
n="$1"
code="
code=$(cat <<EOF
fn main(a_start: Field, b_start: Field) -> pub Field {
let mut a = a_start;
let mut b = b_start;
Expand All @@ -15,16 +15,20 @@ fn main(a_start: Field, b_start: Field) -> pub Field {
}
b
}"
toml="
}
EOF
)

toml=$(cat <<EOF
[package]
name = "fib_$n"
type = "bin"
authors = [""]
compiler_version = "0.10.3"
[dependencies]
"
EOF
)

mkdir -p "$n/src"
echo "$code" > "$n/src/main.nr"
Expand Down
7 changes: 7 additions & 0 deletions noir/pkgs/pedersen/3226/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[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)
}
7 changes: 7 additions & 0 deletions noir/pkgs/pedersen/323/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[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)
}
7 changes: 7 additions & 0 deletions noir/pkgs/pedersen/33/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[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)
}
36 changes: 36 additions & 0 deletions noir/pkgs/pedersen/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/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=$(cat <<EOF
[package]
name = "pedersen_$n"
type = "bin"
authors = [""]
compiler_version = "0.10.3"
[dependencies]
EOF
)

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 c4fe044

Please sign in to comment.