Skip to content

Commit

Permalink
chore: add no-predicate to hash implementations (#5253)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Related to #4688 

## Summary\*
Adding the no-predicate attribute to the hash implementations of the
stdlib


## Additional Context



## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
guipublic authored Jun 17, 2024
1 parent d9b4712 commit b351cc0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions noir_stdlib/src/hash/mimc.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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<N>(array: [Field; N]) -> Field {
let exponent = 7;
let mut r = 0;
Expand Down
17 changes: 17 additions & 0 deletions noir_stdlib/src/hash/poseidon/bn254.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ 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<N>(msg: [Field; N]) -> Field {
absorb(consts::x5_5_config(), [0; 5], 4, 1, msg)[1]
}

// 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() {
Expand All @@ -21,6 +23,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() {
Expand All @@ -30,6 +33,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() {
Expand All @@ -39,6 +43,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() {
Expand All @@ -48,6 +53,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() {
Expand All @@ -57,6 +63,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() {
Expand All @@ -66,6 +73,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() {
Expand All @@ -75,6 +83,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() {
Expand All @@ -84,6 +93,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() {
Expand All @@ -93,6 +103,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() {
Expand All @@ -102,6 +113,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() {
Expand All @@ -111,6 +123,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() {
Expand All @@ -120,6 +133,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() {
Expand All @@ -129,6 +143,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() {
Expand All @@ -138,6 +153,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() {
Expand All @@ -147,6 +163,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() {
Expand Down
1 change: 1 addition & 0 deletions noir_stdlib/src/sha256.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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<N>(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
Expand Down
1 change: 1 addition & 0 deletions noir_stdlib/src/sha512.nr
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn msg_u8_to_u64(msg: [u8; 128]) -> [u64; 16] {
msg64
}
// SHA-512 hash function
#[no_predicates]
pub fn digest<N>(msg: [u8; N]) -> [u8; 64] {
let mut msg_block: [u8; 128] = [0; 128];
// noir-fmt:ignore
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/regression_5252/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "regression_5252"
version = "0.1.0"
type = "bin"
authors = [""]

[dependencies]
6 changes: 6 additions & 0 deletions test_programs/execution_success/regression_5252/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +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]
23 changes: 23 additions & 0 deletions test_programs/execution_success/regression_5252/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use dep::std::hash::{mimc, poseidon, poseidon2::Poseidon2};

global NUM_HASHES = 3;
global HASH_LENGTH = 20;

pub fn poseidon_hash<N>(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
}

0 comments on commit b351cc0

Please sign in to comment.