pub struct PermutationRelation<T, const WIDTH: usize> { /* private fields */ }Expand description
A relation over evaluations of a permutation acting on WIDTH wires.
Handles are reference counted: cloning one, as
DuplexSponge::from does, shares the relation
rather than forking it, so the queries a sponge makes are visible on every
handle.
use spongefish::{DuplexSponge, DuplexSpongeInterface};
use spongefish_circuit::PermutationRelation;
let relation = PermutationRelation::<u32, 4>::new();
let public = relation.allocate_vars_with(&[1, 2]);
let secret = relation.allocate_vars::<2>();
let mut sponge = DuplexSponge::<_, 4, 2>::from(relation.clone());
let [digest] = sponge.absorb(&public).absorb(&secret).squeeze_array();
relation.set_var(digest, 42);
let instance = relation.compile().unwrap();
assert_eq!(instance.queries().len(), 2);Implementations§
Source§impl<T: Unit, const WIDTH: usize> PermutationRelation<T, WIDTH>
impl<T: Unit, const WIDTH: usize> PermutationRelation<T, WIDTH>
Sourcepub fn new() -> Self
pub fn new() -> Self
An unlabeled relation over a fresh VarAllocator.
Sourcepub fn labeled(label: impl Into<String>) -> Self
pub fn labeled(label: impl Into<String>) -> Self
A relation naming its permutation, such as keccak-f[1600].
The label travels with the compiled instance and its byte encoding, so a proof system can check it is being handed the permutation it implements. It is free text; the relation does not interpret it.
Sourcepub fn with_allocator(allocator: VarAllocator<T>) -> Self
pub fn with_allocator(allocator: VarAllocator<T>) -> Self
A relation sharing allocator with other relations.
Sourcepub fn label(&self) -> &str
pub fn label(&self) -> &str
The permutation’s label, empty unless set by Self::labeled.
Sourcepub const fn allocator(&self) -> &VarAllocator<T>
pub const fn allocator(&self) -> &VarAllocator<T>
The allocator, for sharing wires with another relation.
Sourcepub fn allocate_var(&self) -> FieldVar
pub fn allocate_var(&self) -> FieldVar
Sourcepub fn allocate_vars<const N: usize>(&self) -> [FieldVar; N]
pub fn allocate_vars<const N: usize>(&self) -> [FieldVar; N]
Sourcepub fn allocate_vars_vec(&self, count: usize) -> Vec<FieldVar>
pub fn allocate_vars_vec(&self, count: usize) -> Vec<FieldVar>
Sourcepub fn allocate_var_with(&self, value: T) -> FieldVarwhere
T: PartialEq,
pub fn allocate_var_with(&self, value: T) -> FieldVarwhere
T: PartialEq,
Sourcepub fn allocate_vars_with<const N: usize>(
&self,
values: &[T; N],
) -> [FieldVar; N]where
T: PartialEq,
pub fn allocate_vars_with<const N: usize>(
&self,
values: &[T; N],
) -> [FieldVar; N]where
T: PartialEq,
Sourcepub fn set_vars<Var, Val>(
&self,
vars: impl IntoIterator<Item = Var>,
values: impl IntoIterator<Item = Val>,
)
pub fn set_vars<Var, Val>( &self, vars: impl IntoIterator<Item = Var>, values: impl IntoIterator<Item = Val>, )
Sourcepub fn allocate_permutation(
&self,
input: &[FieldVar; WIDTH],
) -> [FieldVar; WIDTH]
pub fn allocate_permutation( &self, input: &[FieldVar; WIDTH], ) -> [FieldVar; WIDTH]
Records a query of the permutation on input, returning fresh output
wires.
Sourcepub fn add_permutation(
&self,
input: [FieldVar; WIDTH],
output: [FieldVar; WIDTH],
)
pub fn add_permutation( &self, input: [FieldVar; WIDTH], output: [FieldVar; WIDTH], )
Records that the permutation maps input to output.
Sourcepub fn add_equation(&self, terms: impl Into<Sum<T>>, image: T)
pub fn add_equation(&self, terms: impl Into<Sum<T>>, image: T)
Adds the equation terms = image.
Terms are built with the operators on FieldVar: x * a + y * b + z
weights x by a, y by b, and z by one. Every wire with a
nonzero weight must be an input or output of some query, or be
assigned; Self::compile rejects the equation otherwise, as nothing
else would fix the wire’s value.
Sourcepub fn queries(&self) -> Vec<QueryAnswerPair<FieldVar, WIDTH>>
pub fn queries(&self) -> Vec<QueryAnswerPair<FieldVar, WIDTH>>
The queries recorded so far.
Sourcepub fn equations(&self) -> Vec<LinearEquation<T>>
pub fn equations(&self) -> Vec<LinearEquation<T>>
The equations recorded so far.
Sourcepub fn public_vars(&self) -> Vec<(FieldVar, T)>
pub fn public_vars(&self) -> Vec<(FieldVar, T)>
Sourcepub fn compile(&self) -> Result<PermutationInstance<T, WIDTH>, InvalidRelation>where
T: PartialEq,
pub fn compile(&self) -> Result<PermutationInstance<T, WIDTH>, InvalidRelation>where
T: PartialEq,
Compiles the relation into a validated PermutationInstance.
Every wire a query or an equation mentions must have been allocated, and every wire with a nonzero weight in an equation must be bound: an input or output of some query, or assigned a value. Wires that are allocated but neither bound nor mentioned are left in place, so wire indices are the same on both sides of this call.
Trait Implementations§
Source§impl<T, const WIDTH: usize> Clone for PermutationRelation<T, WIDTH>
impl<T, const WIDTH: usize> Clone for PermutationRelation<T, WIDTH>
Source§impl<T: Unit, const WIDTH: usize> Permutation<WIDTH> for PermutationRelation<T, WIDTH>
impl<T: Unit, const WIDTH: usize> Permutation<WIDTH> for PermutationRelation<T, WIDTH>
Source§fn permute_mut(&self, state: &mut [Self::U; WIDTH])
fn permute_mut(&self, state: &mut [Self::U; WIDTH])
A query mints fresh output wires rather than mixing the state in
place, so both maps go through Self::allocate_permutation.
Auto Trait Implementations§
impl<T, const WIDTH: usize> !RefUnwindSafe for PermutationRelation<T, WIDTH>
impl<T, const WIDTH: usize> !UnwindSafe for PermutationRelation<T, WIDTH>
impl<T, const WIDTH: usize> Freeze for PermutationRelation<T, WIDTH>where
VarAllocator<T>: Freeze,
Arc<RwLock<Vec<QueryAnswerPair<FieldVar, WIDTH>>>>: Freeze,
Arc<RwLock<Vec<LinearEquation<T>>>>: Freeze,
impl<T, const WIDTH: usize> Send for PermutationRelation<T, WIDTH>where
VarAllocator<T>: Send,
Arc<RwLock<Vec<QueryAnswerPair<FieldVar, WIDTH>>>>: Send,
Arc<RwLock<Vec<LinearEquation<T>>>>: Send,
impl<T, const WIDTH: usize> Sync for PermutationRelation<T, WIDTH>where
VarAllocator<T>: Sync,
Arc<RwLock<Vec<QueryAnswerPair<FieldVar, WIDTH>>>>: Sync,
Arc<RwLock<Vec<LinearEquation<T>>>>: Sync,
impl<T, const WIDTH: usize> Unpin for PermutationRelation<T, WIDTH>where
VarAllocator<T>: Unpin,
Arc<RwLock<Vec<QueryAnswerPair<FieldVar, WIDTH>>>>: Unpin,
Arc<RwLock<Vec<LinearEquation<T>>>>: Unpin,
impl<T, const WIDTH: usize> UnsafeUnpin for PermutationRelation<T, WIDTH>where
VarAllocator<T>: UnsafeUnpin,
Arc<RwLock<Vec<QueryAnswerPair<FieldVar, WIDTH>>>>: UnsafeUnpin,
Arc<RwLock<Vec<LinearEquation<T>>>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more