-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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]; | ||
|
@@ -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() | ||
} | ||
|
||
fn hash_bi(b_idx: u8, b0: [u8; 32], b1: [u8; 32]) -> [u8; 32] { | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same about conforming spec as in |
||
} | ||
|
||
#[test] | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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>( | ||
|
@@ -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); | ||
|
@@ -109,7 +110,7 @@ fn sha256_12_coordinates( | |
} | ||
} | ||
|
||
sha256(res) | ||
hash([to_field(res)], res.len()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, can you grant me write access to your fork? |
||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
|
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, why need this line? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow, nice trick |
||
|
||
as_field | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess here import shall be |
||
|
||
global MSG_LEN: u32 = 32; | ||
|
||
|
@@ -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 { | ||
|
There was a problem hiding this comment.
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 sureThere was a problem hiding this comment.
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