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

feat: standardize pedersen functions to return EmbeddedCurvePoint #5190

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 4 additions & 8 deletions noir_stdlib/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod poseidon2;
use crate::default::Default;
use crate::uint128::U128;
use crate::sha256::{digest, sha256_var};
use crate::embedded_curve_ops::EmbeddedCurvePoint;

#[foreign(sha256)]
// docs:start:sha256
Expand All @@ -25,22 +26,17 @@ pub fn blake3<N>(input: [u8; N]) -> [u8; 32]
{}

// docs:start:pedersen_commitment
struct PedersenPoint {
x : Field,
y : Field,
}

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

#[foreign(pedersen_commitment)]
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 {
pub fn pedersen_commitment_with_separator<N>(input: [Field; N], separator: u32) -> EmbeddedCurvePoint {
let values = __pedersen_commitment_with_separator(input, separator);
PedersenPoint { x: values[0], y: values[1] }
EmbeddedCurvePoint { x: values[0], y: values[1], is_infinite: false }
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
}

// docs:start:pedersen_hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ y = "1"
[expected_commitment]
x = "0x054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402"
y = "0x209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126"
is_infinite = false
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// docs:start:pedersen-commitment
use dep::std;

fn main(x: Field, y: Field, expected_commitment: std::hash::PedersenPoint) {
fn main(x: Field, y: Field, expected_commitment: std::embedded_curve_ops::EmbeddedCurvePoint) {
let commitment = std::hash::pedersen_commitment([x, y]);
assert_eq(commitment.x, expected_commitment.x);
assert_eq(commitment.y, expected_commitment.y);
Expand Down
2 changes: 1 addition & 1 deletion test_programs/execution_success/schnorr/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main(
let pub_key = embedded_curve_ops::EmbeddedCurvePoint { x: pub_key_x, y: pub_key_y, is_infinite: false };
let valid_signature = verify_signature_noir(pub_key, signature, message2);
assert(valid_signature);
assert_valid_signature(pub_key,signature,message2);
assert_valid_signature(pub_key, signature, message2);
}

// TODO: to put in the stdlib once we have numeric generics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ fn test_should_fail_without_runtime_match() {
}

struct InvalidPointError {
point: dep::std::hash::PedersenPoint,
point: dep::std::embedded_curve_ops::EmbeddedCurvePoint,
}

#[test(should_fail_with = "InvalidPointError { point: PedersenPoint { x: 0x1cea3a116d01eb94d568ef04c3dfbc39f96f015ed801ab8958e360d406503ce0, y: 0x2721b237df87234acc36a238b8f231a3d31d18fe3845fff4cc59f0bd873818f8 } }")]
#[test(should_fail_with = "InvalidPointError { point: EmbeddedCurvePoint { x: 0x1cea3a116d01eb94d568ef04c3dfbc39f96f015ed801ab8958e360d406503ce0, y: 0x2721b237df87234acc36a238b8f231a3d31d18fe3845fff4cc59f0bd873818f8, is_infinite: false } }")]
fn test_should_fail_with_struct() {
let hash = dep::std::hash::pedersen_commitment([27]);
assert_eq(hash.x, 0, InvalidPointError { point: hash });
Expand All @@ -35,7 +35,7 @@ fn test_should_fail_with_basic_type_fmt_string() {
assert_eq(a, b, f"A: {a} is not 1!");
}

#[test(should_fail_with = "Invalid hash: PedersenPoint { x: 0x1cea3a116d01eb94d568ef04c3dfbc39f96f015ed801ab8958e360d406503ce0, y: 0x2721b237df87234acc36a238b8f231a3d31d18fe3845fff4cc59f0bd873818f8 }")]
#[test(should_fail_with = "Invalid hash: EmbeddedCurvePoint { x: 0x1cea3a116d01eb94d568ef04c3dfbc39f96f015ed801ab8958e360d406503ce0, y: 0x2721b237df87234acc36a238b8f231a3d31d18fe3845fff4cc59f0bd873818f8, is_infinite: false }")]
fn test_should_fail_with_struct_fmt_string() {
let hash = dep::std::hash::pedersen_commitment([27]);
assert_eq(hash.x, 0, f"Invalid hash: {hash}");
Expand Down Expand Up @@ -63,7 +63,7 @@ unconstrained fn unconstrained_test_should_fail_without_runtime_match() {
assert_eq(dep::std::hash::pedersen_commitment([27]).x, 0);
}

#[test(should_fail_with = "InvalidPointError { point: PedersenPoint { x: 0x1cea3a116d01eb94d568ef04c3dfbc39f96f015ed801ab8958e360d406503ce0, y: 0x2721b237df87234acc36a238b8f231a3d31d18fe3845fff4cc59f0bd873818f8 } }")]
#[test(should_fail_with = "InvalidPointError { point: EmbeddedCurvePoint { x: 0x1cea3a116d01eb94d568ef04c3dfbc39f96f015ed801ab8958e360d406503ce0, y: 0x2721b237df87234acc36a238b8f231a3d31d18fe3845fff4cc59f0bd873818f8, is_infinite: false } }")]
unconstrained fn unconstrained_test_should_fail_with_struct() {
let hash = dep::std::hash::pedersen_commitment([27]);
assert_eq(hash.x, 0, InvalidPointError { point: hash });
Expand All @@ -76,7 +76,7 @@ unconstrained fn unconstrained_test_should_fail_with_basic_type_fmt_string() {
assert_eq(a, b, f"A: {a} is not 1!");
}

#[test(should_fail_with = "Invalid hash: PedersenPoint { x: 0x1cea3a116d01eb94d568ef04c3dfbc39f96f015ed801ab8958e360d406503ce0, y: 0x2721b237df87234acc36a238b8f231a3d31d18fe3845fff4cc59f0bd873818f8 }")]
#[test(should_fail_with = "Invalid hash: EmbeddedCurvePoint { x: 0x1cea3a116d01eb94d568ef04c3dfbc39f96f015ed801ab8958e360d406503ce0, y: 0x2721b237df87234acc36a238b8f231a3d31d18fe3845fff4cc59f0bd873818f8, is_infinite: false }")]
unconstrained fn unconstrained_test_should_fail_with_struct_fmt_string() {
let hash = dep::std::hash::pedersen_commitment([27]);
assert_eq(hash.x, 0, f"Invalid hash: {hash}");
Expand Down
Loading