Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenciak committed Jun 6, 2024
1 parent 2329d91 commit 6d58de2
Show file tree
Hide file tree
Showing 4 changed files with 2,416 additions and 2,351 deletions.
15 changes: 5 additions & 10 deletions scripts/poseidon2_rust_params.sage
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ def to_field(value, p):
value = hex(int(value))[2:]
value = "0x" + value.zfill(l - 2)
# print("from_hex(\"{}\"),".format(value))
print(f"f({value}),")
print(f"BabyBear::from_canonical_u32({value}),")


def generate_constants_file():
Expand All @@ -615,15 +615,10 @@ def generate_constants_file():
""")
print()
print("use lazy_static::*;")
print("use hybrid_array::{Array, typenum::*};")
print("use p3_baby_bear::BabyBear;")
print("use p3_field::AbstractField;")
print()
print("""
fn f(u: u32) -> BabyBear {
BabyBear::from_canonical_u32(u)
}
"""
)
for t in range(4, 44, 4):
p = 2013265921 # BabyBear
n = len(p.bits())
Expand Down Expand Up @@ -653,10 +648,10 @@ fn f(u: u32) -> BabyBear {
MATRIX_PARTIAL_DIAGONAL_M_1 = [matrix_partial_m_1(MATRIX_PARTIAL, NUM_CELLS, F)[i,i] for i in range(0, NUM_CELLS)]

# Efficient partial matrix (diagonal - 1)
print("pub static ref MATRIX_DIAG_{}_BABYBEAR: [BabyBear; {}]= [".format(t, t))
print("pub static ref MATRIX_DIAG_{}_BABYBEAR: Array<BabyBear, U{}> = Array::clone_from_slice([".format(t, t))
for val in MATRIX_PARTIAL_DIAGONAL_M_1:
to_field(val, p)
print("];")
to_field(val - 1, p)
print("].as_ref());")
print()


Expand Down
11 changes: 5 additions & 6 deletions src/poseidon/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::slice;

use hybrid_array::{typenum::*, Array, ArraySize};
use p3_baby_bear::BabyBear;
use p3_field::{AbstractField, Field};
use p3_field::Field;
use p3_poseidon2::matmul_internal;
use p3_poseidon2::{DiffusionPermutation, Poseidon2, Poseidon2ExternalMatrixGeneral};
use p3_symmetric::Permutation;

Expand Down Expand Up @@ -51,8 +52,9 @@ macro_rules! impl_poseidon_config {
const R_F: usize = $r_f;
type R = $r_t;

#[inline]
fn matrix_diag() -> &'static Array<Self::F, Self::WIDTH> {
Array::from_slice(&*$diag)
$diag.as_ref()
}

fn round_constants_iter() -> impl IntoIterator<Item = &'static [Self::F]> {
Expand All @@ -67,10 +69,7 @@ macro_rules! impl_poseidon_config {

impl Permutation<[$field; $width]> for InternalDiffusion {
fn permute_mut(&self, input: &mut [$field; $width]) {
let sum: $field = input.iter().copied().sum();
for i in 0..$width {
input[i] = sum + ($diag[i] + $field::neg_one()) * input[i];
}
matmul_internal(input, *$diag.as_ref());
}
}

Expand Down
Loading

0 comments on commit 6d58de2

Please sign in to comment.