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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::{witness_mode, Assignment, Inject, LinearCombination, Mode, Variable, R1CS};
use snarkvm_curves::AffineCurve;
use snarkvm_fields::traits::*;
use core::{fmt, hash};
pub trait Environment: 'static + Copy + Clone + fmt::Debug + fmt::Display + Eq + PartialEq + hash::Hash {
type Network: console::Network<Affine = Self::Affine, Field = Self::BaseField, Scalar = Self::ScalarField>;
type Affine: AffineCurve<
BaseField = Self::BaseField,
ScalarField = Self::ScalarField,
Coordinates = (Self::BaseField, Self::BaseField),
>;
type BaseField: PrimeField + SquareRootField + Copy;
type ScalarField: PrimeField<BigInteger = <Self::BaseField as PrimeField>::BigInteger> + Copy;
const EDWARDS_A: Self::BaseField = <Self::Network as console::Environment>::EDWARDS_A;
const EDWARDS_D: Self::BaseField = <Self::Network as console::Environment>::EDWARDS_D;
const MONTGOMERY_A: Self::BaseField = <Self::Network as console::Environment>::MONTGOMERY_A;
const MONTGOMERY_B: Self::BaseField = <Self::Network as console::Environment>::MONTGOMERY_B;
const MAX_STRING_BYTES: u32 = <Self::Network as console::Environment>::MAX_STRING_BYTES;
fn zero() -> LinearCombination<Self::BaseField>;
fn one() -> LinearCombination<Self::BaseField>;
fn new_variable(mode: Mode, value: Self::BaseField) -> Variable<Self::BaseField>;
fn new_witness<Fn: FnOnce() -> Output::Primitive, Output: Inject>(mode: Mode, value: Fn) -> Output;
fn scope<S: Into<String>, Fn, Output>(name: S, logic: Fn) -> Output
where
Fn: FnOnce() -> Output;
fn enforce<Fn, A, B, C>(constraint: Fn)
where
Fn: FnOnce() -> (A, B, C),
A: Into<LinearCombination<Self::BaseField>>,
B: Into<LinearCombination<Self::BaseField>>,
C: Into<LinearCombination<Self::BaseField>>;
fn assert<Boolean: Into<LinearCombination<Self::BaseField>>>(boolean: Boolean) {
Self::enforce(|| (boolean, Self::one(), Self::one()))
}
fn assert_eq<A, B>(a: A, b: B)
where
A: Into<LinearCombination<Self::BaseField>>,
B: Into<LinearCombination<Self::BaseField>>,
{
Self::enforce(|| (a, Self::one(), b))
}
fn assert_neq<A, B>(a: A, b: B)
where
A: Into<LinearCombination<Self::BaseField>>,
B: Into<LinearCombination<Self::BaseField>>,
{
let (a, b) = (a.into(), b.into());
let mode = witness_mode!(a, b);
let a_minus_b = a - b;
let multiplier = match a_minus_b.value().inverse() {
Some(inverse) => Self::new_variable(mode, inverse).into(),
None => Self::zero(),
};
Self::enforce(|| (a_minus_b, multiplier, Self::one()));
}
fn is_satisfied() -> bool;
fn is_satisfied_in_scope() -> bool;
fn num_constants() -> u64;
fn num_public() -> u64;
fn num_private() -> u64;
fn num_constraints() -> u64;
fn num_gates() -> u64;
fn count() -> (u64, u64, u64, u64, u64) {
(Self::num_constants(), Self::num_public(), Self::num_private(), Self::num_constraints(), Self::num_gates())
}
fn num_constants_in_scope() -> u64;
fn num_public_in_scope() -> u64;
fn num_private_in_scope() -> u64;
fn num_constraints_in_scope() -> u64;
fn num_gates_in_scope() -> u64;
fn count_in_scope() -> (u64, u64, u64, u64, u64) {
(
Self::num_constants_in_scope(),
Self::num_public_in_scope(),
Self::num_private_in_scope(),
Self::num_constraints_in_scope(),
Self::num_gates_in_scope(),
)
}
fn halt<S: Into<String>, T>(message: S) -> T {
<Self::Network as console::Environment>::halt(message)
}
fn inject_r1cs(r1cs: R1CS<Self::BaseField>);
fn eject_r1cs_and_reset() -> R1CS<Self::BaseField>;
fn eject_assignment_and_reset() -> Assignment<<Self::Network as console::Environment>::Field>;
fn reset();
}