Skip to content

feat: plume with poseidon2 hashes #8

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

Open
wants to merge 4 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 = "main", git = "https://github.com/noir-lang/noir-bignum" }
noir_bigcurve = { tag = "main", git = "https://github.com/noir-lang/noir_bigcurve" }
nodash = { tag = "main", git = "https://github.com/olehmisar/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
10 changes: 5 additions & 5 deletions crates/plume/src/hash_to_field.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn hash_to_field<let N: u32>(msg: [u8; N]) -> Secp256k1 {
}

fn bytes_to_registers(ui: [u8; 48]) -> Secp256k1Fq {
let shift = BigNum::from_slice([0, 0x100, 0]);
let shift = Secp256k1Fq::from_slice([0, 0x100, 0]);
let mut small = [0 as u8; 32];
let mut big = [0 as u8; 32];

Expand All @@ -40,8 +40,8 @@ fn bytes_to_registers(ui: [u8; 48]) -> Secp256k1Fq {
for i in 0..32 {
big[i] = ui[i];
}
let res = BigNum::from_be_bytes(big);
res * shift + BigNum::from_be_bytes(small)
let res = Secp256k1Fq::from_be_bytes(big);
res * shift + Secp256k1Fq::from_be_bytes(small)
}

#[test]
Expand All @@ -54,7 +54,7 @@ fn test_u0_bytes_to_registers() {

let actual = bytes_to_registers(u0_bytes);

let expected = BigNum::from_slice([
let expected = Secp256k1Fq::from_slice([
0x491f544767e18a4873f397b08a2b61,
0xab5d3679a1f7601e3bdf94ced1f43e,
0x128a,
Expand All @@ -73,7 +73,7 @@ fn test_u1_bytes_to_registers() {

let actual = bytes_to_registers(u1_bytes);

let expected = BigNum::from_slice([
let expected = Secp256k1Fq::from_slice([
0x1be76a03518b044daaa0f2e4689e00,
0xb65da3b595a813d0fdcc75c895dc53,
0x5897,
Expand Down
38 changes: 19 additions & 19 deletions crates/plume/src/iso_map.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,70 @@ use bignum::BigNum;
use noir_bigcurve::BigCurve;
use noir_bigcurve::curves::secp256k1::{Secp256k1, Secp256k1Fq};

global K1_0: Secp256k1Fq = BigNum::from_slice([
global K1_0: Secp256k1Fq = Secp256k1Fq::from_slice([
0xe38e38e38e38e38e38e38daaaaa8c7,
0xe38e38e38e38e38e38e38e38e38e38,
0x8e38,
]);
global K1_1: Secp256k1Fq = BigNum::from_slice([
global K1_1: Secp256k1Fq = Secp256k1Fq::from_slice([
0x95d2fc0bf63b92dfff1044f17c6581,
0xd4c80bc321d5b9f315cea7fd44c5d5,
0x7d3,
]);
global K1_2: Secp256k1Fq = BigNum::from_slice([
global K1_2: Secp256k1Fq = Secp256k1Fq::from_slice([
0x506144037c40314ecbd0b53d9dd262,
0x328d23f234e6e2a413deca25caece4,
0x534c,
]);
global K1_3: Secp256k1Fq = BigNum::from_slice([
global K1_3: Secp256k1Fq = Secp256k1Fq::from_slice([
0xe38e38e38e38e38e38e38daaaaa88c,
0xe38e38e38e38e38e38e38e38e38e38,
0x8e38,
]);

global K2_0: Secp256k1Fq = BigNum::from_slice([
global K2_0: Secp256k1Fq = Secp256k1Fq::from_slice([
0xcd409542f8487d9fe6b745781eb49b,
0x71193d94918a9ca34ccbb7b640dd86,
0xd357,
]);
global K2_1: Secp256k1Fq = BigNum::from_slice([
global K2_1: Secp256k1Fq = Secp256k1Fq::from_slice([
0xd36b641f5e41bbc52a56612a8c6d14,
0xc6f64383dc1df7c4b2d51b54225406,
0xedad,
]);

global K3_0: Secp256k1Fq = BigNum::from_slice([
global K3_0: Secp256k1Fq = Secp256k1Fq::from_slice([
0x684bda12f684bda12f684b8e38e23c,
0x12f684bda12f684bda12f684bda12f,
0x4bda,
]);
global K3_1: Secp256k1Fq = BigNum::from_slice([
global K3_1: Secp256k1Fq = Secp256k1Fq::from_slice([
0x7ab046d686da6fdffc90fc201d71a3,
0xc32d5cb7c0fa9d0a54b12a0a6d564,
0xc75e,
]);
global K3_2: Secp256k1Fq = BigNum::from_slice([
global K3_2: Secp256k1Fq = Secp256k1Fq::from_slice([
0x2830a201be2018a765e85a9ecee931,
0x194691f91a73715209ef6512e57672,
0x29a6,
]);
global K3_3: Secp256k1Fq = BigNum::from_slice([
global K3_3: Secp256k1Fq = Secp256k1Fq::from_slice([
0xa12f684bda12f684bda12f38e38d84,
0x4bda12f684bda12f684bda12f684bd,
0x2f68,
]);

global K4_0: Secp256k1Fq = BigNum::from_slice([
global K4_0: Secp256k1Fq = Secp256k1Fq::from_slice([
0xfffffffffffffffffffffefffff93b,
0xffffffffffffffffffffffffffffff,
0xffff,
]);
global K4_1: Secp256k1Fq = BigNum::from_slice([
global K4_1: Secp256k1Fq = Secp256k1Fq::from_slice([
0x67c1bfc8e8d978dfb425d2685c2573,
0x534bb8bdb49fd5e9e6632722c29894,
0x7a06,
]);
global K4_2: Secp256k1Fq = BigNum::from_slice([
global K4_2: Secp256k1Fq = Secp256k1Fq::from_slice([
0x3d21162f0d6299a7bf8192bfd2a76f,
0xaa716545ca2cf3a70c3fa8fe337e0a,
0x6484,
Expand Down Expand Up @@ -112,13 +112,13 @@ fn y_den(x: Secp256k1Fq, x2: Secp256k1Fq, x3: Secp256k1Fq) -> Secp256k1Fq {

#[test]
fn test_iso_map() {
let p: Secp256k1 = BigCurve {
x: BigNum::from_slice([
let p = Secp256k1 {
x: Secp256k1Fq::from_slice([
0xf7ae5f23c431edeb6be5bb360925ea,
0xce532d92416a9488104157036455a0,
0x8dec,
]),
y: BigNum::from_slice([
y: Secp256k1Fq::from_slice([
0x13d22e4d45e5bef361e486c6a5da4a,
0xae1c326847bd4a133e5dee6b2ca67c,
0xe249,
Expand All @@ -128,13 +128,13 @@ fn test_iso_map() {

let actual = iso_map(p);

let expected: Secp256k1 = BigCurve {
x: BigNum::from_slice([
let expected = Secp256k1 {
x: Secp256k1Fq::from_slice([
0x7c2a947a20fd9ad71039f8b0e29ff8,
0x6d261a5e00fe5cf45e827b507643e6,
0xf89d,
]),
y: BigNum::from_slice([
y: Secp256k1Fq::from_slice([
0x1ce936d563bc1cee1dcffc806caf57,
0x55e0cc34a9176ead91c6c3acb1aacb,
0xb338,
Expand Down
Loading