loosen trait from PrimeField to Field

This commit is contained in:
Daniel Tehrani
2024-02-02 15:52:31 +09:00
parent b798817998
commit 573209c0cd
4 changed files with 16 additions and 12 deletions

View File

@@ -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<F: PrimeField>() -> PoseidonConstants<F> {
pub fn secp256k1_w3<F: Field + FromStr>() -> PoseidonConstants<F> {
let num_full_rounds = 8;
let num_partial_rounds = 56;

View File

@@ -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<F: PrimeField>() -> PoseidonConstants<F> {
pub fn secp256k1_w9<F: Field + FromStr>() -> PoseidonConstants<F> {
let num_full_rounds = 8;
let num_partial_rounds = 57;

View File

@@ -1,10 +1,10 @@
pub mod constants;
pub mod sponge;
use ark_ff::PrimeField;
use ark_ff::Field;
#[derive(Clone)]
pub struct PoseidonConstants<F: PrimeField> {
pub struct PoseidonConstants<F: Field> {
pub round_keys: Vec<F>,
pub mds_matrix: Vec<Vec<F>>,
pub num_full_rounds: usize,
@@ -14,13 +14,13 @@ pub struct PoseidonConstants<F: PrimeField> {
const CAPACITY: usize = 1; // We fix the capacity to be one.
#[derive(Clone)]
pub struct Poseidon<F: PrimeField, const WIDTH: usize> {
pub struct Poseidon<F: Field, const WIDTH: usize> {
pub state: [F; WIDTH],
pub constants: PoseidonConstants<F>,
pub pos: usize,
}
impl<F: PrimeField, const WIDTH: usize> Poseidon<F, WIDTH> {
impl<F: Field, const WIDTH: usize> Poseidon<F, WIDTH> {
pub fn new(constants: PoseidonConstants<F>) -> Self {
let state = [F::zero(); WIDTH];
Self {

View File

@@ -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<F: PrimeField, const W: usize> {
pub struct PoseidonSponge<F: Field, const W: usize> {
pub absorb_pos: usize,
pub squeeze_pos: usize,
pub io_count: usize,
@@ -33,7 +33,7 @@ pub struct PoseidonSponge<F: PrimeField, const W: usize> {
poseidon: Poseidon<F, W>,
}
impl<F: PrimeField, const WIDTH: usize> PoseidonSponge<F, WIDTH> {
impl<F: Field, const WIDTH: usize> PoseidonSponge<F, WIDTH> {
pub fn new(
constants: PoseidonConstants<F>,
domain_separator: &[u8],
@@ -118,7 +118,7 @@ impl<F: PrimeField, const WIDTH: usize> PoseidonSponge<F, WIDTH> {
// 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]) {