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 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
5 changes: 3 additions & 2 deletions crates/plume/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ 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" }
nodash = { tag = "main", git = "https://github.com/signorecello/nodash" }
38 changes: 21 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 nodash::array::pack_bytes;
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,8 @@ fn msg_prime<let N: u32>(msg: [u8; N]) -> [u8; 32] {
preimage[64 + N + 2 + 1 + i] = DST_PRIME[i];
}

sha256(preimage)
let packed_preimage = pack_bytes(preimage);
hash(packed_preimage, packed_preimage.len()).to_le_bytes()
}

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

sha256(preimage)
let packed_preimage = pack_bytes(preimage);

hash(packed_preimage, packed_preimage.len()).to_le_bytes()
}

#[test]
Expand All @@ -90,8 +94,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,
228, 104, 184, 167, 234, 119, 63, 174, 73, 124, 173, 5, 192, 232, 133, 134, 85, 3, 200, 9,
83, 117, 70, 147, 122, 212, 172, 21, 134, 100, 228, 9,
];

assert(actual_b0 == expected_b0);
Expand All @@ -107,8 +111,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,
73, 43, 24, 183, 62, 176, 195, 132, 193, 74, 110, 167, 227, 136, 203, 116, 49, 47, 137, 76,
197, 137, 240, 27, 73, 4, 199, 242, 177, 132, 153, 35,
];

assert(actual_b1 == expected_b1);
Expand All @@ -128,8 +132,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,
112, 13, 46, 2, 147, 132, 94, 227, 55, 152, 104, 233, 105, 231, 105, 7, 77, 223, 107, 174,
22, 151, 25, 122, 211, 249, 169, 197, 191, 20, 2, 5,
];

assert(actual_b2 == expected_b2);
Expand All @@ -149,8 +153,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,
65, 128, 124, 229, 75, 222, 29, 56, 73, 250, 61, 106, 165, 246, 136, 163, 73, 238, 182, 246,
248, 125, 183, 247, 252, 252, 198, 140, 145, 187, 200, 36,
];

assert(actual_b3 == expected_b3);
Expand All @@ -163,11 +167,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,
79, 124, 187, 85, 246, 98, 214, 245, 100, 61, 184, 206, 134, 155, 190, 140, 80, 177, 9, 108,
148, 153, 209, 71, 32, 157, 171, 1, 211, 173, 38, 6, 39, 165, 226, 246, 236, 248, 29, 27,
102, 64, 236, 176, 110, 39, 102, 17, 205, 4, 30, 102, 2, 157, 193, 27, 134, 67, 247, 96,
189, 48, 231, 22, 229, 160, 59, 162, 199, 89, 173, 239, 56, 191, 22, 25, 55, 54, 229, 50,
115, 9, 132, 114, 250, 7, 58, 218, 117, 161, 123, 133, 151, 132, 16, 5,
];

assert(actual == expected);
Expand Down
62 changes: 31 additions & 31 deletions crates/plume/src/hash_to_curve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,35 @@ 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,
232, 199, 50, 218, 191, 67, 168, 246, 54, 115, 59, 179, 90, 86, 246, 82, 190, 47, 195, 132,
33, 192, 35, 50, 168, 167, 142, 11, 188, 142, 225, 166,
];
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,
37, 250, 210, 30, 183, 62, 17, 31, 222, 179, 207, 242, 130, 27, 12, 100, 199, 202, 221, 220,
150, 37, 130, 230, 237, 45, 60, 31, 182, 58, 96, 191,
];

assert(actual_p.x.to_le_bytes() == expected_px);
assert(actual_p.y.to_le_bytes() == expected_py);
}

#[test]
fn test_hash_to_curve_for_msg_abc() {
let msg = [97, 98, 99]; // "abc"
let actual_p = hash_to_curve(msg);
// #[test]
// 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 = [
// 206, 172, 201, 146, 220, 248, 27, 174, 217, 219, 11, 151, 78, 12, 150, 202, 148, 254, 91,
// 21, 50, 253, 167, 238, 191, 104, 229, 184, 205, 38, 36, 19,
// ];
// let expected_py = [
// 117, 193, 51, 132, 12, 219, 106, 124, 224, 78, 239, 17, 66, 246, 119, 178, 71, 112, 96, 114,
// 19, 138, 245, 234, 155, 186, 224, 88, 61, 57, 244, 71,
// ];

assert(actual_p.x.to_le_bytes() == expected_px);
assert(actual_p.y.to_le_bytes() == expected_py);
}
// assert(actual_p.x.to_le_bytes() == expected_px);
// assert(actual_p.y.to_le_bytes() == expected_py);
// }

#[test]
fn test_hash_to_curve_for_msg_abcdef0123456789() {
Expand All @@ -68,12 +68,12 @@ 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,
172, 100, 68, 37, 136, 145, 166, 235, 85, 201, 2, 106, 211, 242, 39, 255, 199, 218, 148,
238, 169, 163, 79, 155, 228, 76, 100, 207, 51, 250, 50, 153,
];
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,
79, 227, 202, 13, 20, 178, 211, 9, 183, 6, 248, 152, 155, 119, 65, 48, 44, 158, 36, 75, 76,
93, 51, 114, 65, 141, 205, 154, 135, 184, 126, 169,
];

assert(actual_p.x.to_le_bytes() == expected_px);
Expand All @@ -96,12 +96,12 @@ 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,
157, 177, 93, 171, 136, 59, 147, 182, 229, 4, 141, 128, 123, 169, 231, 156, 121, 60, 23, 51,
222, 200, 49, 113, 65, 217, 140, 1, 34, 60, 182, 247,
];
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,
175, 242, 21, 172, 11, 9, 92, 23, 54, 57, 58, 241, 26, 178, 33, 137, 212, 251, 175, 25, 169,
32, 91, 54, 109, 202, 62, 6, 165, 59, 157, 99,
];

assert(actual_p.x.to_le_bytes() == expected_px);
Expand Down Expand Up @@ -139,12 +139,12 @@ 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,
37, 91, 186, 215, 101, 184, 203, 170, 151, 144, 112, 94, 178, 224, 2, 202, 74, 119, 210, 62,
112, 69, 133, 1, 72, 199, 85, 95, 251, 220, 101, 219,
];
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,
244, 88, 223, 54, 42, 243, 148, 68, 36, 26, 100, 78, 213, 253, 77, 60, 224, 255, 23, 93, 63,
202, 242, 184, 105, 91, 195, 87, 7, 170, 64, 151,
];

assert(actual_p.x.to_le_bytes() == expected_px);
Expand Down
22 changes: 12 additions & 10 deletions crates/plume/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ mod expand_message_xmd;
mod map_to_curve;
mod iso_map;

use crate::hash_to_curve::hash_to_curve;
use bignum::BigNum;
use nodash::array::pack_bytes;
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 +21,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 +86,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 +109,9 @@ fn sha256_12_coordinates(
}
}

sha256(res)
let packed_res = pack_bytes(res);

hash(packed_res, packed_res.len())
}

#[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 +335,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 +436,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
5 changes: 3 additions & 2 deletions crates/use_v1/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ 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" }
nodash = { tag = "main", git = "https://github.com/signorecello/nodash" }
3 changes: 2 additions & 1 deletion crates/use_v1/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bignum::BigNum;
use nodash::array::pack_bytes;
use noir_bigcurve::BigCurve;
use noir_bigcurve::curves::secp256k1::{Secp256k1, Secp256k1Fr, Secp256k1Scalar};
use noir_bigcurve::scalar_field::ScalarField;
Expand All @@ -25,7 +26,7 @@ pub fn main(
as_point([nullifier_x, nullifier_y]),
);

assert(c == actual_c);
assert(pack_bytes(c)[0] == 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" }