From 573209c0cd11dc74e5e5e6708a4a87205c157267 Mon Sep 17 00:00:00 2001 From: Daniel Tehrani Date: Fri, 2 Feb 2024 15:52:31 +0900 Subject: [PATCH] loosen trait from `PrimeField` to `Field` --- src/constants/secp256k1_w3.rs | 6 ++++-- src/constants/secp256k1_w9.rs | 6 ++++-- src/lib.rs | 8 ++++---- src/sponge.rs | 8 ++++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/constants/secp256k1_w3.rs b/src/constants/secp256k1_w3.rs index 613bc02..01be3dd 100644 --- a/src/constants/secp256k1_w3.rs +++ b/src/constants/secp256k1_w3.rs @@ -1,11 +1,13 @@ +use std::str::FromStr; + use crate::PoseidonConstants; -use ark_ff::PrimeField; +use ark_ff::Field; // We dynamically set the constants for the secp256k1 curve instead a hardcoding, // because hardcoding requires us to use the `ark_secp256k1::Fq` type, which // is hard to use in structs/functions defined with generic types. -pub fn secp256k1_w3() -> PoseidonConstants { +pub fn secp256k1_w3() -> PoseidonConstants { let num_full_rounds = 8; let num_partial_rounds = 56; diff --git a/src/constants/secp256k1_w9.rs b/src/constants/secp256k1_w9.rs index 0e335bc..8a8c8d9 100644 --- a/src/constants/secp256k1_w9.rs +++ b/src/constants/secp256k1_w9.rs @@ -1,11 +1,13 @@ +use std::str::FromStr; + use crate::PoseidonConstants; -use ark_ff::PrimeField; +use ark_ff::Field; // We dynamically set the constants for the secp256k1 curve instead a hardcoding, // because hardcoding requires us to use the `ark_secp256k1::Fq` type, which // is hard to use in structs/functions defined with generic types. -pub fn secp256k1_w9() -> PoseidonConstants { +pub fn secp256k1_w9() -> PoseidonConstants { let num_full_rounds = 8; let num_partial_rounds = 57; diff --git a/src/lib.rs b/src/lib.rs index 4f69ecd..f67aaae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,10 @@ pub mod constants; pub mod sponge; -use ark_ff::PrimeField; +use ark_ff::Field; #[derive(Clone)] -pub struct PoseidonConstants { +pub struct PoseidonConstants { pub round_keys: Vec, pub mds_matrix: Vec>, pub num_full_rounds: usize, @@ -14,13 +14,13 @@ pub struct PoseidonConstants { const CAPACITY: usize = 1; // We fix the capacity to be one. #[derive(Clone)] -pub struct Poseidon { +pub struct Poseidon { pub state: [F; WIDTH], pub constants: PoseidonConstants, pub pos: usize, } -impl Poseidon { +impl Poseidon { pub fn new(constants: PoseidonConstants) -> Self { let state = [F::zero(); WIDTH]; Self { diff --git a/src/sponge.rs b/src/sponge.rs index 533a078..6e2b39c 100644 --- a/src/sponge.rs +++ b/src/sponge.rs @@ -1,5 +1,5 @@ use crate::{Poseidon, PoseidonConstants}; -use ark_ff::PrimeField; +use ark_ff::Field; use std::result::Result; use tiny_keccak::{Hasher, Keccak}; @@ -22,7 +22,7 @@ impl IOPattern { // Implements SAFE (Sponge API for Field Elements): https://hackmd.io/bHgsH6mMStCVibM_wYvb2w #[derive(Clone)] -pub struct PoseidonSponge { +pub struct PoseidonSponge { pub absorb_pos: usize, pub squeeze_pos: usize, pub io_count: usize, @@ -33,7 +33,7 @@ pub struct PoseidonSponge { poseidon: Poseidon, } -impl PoseidonSponge { +impl PoseidonSponge { pub fn new( constants: PoseidonConstants, domain_separator: &[u8], @@ -118,7 +118,7 @@ impl PoseidonSponge { // TODO: Support variable field size tag.extend_from_slice(&[0; 16]); - F::from_le_bytes_mod_order(&tag) + F::from_random_bytes(&tag).unwrap() } pub fn absorb(&mut self, x: &[F]) {