snarkvm_circuit_environment/
environment.rs1use crate::{Assignment, Inject, LinearCombination, Mode, R1CS, Variable, witness_mode};
17use snarkvm_curves::AffineCurve;
18use snarkvm_fields::traits::*;
19
20use core::{fmt, hash};
21
22pub trait Environment: 'static + Copy + Clone + fmt::Debug + fmt::Display + Eq + PartialEq + hash::Hash {
24 type Network: console::Network<Affine = Self::Affine, Field = Self::BaseField, Scalar = Self::ScalarField>;
25
26 type Affine: AffineCurve<
27 BaseField = Self::BaseField,
28 ScalarField = Self::ScalarField,
29 Coordinates = (Self::BaseField, Self::BaseField),
30 >;
31 type BaseField: PrimeField + SquareRootField + Copy;
32 type ScalarField: PrimeField<BigInteger = <Self::BaseField as PrimeField>::BigInteger> + Copy;
33
34 const EDWARDS_A: Self::BaseField = <Self::Network as console::Environment>::EDWARDS_A;
36 const EDWARDS_D: Self::BaseField = <Self::Network as console::Environment>::EDWARDS_D;
38
39 const MONTGOMERY_A: Self::BaseField = <Self::Network as console::Environment>::MONTGOMERY_A;
41 const MONTGOMERY_B: Self::BaseField = <Self::Network as console::Environment>::MONTGOMERY_B;
43
44 const MAX_STRING_BYTES: u32 = <Self::Network as console::Environment>::MAX_STRING_BYTES;
46
47 fn zero() -> LinearCombination<Self::BaseField>;
49
50 fn one() -> LinearCombination<Self::BaseField>;
52
53 fn new_variable(mode: Mode, value: Self::BaseField) -> Variable<Self::BaseField>;
55
56 fn new_witness<Fn: FnOnce() -> Output::Primitive, Output: Inject>(mode: Mode, value: Fn) -> Output;
58
59 fn scope<S: Into<String>, Fn, Output>(name: S, logic: Fn) -> Output
61 where
62 Fn: FnOnce() -> Output;
63
64 fn enforce<Fn, A, B, C>(constraint: Fn)
66 where
67 Fn: FnOnce() -> (A, B, C),
68 A: Into<LinearCombination<Self::BaseField>>,
69 B: Into<LinearCombination<Self::BaseField>>,
70 C: Into<LinearCombination<Self::BaseField>>;
71
72 fn assert<Boolean: Into<LinearCombination<Self::BaseField>>>(boolean: Boolean) {
74 Self::enforce(|| (boolean, Self::one(), Self::one()))
75 }
76
77 fn assert_eq<A, B>(a: A, b: B)
79 where
80 A: Into<LinearCombination<Self::BaseField>>,
81 B: Into<LinearCombination<Self::BaseField>>,
82 {
83 Self::enforce(|| (a, Self::one(), b))
84 }
85
86 fn assert_neq<A, B>(a: A, b: B)
88 where
89 A: Into<LinearCombination<Self::BaseField>>,
90 B: Into<LinearCombination<Self::BaseField>>,
91 {
92 let (a, b) = (a.into(), b.into());
93 let mode = witness_mode!(a, b);
94
95 let a_minus_b = a - b;
97
98 let multiplier = match a_minus_b.value().inverse() {
100 Some(inverse) => Self::new_variable(mode, inverse).into(),
101 None => Self::zero(),
102 };
103
104 Self::enforce(|| (a_minus_b, multiplier, Self::one()));
106 }
107
108 fn is_satisfied() -> bool;
110
111 fn is_satisfied_in_scope() -> bool;
113
114 fn num_constants() -> u64;
116
117 fn num_public() -> u64;
119
120 fn num_private() -> u64;
122
123 fn num_variables() -> u64;
125
126 fn num_constraints() -> u64;
128
129 fn num_nonzeros() -> (u64, u64, u64);
131
132 fn count() -> (u64, u64, u64, u64, (u64, u64, u64)) {
134 (Self::num_constants(), Self::num_public(), Self::num_private(), Self::num_constraints(), Self::num_nonzeros())
135 }
136
137 fn num_constants_in_scope() -> u64;
139
140 fn num_public_in_scope() -> u64;
142
143 fn num_private_in_scope() -> u64;
145
146 fn num_constraints_in_scope() -> u64;
148
149 fn num_nonzeros_in_scope() -> (u64, u64, u64);
151
152 fn count_in_scope() -> (u64, u64, u64, u64, (u64, u64, u64)) {
154 (
155 Self::num_constants_in_scope(),
156 Self::num_public_in_scope(),
157 Self::num_private_in_scope(),
158 Self::num_constraints_in_scope(),
159 Self::num_nonzeros_in_scope(),
160 )
161 }
162
163 fn get_variable_limit() -> Option<u64>;
165
166 fn set_variable_limit(limit: Option<u64>);
168
169 fn get_constraint_limit() -> Option<u64>;
171
172 fn set_constraint_limit(limit: Option<u64>);
174
175 fn halt<S: Into<String>, T>(message: S) -> T {
177 <Self::Network as console::Environment>::halt(message)
178 }
179
180 fn inject_r1cs(r1cs: R1CS<Self::BaseField>);
182
183 fn eject_r1cs_and_reset() -> R1CS<Self::BaseField>;
185
186 fn eject_assignment_and_reset() -> Assignment<<Self::Network as console::Environment>::Field>;
188
189 fn reset();
191}