1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use 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()
}
}