Skip to content

Commit

Permalink
use for-poseidon377 branch of decaf377
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Apr 2, 2024
1 parent 645976f commit 0e50735
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 64 deletions.
2 changes: 1 addition & 1 deletion poseidon-consistency/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/penumbra-zone/poseidon377"
poseidon-parameters = { path = "../poseidon-parameters" }
poseidon-paramgen = { path = "../poseidon-paramgen", default-features = false }
poseidon-permutation = { path="../poseidon-permutation", default-features = false }
decaf377 = { version = "0.8", default-features = false }
decaf377 = { git = "https://github.com/penumbra-zone/decaf377.git", branch="for-poseidon377", default-features = false }

[dev-dependencies]
criterion = { version = "0.4", features=["html_reports"] }
Expand Down
2 changes: 1 addition & 1 deletion poseidon-parameters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ keywords = ["no_std"]
repository = "https://github.com/penumbra-zone/poseidon377"

[dependencies]
decaf377 = { version="0.8.0", default-features = false }
decaf377 = { git = "https://github.com/penumbra-zone/decaf377.git", branch="for-poseidon377", default-features = false }

[dev-dependencies]
ark-ff = { version = "^0.4.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion poseidon-parameters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait StuffThatNeedsToGoInDecaf377 {
// TEMP
impl StuffThatNeedsToGoInDecaf377 for Fq {
fn pow<S: AsRef<[u64]>>(&self, exp: S) -> Self {
let mut res = Fq::one();
let mut res = Fq::from(1u64);
let exp_u64 = exp.as_ref();
for _ in 0..exp_u64[0] {
res *= self;
Expand Down
14 changes: 7 additions & 7 deletions poseidon-parameters/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<const N_ROWS: usize, const N_ELEMENTS: usize> SquareMatrixOperations
}

let determinant = self.determinant();
if determinant == Fq::zero() {
if determinant == Fq::from(0u64) {
return Err(PoseidonParameterError::NoMatrixInverse);
}

Expand All @@ -220,20 +220,20 @@ impl<const N_ROWS: usize, const N_ELEMENTS: usize> SquareMatrixOperations
.hadamard_product(&cofactor_matrix)
.expect("minor and cofactor matrix have correct dimensions");
let adj = signed_minors.transpose();
let matrix_inverse = adj * (Fq::one() / determinant);
let matrix_inverse = adj * (Fq::from(1u64) / determinant);

debug_assert_eq!(square_mat_mul(self, &matrix_inverse), identity);
Ok(matrix_inverse)
}

/// Construct an identity matrix
fn identity() -> Self {
let elements = [Fq::zero(); N_ELEMENTS];
let elements = [Fq::from(0u64); N_ELEMENTS];
let mut m = Self::new(&elements);

// Set diagonals to 1
for i in 0..N_ROWS {
m.set_element(i, i, Fq::one());
m.set_element(i, i, Fq::from(1u64));
}

m
Expand Down Expand Up @@ -266,14 +266,14 @@ impl<const N_ROWS: usize, const N_ELEMENTS: usize> SquareMatrixOperations
/// Compute the cofactor matrix, i.e. $C_{ij} = (-1)^{i+j}$
fn cofactors(&self) -> Self {
let dim = self.n_rows();
let mut elements = [Fq::zero(); N_ELEMENTS];
let mut elements = [Fq::from(0u64); N_ELEMENTS];

// TODO: non arkworks Fq::pow
use crate::StuffThatNeedsToGoInDecaf377;
let mut index = 0;
for i in 0..dim {
for j in 0..dim {
elements[index] = (-Fq::one()).pow([(i + j) as u64]);
elements[index] = (-Fq::from(1u64)).pow([(i + j) as u64]);
index += 1;
}
}
Expand Down Expand Up @@ -404,7 +404,7 @@ fn determinant<
>(
matrix: &SquareMatrix<DIM, N_ELEMENTS>,
) -> Fq {
let mut det = Fq::zero();
let mut det = Fq::from(0u64);
let mut levi_civita = true;

for i in 0..DIM {
Expand Down
4 changes: 2 additions & 2 deletions poseidon-parameters/src/mds_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<
///
/// Ref: p.20 of the Poseidon paper
pub fn w(&self) -> Matrix<STATE_SIZE_MINUS_1, 1, STATE_SIZE_MINUS_1> {
let mut elements = [Fq::zero(); STATE_SIZE_MINUS_1];
let mut elements = [Fq::from(0u64); STATE_SIZE_MINUS_1];
for i in 1..self.n_rows() {
elements[i - 1] = self.get_element(i, 0);
}
Expand All @@ -113,7 +113,7 @@ impl<
/// Ref: p.20 of the Poseidon paper
pub fn hat(&self) -> SquareMatrix<STATE_SIZE_MINUS_1, NUM_ELEMENTS_STATE_SIZE_MINUS_1_2> {
let dim = self.n_rows();
let mut mhat_elements = [Fq::zero(); NUM_ELEMENTS_STATE_SIZE_MINUS_1_2];
let mut mhat_elements = [Fq::from(0u64); NUM_ELEMENTS_STATE_SIZE_MINUS_1_2];
let mut index = 0;
for i in 1..dim {
for j in 1..dim {
Expand Down
77 changes: 51 additions & 26 deletions poseidon-parameters/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@ use crate::{matrix::SquareMatrix, matrix_ops::SquareMatrixOperations};
#[test]
fn identity_matrix() {
let identity = SquareMatrix::<2, 4>::identity();
assert_eq!(identity.get_element(0, 0), Fq::one());
assert_eq!(identity.get_element(0, 1), Fq::zero());
assert_eq!(identity.get_element(1, 1), Fq::one());
assert_eq!(identity.get_element(1, 0), Fq::zero());
assert_eq!(identity.get_element(0, 0), Fq::from(1u64));
assert_eq!(identity.get_element(0, 1), Fq::from(0u64));
assert_eq!(identity.get_element(1, 1), Fq::from(1u64));
assert_eq!(identity.get_element(1, 0), Fq::from(0u64));
}

#[test]
fn square_matmul() {
let identity = SquareMatrix::<2, 4>::identity();

let elements = &[Fq::one(), Fq::from(2u64), Fq::from(3u64), Fq::from(4u64)];
let elements = &[
Fq::from(1u64),
Fq::from(2u64),
Fq::from(3u64),
Fq::from(4u64),
];
let matrix_2x2 = SquareMatrix::<2, 4>::new(elements);

let res: SquareMatrix<2, 4> = square_mat_mul(&matrix_2x2, &identity);
assert_eq!(res.get_element(0, 0), Fq::one());
assert_eq!(res.get_element(0, 0), Fq::from(1u64));
assert_eq!(res.get_element(0, 1), Fq::from(2u64));
assert_eq!(res.get_element(1, 0), Fq::from(3u64));
assert_eq!(res.get_element(1, 1), Fq::from(4u64));
Expand All @@ -33,7 +38,7 @@ fn square_matmul() {
#[test]
fn nonsquare_matmul_happy() {
let test_elements = &[
Fq::one(),
Fq::from(1u64),
Fq::from(2u64),
Fq::from(3u64),
Fq::from(4u64),
Expand All @@ -58,7 +63,7 @@ fn nonsquare_matmul_happy() {
#[test]
fn hadamard_product() {
let test_elements: [Fq; 6] = [
Fq::one(),
Fq::from(1u64),
Fq::from(2u64),
Fq::from(3u64),
Fq::from(4u64),
Expand All @@ -79,7 +84,7 @@ fn hadamard_product() {
#[test]
fn transpose() {
let test_elements = &[
Fq::one(),
Fq::from(1u64),
Fq::from(2u64),
Fq::from(3u64),
Fq::from(4u64),
Expand All @@ -99,11 +104,16 @@ fn transpose() {
assert_eq!(res.get_element(0, 2), Fq::from(5u64));
assert_eq!(res.get_element(1, 2), Fq::from(6u64));

let test_elements = &[Fq::one(), Fq::from(2u64), Fq::from(3u64), Fq::from(4u64)];
let test_elements = &[
Fq::from(1u64),
Fq::from(2u64),
Fq::from(3u64),
Fq::from(4u64),
];
let matrix_2x2 = SquareMatrix::<2, 4>::new(test_elements);

let res = matrix_2x2.transpose();
assert_eq!(res.get_element(0, 0), Fq::one());
assert_eq!(res.get_element(0, 0), Fq::from(1u64));
assert_eq!(res.get_element(0, 1), Fq::from(3u64));
assert_eq!(res.get_element(1, 0), Fq::from(2u64));
assert_eq!(res.get_element(1, 1), Fq::from(4u64));
Expand All @@ -112,18 +122,25 @@ fn transpose() {
#[test]
fn cofactors() {
let identity_1x1 = SquareMatrix::<1, 1>::identity();
let test_elements = &[Fq::one()];
let test_elements = &[Fq::from(1u64)];
let expected_res = SquareMatrix::new(test_elements);
assert_eq!(identity_1x1.cofactors(), expected_res);

let identity_2x2 = SquareMatrix::<2, 4>::identity();
let test_elements = &[Fq::one(), -Fq::one(), -Fq::one(), Fq::one()];
let test_elements = &[
Fq::from(1u64),
-Fq::from(1u64),
-Fq::from(1u64),
Fq::from(1u64),
];
let expected_res = SquareMatrix::new(test_elements);
assert_eq!(identity_2x2.cofactors(), expected_res);
}

fn fq_strategy() -> BoxedStrategy<Fq> {
any::<[u64; 4]>().prop_map(Fq::from_le_limbs).boxed()
any::<[u8; 16]>()
.prop_map(|bytes| Fq::from_le_bytes_mod_order(&bytes[..]))
.boxed()
}

proptest! {
Expand All @@ -145,8 +162,12 @@ fn inverse() {
SquareMatrix::<1, 1>::identity()
);

let matrix_2x2 =
SquareMatrix::<2, 4>::new(&[Fq::one(), Fq::from(2u64), Fq::from(3u64), Fq::from(4u64)]);
let matrix_2x2 = SquareMatrix::<2, 4>::new(&[
Fq::from(1u64),
Fq::from(2u64),
Fq::from(3u64),
Fq::from(4u64),
]);

let res = matrix_2x2.inverse().unwrap();
assert_eq!(
Expand Down Expand Up @@ -184,28 +205,32 @@ fn inverse() {
Fq::from(2u64),
-Fq::from(3u64),
Fq::from(0u64),
]) * (Fq::one() / Fq::from(10u64));
]) * (Fq::from(1u64) / Fq::from(10u64));
assert_eq!(res, expected_res);
}

#[test]
fn create_matrix_from_array() {
let matrix_2x2 =
SquareMatrix::<2, 4>::new(&[Fq::one(), Fq::from(2u64), Fq::from(3u64), Fq::from(4u64)]);
assert_eq!(matrix_2x2.get_element(0, 0), Fq::one());
let matrix_2x2 = SquareMatrix::<2, 4>::new(&[
Fq::from(1u64),
Fq::from(2u64),
Fq::from(3u64),
Fq::from(4u64),
]);
assert_eq!(matrix_2x2.get_element(0, 0), Fq::from(1u64));
assert_eq!(matrix_2x2.get_element(0, 1), Fq::from(2u64));
assert_eq!(matrix_2x2.get_element(1, 0), Fq::from(3u64));
assert_eq!(matrix_2x2.get_element(1, 1), Fq::from(4u64));

let matrix_2x3 = Matrix::<2, 3, 6>::new(&[
Fq::one(),
Fq::from(1u64),
Fq::from(2u64),
Fq::from(3u64),
Fq::from(4u64),
Fq::from(5u64),
Fq::from(6u64),
]);
assert_eq!(matrix_2x3.get_element(0, 0), Fq::one());
assert_eq!(matrix_2x3.get_element(0, 0), Fq::from(1u64));
assert_eq!(matrix_2x3.get_element(0, 1), Fq::from(2u64));
assert_eq!(matrix_2x3.get_element(0, 2), Fq::from(3u64));
assert_eq!(matrix_2x3.get_element(1, 0), Fq::from(4u64));
Expand All @@ -215,11 +240,11 @@ fn create_matrix_from_array() {

#[test]
fn determinant() {
let matrix_1x1 = SquareMatrix::<1, 1>::new(&[Fq::one()]);
assert_eq!(matrix_1x1.determinant(), Fq::one());
let matrix_1x1 = SquareMatrix::<1, 1>::new(&[Fq::from(1u64)]);
assert_eq!(matrix_1x1.determinant(), Fq::from(1u64));

let a = Fq::one();
let b = Fq::one() + Fq::one();
let a = Fq::from(1u64);
let b = Fq::from(1u64) + Fq::from(1u64);
let c = Fq::from(3u64);
let d = Fq::from(4u64);
let matrix_2x2 = SquareMatrix::<2, 4>::new(&[a, b, c, d]);
Expand Down
2 changes: 1 addition & 1 deletion poseidon-paramgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ rand_core = { version = "0.6.3", default-features = false, features = ["getrando
poseidon-parameters = { path = "../poseidon-parameters", default-features = false, version = "1.0" }

[dev-dependencies]
decaf377 = "0.8"
decaf377 = { git = "https://github.com/penumbra-zone/decaf377.git", branch="for-poseidon377", default-features = false }
ark-bn254 = "0.4"
ark-ed-on-bls12-377 = "0.4"
ark-ed-on-bls12-381 = "0.4"
Expand Down
3 changes: 2 additions & 1 deletion poseidon-permutation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/penumbra-zone/poseidon377"

[dependencies]
decaf377 = { version="0.8", default-features = false }
decaf377 = { git = "https://github.com/penumbra-zone/decaf377.git", branch="for-poseidon377", default-features = false }
ark-ff = { version = "0.4", default-features = false, optional=true }
ark-std = { version = "^0.4.0", default-features = false, optional=true }
ark-r1cs-std = {version = "0.4", default-features = false, optional=true }
Expand All @@ -23,4 +23,5 @@ std = [
"ark-ff/std",
"ark-std/std",
"poseidon-parameters/std",
"decaf377/std",
]
12 changes: 6 additions & 6 deletions poseidon-permutation/src/permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<
) -> Self {
Self {
parameters,
state_words: [Fq::zero(); STATE_SIZE],
state_words: [Fq::from(0u64); STATE_SIZE],
}
}

Expand Down Expand Up @@ -222,7 +222,7 @@ impl<
fn partial_sub_words(&mut self) {
match self.parameters.alpha {
Alpha::Exponent(exp) => self.state_words[0] = (self.state_words[0]).pow([exp as u64]),
Alpha::Inverse => self.state_words[0] = Fq::one() / self.state_words[0],
Alpha::Inverse => self.state_words[0] = Fq::from(1u64) / self.state_words[0],
}
}

Expand All @@ -236,15 +236,15 @@ impl<
}
Alpha::Inverse => {
for i in 0..STATE_SIZE {
self.state_words[i] = Fq::one() / self.state_words[i];
self.state_words[i] = Fq::from(1u64) / self.state_words[i];
}
}
}
}

/// Applies the `MixLayer` using the M_i matrix.
fn mix_layer_mi(&mut self) {
let mut new_state_words = [Fq::zero(); STATE_SIZE];
let mut new_state_words = [Fq::from(0u64); STATE_SIZE];
for (i, row) in self.parameters.optimized_mds.M_i.iter_rows().enumerate() {
let sum = row
.iter()
Expand All @@ -258,7 +258,7 @@ impl<

/// Applies the `MixLayer` using the MDS matrix.
fn mix_layer_mds(&mut self) {
let mut new_state_words = [Fq::zero(); STATE_SIZE];
let mut new_state_words = [Fq::from(0u64); STATE_SIZE];

for (i, row) in self.parameters.mds.0 .0.iter_rows().enumerate() {
let sum = row
Expand All @@ -275,7 +275,7 @@ impl<
fn sparse_mat_mul(&mut self, round_number: usize) {
// mul_row = [(state_words[0] * v[i]) for i in range(0, t-1)]
// add_row = [(mul_row[i] + state_words[i+1]) for i in range(0, t-1)]
let mut add_row = [Fq::zero(); STATE_SIZE_MINUS_1];
let mut add_row = [Fq::from(0u64); STATE_SIZE_MINUS_1];
for (i, x) in self.parameters.optimized_mds.v_collection[round_number]
.elements
.iter()
Expand Down
8 changes: 2 additions & 6 deletions poseidon377/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ ark-groth16 = { version = "0.4", default-features = false, optional=true }
ark-r1cs-std = {version = "0.4", default-features = false, optional=true }
ark-relations = { version="0.4", default-features = false, optional=true }
ark-snark = { version = "0.4", default-features = false, optional=true }
decaf377 = { version="0.8", default-features = false, features = ["r1cs"] }
num-bigint = { version = "0.4.3", default-features = false }
once_cell = { version = "1.8", default-features = false }
decaf377 = { git = "https://github.com/penumbra-zone/decaf377.git", branch="for-poseidon377", default-features = false }
poseidon-parameters = { version="1.0", path = "../poseidon-parameters", default-features = false }
poseidon-permutation = { version="1.0", path = "../poseidon-permutation", default-features = false }

Expand All @@ -37,7 +35,7 @@ ark-test-curves = { version = "0.4", features = ["bls12_381_curve", "mnt4_753_cu

[features]
default = ["arkworks"]
alloc = ["once_cell/alloc"]
alloc = ["decaf377/alloc"]
arkworks = ["std",
"decaf377/arkworks",
"ark-ff/std",
Expand All @@ -49,8 +47,6 @@ arkworks = ["std",
"ark-serialize/std"]
std = [
"alloc",
"num-bigint/std",
"once_cell/std",
]
r1cs = [
"arkworks",
Expand Down
Loading

0 comments on commit 0e50735

Please sign in to comment.