Skip to content

Commit

Permalink
chore!: change stdlib function pedersen to pedersen_commitment (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Oct 29, 2023
1 parent 153cb83 commit 964b777
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 30 deletions.
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 @@ -79,7 +79,8 @@ ACIR Supported OPCODES:
- 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 @@ keywords:

## 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 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)
}

#[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];
};
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);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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_with_separator([x, y], 0);
let res = std::hash::pedersen_commitment_with_separator([x, y], 0);
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 @@ unconstrained fn main(x: Field, y: Field, salt: Field, out_x: Field, out_y: Fiel
state = state * 8 + raw_data[i];
}
state += salt;
let hash = std::hash::pedersen_with_separator([state], 0);
assert(std::hash::pedersen_with_separator([43], 0)[0] == hash[0]);
let hash = std::hash::pedersen_commitment_with_separator([state], 0);
assert(std::hash::pedersen_commitment_with_separator([43], 0)[0] == hash[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);
}

0 comments on commit 964b777

Please sign in to comment.