From b6b3a24dbd8fee21968878a3d18006ea20e97017 Mon Sep 17 00:00:00 2001 From: guipublic Date: Fri, 14 Jun 2024 15:11:40 +0000 Subject: [PATCH 1/5] Add no-predicate to hash implementations --- noir_stdlib/src/hash/mimc.nr | 2 ++ noir_stdlib/src/hash/poseidon.nr | 2 ++ noir_stdlib/src/sha256.nr | 1 + noir_stdlib/src/sha512.nr | 1 + 4 files changed, 6 insertions(+) diff --git a/noir_stdlib/src/hash/mimc.nr b/noir_stdlib/src/hash/mimc.nr index 6c5502c2fbf..131b8943a36 100644 --- a/noir_stdlib/src/hash/mimc.nr +++ b/noir_stdlib/src/hash/mimc.nr @@ -116,6 +116,7 @@ global MIMC_BN254_CONSTANTS: [Field; MIMC_BN254_ROUNDS] = [ //mimc implementation with hardcoded parameters for BN254 curve. #[field(bn254)] +#[no_predicates] pub fn mimc_bn254(array: [Field; N]) -> Field { let exponent = 7; let mut r = 0; @@ -132,6 +133,7 @@ struct MimcHasher { impl Hasher for MimcHasher { #[field(bn254)] + #[no_predicates] fn finish(self) -> Field { let exponent = 7; let mut r = 0; diff --git a/noir_stdlib/src/hash/poseidon.nr b/noir_stdlib/src/hash/poseidon.nr index c4b5f0fcb6f..3a4523bd8a3 100644 --- a/noir_stdlib/src/hash/poseidon.nr +++ b/noir_stdlib/src/hash/poseidon.nr @@ -47,6 +47,7 @@ pub fn config( PoseidonConfig { t, rf, rp, alpha, round_constants, mds, presparse_mds, sparse_mds } } +#[no_predicates] pub fn permute(pos_conf: PoseidonConfig, mut state: [Field; T]) -> [Field; T] { let PoseidonConfig {t, rf, rp, alpha, round_constants, mds, presparse_mds, sparse_mds } = pos_conf; @@ -176,6 +177,7 @@ struct PoseidonHasher{ impl Hasher for PoseidonHasher { #[field(bn254)] + #[no_predicates] fn finish(self) -> Field { let mut result = 0; let len = self._state.len(); diff --git a/noir_stdlib/src/sha256.nr b/noir_stdlib/src/sha256.nr index d856043fcfa..6057876951d 100644 --- a/noir_stdlib/src/sha256.nr +++ b/noir_stdlib/src/sha256.nr @@ -38,6 +38,7 @@ fn hash_final_block(msg_block: [u8; 64], mut state: [u32; 8]) -> [u8; 32] { } // Variable size SHA-256 hash +#[no_predicates] pub fn sha256_var(msg: [u8; N], message_size: u64) -> [u8; 32] { let mut msg_block: [u8; 64] = [0; 64]; let mut h: [u32; 8] = [1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225]; // Intermediate hash, starting with the canonical initial value diff --git a/noir_stdlib/src/sha512.nr b/noir_stdlib/src/sha512.nr index 0f8ffcfcb1c..4e46840ebb7 100644 --- a/noir_stdlib/src/sha512.nr +++ b/noir_stdlib/src/sha512.nr @@ -87,6 +87,7 @@ fn msg_u8_to_u64(msg: [u8; 128]) -> [u64; 16] { msg64 } // SHA-512 hash function +#[no_predicates] pub fn digest(msg: [u8; N]) -> [u8; 64] { let mut msg_block: [u8; 128] = [0; 128]; // noir-fmt:ignore From 2e7b24f757226325b211bb343061a228392acc40 Mon Sep 17 00:00:00 2001 From: guipublic Date: Fri, 14 Jun 2024 16:03:30 +0000 Subject: [PATCH 2/5] move the no-predicate to non-self functions --- noir_stdlib/src/hash/mimc.nr | 1 - noir_stdlib/src/hash/poseidon.nr | 1 - noir_stdlib/src/hash/poseidon/bn254.nr | 16 ++++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/noir_stdlib/src/hash/mimc.nr b/noir_stdlib/src/hash/mimc.nr index 131b8943a36..e90bacb75c2 100644 --- a/noir_stdlib/src/hash/mimc.nr +++ b/noir_stdlib/src/hash/mimc.nr @@ -133,7 +133,6 @@ struct MimcHasher { impl Hasher for MimcHasher { #[field(bn254)] - #[no_predicates] fn finish(self) -> Field { let exponent = 7; let mut r = 0; diff --git a/noir_stdlib/src/hash/poseidon.nr b/noir_stdlib/src/hash/poseidon.nr index 3a4523bd8a3..740b4c2037e 100644 --- a/noir_stdlib/src/hash/poseidon.nr +++ b/noir_stdlib/src/hash/poseidon.nr @@ -177,7 +177,6 @@ struct PoseidonHasher{ impl Hasher for PoseidonHasher { #[field(bn254)] - #[no_predicates] fn finish(self) -> Field { let mut result = 0; let len = self._state.len(); diff --git a/noir_stdlib/src/hash/poseidon/bn254.nr b/noir_stdlib/src/hash/poseidon/bn254.nr index 54f22884e29..6faf22fb3a3 100644 --- a/noir_stdlib/src/hash/poseidon/bn254.nr +++ b/noir_stdlib/src/hash/poseidon/bn254.nr @@ -12,6 +12,7 @@ pub fn sponge(msg: [Field; N]) -> Field { // Various instances of the Poseidon hash function // Consistent with Circom's implementation +#[no_predicates] pub fn hash_1(input: [Field; 1]) -> Field { let mut state = [0; 2]; for i in 0..input.len() { @@ -21,6 +22,7 @@ pub fn hash_1(input: [Field; 1]) -> Field { perm::x5_2(state)[0] } +#[no_predicates] pub fn hash_2(input: [Field; 2]) -> Field { let mut state = [0; 3]; for i in 0..input.len() { @@ -30,6 +32,7 @@ pub fn hash_2(input: [Field; 2]) -> Field { perm::x5_3(state)[0] } +#[no_predicates] pub fn hash_3(input: [Field; 3]) -> Field { let mut state = [0; 4]; for i in 0..input.len() { @@ -39,6 +42,7 @@ pub fn hash_3(input: [Field; 3]) -> Field { perm::x5_4(state)[0] } +#[no_predicates] pub fn hash_4(input: [Field; 4]) -> Field { let mut state = [0; 5]; for i in 0..input.len() { @@ -48,6 +52,7 @@ pub fn hash_4(input: [Field; 4]) -> Field { perm::x5_5(state)[0] } +#[no_predicates] pub fn hash_5(input: [Field; 5]) -> Field { let mut state = [0; 6]; for i in 0..input.len() { @@ -57,6 +62,7 @@ pub fn hash_5(input: [Field; 5]) -> Field { perm::x5_6(state)[0] } +#[no_predicates] pub fn hash_6(input: [Field; 6]) -> Field { let mut state = [0; 7]; for i in 0..input.len() { @@ -66,6 +72,7 @@ pub fn hash_6(input: [Field; 6]) -> Field { perm::x5_7(state)[0] } +#[no_predicates] pub fn hash_7(input: [Field; 7]) -> Field { let mut state = [0; 8]; for i in 0..input.len() { @@ -75,6 +82,7 @@ pub fn hash_7(input: [Field; 7]) -> Field { perm::x5_8(state)[0] } +#[no_predicates] pub fn hash_8(input: [Field; 8]) -> Field { let mut state = [0; 9]; for i in 0..input.len() { @@ -84,6 +92,7 @@ pub fn hash_8(input: [Field; 8]) -> Field { perm::x5_9(state)[0] } +#[no_predicates] pub fn hash_9(input: [Field; 9]) -> Field { let mut state = [0; 10]; for i in 0..input.len() { @@ -93,6 +102,7 @@ pub fn hash_9(input: [Field; 9]) -> Field { perm::x5_10(state)[0] } +#[no_predicates] pub fn hash_10(input: [Field; 10]) -> Field { let mut state = [0; 11]; for i in 0..input.len() { @@ -102,6 +112,7 @@ pub fn hash_10(input: [Field; 10]) -> Field { perm::x5_11(state)[0] } +#[no_predicates] pub fn hash_11(input: [Field; 11]) -> Field { let mut state = [0; 12]; for i in 0..input.len() { @@ -111,6 +122,7 @@ pub fn hash_11(input: [Field; 11]) -> Field { perm::x5_12(state)[0] } +#[no_predicates] pub fn hash_12(input: [Field; 12]) -> Field { let mut state = [0; 13]; for i in 0..input.len() { @@ -120,6 +132,7 @@ pub fn hash_12(input: [Field; 12]) -> Field { perm::x5_13(state)[0] } +#[no_predicates] pub fn hash_13(input: [Field; 13]) -> Field { let mut state = [0; 14]; for i in 0..input.len() { @@ -129,6 +142,7 @@ pub fn hash_13(input: [Field; 13]) -> Field { perm::x5_14(state)[0] } +#[no_predicates] pub fn hash_14(input: [Field; 14]) -> Field { let mut state = [0; 15]; for i in 0..input.len() { @@ -138,6 +152,7 @@ pub fn hash_14(input: [Field; 14]) -> Field { perm::x5_15(state)[0] } +#[no_predicates] pub fn hash_15(input: [Field; 15]) -> Field { let mut state = [0; 16]; for i in 0..input.len() { @@ -147,6 +162,7 @@ pub fn hash_15(input: [Field; 15]) -> Field { perm::x5_16(state)[0] } +#[no_predicates] pub fn hash_16(input: [Field; 16]) -> Field { let mut state = [0; 17]; for i in 0..input.len() { From a5985f8f0ccb64d0de2558fa870c4171e20a2fb9 Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 17 Jun 2024 08:37:24 +0000 Subject: [PATCH 3/5] no predicate only for bn254 poseidon --- noir_stdlib/src/hash/poseidon.nr | 1 - noir_stdlib/src/hash/poseidon/bn254.nr | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/noir_stdlib/src/hash/poseidon.nr b/noir_stdlib/src/hash/poseidon.nr index 740b4c2037e..c4b5f0fcb6f 100644 --- a/noir_stdlib/src/hash/poseidon.nr +++ b/noir_stdlib/src/hash/poseidon.nr @@ -47,7 +47,6 @@ pub fn config( PoseidonConfig { t, rf, rp, alpha, round_constants, mds, presparse_mds, sparse_mds } } -#[no_predicates] pub fn permute(pos_conf: PoseidonConfig, mut state: [Field; T]) -> [Field; T] { let PoseidonConfig {t, rf, rp, alpha, round_constants, mds, presparse_mds, sparse_mds } = pos_conf; diff --git a/noir_stdlib/src/hash/poseidon/bn254.nr b/noir_stdlib/src/hash/poseidon/bn254.nr index 6faf22fb3a3..9d3accb1ebd 100644 --- a/noir_stdlib/src/hash/poseidon/bn254.nr +++ b/noir_stdlib/src/hash/poseidon/bn254.nr @@ -6,6 +6,7 @@ use crate::hash::poseidon::{PoseidonConfig, absorb}; // Variable-length Poseidon-128 sponge as suggested in second bullet point of ยง3 of https://eprint.iacr.org/2019/458.pdf #[field(bn254)] +#[no_predicates] pub fn sponge(msg: [Field; N]) -> Field { absorb(consts::x5_5_config(), [0; 5], 4, 1, msg)[1] } From 18ad1710b1ae6210ac34fbb2d0d9b92f5e35b0c3 Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 17 Jun 2024 09:25:35 +0000 Subject: [PATCH 4/5] add test case --- .../regression_5252/Nargo.toml | 7 ++++++ .../regression_5252/Prover.toml | 5 ++++ .../regression_5252/src/main.nr | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 test_programs/execution_success/regression_5252/Nargo.toml create mode 100644 test_programs/execution_success/regression_5252/Prover.toml create mode 100644 test_programs/execution_success/regression_5252/src/main.nr diff --git a/test_programs/execution_success/regression_5252/Nargo.toml b/test_programs/execution_success/regression_5252/Nargo.toml new file mode 100644 index 00000000000..855507dfaf3 --- /dev/null +++ b/test_programs/execution_success/regression_5252/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "regression_5252" +version = "0.1.0" +type = "bin" +authors = [""] + +[dependencies] \ No newline at end of file diff --git a/test_programs/execution_success/regression_5252/Prover.toml b/test_programs/execution_success/regression_5252/Prover.toml new file mode 100644 index 00000000000..b8154f0b1e4 --- /dev/null +++ b/test_programs/execution_success/regression_5252/Prover.toml @@ -0,0 +1,5 @@ +to_hash = [[1,5,9,2,24,563,3545,5,52,4244,43,2,7373567,2,286762,7,2457,24,2456,2456], +[2234,2,26,27,24566,132452,3452456344567,657,45674657,4567467,45674,4567456,4567,23454,2345,2345345245,25252345,2435234524366,8678678,67867567], +[9887575467567,5367367243617,46244567783,64673425,67456573456,4673457,46735,745674,6574,567456,7456,84,683,683,8368,38,32,16,7,98], +[465656,234324,4353,5245246,2567345674567,5634563456,7676474,4747,4567456746,56,4657456,4657,4567,46,7,8,98,87,76,57]] +enable = [1,1,0,1] diff --git a/test_programs/execution_success/regression_5252/src/main.nr b/test_programs/execution_success/regression_5252/src/main.nr new file mode 100644 index 00000000000..2dfa4730bb5 --- /dev/null +++ b/test_programs/execution_success/regression_5252/src/main.nr @@ -0,0 +1,23 @@ +use dep::std::hash::{mimc, poseidon, poseidon2::Poseidon2}; + +global NUM_HASHES = 4; +global HASH_LENGTH = 20; + +pub fn poseidon_hash(inputs: [Field; N]) -> Field { + Poseidon2::hash(inputs, inputs.len()) +} + +fn main( + to_hash: [[Field; HASH_LENGTH]; NUM_HASHES], + enable: [bool; NUM_HASHES] +) -> pub [Field; NUM_HASHES] { + let mut result = [0; NUM_HASHES]; + for i in 0..NUM_HASHES { + let enable = enable[i]; + let to_hash = to_hash[i]; + if enable { + result[i] = poseidon_hash(to_hash) + poseidon::bn254::sponge(to_hash) + mimc::mimc_bn254(to_hash); + } + } + result +} From e5a2ed072b3bba4fe38e0fe9caaafa38880aa98a Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 17 Jun 2024 09:47:00 +0000 Subject: [PATCH 5/5] reduce size of the text case --- test_programs/execution_success/regression_5252/Prover.toml | 5 +++-- test_programs/execution_success/regression_5252/src/main.nr | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test_programs/execution_success/regression_5252/Prover.toml b/test_programs/execution_success/regression_5252/Prover.toml index b8154f0b1e4..82776b4463d 100644 --- a/test_programs/execution_success/regression_5252/Prover.toml +++ b/test_programs/execution_success/regression_5252/Prover.toml @@ -1,5 +1,6 @@ to_hash = [[1,5,9,2,24,563,3545,5,52,4244,43,2,7373567,2,286762,7,2457,24,2456,2456], [2234,2,26,27,24566,132452,3452456344567,657,45674657,4567467,45674,4567456,4567,23454,2345,2345345245,25252345,2435234524366,8678678,67867567], [9887575467567,5367367243617,46244567783,64673425,67456573456,4673457,46735,745674,6574,567456,7456,84,683,683,8368,38,32,16,7,98], -[465656,234324,4353,5245246,2567345674567,5634563456,7676474,4747,4567456746,56,4657456,4657,4567,46,7,8,98,87,76,57]] -enable = [1,1,0,1] +#[465656,234324,4353,5245246,2567345674567,5634563456,7676474,4747,4567456746,56,4657456,4657,4567,46,7,8,98,87,76,57] +] +enable = [1,1,0] diff --git a/test_programs/execution_success/regression_5252/src/main.nr b/test_programs/execution_success/regression_5252/src/main.nr index 2dfa4730bb5..315807c3396 100644 --- a/test_programs/execution_success/regression_5252/src/main.nr +++ b/test_programs/execution_success/regression_5252/src/main.nr @@ -1,6 +1,6 @@ use dep::std::hash::{mimc, poseidon, poseidon2::Poseidon2}; -global NUM_HASHES = 4; +global NUM_HASHES = 3; global HASH_LENGTH = 20; pub fn poseidon_hash(inputs: [Field; N]) -> Field {