sonobe_primitives/relations/mod.rs
1//! This module defines the core relation traits for generic witness-instance
2//! satisfaction checks and satisfying pair generation.
3//!
4//! These traits are intentionally generic so that different arithmetizations
5//! (R1CS, CCS) and different forms (plain, relaxed) can all implement them.
6
7use ark_relations::gr1cs::SynthesisError;
8use ark_std::{error::Error, rand::RngCore};
9
10/// [`Relation`] checks whether a witness `W` and an instance `U` satisfy the
11/// specified relation.
12pub trait Relation<W, U> {
13 /// [`Relation::Error`] defines the error type that may occur when checking
14 /// the relation.
15 type Error: Error;
16
17 /// [`Relation::check_relation`] returns `Ok(())` when `w` and `u` satisfy
18 /// `self`, or an error otherwise.
19 fn check_relation(&self, w: &W, u: &U) -> Result<(), Self::Error>;
20}
21
22/// [`RelationGadget`] is the in-circuit counterpart of [`Relation`].
23pub trait RelationGadget<WVar, UVar> {
24 /// [`RelationGadget::check_relation`] generates constraints enforcing that
25 /// `w` and `u` satisfy the relation.
26 fn check_relation(&self, w: &WVar, u: &UVar) -> Result<(), SynthesisError>;
27}
28
29/// [`WitnessInstanceSampler`] allows sampling a random witness-instance pair
30/// that satisfies the relation.
31pub trait WitnessInstanceSampler<W, U> {
32 /// [`WitnessInstanceSampler::Source`] defines the type of the source from
33 /// which a satisfying pair is sampled.
34 type Source;
35
36 /// [`WitnessInstanceSampler::Error`] defines the error type that may occur
37 /// when sampling a satisfying pair.
38 type Error: Error;
39
40 /// [`WitnessInstanceSampler::sample`] draws a random satisfying pair.
41 fn sample(&self, source: Self::Source, rng: impl RngCore) -> Result<(W, U), Self::Error>;
42}