Skip to content

Commit

Permalink
feat!: return Pedersen structure in stdlib (#3190)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
Co-authored-by: kek kek kek <andriy.n@obox.systems>
Co-authored-by: kevaundray <kevtheappdev@gmail.com>
Co-authored-by: jfecher <jake@aztecprotocol.com>
Co-authored-by: josh crites <jc@joshcrites.com>
Co-authored-by: github-merge-queue[bot] <github-merge-queue[bot]@users.noreply.github.com>
Co-authored-by: Martin Verzilli <martin.verzilli@gmail.com>
Co-authored-by: Savio <72797635+Savio-Sou@users.noreply.github.com>
Co-authored-by: Alex Gherghisan <alexghr@users.noreply.github.com>
Co-authored-by: vezenovm <mvezenov@gmail.com>
Co-authored-by: José Pedro Sousa <jose@aztecprotocol.com>
Co-authored-by: jfecher <jfecher11@gmail.com>
Co-authored-by: Álvaro Rodríguez <sirasistant@gmail.com>
Co-authored-by: Gustavo Giráldez <ggiraldez@manas.tech>
Co-authored-by: Jan Beneš <janbenes1234@gmail.com>
Co-authored-by: Maddiaa <47148561+Maddiaa0@users.noreply.github.com>
Co-authored-by: Tom French <tom@tomfren.ch>
  • Loading branch information
18 people authored Oct 31, 2023
1 parent f502108 commit be30d59
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 16 deletions.
17 changes: 15 additions & 2 deletions noir_stdlib/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ pub fn sha256<N>(_input : [u8; N]) -> [u8; 32] {}
#[foreign(blake2s)]
pub fn blake2s<N>(_input : [u8; N]) -> [u8; 32] {}

pub fn pedersen_commitment<N>(input : [Field; N]) -> [Field; 2] {
struct PedersenPoint {
x : Field,
y : Field,
}

pub fn pedersen_commitment<N>(input : [Field; N]) -> PedersenPoint {
pedersen_commitment_with_separator(input, 0)
}

#[foreign(pedersen)]
pub fn pedersen_commitment_with_separator<N>(_input : [Field; N], _separator : u32) -> [Field; 2] {}
pub fn __pedersen_commitment_with_separator<N>(_input : [Field; N], _separator : u32) -> [Field; 2] {}

pub fn pedersen_commitment_with_separator<N>(input : [Field; N], separator : u32) -> PedersenPoint {
let values = __pedersen_commitment_with_separator(input, separator);
PedersenPoint {
x: values[0],
y: values[1],
}
}

pub fn pedersen_hash<N>(input : [Field; N]) -> Field {
pedersen_hash_with_separator(input, 0)
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ pub fn wrapping_mul<T>(x : T, y: T) -> T {
/// Shift-left x by y bits
/// If the result overflow the bitsize; it does not fail and returns 0 instead
#[builtin(wrapping_shift_left)]
pub fn wrapping_shift_left<T>(x : T, y: T) -> T {}
pub fn wrapping_shift_left<T>(_x : T, _y: T) -> T {}
1 change: 1 addition & 0 deletions tooling/debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use acvm::BlackBoxFunctionSolver;
use acvm::{acir::circuit::Circuit, acir::native_types::WitnessMap};

use nargo::artifacts::debug::DebugArtifact;

use nargo::NargoError;

pub fn debug_circuit<B: BlackBoxFunctionSolver>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ impl F for Bar {
fn f3(self) -> Field { 30 }
}


// Impls on mutable references are temporarily disabled
// impl F for &mut Bar {
// fn f1(self) -> Field { 101 }
// fn f5(self) -> Field { 505 }
// }


fn main(x: Field) {
let first = Foo::method2(x);
assert(first == 3 * x);

let bar = Bar{};

assert(bar.f1() == 10, "1");
assert(bar.f2() == 2, "2");
assert(bar.f3() == 30, "3");
Expand All @@ -71,3 +74,4 @@ fn main(x: Field) {
assert(bar_mut.f4() == 4, "14");
assert(bar_mut.f5() == 50, "15");
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use dep::std;

unconstrained fn main(x: Field, y: Field, salt: Field, out_x: Field, out_y: Field, out_hash: Field) {
let res = std::hash::pedersen_commitment_with_separator([x, y], 0);
assert(res[0] == out_x);
assert(res[1] == out_y);
assert(res.x == out_x);
assert(res.y == out_y);


let res_hash = std::hash::pedersen_hash_with_separator([x, y], 0);
assert_eq(res_hash, out_hash);

assert(res_hash != res[0]);
assert(res_hash != res.x);


let raw_data = [x, y];
let mut state = 0;
Expand All @@ -16,6 +19,6 @@ unconstrained fn main(x: Field, y: Field, salt: Field, out_x: Field, out_y: Fiel
}
state += salt;
let hash = std::hash::pedersen_commitment_with_separator([state], 0);
assert(std::hash::pedersen_commitment_with_separator([43], 0)[0] == hash[0]);
assert(std::hash::pedersen_commitment_with_separator([43], 0).x == hash.x);
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use dep::std;

fn main(x: Field, y: Field, salt: Field, out_x: Field, out_y: Field, out_hash: Field) {
let res = std::hash::pedersen_commitment([x, y]);
assert(res[0] == out_x);
assert(res[1] == out_y);
assert(res.x == out_x);
assert(res.y == out_y);

let res_hash = std::hash::pedersen_hash_with_separator([x, y], 0);
assert_eq(res_hash, out_hash);

assert(res_hash != res[0]);
assert(res_hash != res.x);


let raw_data = [x, y];
let mut state = 0;
Expand All @@ -16,6 +18,6 @@ fn main(x: Field, y: Field, salt: Field, out_x: Field, out_y: Field, out_hash: F
}
state += salt;
let hash = std::hash::pedersen_commitment([state]);
assert(std::hash::pedersen_commitment([43])[0] == hash[0]);
assert(std::hash::pedersen_commitment([43]).x == hash.x);
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ fn main(
let note_commitment = std::hash::pedersen_commitment([pubkey_x, pubkey_y]);

// Compute input note nullifier
let nullifier = std::hash::pedersen_commitment([note_commitment[0], index, priv_key]);
let nullifier = std::hash::pedersen_commitment([note_commitment.x, index, priv_key]);

// Compute output note nullifier
let receiver_note_commitment = std::hash::pedersen_commitment([to_pubkey_x, to_pubkey_y]);

// Check that the input note nullifier is in the root
assert(note_root == std::merkle::compute_merkle_root(note_commitment[0], index, note_hash_path));
assert(note_root == std::merkle::compute_merkle_root(note_commitment.x, index, note_hash_path));

[nullifier[0], receiver_note_commitment[0]]
[nullifier.x, receiver_note_commitment.x]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ fn test_should_fail_without_match() {

#[test(should_fail_with = "Not equal")]
fn test_should_fail_with_runtime_match() {
assert_eq(dep::std::hash::pedersen_commitment([27])[0], 0, "Not equal");
assert_eq(dep::std::hash::pedersen_commitment([27]).x, 0, "Not equal");
}

#[test(should_fail)]
fn test_should_fail_without_runtime_match() {
assert_eq(dep::std::hash::pedersen_commitment([27])[0], 0);
assert_eq(dep::std::hash::pedersen_commitment([27]).x, 0);
}

0 comments on commit be30d59

Please sign in to comment.