Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: change stdlib function pedersen to pedersen_commitment #3341

Merged
merged 5 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion docs/docs/examples/merkle-proof.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main(message : [Field; 62], index : Field, hashpath : [Field; 40], root : Fie

The message is hashed using `hash_to_field`. The specific hash function that is being used is chosen
by the backend. The only requirement is that this hash function can heuristically be used as a
random oracle. If only collision resistance is needed, then one can call `std::hash::pedersen`
random oracle. If only collision resistance is needed, then one can call `std::hash::pedersen_hash`
instead.

```rust
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
### Solidity Developers

Noir currently includes a command to create a Solidity contract which verifies your Noir program. This will
be modularised in the future; however, as of the alpha, you can use the [`nargo codegen-verifier`](./nargo/commands#nargo-codegen-verifier) command to create

Check warning on line 37 in docs/docs/index.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (modularised)
a verifier contract.

### Protocol Developers
Expand All @@ -55,7 +55,7 @@

Noir is simple and flexible in its design, as it does not compile immediately to a fixed
NP-complete language. Instead, Noir compiles to an intermediate language (ACIR), which itself can be compiled
to an arithmetic circuit (if choosing to target Aztec's barretenberg backend) or a rank-1 constraint system (if choosing to target an R1CS backend like Arkwork's Marlin backend, or others).

Check warning on line 58 in docs/docs/index.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (Arkwork's)

This in itself brings up a few challenges within the design process, but allows one to decouple the programming language completely from the backend. This is similar in theory to LLVM.

Expand All @@ -79,7 +79,8 @@
- Blake2s
- Schnorr signature verification
- MerkleMembership
- Pedersen
- Pedersen Commitment
- Pedersen Hash
- HashToField

## Libraries
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/standard_library/black_box_fns.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Here is a list of the current black box functions that are supported by UltraPlo
- [SHA256](./cryptographic_primitives/hashes#sha256)
- [Schnorr signature verification](./cryptographic_primitives/schnorr)
- [Blake2s](./cryptographic_primitives/hashes#blake2s)
- [Pedersen](./cryptographic_primitives/hashes#pedersen)
- [Pedersen Hash](./cryptographic_primitives/hashes#pedersen_hash)
- [Pedersen Commitment](./cryptographic_primitives/hashes#pedersen_commitment)
- [HashToField128Security](./cryptographic_primitives/hashes#hash_to_field)
- [ECDSA signature verification](./cryptographic_primitives/ecdsa_sig_verification)
- [Fixed base scalar multiplication](./cryptographic_primitives/scalar)
Expand Down
27 changes: 24 additions & 3 deletions docs/docs/standard_library/cryptographic_primitives/00_hashes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,41 @@ fn main() {

<BlackBoxInfo />

## pedersen
## pedersen_hash

Given an array of Fields, returns the Pedersen hash.

```rust
fn pedersen(_input : [Field]) -> [Field; 2]
fn pedersen_hash(_input : [Field]) -> Field
```

example:

```rust
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::hash::pedersen(x);
let hash = std::hash::pedersen_hash(x);
}
```

<BlackBoxInfo />

<BlackBoxInfo />

## pedersen_commitment

Given an array of Fields, returns the Pedersen commitment.

```rust
fn pedersen_commitment(_input : [Field]) -> [Field; 2]
```

example:

```rust
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let commitment = std::hash::pedersen_commitment(x);
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard_library/merkle_trees.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

## compute_merkle_root

Returns the root of the tree from the provided leaf and its hash path, using a [Pedersen hash](cryptographic_primitives/00_hashes.mdx#pedersen).
Returns the root of the tree from the provided leaf and its hash path, using a [Pedersen hash](cryptographic_primitives/00_hashes.mdx#pedersen_hash).

```rust
fn compute_merkle_root(leaf : Field, index : Field, hash_path: [Field]) -> Field
Expand All @@ -39,13 +39,13 @@
*/
fn main(index: Field, priv_key: Field, secret: Field, note_hash_path: [Field; 3]) {

let pubkey = std::scalar_mul::fixed_base_embedded_curve(priv_key);

Check warning on line 42 in docs/docs/standard_library/merkle_trees.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (pubkey)
let pubkey_x = pubkey[0];

Check warning on line 43 in docs/docs/standard_library/merkle_trees.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (pubkey)

Check warning on line 43 in docs/docs/standard_library/merkle_trees.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (pubkey)
let pubkey_y = pubkey[1];

Check warning on line 44 in docs/docs/standard_library/merkle_trees.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (pubkey)

Check warning on line 44 in docs/docs/standard_library/merkle_trees.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (pubkey)
let note_commitment = std::hash::pedersen([pubkey_x, pubkey_y, secret]);

let root = std::merkle::compute_merkle_root(note_commitment[0], index, note_hash_path);
std::println(root);

Check warning on line 48 in docs/docs/standard_library/merkle_trees.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (println)
}
```

Expand Down
6 changes: 3 additions & 3 deletions noir_stdlib/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ pub fn sha256<N>(_input : [u8; N]) -> [u8; 32] {}
#[foreign(blake2s)]
pub fn blake2s<N>(_input : [u8; N]) -> [u8; 32] {}

pub fn pedersen<N>(input : [Field; N]) -> [Field; 2] {
pedersen_with_separator(input, 0)
pub fn pedersen_commitment<N>(input : [Field; N]) -> [Field; 2] {
pedersen_commitment_with_separator(input, 0)
}
kevaundray marked this conversation as resolved.
Show resolved Hide resolved

#[foreign(pedersen)]
pub fn pedersen_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_hash<N>(input : [Field; N]) -> Field {
pedersen_hash_with_separator(input, 0)
Expand Down
4 changes: 2 additions & 2 deletions noir_stdlib/src/merkle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub fn compute_merkle_root<N>(leaf: Field, index: Field, hash_path: [Field; N])
} else {
(current, hash_path[i])
};

current = crate::hash::pedersen([hash_left, hash_right])[0];
// TODO(Kev): This should be changed to use pedersen_hash
current = crate::hash::pedersen_commitment([hash_left, hash_right])[0];
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
};
current
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use dep::std;
fn main(x: Field) {
let bytes = x.to_be_bytes(32);

let hash = std::hash::pedersen([x]);
let hash = std::hash::pedersen_commitment([x]);
let _p1 = std::scalar_mul::fixed_base_embedded_curve(x, 0);
}
4 changes: 2 additions & 2 deletions tooling/nargo_cli/tests/execution_success/eddsa/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ fn main(msg: pub Field, _priv_key_a: Field, _priv_key_b: Field) {

// Manually computed as fields can't use modulo. Importantantly the commitment is within
// the subgroup order. Note that choice of hash is flexible for this step.
// let r_a = hash::pedersen([_priv_key_a, msg])[0] % bjj.suborder; // modulus computed manually
// let r_a = hash::pedersen_commitment([_priv_key_a, msg])[0] % bjj.suborder; // modulus computed manually
let r_a = 1414770703199880747815475415092878800081323795074043628810774576767372531818;
// let r_b = hash::pedersen([_priv_key_b, msg])[0] % bjj.suborder; // modulus computed manually
// let r_b = hash::pedersen_commitment([_priv_key_b, msg])[0] % bjj.suborder; // modulus computed manually
let r_b = 571799555715456644614141527517766533395606396271089506978608487688924659618;

let r8_a = bjj.curve.mul(r_a, bjj.base8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod import;
use crate::import::hello;

fn main(x : Field, y : Field) {
let _k = dep::std::hash::pedersen([x]);
let _k = dep::std::hash::pedersen_commitment([x]);
let _l = hello(x);

assert(x != import::hello(y));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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([x, y]);
let res = std::hash::pedersen_commitment([x, y]);
assert(res[0] == out_x);
assert(res[1] == out_y);
let res_hash = std::hash::pedersen_hash_with_separator([x, y], 0);
Expand All @@ -15,7 +15,7 @@ fn main(x: Field, y: Field, salt: Field, out_x: Field, out_y: Field, out_hash: F
state = state * 8 + raw_data[i];
}
state += salt;
let hash = std::hash::pedersen([state]);
assert(std::hash::pedersen([43])[0] == hash[0]);
let hash = std::hash::pedersen_commitment([state]);
assert(std::hash::pedersen_commitment([43])[0] == hash[0]);
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ fn main(
let pubkey_y = pubkey[1];

// Compute input note commitment
let note_commitment = std::hash::pedersen([pubkey_x, pubkey_y]);
let note_commitment = std::hash::pedersen_commitment([pubkey_x, pubkey_y]);

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

// Compute output note nullifier
let receiver_note_commitment = std::hash::pedersen([to_pubkey_x, to_pubkey_y]);
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));
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/tests/execution_success/strings/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main(message : pub str<11>, y : Field, hex_as_string : str<4>, hex_as_field :
std::println(bad_message);
assert(message != bad_message);

let hash = std::hash::pedersen([x]);
let hash = std::hash::pedersen_commitment([x]);
std::println(hash);

assert(hex_as_string == "0x41");
Expand All @@ -48,7 +48,7 @@ fn test_prints_array() {

std::println(array);

let hash = std::hash::pedersen(array);
let hash = std::hash::pedersen_commitment(array);
std::println(hash);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fn test_with_extra_space() {
// The assert message has a space
#[test(should_fail_with = "Not equal")]
fn test_runtime_mismatch() {
assert_eq(dep::std::hash::pedersen([27])[0], 0, "Not equal ");
assert_eq(dep::std::hash::pedersen_commitment([27])[0], 0, "Not equal ");
}
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([27])[0], 0, "Not equal");
assert_eq(dep::std::hash::pedersen_commitment([27])[0], 0, "Not equal");
}

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