From be30d59e61a9dff6ab94ffb97365c1282c331643 Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:28:57 +0100 Subject: [PATCH] feat!: return Pedersen structure in stdlib (#3190) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> Co-authored-by: kek kek kek Co-authored-by: kevaundray Co-authored-by: jfecher Co-authored-by: josh crites Co-authored-by: github-merge-queue[bot] Co-authored-by: Martin Verzilli Co-authored-by: Savio <72797635+Savio-Sou@users.noreply.github.com> Co-authored-by: Alex Gherghisan Co-authored-by: vezenovm Co-authored-by: José Pedro Sousa Co-authored-by: jfecher Co-authored-by: Álvaro Rodríguez Co-authored-by: Gustavo Giráldez Co-authored-by: Jan Beneš Co-authored-by: Maddiaa <47148561+Maddiaa0@users.noreply.github.com> Co-authored-by: Tom French --- noir_stdlib/src/hash.nr | 17 +++++++++++++++-- noir_stdlib/src/lib.nr | 2 +- tooling/debugger/src/lib.rs | 1 + .../trait_override_implementation/src/main.nr | 4 ++++ .../brillig_pedersen/src/main.nr | 11 +++++++---- .../pedersen_check/src/main.nr | 10 ++++++---- .../execution_success/simple_shield/src/main.nr | 6 +++--- .../should_fail_with_matches/src/main.nr | 4 ++-- 8 files changed, 39 insertions(+), 16 deletions(-) diff --git a/noir_stdlib/src/hash.nr b/noir_stdlib/src/hash.nr index fdd303e81d8..f6a468048a0 100644 --- a/noir_stdlib/src/hash.nr +++ b/noir_stdlib/src/hash.nr @@ -7,12 +7,25 @@ pub fn sha256(_input : [u8; N]) -> [u8; 32] {} #[foreign(blake2s)] pub fn blake2s(_input : [u8; N]) -> [u8; 32] {} -pub fn pedersen_commitment(input : [Field; N]) -> [Field; 2] { +struct PedersenPoint { + x : Field, + y : Field, +} + +pub fn pedersen_commitment(input : [Field; N]) -> PedersenPoint { pedersen_commitment_with_separator(input, 0) } #[foreign(pedersen)] -pub fn pedersen_commitment_with_separator(_input : [Field; N], _separator : u32) -> [Field; 2] {} +pub fn __pedersen_commitment_with_separator(_input : [Field; N], _separator : u32) -> [Field; 2] {} + +pub fn pedersen_commitment_with_separator(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(input : [Field; N]) -> Field { pedersen_hash_with_separator(input, 0) diff --git a/noir_stdlib/src/lib.nr b/noir_stdlib/src/lib.nr index 2e34c017db6..e6b56d29542 100644 --- a/noir_stdlib/src/lib.nr +++ b/noir_stdlib/src/lib.nr @@ -63,4 +63,4 @@ pub fn wrapping_mul(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(x : T, y: T) -> T {} +pub fn wrapping_shift_left(_x : T, _y: T) -> T {} diff --git a/tooling/debugger/src/lib.rs b/tooling/debugger/src/lib.rs index 42ae79fe411..7c6a9e9f618 100644 --- a/tooling/debugger/src/lib.rs +++ b/tooling/debugger/src/lib.rs @@ -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( diff --git a/tooling/nargo_cli/tests/compile_success_empty/trait_override_implementation/src/main.nr b/tooling/nargo_cli/tests/compile_success_empty/trait_override_implementation/src/main.nr index f359937b739..763784f64e8 100644 --- a/tooling/nargo_cli/tests/compile_success_empty/trait_override_implementation/src/main.nr +++ b/tooling/nargo_cli/tests/compile_success_empty/trait_override_implementation/src/main.nr @@ -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"); @@ -71,3 +74,4 @@ fn main(x: Field) { assert(bar_mut.f4() == 4, "14"); assert(bar_mut.f5() == 50, "15"); } + diff --git a/tooling/nargo_cli/tests/execution_success/brillig_pedersen/src/main.nr b/tooling/nargo_cli/tests/execution_success/brillig_pedersen/src/main.nr index b7de745a342..1a793dd3f05 100644 --- a/tooling/nargo_cli/tests/execution_success/brillig_pedersen/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/brillig_pedersen/src/main.nr @@ -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; @@ -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); } diff --git a/tooling/nargo_cli/tests/execution_success/pedersen_check/src/main.nr b/tooling/nargo_cli/tests/execution_success/pedersen_check/src/main.nr index ff4e9539d7e..f25c1f68a18 100644 --- a/tooling/nargo_cli/tests/execution_success/pedersen_check/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/pedersen_check/src/main.nr @@ -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; @@ -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); } diff --git a/tooling/nargo_cli/tests/execution_success/simple_shield/src/main.nr b/tooling/nargo_cli/tests/execution_success/simple_shield/src/main.nr index ef6e5dbde12..f8fec2cb4d9 100644 --- a/tooling/nargo_cli/tests/execution_success/simple_shield/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/simple_shield/src/main.nr @@ -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] } diff --git a/tooling/nargo_cli/tests/noir_test_success/should_fail_with_matches/src/main.nr b/tooling/nargo_cli/tests/noir_test_success/should_fail_with_matches/src/main.nr index cbbc2144631..d2b7d155a32 100644 --- a/tooling/nargo_cli/tests/noir_test_success/should_fail_with_matches/src/main.nr +++ b/tooling/nargo_cli/tests/noir_test_success/should_fail_with_matches/src/main.nr @@ -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); }