Skip to content

Commit

Permalink
Replace stdlib functions with methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ax0 committed Feb 17, 2023
1 parent ce48c4a commit be6f0c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
26 changes: 12 additions & 14 deletions noir_stdlib/src/hash/poseidon.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod bn254; // Instantiations of Poseidon for prime field of the same order as BN254

use crate::array;
use crate::pow_32;
use crate::field::modulus_num_bits;

struct PoseidonConfig<M,N> {
Expand All @@ -22,8 +20,8 @@ fn config<M,N>(
mds: [Field; N])
-> PoseidonConfig<M,N> {
// Input checks
constrain t as u8 * (rf + rp) == array::len(ark) as u8;
constrain t * t == array::len(mds);
constrain t as u8 * (rf + rp) == ark.len() as u8;
constrain t * t == mds.len();
constrain alpha != 0;

PoseidonConfig {t, rf, rp, alpha, ark, mds}
Expand All @@ -36,22 +34,22 @@ fn permute<M,N,O>(
-> [Field; O] {
let PoseidonConfig {t, rf, rp, alpha, ark, mds} = pos_conf;

constrain t == array::len(state);
constrain t == state.len();

let mut count = 0;

// for r in 0..rf + rp
for r in 0..(array::len(ark)/array::len(state)) {
for i in 0..array::len(state) {
for r in 0..(ark.len()/state.len()) {
for i in 0..state.len() {
state[i] = state[i] + ark[count + i];
} // Shift by round constants

state[0] = pow_32(state[0], alpha);
state[0] = state[0].pow_32(alpha);

// Check whether we are in a full round
if (r as u8 < rf/2) | (r as u8 >= rf/2 + rp) {
for i in 1..array::len(state) {
state[i] = pow_32(state[i], alpha);
for i in 1..state.len() {
state[i] = state[i].pow_32(alpha);
}
}

Expand All @@ -74,7 +72,7 @@ fn absorb<M,N,O,P>(

let mut i = 0;

for k in 0..array::len(msg) {
for k in 0..msg.len() {
// Add current block to state
state[capacity + i] += msg[k];
i = i+1;
Expand Down Expand Up @@ -106,10 +104,10 @@ fn check_security(rate: Field, width: Field, security: Field) -> bool {
fn apply_matrix<N>(a: [Field], x: [Field; N]) -> [Field; N] {
let mut y = x;

for i in 0..array::len(x) {
for i in 0..x.len() {
y[i] = 0;
for j in 0..array::len(x) {
y[i] = y[i] + a[array::len(x)*i + j]* x[j];
for j in 0..x.len() {
y[i] = y[i] + a[x.len()*i + j]* x[j];
}
}

Expand Down
24 changes: 11 additions & 13 deletions noir_stdlib/src/hash/poseidon/bn254.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ mod perm;
mod consts;

use crate::hash::poseidon::PoseidonConfig;
use crate::array;
use crate::pow_32;
use crate::hash::poseidon::apply_matrix;

// Optimised permutation for this particular field; uses hardcoded rf and rp values,
Expand All @@ -15,22 +13,22 @@ fn permute<M,N,O>(
-> [Field; O] {
let PoseidonConfig {t, rf: config_rf, rp: config_rp, alpha, ark, mds} = pos_conf;
let rf = 8;
let rp = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68][array::len(state) - 2];
let rp = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68][state.len() - 2];

constrain t == array::len(state);
constrain t == state.len();
constrain rf == config_rf as Field;
constrain rp == config_rp as Field;

let mut count = 0;

// First half of full rounds
for _r in 0..rf/2 {
for i in 0..array::len(state) {
for i in 0..state.len() {
state[i] = state[i] + ark[count + i];
} // Shift by round constants

for i in 0..array::len(state) {
state[i] = pow_32(state[i], alpha);
for i in 0..state.len() {
state[i] = state[i].pow_32(alpha);
}

state = apply_matrix(mds, state); // Apply MDS matrix
Expand All @@ -39,24 +37,24 @@ fn permute<M,N,O>(

// Partial rounds
for _r in 0..rp {
for i in 0..array::len(state) {
for i in 0..state.len() {
state[i] = state[i] + ark[count + i];
} // Shift by round constants

state[0] = pow_32(state[0], alpha);
state[0] = state[0].pow_32(alpha);

state = apply_matrix(mds, state); // Apply MDS matrix
count = count + t;
}

// Second half of full rounds
for _r in 0..rf/2 {
for i in 0..array::len(state) {
for i in 0..state.len() {
state[i] = state[i] + ark[count + i];
} // Shift by round constants

for i in 0..array::len(state) {
state[i] = pow_32(state[i], alpha);
for i in 0..state.len() {
state[i] = state[i].pow_32(alpha);
}

state = apply_matrix(mds, state); // Apply MDS matrix
Expand All @@ -79,7 +77,7 @@ fn absorb<M,N,O,P>(

let mut i = 0;

for k in 0..array::len(msg) {
for k in 0..msg.len() {
// Add current block to state
state[capacity + i] += msg[k];
i = i+1;
Expand Down

0 comments on commit be6f0c7

Please sign in to comment.