Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
Generalize symmetric primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
dlubarov committed Mar 30, 2023
1 parent 7261c03 commit 594c7ee
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["brakedown", "challenger", "code", "field", "pcs", "multistark", "symmetric", "tensor_pcs"]
members = ["brakedown", "challenger", "code", "field", "fri", "pcs", "multistark", "symmetric", "tensor_pcs"]
4 changes: 2 additions & 2 deletions challenger/src/duplex_challenger.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::marker::PhantomData;
use hyperfield::field::Field;
use p3_symmetric::permutation::AlgebraicPermutation;
use p3_symmetric::permutation::CryptographicPermutation;

pub struct DuplexChallenger<F: Field, P: AlgebraicPermutation<F, WIDTH>, const WIDTH: usize> {
pub struct DuplexChallenger<F: Field, P: CryptographicPermutation<F, WIDTH>, const WIDTH: usize> {
_permutation: P,
_phantom_f: PhantomData<F>,
}
8 changes: 4 additions & 4 deletions challenger/src/hash_challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use alloc::vec;
use alloc::vec::Vec;
use core::marker::PhantomData;
use hyperfield::field::Field;
use p3_symmetric::hash::AlgebraicHash;
use p3_symmetric::hasher::CryptographicHasher;

pub struct HashChallenger<F: Field, H: AlgebraicHash<F, OUT_WIDTH>, const OUT_WIDTH: usize> {
pub struct HashChallenger<F: Field, H: CryptographicHasher<F, OUT_WIDTH>, const OUT_WIDTH: usize> {
input_buffer: Vec<F>,
output_buffer: Vec<F>,
_phantom_f: PhantomData<F>,
_phantom_h: PhantomData<H>,
}

impl<F: Field, H: AlgebraicHash<F, OUT_WIDTH>, const OUT_WIDTH: usize>
impl<F: Field, H: CryptographicHasher<F, OUT_WIDTH>, const OUT_WIDTH: usize>
HashChallenger<F, H, OUT_WIDTH>
{
pub fn new(initial_state: Vec<F>) -> Self {
Expand All @@ -35,7 +35,7 @@ impl<F: Field, H: AlgebraicHash<F, OUT_WIDTH>, const OUT_WIDTH: usize>
}
}

impl<F: Field, H: AlgebraicHash<F, OUT_WIDTH>, const OUT_WIDTH: usize> Challenger<F>
impl<F: Field, H: CryptographicHasher<F, OUT_WIDTH>, const OUT_WIDTH: usize> Challenger<F>
for HashChallenger<F, H, OUT_WIDTH>
{
fn observe_element(&mut self, element: F) {
Expand Down
1 change: 1 addition & 0 deletions field/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub trait Field:

pub trait FieldExtension: Field {
type Base: Field;

const D: usize;

fn to_base_array(&self) -> [Self::Base; Self::D];
Expand Down
9 changes: 9 additions & 0 deletions fri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "p3-fri"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
hyperfield = { path = "../field" }
hyperpcs = { path = "../pcs" }
23 changes: 23 additions & 0 deletions fri/src/fri_pcs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// use alloc::vec::Vec;
// use hyperfield::field::Field;
// use hyperfield::matrix::dense::DenseMatrix;
// use hyperpcs::{PCS, UnivariatePCS};
// use crate::proof::FriProof;
//
// pub struct FriPcs<F: Field, H: H>;
//
// impl<F: Field> PCS<F> for FriPcs {
// type Commitment = Hash;
// type ProverData = ProverData;
// type Proof = FriProof<F>;
//
// fn commit_batches(polynomials: Vec<DenseMatrix<F>>) -> (Commitment, ProverData) {
// todo!()
// }
// }
//
// impl<F: Field> UnivariatePCS<F> for FriPcs {}
//
// struct ProverData {
// // merkle_tree: MerkleTree<F, Hasher>,
// }
6 changes: 6 additions & 0 deletions fri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![no_std]

extern crate alloc;

pub mod fri_pcs;
pub mod proof;
5 changes: 5 additions & 0 deletions fri/src/proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use hyperfield::field::Field;

pub struct FriProof<F: Field> {
_todo: F,
}
6 changes: 0 additions & 6 deletions symmetric/src/hash.rs

This file was deleted.

5 changes: 5 additions & 0 deletions symmetric/src/hasher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use alloc::vec::Vec;

pub trait CryptographicHasher<T, const OUT_WIDTH: usize> {
fn hash(input: Vec<T>) -> [T; OUT_WIDTH];
}
2 changes: 1 addition & 1 deletion symmetric/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

extern crate alloc;

pub mod hash;
pub mod hasher;
pub mod permutation;
pub mod sponge;
6 changes: 2 additions & 4 deletions symmetric/src/permutation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use hyperfield::field::Field;

pub trait AlgebraicPermutation<F: Field, const WIDTH: usize> {
fn permute(input: [F; WIDTH]) -> [F; WIDTH];
pub trait CryptographicPermutation<T, const WIDTH: usize> {
fn permute(input: [T; WIDTH]) -> [T; WIDTH];
}
25 changes: 11 additions & 14 deletions symmetric/src/sponge.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
use crate::hash::AlgebraicHash;
use crate::permutation::AlgebraicPermutation;
use crate::hasher::CryptographicHasher;
use crate::permutation::CryptographicPermutation;
use alloc::vec::Vec;
use core::marker::PhantomData;
use hyperfield::field::Field;

/// A padding-free, overwrite-mode sponge function.
pub struct PaddingFreeAlgebraicSponge<F, P, const RATE: usize, const CAPACITY: usize>
pub struct PaddingFreeSponge<T, P, const RATE: usize, const CAPACITY: usize>
where
F: Field,
P: AlgebraicPermutation<F, { RATE + CAPACITY }>,
P: CryptographicPermutation<T, { RATE + CAPACITY }>,
{
_phantom_f: PhantomData<F>,
_phantom_f: PhantomData<T>,
_phantom_p: PhantomData<P>,
}

impl<F, P, const RATE: usize, const CAPACITY: usize> AlgebraicHash<F, RATE>
for PaddingFreeAlgebraicSponge<F, P, RATE, CAPACITY>
impl<T: Default + Copy, P, const RATE: usize, const CAPACITY: usize> CryptographicHasher<T, RATE>
for PaddingFreeSponge<T, P, RATE, CAPACITY>
where
F: Field,
P: AlgebraicPermutation<F, { RATE + CAPACITY }>,
P: CryptographicPermutation<T, { RATE + CAPACITY }>,
{
fn hash(input: Vec<F>) -> [F; RATE] {
let mut state = [F::ZERO; RATE + CAPACITY];
fn hash(input: Vec<T>) -> [T; RATE] {
let mut state = [T::default(); RATE + CAPACITY];
for input_chunk in input.chunks(RATE) {
state[..input_chunk.len()].copy_from_slice(input_chunk);
state = P::permute(state);
}
let mut output = [F::ZERO; RATE];
let mut output = [T::default(); RATE];
for i in 0..RATE {
output[i] = state[i];
}
Expand Down

0 comments on commit 594c7ee

Please sign in to comment.