ark_relations/r1cs/
mod.rsuse ark_std::vec::Vec;
pub type Result<T> = core::result::Result<T, SynthesisError>;
#[macro_use]
mod impl_lc;
mod constraint_system;
mod error;
#[cfg(feature = "std")]
mod trace;
#[cfg(feature = "std")]
pub use crate::r1cs::trace::{ConstraintLayer, ConstraintTrace, TraceStep, TracingMode};
pub use tracing::info_span;
pub use ark_ff::{Field, ToConstraintField};
pub use constraint_system::{
ConstraintMatrices, ConstraintSynthesizer, ConstraintSystem, ConstraintSystemRef, Namespace,
OptimizationGoal, SynthesisMode,
};
pub use error::SynthesisError;
use core::cmp::Ordering;
pub type Matrix<F> = Vec<Vec<(F, usize)>>;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct LcIndex(usize);
#[derive(Copy, Clone, PartialEq, Debug, Eq)]
pub enum Variable {
Zero,
One,
Instance(usize),
Witness(usize),
SymbolicLc(LcIndex),
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct LinearCombination<F: Field>(pub Vec<(F, Variable)>);
#[macro_export]
macro_rules! ns {
($cs:expr, $name:expr) => {{
let span = $crate::r1cs::info_span!(target: "r1cs", $name);
let id = span.id();
let _enter_guard = span.enter();
core::mem::forget(_enter_guard);
core::mem::forget(span);
$crate::r1cs::Namespace::new($cs.clone(), id)
}};
}
impl Variable {
#[inline]
pub fn is_zero(&self) -> bool {
matches!(self, Variable::Zero)
}
#[inline]
pub fn is_one(&self) -> bool {
matches!(self, Variable::One)
}
#[inline]
pub fn is_instance(&self) -> bool {
matches!(self, Variable::Instance(_))
}
#[inline]
pub fn is_witness(&self) -> bool {
matches!(self, Variable::Witness(_))
}
#[inline]
pub fn is_lc(&self) -> bool {
matches!(self, Variable::SymbolicLc(_))
}
#[inline]
pub fn get_lc_index(&self) -> Option<LcIndex> {
match self {
Variable::SymbolicLc(index) => Some(*index),
_ => None,
}
}
#[inline]
pub fn get_index_unchecked(&self, witness_offset: usize) -> Option<usize> {
match self {
Variable::One => Some(0),
Variable::Instance(i) => Some(*i),
Variable::Witness(i) => Some(witness_offset + *i),
_ => None,
}
}
}
impl PartialOrd for Variable {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
use Variable::*;
match (self, other) {
(Zero, Zero) => Some(Ordering::Equal),
(One, One) => Some(Ordering::Equal),
(Zero, _) => Some(Ordering::Less),
(One, _) => Some(Ordering::Less),
(_, Zero) => Some(Ordering::Greater),
(_, One) => Some(Ordering::Greater),
(Instance(i), Instance(j)) | (Witness(i), Witness(j)) => i.partial_cmp(j),
(Instance(_), Witness(_)) => Some(Ordering::Less),
(Witness(_), Instance(_)) => Some(Ordering::Greater),
(SymbolicLc(i), SymbolicLc(j)) => i.partial_cmp(j),
(_, SymbolicLc(_)) => Some(Ordering::Less),
(SymbolicLc(_), _) => Some(Ordering::Greater),
}
}
}
impl Ord for Variable {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}