Skip to main content

PermutationRelation

Struct PermutationRelation 

Source
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>

Source

pub fn new() -> Self

An unlabeled relation over a fresh VarAllocator.

Source

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.

Source

pub fn with_allocator(allocator: VarAllocator<T>) -> Self

A relation sharing allocator with other relations.

Source

pub fn label(&self) -> &str

The permutation’s label, empty unless set by Self::labeled.

Source

pub const fn allocator(&self) -> &VarAllocator<T>

The allocator, for sharing wires with another relation.

Source

pub fn allocate_var(&self) -> FieldVar

Source

pub fn allocate_vars<const N: usize>(&self) -> [FieldVar; N]

Source

pub fn allocate_vars_vec(&self, count: usize) -> Vec<FieldVar>

Source

pub fn allocate_var_with(&self, value: T) -> FieldVar
where T: PartialEq,

Source

pub fn allocate_vars_with<const N: usize>( &self, values: &[T; N], ) -> [FieldVar; N]
where T: PartialEq,

Source

pub fn allocate_vars_vec_with(&self, values: &[T]) -> Vec<FieldVar>
where T: PartialEq,

Source

pub fn set_var(&self, var: FieldVar, value: T)
where T: PartialEq,

Source

pub fn set_vars<Var, Val>( &self, vars: impl IntoIterator<Item = Var>, values: impl IntoIterator<Item = Val>, )
where Var: Borrow<FieldVar>, Val: Borrow<T>, T: PartialEq,

Source

pub fn allocate_permutation( &self, input: &[FieldVar; WIDTH], ) -> [FieldVar; WIDTH]

Records a query of the permutation on input, returning fresh output wires.

Source

pub fn add_permutation( &self, input: [FieldVar; WIDTH], output: [FieldVar; WIDTH], )

Records that the permutation maps input to output.

Source

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.

Source

pub fn queries(&self) -> Vec<QueryAnswerPair<FieldVar, WIDTH>>

The queries recorded so far.

Source

pub fn equations(&self) -> Vec<LinearEquation<T>>

The equations recorded so far.

Source

pub fn public_vars(&self) -> Vec<(FieldVar, T)>

Source

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>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Unit, const WIDTH: usize> Default for PermutationRelation<T, WIDTH>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Unit, const WIDTH: usize> Permutation<WIDTH> for PermutationRelation<T, WIDTH>

Source§

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.

Source§

type U = FieldVar

The Unit defining the alphabet for the permutation function.
Source§

fn permute(&self, state: &[Self::U; WIDTH]) -> [Self::U; WIDTH]

The permutation function. Read more

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>

§

impl<T, const WIDTH: usize> Send for PermutationRelation<T, WIDTH>

§

impl<T, const WIDTH: usize> Sync for PermutationRelation<T, WIDTH>

§

impl<T, const WIDTH: usize> Unpin for PermutationRelation<T, WIDTH>

§

impl<T, const WIDTH: usize> UnsafeUnpin for PermutationRelation<T, WIDTH>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more