Skip to content

Commit

Permalink
remove poseidon array numeric generics
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm committed Jul 26, 2023
1 parent 4ed199c commit 0bcd7e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
34 changes: 17 additions & 17 deletions noir_stdlib/src/hash/poseidon.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ mod bn254; // Instantiations of Poseidon for prime field of the same order as BN

use crate::field::modulus_num_bits;

struct PoseidonConfig<M,N> {
struct PoseidonConfig {
t: comptime Field, // Width, i.e. state size
rf: comptime u8, // Number of full rounds; should be even
rp: comptime u8, // Number of partial rounds
alpha: comptime Field, // S-box power; depends on the underlying field
ark: [Field; M], // Additive round keys
mds: [Field; N] // MDS Matrix in row-major order
ark: [Field], // Additive round keys
mds: [Field] // MDS Matrix in row-major order
}

fn config<M,N>(
fn config(
t: comptime Field,
rf: comptime u8,
rp: comptime u8,
alpha: comptime Field,
ark: [Field; M],
mds: [Field; N])
-> PoseidonConfig<M,N> {
ark: [Field],
mds: [Field])
-> PoseidonConfig {
// Input checks
assert(t as u8 * (rf + rp) == ark.len() as u8);
assert(t * t == mds.len());
Expand All @@ -28,10 +28,10 @@ fn config<M,N>(
}

// General Poseidon permutation on elements of type Field
fn permute<M,N,O>(
pos_conf: PoseidonConfig<M, N>,
mut state: [Field; O])
-> [Field; O] {
fn permute(
pos_conf: PoseidonConfig,
mut state: [Field])
-> [Field] {
let PoseidonConfig {t, rf, rp, alpha, ark, mds} = pos_conf;

assert(t == state.len());
Expand Down Expand Up @@ -61,13 +61,13 @@ fn permute<M,N,O>(
}

// Absorption. Fully absorbs input message.
fn absorb<M,N,O,P>(
pos_conf: PoseidonConfig<M, N>,
mut state: [Field; O], // Initial state; usually [0; O]
fn absorb(
pos_conf: PoseidonConfig,
mut state: [Field], // Initial state; usually [0; O]
rate: comptime Field, // Rate
capacity: comptime Field, // Capacity; usually 1
msg: [Field; P]) // Arbitrary length message
-> [Field; O] {
msg: [Field]) // Arbitrary length message
-> [Field] {
assert(pos_conf.t == rate + capacity);

let mut i = 0;
Expand Down Expand Up @@ -101,7 +101,7 @@ fn check_security(rate: Field, width: Field, security: Field) -> bool {
}

// A*x where A is an n x n matrix in row-major order and x an n-vector
fn apply_matrix<N>(a: [Field], x: [Field; N]) -> [Field; N] {
fn apply_matrix(a: [Field], x: [Field]) -> [Field] {
let mut y = x;

for i in 0..x.len() {
Expand Down
20 changes: 10 additions & 10 deletions noir_stdlib/src/hash/poseidon/bn254.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ 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.
fn permute<M,N,O>(
pos_conf: PoseidonConfig<M, N>,
mut state: [Field; O])
-> [Field; O] {
fn permute(
pos_conf: PoseidonConfig,
mut state: [Field])
-> [Field] {
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][state.len() - 2];
Expand Down Expand Up @@ -65,13 +65,13 @@ fn permute<M,N,O>(
}

// Corresponding absorption.
fn absorb<M,N,O,P>(
pos_conf: PoseidonConfig<M, N>,
mut state: [Field; O], // Initial state; usually [0; O]
fn absorb(
pos_conf: PoseidonConfig,
mut state: [Field], // Initial state; usually [0; O]
rate: comptime Field, // Rate
capacity: comptime Field, // Capacity; usually 1
msg: [Field; P] // Arbitrary length message
) -> [Field; O] {
msg: [Field] // Arbitrary length message
) -> [Field] {

assert(pos_conf.t == rate + capacity);

Expand All @@ -98,7 +98,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
fn sponge<N>(msg: [Field; N]) -> Field {
fn sponge(msg: [Field]) -> Field {
absorb(consts::x5_5_config(), [0;5], 4, 1, msg)[1]
}

Expand Down

0 comments on commit 0bcd7e3

Please sign in to comment.