pub struct LinearRelation<G: PrimeGroup> {
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: PrimeGroup> LinearRelation<G>
impl<G: PrimeGroup> LinearRelation<G>
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 ofLinearCombinationrepresenting 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 ofLinearCombinationrepresenting 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_scalars_vec(&mut self, n: usize) -> Vec<ScalarVar<G>>
pub fn allocate_scalars_vec(&mut self, n: usize) -> Vec<ScalarVar<G>>
Allocates a vector of new scalar variables.
§Returns
A vector 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_vec(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_element_with(&mut self, element: G) -> GroupVar<G>
pub fn allocate_element_with(&mut self, element: G) -> GroupVar<G>
Allocates a point variable (group element) and sets it immediately to the given value
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 allocate_elements_vec(&mut self, n: usize) -> Vec<GroupVar<G>>
pub fn allocate_elements_vec(&mut self, n: usize) -> Vec<GroupVar<G>>
Allocates a vector of new point variables (group elements).
§Returns
A vector 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_vec(10);Sourcepub fn allocate_elements_with(&mut self, elements: &[G]) -> Vec<GroupVar<G>>
pub fn allocate_elements_with(&mut self, elements: &[G]) -> Vec<GroupVar<G>>
Allocates a point variable (group element) and sets it immediately to the given value.
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::Scalar]) -> Result<(), Error>where
G: MultiScalarMul,
pub fn compute_image(&mut self, scalars: &[G::Scalar]) -> Result<(), Error>where
G: MultiScalarMul,
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>, InvalidInstance>
pub fn image(&self) -> Result<Vec<G>, InvalidInstance>
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 canonical(&self) -> Result<CanonicalLinearRelation<G>, InvalidInstance>where
G: MultiScalarMul,
pub fn canonical(&self) -> Result<CanonicalLinearRelation<G>, InvalidInstance>where
G: MultiScalarMul,
Construct a CanonicalLinearRelation from this generalized linear relation.
The construction may fail if the linear relation is malformed, unsatisfiable, or trivial.
Source§impl<G> LinearRelation<G>where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize + MultiScalarMul,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
impl<G> LinearRelation<G>where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize + MultiScalarMul,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
Sourcepub fn into_nizk(
self,
session_identifier: &[u8],
) -> Result<Nizk<CanonicalLinearRelation<G>>>where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
pub fn into_nizk(
self,
session_identifier: &[u8],
) -> Result<Nizk<CanonicalLinearRelation<G>>>where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
Convert this LinearRelation into a non-interactive zero-knowledge protocol using the Fiat-Shamir transform.
This is a convenience method that combines .canonical() and .into_nizk().
§Parameters
session_identifier: Domain separator bytes for the Fiat-Shamir transform
§Returns
A Nizk 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 directly
let nizk = relation.into_nizk(b"my-protocol-v1").unwrap();
let proof = nizk.prove_batchable(&vec![x], &mut OsRng).unwrap();
assert!(nizk.verify_batchable(&proof).is_ok());Trait Implementations§
Source§impl<G: Clone + PrimeGroup> Clone for LinearRelation<G>
impl<G: Clone + PrimeGroup> Clone for LinearRelation<G>
Source§fn clone(&self) -> LinearRelation<G>
fn clone(&self) -> LinearRelation<G>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<G: Debug + PrimeGroup> Debug for LinearRelation<G>
impl<G: Debug + PrimeGroup> Debug for LinearRelation<G>
Source§impl<G: Default + PrimeGroup> Default for LinearRelation<G>
impl<G: Default + PrimeGroup> Default for LinearRelation<G>
Source§fn default() -> LinearRelation<G>
fn default() -> LinearRelation<G>
Source§impl<G: PrimeGroup + MultiScalarMul> TryFrom<&LinearRelation<G>> for CanonicalLinearRelation<G>
impl<G: PrimeGroup + MultiScalarMul> TryFrom<&LinearRelation<G>> for CanonicalLinearRelation<G>
Source§type Error = InvalidInstance
type Error = InvalidInstance
Source§impl<G: PrimeGroup + MultiScalarMul> TryFrom<LinearRelation<G>> for CanonicalLinearRelation<G>
impl<G: PrimeGroup + MultiScalarMul> TryFrom<LinearRelation<G>> for CanonicalLinearRelation<G>
Source§type Error = InvalidInstance
type Error = InvalidInstance
Source§impl<G: PrimeGroup + MultiScalarMul> TryFrom<LinearRelation<G>> for ComposedRelation<G>
impl<G: PrimeGroup + MultiScalarMul> TryFrom<LinearRelation<G>> for ComposedRelation<G>
Source§type Error = InvalidInstance
type Error = InvalidInstance
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> UnsafeUnpin 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.