Skip to content

Commit

Permalink
chore: add bn254 attribute when needed in the stdlib (#3208)
Browse files Browse the repository at this point in the history
  • Loading branch information
guipublic authored Oct 26, 2023
1 parent 8369871 commit cb81192
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions noir_stdlib/src/ec/consts/te.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ struct BabyJubjub {
suborder: Field,
}

#[field(bn254)]
pub fn baby_jubjub() -> BabyJubjub {
assert(compat::is_bn254());

BabyJubjub {
// Baby Jubjub (ERC-2494) parameters in affine representation
curve: TECurve::new(
Expand Down
1 change: 1 addition & 0 deletions noir_stdlib/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn mimc<N>(x: Field, k: Field, constants: [Field; N], exp : Field) -> Field {
global MIMC_BN254_ROUNDS = 91;

//mimc implementation with hardcoded parameters for BN254 curve.
#[field(bn254)]
pub fn mimc_bn254<N>(array: [Field; N]) -> Field {
//mimc parameters
let exponent = 7;
Expand Down
3 changes: 3 additions & 0 deletions noir_stdlib/src/hash/poseidon/bn254.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::hash::poseidon::apply_matrix;

// Optimised permutation for this particular field; uses hardcoded rf and rp values,
// which should agree with those in pos_conf.
#[field(bn254)]
pub fn permute<M,N,O>(
pos_conf: PoseidonConfig<M, N>,
mut state: [Field; O])
Expand Down Expand Up @@ -65,6 +66,7 @@ pub fn permute<M,N,O>(
}

// Corresponding absorption.
#[field(bn254)]
fn absorb<M,N,O,P>(
pos_conf: PoseidonConfig<M, N>,
mut state: [Field; O], // Initial state; usually [0; O]
Expand Down Expand Up @@ -98,6 +100,7 @@ fn absorb<M,N,O,P>(
}

// Variable-length Poseidon-128 sponge as suggested in second bullet point of §3 of https://eprint.iacr.org/2019/458.pdf
#[field(bn254)]
pub fn sponge<N>(msg: [Field; N]) -> Field {
absorb(consts::x5_5_config(), [0;5], 4, 1, msg)[1]
}
Expand Down
16 changes: 16 additions & 0 deletions noir_stdlib/src/hash/poseidon/bn254/perm.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::hash::poseidon::bn254::consts;
use crate::hash::poseidon::bn254::permute;
use crate::hash::poseidon::PoseidonConfig;

#[field(bn254)]
pub fn x5_2(mut state: [Field; 2]) -> [Field; 2] {
state = permute(
consts::x5_2_config(),
Expand All @@ -11,6 +12,7 @@ pub fn x5_2(mut state: [Field; 2]) -> [Field; 2] {
state
}

#[field(bn254)]
pub fn x5_3(mut state: [Field; 3]) -> [Field; 3] {
state = permute(
consts::x5_3_config(),
Expand All @@ -19,6 +21,7 @@ pub fn x5_3(mut state: [Field; 3]) -> [Field; 3] {
state
}

#[field(bn254)]
pub fn x5_4(mut state: [Field; 4]) -> [Field; 4] {
state = permute(
consts::x5_4_config(),
Expand All @@ -27,6 +30,7 @@ pub fn x5_4(mut state: [Field; 4]) -> [Field; 4] {
state
}

#[field(bn254)]
pub fn x5_5(mut state: [Field; 5]) -> [Field; 5] {
state = permute(
consts::x5_5_config(),
Expand All @@ -35,6 +39,7 @@ pub fn x5_5(mut state: [Field; 5]) -> [Field; 5] {
state
}

#[field(bn254)]
pub fn x5_6(mut state: [Field; 6]) -> [Field; 6] {
state = permute(
consts::x5_6_config(),
Expand All @@ -43,6 +48,7 @@ pub fn x5_6(mut state: [Field; 6]) -> [Field; 6] {
state
}

#[field(bn254)]
pub fn x5_7(mut state: [Field; 7]) -> [Field; 7] {
state = permute(
consts::x5_7_config(),
Expand All @@ -51,6 +57,7 @@ pub fn x5_7(mut state: [Field; 7]) -> [Field; 7] {
state
}

#[field(bn254)]
pub fn x5_8(mut state: [Field; 8]) -> [Field; 8] {
state = permute(
consts::x5_8_config(),
Expand All @@ -59,6 +66,7 @@ pub fn x5_8(mut state: [Field; 8]) -> [Field; 8] {
state
}

#[field(bn254)]
pub fn x5_9(mut state: [Field; 9]) -> [Field; 9] {
state = permute(
consts::x5_9_config(),
Expand All @@ -67,6 +75,7 @@ pub fn x5_9(mut state: [Field; 9]) -> [Field; 9] {
state
}

#[field(bn254)]
pub fn x5_10(mut state: [Field; 10]) -> [Field; 10] {
state = permute(
consts::x5_10_config(),
Expand All @@ -75,6 +84,7 @@ pub fn x5_10(mut state: [Field; 10]) -> [Field; 10] {
state
}

#[field(bn254)]
pub fn x5_11(mut state: [Field; 11]) -> [Field; 11] {
state = permute(
consts::x5_11_config(),
Expand All @@ -83,6 +93,7 @@ pub fn x5_11(mut state: [Field; 11]) -> [Field; 11] {
state
}

#[field(bn254)]
pub fn x5_12(mut state: [Field; 12]) -> [Field; 12] {
state = permute(
consts::x5_12_config(),
Expand All @@ -91,6 +102,7 @@ pub fn x5_12(mut state: [Field; 12]) -> [Field; 12] {
state
}

#[field(bn254)]
pub fn x5_13(mut state: [Field; 13]) -> [Field; 13] {
state = permute(
consts::x5_13_config(),
Expand All @@ -99,6 +111,7 @@ pub fn x5_13(mut state: [Field; 13]) -> [Field; 13] {
state
}

#[field(bn254)]
pub fn x5_14(mut state: [Field; 14]) -> [Field; 14] {
state = permute(
consts::x5_14_config(),
Expand All @@ -107,6 +120,7 @@ pub fn x5_14(mut state: [Field; 14]) -> [Field; 14] {
state
}

#[field(bn254)]
pub fn x5_15(mut state: [Field; 15]) -> [Field; 15] {
state = permute(
consts::x5_15_config(),
Expand All @@ -115,6 +129,7 @@ pub fn x5_15(mut state: [Field; 15]) -> [Field; 15] {
state
}

#[field(bn254)]
pub fn x5_16(mut state: [Field; 16]) -> [Field; 16] {
state = permute(
consts::x5_16_config(),
Expand All @@ -123,6 +138,7 @@ pub fn x5_16(mut state: [Field; 16]) -> [Field; 16] {
state
}

#[field(bn254)]
pub fn x5_17(mut state: [Field; 17]) -> [Field; 17] {
state = permute(
consts::x5_17_config(),
Expand Down

0 comments on commit cb81192

Please sign in to comment.