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: plume with poseidon2 hashes #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions crates/plume/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ description = "PLUME implementation in Noir."
compiler_version = ">=0.36.0"

[dependencies]
bignum = {tag = "v0.4.1", git = "https://github.com/noir-lang/noir-bignum"}
noir_bigcurve = {tag = "v0.5.0", git = "https://github.com/noir-lang/noir_bigcurve"}
bignum = { tag = "v0.4.2", git = "https://github.com/noir-lang/noir-bignum" }
noir_bigcurve = { tag = "zpedro/v0.4.2", git = "https://github.com/noir-lang/noir_bigcurve" }
35 changes: 18 additions & 17 deletions crates/plume/src/expand_message_xmd.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#name-expand_message_xmd
*/

use std::hash::sha256;
use crate::utils::to_field;
use std::hash::poseidon2::Poseidon2::hash;

comptime global DST_PRIME: [u8; 50] = [
81, 85, 85, 88, 45, 86, 48, 49, 45, 67, 83, 48, 50, 45, 119, 105, 116, 104, 45, 115, 101, 99,
Expand Down Expand Up @@ -35,7 +36,7 @@ pub fn expand_message_xmd<let N: u32>(msg: [u8; N]) -> [u8; 96] {
}

fn msg_prime<let N: u32>(msg: [u8; N]) -> [u8; 32] {
let mut preimage = [0 as u8; 64 + N + 2 + 1 + 50];
let mut preimage = [0; 64 + N + 2 + 1 + 50];

for i in 0..N {
preimage[64 + i] = msg[i];
Expand All @@ -52,7 +53,7 @@ fn msg_prime<let N: u32>(msg: [u8; N]) -> [u8; 32] {
preimage[64 + N + 2 + 1 + i] = DST_PRIME[i];
}

sha256(preimage)
hash([to_field(preimage)], preimage.len()).to_le_bytes()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not quite part of the PLUME, but rather of the hash-to-curve algorithm, it might be not conforming to requirements given in spec. Though, I am not particularly sure

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if I hash the whole thing by breaking it into limbs and hashing the limbs, it would conform to the spec. But I am not a cryptographer, so I may be wrong

}

fn hash_bi(b_idx: u8, b0: [u8; 32], b1: [u8; 32]) -> [u8; 32] {
Expand Down Expand Up @@ -80,7 +81,7 @@ fn hash_b(b_idx: u8, b: [u8; 32]) -> [u8; 32] {
preimage[32 + 1 + i] = DST_PRIME[i];
}

sha256(preimage)
hash([to_field(preimage)], 32 + 1 + 50).to_le_bytes()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about conforming spec as in msg_prime

}

#[test]
Expand All @@ -90,8 +91,8 @@ fn test_b0() {
let actual_b0 = msg_prime(msg);

let expected_b0 = [
99, 4, 75, 36, 124, 254, 65, 234, 207, 65, 212, 122, 206, 186, 87, 48, 157, 28, 243, 255,
59, 178, 30, 40, 136, 85, 202, 99, 135, 177, 127, 169,
244, 228, 186, 229, 167, 240, 72, 35, 72, 201, 30, 135, 116, 200, 92, 252, 236, 82, 101,
168, 168, 57, 38, 207, 131, 64, 78, 122, 157, 1, 18, 24,
];

assert(actual_b0 == expected_b0);
Expand All @@ -107,8 +108,8 @@ fn test_b1() {
let actual_b1 = hash_b(1, b0);

let expected_b1 = [
232, 52, 124, 173, 72, 171, 78, 49, 157, 123, 39, 85, 32, 234, 129, 207, 18, 138, 171, 93,
54, 121, 161, 247, 96, 30, 59, 222, 172, 154, 81, 208,
89, 48, 72, 196, 27, 115, 11, 241, 49, 124, 218, 4, 173, 23, 162, 23, 197, 100, 206, 65,
138, 54, 118, 91, 227, 59, 205, 92, 169, 126, 183, 19,
];

assert(actual_b1 == expected_b1);
Expand All @@ -128,8 +129,8 @@ fn test_b2() {
let actual_b2 = hash_bi(2, b0, b1);

let expected_b2 = [
197, 77, 255, 208, 84, 39, 78, 219, 36, 136, 85, 230, 17, 144, 196, 98, 167, 187, 97, 236,
186, 142, 64, 10, 154, 118, 213, 174, 1, 78, 135, 255,
133, 159, 203, 25, 88, 52, 139, 16, 136, 49, 25, 62, 168, 65, 240, 66, 165, 74, 249, 195,
42, 193, 228, 164, 33, 237, 94, 227, 186, 125, 68, 11,
];

assert(actual_b2 == expected_b2);
Expand All @@ -149,8 +150,8 @@ fn test_b3() {
let actual_b3 = hash_bi(3, b0, b2);

let expected_b3 = [
88, 151, 182, 93, 163, 181, 149, 168, 19, 208, 253, 203, 206, 13, 49, 111, 118, 108, 238,
235, 111, 248, 76, 222, 204, 214, 155, 224, 231, 179, 153, 209,
145, 148, 34, 154, 234, 103, 0, 76, 186, 111, 76, 140, 236, 216, 242, 103, 15, 219, 30, 210,
242, 150, 26, 87, 138, 33, 62, 148, 77, 121, 194, 43,
];

assert(actual_b3 == expected_b3);
Expand All @@ -163,11 +164,11 @@ fn tests_expand_message_xmd() {
let actual = expand_message_xmd(msg);

let expected = [
232, 52, 124, 173, 72, 171, 78, 49, 157, 123, 39, 85, 32, 234, 129, 207, 18, 138, 171, 93,
54, 121, 161, 247, 96, 30, 59, 222, 172, 154, 81, 208, 197, 77, 255, 208, 84, 39, 78, 219,
36, 136, 85, 230, 17, 144, 196, 98, 167, 187, 97, 236, 186, 142, 64, 10, 154, 118, 213, 174,
1, 78, 135, 255, 88, 151, 182, 93, 163, 181, 149, 168, 19, 208, 253, 203, 206, 13, 49, 111,
118, 108, 238, 235, 111, 248, 76, 222, 204, 214, 155, 224, 231, 179, 153, 209,
103, 141, 171, 70, 97, 109, 246, 57, 155, 74, 200, 143, 190, 146, 248, 216, 99, 128, 203,
147, 154, 97, 9, 16, 20, 126, 227, 220, 17, 82, 92, 10, 135, 163, 156, 89, 40, 209, 79, 185,
200, 163, 90, 58, 204, 178, 133, 126, 51, 174, 97, 122, 161, 186, 14, 10, 117, 173, 249,
129, 22, 15, 216, 39, 47, 75, 79, 183, 145, 85, 2, 79, 86, 203, 240, 113, 245, 201, 57, 46,
215, 177, 234, 222, 89, 150, 143, 80, 214, 21, 193, 27, 60, 38, 84, 33,
];

assert(actual == expected);
Expand Down
50 changes: 10 additions & 40 deletions crates/plume/src/hash_to_curve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@ fn test_hash_to_curve_for_empty_msg() {

let actual_p = hash_to_curve(msg);

let expected_px = [
70, 19, 235, 223, 45, 133, 181, 183, 169, 171, 30, 132, 139, 196, 121, 20, 134, 115, 109,
190, 241, 174, 235, 23, 230, 174, 145, 226, 144, 226, 202, 193,
];
let expected_py = [
103, 16, 229, 96, 169, 6, 196, 100, 27, 169, 195, 150, 201, 133, 89, 225, 109, 175, 148, 42,
2, 139, 240, 38, 97, 17, 174, 7, 142, 103, 250, 100,
];
let expected_px = [233, 215, 131, 22, 103, 248, 192, 129, 247, 34, 221, 149, 101, 63, 192, 23, 130, 54, 49, 248, 190, 109, 20, 186, 1, 109, 170, 98, 42, 97, 181, 32];
let expected_py = [138, 99, 209, 70, 180, 154, 164, 92, 238, 130, 149, 179, 153, 204, 201, 160, 155, 88, 12, 232, 163, 174, 226, 100, 73, 95, 96, 72, 39, 169, 97, 94];

assert(actual_p.x.to_le_bytes() == expected_px);
assert(actual_p.y.to_le_bytes() == expected_py);
Expand All @@ -48,14 +42,8 @@ fn test_hash_to_curve_for_msg_abc() {
let msg = [97, 98, 99]; // "abc"
let actual_p = hash_to_curve(msg);

let expected_px = [
75, 44, 203, 179, 31, 241, 159, 189, 96, 87, 32, 249, 249, 236, 182, 114, 238, 108, 12, 18,
147, 34, 81, 107, 41, 219, 66, 171, 30, 224, 119, 51,
];
let expected_py = [
246, 113, 131, 156, 123, 108, 124, 72, 141, 104, 148, 111, 17, 182, 15, 144, 224, 190, 177,
1, 42, 56, 77, 4, 209, 235, 239, 51, 15, 137, 149, 127,
];
let expected_px = [200, 167, 7, 215, 99, 12, 174, 165, 113, 245, 225, 17, 158, 254, 251, 117, 165, 193, 66, 177, 221, 10, 6, 246, 8, 196, 65, 64, 112, 168, 204, 82];
let expected_py = [52, 119, 250, 33, 162, 65, 185, 104, 178, 251, 186, 220, 142, 121, 152, 101, 4, 65, 143, 200, 203, 5, 149, 53, 0, 239, 156, 206, 115, 126, 64, 82];

assert(actual_p.x.to_le_bytes() == expected_px);
assert(actual_p.y.to_le_bytes() == expected_py);
Expand All @@ -67,14 +55,8 @@ fn test_hash_to_curve_for_msg_abcdef0123456789() {

let actual_p = hash_to_curve(msg);

let expected_px = [
58, 14, 75, 103, 208, 36, 179, 72, 88, 243, 211, 132, 203, 165, 131, 7, 169, 10, 38, 55, 1,
167, 228, 8, 254, 241, 147, 242, 131, 64, 197, 186,
];
let expected_py = [
40, 216, 88, 151, 161, 188, 39, 238, 189, 152, 179, 86, 231, 220, 106, 23, 64, 156, 56, 244,
252, 96, 139, 80, 196, 195, 212, 133, 96, 71, 54, 68,
];
let expected_px = [27, 237, 43, 181, 233, 108, 113, 207, 191, 226, 130, 203, 134, 10, 51, 194, 144, 130, 225, 110, 210, 13, 103, 196, 133, 203, 19, 244, 37, 73, 48, 87];
let expected_py = [113, 154, 197, 61, 183, 45, 39, 110, 234, 79, 106, 203, 38, 85, 205, 209, 41, 43, 20, 1, 202, 138, 252, 222, 200, 99, 100, 127, 88, 36, 124, 222];

assert(actual_p.x.to_le_bytes() == expected_px);
assert(actual_p.y.to_le_bytes() == expected_py);
Expand All @@ -95,14 +77,8 @@ fn test_hash_to_curve_for_msg_q128() {

let actual_p = hash_to_curve(msg);

let expected_px = [
233, 144, 24, 228, 2, 177, 114, 127, 42, 245, 171, 155, 131, 83, 184, 239, 29, 136, 30, 31,
2, 47, 86, 170, 55, 58, 51, 133, 199, 123, 22, 226,
];
let expected_py = [
115, 216, 133, 102, 30, 106, 73, 184, 159, 150, 62, 165, 198, 251, 25, 55, 118, 100, 213,
124, 54, 79, 237, 255, 103, 88, 195, 92, 217, 29, 64, 242,
];
let expected_px = [248, 87, 92, 202, 45, 60, 228, 209, 225, 47, 136, 219, 120, 12, 12, 213, 26, 18, 250, 110, 12, 5, 227, 137, 35, 11, 117, 228, 194, 163, 6, 194];
let expected_py = [0, 46, 224, 173, 231, 171, 158, 28, 43, 157, 236, 134, 117, 21, 102, 60, 207, 169, 12, 34, 49, 29, 234, 240, 136, 169, 87, 183, 118, 181, 148, 77];

assert(actual_p.x.to_le_bytes() == expected_px);
assert(actual_p.y.to_le_bytes() == expected_py);
Expand Down Expand Up @@ -138,14 +114,8 @@ fn test_hash_to_curve_for_msg_a512() {

let actual_p = hash_to_curve(msg);

let expected_px = [
152, 201, 183, 140, 31, 140, 253, 230, 228, 82, 129, 35, 56, 173, 213, 190, 213, 229, 126,
10, 10, 138, 232, 71, 182, 185, 240, 170, 90, 211, 200, 227,
];
let expected_py = [
166, 58, 130, 229, 142, 136, 181, 36, 48, 128, 227, 199, 37, 71, 12, 47, 204, 33, 34, 38,
78, 210, 169, 86, 47, 241, 27, 24, 182, 238, 70, 132,
];
let expected_px = [27, 3, 250, 41, 101, 101, 190, 55, 196, 31, 239, 250, 66, 140, 239, 9, 80, 41, 174, 230, 15, 251, 245, 110, 247, 121, 160, 84, 234, 101, 218, 80];
let expected_py = [16, 78, 50, 6, 62, 254, 196, 254, 20, 157, 141, 156, 97, 34, 166, 239, 68, 24, 52, 52, 173, 122, 63, 126, 230, 205, 39, 56, 141, 8, 238, 198];

assert(actual_p.x.to_le_bytes() == expected_px);
assert(actual_p.y.to_le_bytes() == expected_py);
Expand Down
21 changes: 11 additions & 10 deletions crates/plume/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ mod hash_to_field;
mod expand_message_xmd;
mod map_to_curve;
mod iso_map;
mod utils;

use crate::hash_to_curve::hash_to_curve;
use crate::utils::to_field;
use bignum::BigNum;
use noir_bigcurve::BigCurve;
use noir_bigcurve::curves::secp256k1::{Secp256k1, Secp256k1Fr, Secp256k1Scalar};
use noir_bigcurve::scalar_field::ScalarField;
use std::hash::sha256;

use crate::hash_to_curve::hash_to_curve;
use std::hash::poseidon2::Poseidon2::hash;

comptime global G: Secp256k1 = BigCurve::one();
comptime global COMPRESSED_SIZE_BYTES: u32 = 33;
Expand All @@ -21,11 +22,11 @@ pub fn plume_v1<let N: u32>(
s: Secp256k1Scalar,
pk: Secp256k1,
nullifier: Secp256k1,
) -> [u8; 32] {
) -> Field {
let (r_point, hashed_to_curve_r, hashed_to_curve) =
check_ec_equations(msg, c, s, pk, nullifier);

sha256_12_coordinates(pk, hashed_to_curve, nullifier, r_point, hashed_to_curve_r)
poseidon_12_coordinates(pk, hashed_to_curve, nullifier, r_point, hashed_to_curve_r)
}

pub fn plume_v2<let N: u32>(
Expand Down Expand Up @@ -86,13 +87,13 @@ fn compress_ec_point(p: Secp256k1) -> [u8; COMPRESSED_SIZE_BYTES] {
compressed
}

fn sha256_12_coordinates(
fn poseidon_12_coordinates(
pk: Secp256k1,
h: Secp256k1,
nullifier: Secp256k1,
gr: Secp256k1,
hr: Secp256k1,
) -> [u8; 32] {
) -> Field {
let mut compressed = [[0 as u8; COMPRESSED_SIZE_BYTES]; 6];
compressed[0] = compress_ec_point(G);
compressed[1] = compress_ec_point(pk);
Expand All @@ -109,7 +110,7 @@ fn sha256_12_coordinates(
}
}

sha256(res)
hash([to_field(res)], res.len())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like we are missing on preimage data when doing conversion to field; res being 198 bytes of information is narrowed down to the prime order of underlying field of appr. 8 bytes for bb prior to hashing. Additionally, here is passed only one field element in array, but res.len() will give 198.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so in this case do you suggest I break it to limbs and pass it as an array maybe?

}

#[test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding all commented-out tests here: they need to be updated and passing nargo test, if uncommented. Currently, there are few data and compilation issues. Sorry for having to deal with commented code, I guess if noir'll support something akin to rust's #[ignore] macro in future, I'll update these

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, feel free to update this branch too. I'm just an Aztec team member on my time off learning a bit about PLUME stuff 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can you grant me write access to your fork?

Expand Down Expand Up @@ -333,7 +334,7 @@ fn test_sha_256_12_coordinates() {
let pk = G.mul(sk);
let nullifier = hashed_to_curve.mul(sk);

let actual = sha256_12_coordinates(pk, hashed_to_curve, nullifier, r_point, hashed_to_curve_r);
let actual = poseidon_12_coordinates(pk, hashed_to_curve, nullifier, r_point, hashed_to_curve_r);

let expected = [
198, 167, 252, 44, 146, 109, 219, 175, 32, 115, 26, 71, 159, 182, 86, 111, 45, 170, 85, 20, 186, 174, 82, 35, 254, 59, 50, 237, 188, 232, 50, 84
Expand Down Expand Up @@ -434,7 +435,7 @@ fn test_plume_v2() {
res[i*COMPRESSED_SIZE_BYTES + j] = compressed[i][j];
}
}
let actual = sha256(res);
let actual = hash(res);

assert(expected == actual);
}
Expand Down
13 changes: 13 additions & 0 deletions crates/plume/src/utils.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub fn to_field<let N: u32>(bytes: [u8; N]) -> Field {
let mut as_field = 0;
let mut offset = 1;
for i in 0..N {
let mut index = i;
index = N - i - 1;
as_field += (bytes[index] as Field) * offset;
offset *= 256;
}
std::as_witness(as_field);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why need this line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This little guy shaves off 200k constraints out of the whole thing 😄 a team member found a bug in the compiler that was causing a huge blowout. Using as_witness forces the compiler to assign intermediate values, which makes it behave as it should

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, nice trick


as_field
}
4 changes: 2 additions & 2 deletions crates/use_v1/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ compiler_version = ">=0.36.0"

[dependencies]
plume = { path = "../plume" }
bignum = {tag = "v0.4.1", git = "https://github.com/noir-lang/noir-bignum"}
noir_bigcurve = {tag = "v0.5.0", git = "https://github.com/noir-lang/noir_bigcurve"}
bignum = { tag = "v0.4.2", git = "https://github.com/noir-lang/noir-bignum" }
noir_bigcurve = { tag = "zpedro/v0.4.2", git = "https://github.com/noir-lang/noir_bigcurve" }
4 changes: 2 additions & 2 deletions crates/use_v1/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use noir_bigcurve::BigCurve;
use noir_bigcurve::curves::secp256k1::{Secp256k1, Secp256k1Fr, Secp256k1Scalar};
use noir_bigcurve::scalar_field::ScalarField;

use plume::plume_v1;
use plume::{plume_v1, to_field};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess here import shall be use plume::utils::to_field to resolve is private and not visible from the current module warning.


global MSG_LEN: u32 = 32;

Expand All @@ -25,7 +25,7 @@ pub fn main(
as_point([nullifier_x, nullifier_y]),
);

assert(c == actual_c);
assert(to_field(c) == actual_c);
}

fn as_scalar(bytes: [u8; 32]) -> Secp256k1Scalar {
Expand Down
4 changes: 2 additions & 2 deletions crates/use_v2/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ compiler_version = ">=0.36.0"

[dependencies]
plume = { path = "../plume" }
bignum = {tag = "v0.4.1", git = "https://github.com/noir-lang/noir-bignum"}
noir_bigcurve = {tag = "v0.5.0", git = "https://github.com/noir-lang/noir_bigcurve"}
bignum = { tag = "v0.4.2", git = "https://github.com/noir-lang/noir-bignum" }
noir_bigcurve = { tag = "zpedro/v0.4.2", git = "https://github.com/noir-lang/noir_bigcurve" }
Loading