pub struct LinearRelation<G>where
G: Group + GroupEncoding,{
pub linear_map: LinearMap<G>,
pub image: Vec<GroupVar<G>>,
}
Expand description
A wrapper struct coupling a LinearMap
with the corresponding expected output (image) elements.
This structure represents the preimage problem for a group linear map: given a set of scalar inputs, determine whether their image under the linear map matches a target set of group elements.
Internally, the constraint system is defined through:
Fields§
§linear_map: LinearMap<G>
The underlying linear map describing the structure of the statement.
image: Vec<GroupVar<G>>
Indices pointing to elements representing the “target” images for each constraint.
Implementations§
Source§impl<G> LinearRelation<G>where
G: Group + GroupEncoding,
impl<G> LinearRelation<G>where
G: Group + GroupEncoding,
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty LinearRelation
.
Sourcepub fn append_equation(
&mut self,
lhs: GroupVar<G>,
rhs: impl Into<LinearCombination<G>>,
)
pub fn append_equation( &mut self, lhs: GroupVar<G>, rhs: impl Into<LinearCombination<G>>, )
Adds a new equation to the statement of the form:
lhs = Σ weight_i * (scalar_i * point_i)
.
§Parameters
lhs
: The image group element variable (left-hand side of the equation).rhs
: An instance ofLinearCombination
representing the linear combination on the right-hand side.
Sourcepub fn allocate_eq(
&mut self,
rhs: impl Into<LinearCombination<G>>,
) -> GroupVar<G>
pub fn allocate_eq( &mut self, rhs: impl Into<LinearCombination<G>>, ) -> GroupVar<G>
Adds a new equation to the statement of the form:
lhs = Σ weight_i * (scalar_i * point_i)
without allocating lhs
.
§Parameters
rhs
: An instance ofLinearCombination
representing the linear combination on the right-hand side.
Sourcepub fn allocate_scalar(&mut self) -> ScalarVar<G>
pub fn allocate_scalar(&mut self) -> ScalarVar<G>
Allocates a scalar variable for use in the linear map.
Sourcepub fn allocate_scalars<const N: usize>(&mut self) -> [ScalarVar<G>; N]
pub fn allocate_scalars<const N: usize>(&mut self) -> [ScalarVar<G>; N]
Allocates space for N
new scalar variables.
§Returns
An array of ScalarVar
representing the newly allocated scalar indices.
§Example
use curve25519_dalek::RistrettoPoint as G;
let mut relation = LinearRelation::<G>::new();
let [var_x, var_y] = relation.allocate_scalars();
let vars = relation.allocate_scalars::<10>();
Sourcepub fn allocate_element(&mut self) -> GroupVar<G>
pub fn allocate_element(&mut self) -> GroupVar<G>
Allocates a point variable (group element) for use in the linear map.
Sourcepub fn allocate_elements<const N: usize>(&mut self) -> [GroupVar<G>; N]
pub fn allocate_elements<const N: usize>(&mut self) -> [GroupVar<G>; N]
Allocates N
point variables (group elements) for use in the linear map.
§Returns
An array of GroupVar
representing the newly allocated group element indices.
§Example
use curve25519_dalek::RistrettoPoint as G;
let mut relation = LinearRelation::<G>::new();
let [var_g, var_h] = relation.allocate_elements();
let vars = relation.allocate_elements::<10>();
Sourcepub fn set_element(&mut self, var: GroupVar<G>, element: G)
pub fn set_element(&mut self, var: GroupVar<G>, element: G)
Sourcepub fn set_elements(
&mut self,
assignments: impl IntoIterator<Item = (GroupVar<G>, G)>,
)
pub fn set_elements( &mut self, assignments: impl IntoIterator<Item = (GroupVar<G>, G)>, )
Sourcepub fn compute_image(
&mut self,
scalars: &[<G as Group>::Scalar],
) -> Result<(), Error>
pub fn compute_image( &mut self, scalars: &[<G as Group>::Scalar], ) -> Result<(), Error>
Evaluates all linear combinations in the linear map with the provided scalars, computing the left-hand side of this constraints (i.e. the image).
After calling this function, all point variables will be assigned.
§Parameters
scalars
: A slice of scalar values corresponding to the scalar variables.
§Returns
Return Ok
on success, and an error if unassigned elements prevent the image from being
computed. Modifies the group elements assigned in the LinearRelation.
Sourcepub fn image(&self) -> Result<Vec<G>, Error>
pub fn image(&self) -> Result<Vec<G>, Error>
Returns the current group elements corresponding to the image variables.
§Returns
A vector of group elements (Vec<G>
) representing the linear map’s image.
Sourcepub fn label(&self) -> Vec<u8> ⓘ
pub fn label(&self) -> Vec<u8> ⓘ
Returns a binary label describing the linear map.
The format is:
- [Ne: u32] number of equations
- For each equation:
- [output_point_index: u32]
- [Nt: u32] number of terms
- Nt × [scalar_index: u32, point_index: u32] term entries
Sourcepub fn into_nizk(
self,
session_identifier: &[u8],
) -> NISigmaProtocol<SchnorrProof<G>, ShakeCodec<G>>where
G: GroupEncoding,
pub fn into_nizk(
self,
session_identifier: &[u8],
) -> NISigmaProtocol<SchnorrProof<G>, ShakeCodec<G>>where
G: GroupEncoding,
Convert this LinearRelation into a non-interactive zero-knowledge protocol using the ShakeCodec and a specified context/domain separator.
§Parameters
context
: Domain separator bytes for the Fiat-Shamir transform
§Returns
A NISigmaProtocol
instance ready for proving and verification
§Example
let mut relation = LinearRelation::<G>::new();
let x_var = relation.allocate_scalar();
let g_var = relation.allocate_element();
let p_var = relation.allocate_eq(x_var * g_var);
relation.set_element(g_var, G::generator());
let x = Scalar::random(&mut OsRng);
relation.compute_image(&[x]).unwrap();
// Convert to NIZK with custom context
let nizk = relation.into_nizk(b"my-protocol-v1");
let proof = nizk.prove_batchable(&vec![x], &mut OsRng).unwrap();
assert!(nizk.verify_batchable(&proof).is_ok());
Trait Implementations§
Source§impl<G> Clone for LinearRelation<G>
impl<G> Clone for LinearRelation<G>
Source§fn clone(&self) -> LinearRelation<G>
fn clone(&self) -> LinearRelation<G>
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<G> Debug for LinearRelation<G>
impl<G> Debug for LinearRelation<G>
Source§impl<G> Default for LinearRelation<G>
impl<G> Default for LinearRelation<G>
Source§fn default() -> LinearRelation<G>
fn default() -> LinearRelation<G>
Source§impl<G> From<LinearRelation<G>> for Protocol<G>where
G: Group + GroupEncoding,
impl<G> From<LinearRelation<G>> for Protocol<G>where
G: Group + GroupEncoding,
Source§fn from(value: LinearRelation<G>) -> Self
fn from(value: LinearRelation<G>) -> Self
Source§impl<G> From<LinearRelation<G>> for SchnorrProof<G>where
G: Group + GroupEncoding,
impl<G> From<LinearRelation<G>> for SchnorrProof<G>where
G: Group + GroupEncoding,
Source§fn from(value: LinearRelation<G>) -> Self
fn from(value: LinearRelation<G>) -> Self
Auto Trait Implementations§
impl<G> Freeze for LinearRelation<G>
impl<G> RefUnwindSafe for LinearRelation<G>
impl<G> Send for LinearRelation<G>
impl<G> Sync for LinearRelation<G>
impl<G> Unpin for LinearRelation<G>
impl<G> UnwindSafe for LinearRelation<G>
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> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.