snarkvm_circuit_environment/testnet_circuit.rs
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{Mode, helpers::Constraint, *};
use core::{
cell::{Cell, RefCell},
fmt,
};
type Field = <console::TestnetV0 as console::Environment>::Field;
thread_local! {
static VARIABLE_LIMIT: Cell<Option<u64>> = const { Cell::new(None) };
static CONSTRAINT_LIMIT: Cell<Option<u64>> = const { Cell::new(None) };
pub(super) static TESTNET_CIRCUIT: RefCell<R1CS<Field>> = RefCell::new(R1CS::new());
static IN_WITNESS: Cell<bool> = const { Cell::new(false) };
static ZERO: LinearCombination<Field> = LinearCombination::zero();
static ONE: LinearCombination<Field> = LinearCombination::one();
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct TestnetCircuit;
impl Environment for TestnetCircuit {
type Affine = <console::TestnetV0 as console::Environment>::Affine;
type BaseField = Field;
type Network = console::TestnetV0;
type ScalarField = <console::TestnetV0 as console::Environment>::Scalar;
/// Returns the `zero` constant.
fn zero() -> LinearCombination<Self::BaseField> {
ZERO.with(|zero| zero.clone())
}
/// Returns the `one` constant.
fn one() -> LinearCombination<Self::BaseField> {
ONE.with(|one| one.clone())
}
/// Returns a new variable of the given mode and value.
fn new_variable(mode: Mode, value: Self::BaseField) -> Variable<Self::BaseField> {
IN_WITNESS.with(|in_witness| {
// Ensure we are not in witness mode.
if !in_witness.get() {
// Ensure that we do not surpass the variable limit for the circuit.
VARIABLE_LIMIT.with(|variable_limit| {
if let Some(limit) = variable_limit.get() {
if Self::num_variables() > limit {
Self::halt(format!("Surpassed the variable limit ({limit})"))
}
}
});
TESTNET_CIRCUIT.with(|circuit| match mode {
Mode::Constant => circuit.borrow_mut().new_constant(value),
Mode::Public => circuit.borrow_mut().new_public(value),
Mode::Private => circuit.borrow_mut().new_private(value),
})
} else {
Self::halt("Tried to initialize a new variable in witness mode")
}
})
}
/// Returns a new witness of the given mode and value.
fn new_witness<Fn: FnOnce() -> Output::Primitive, Output: Inject>(mode: Mode, logic: Fn) -> Output {
IN_WITNESS.with(|in_witness| {
// Set the entire environment to witness mode.
in_witness.replace(true);
// Run the logic.
let output = logic();
// Return the entire environment from witness mode.
in_witness.replace(false);
Inject::new(mode, output)
})
}
/// Enters a new scope for the environment.
fn scope<S: Into<String>, Fn, Output>(name: S, logic: Fn) -> Output
where
Fn: FnOnce() -> Output,
{
IN_WITNESS.with(|in_witness| {
// Ensure we are not in witness mode.
if !in_witness.get() {
TESTNET_CIRCUIT.with(|circuit| {
// Set the entire environment to the new scope.
let name = name.into();
if let Err(error) = circuit.borrow_mut().push_scope(&name) {
Self::halt(error)
}
// Run the logic.
let output = logic();
// Return the entire environment to the previous scope.
if let Err(error) = circuit.borrow_mut().pop_scope(name) {
Self::halt(error)
}
output
})
} else {
Self::halt("Tried to initialize a new scope in witness mode")
}
})
}
/// Adds one constraint enforcing that `(A * B) == C`.
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>>,
{
IN_WITNESS.with(|in_witness| {
// Ensure we are not in witness mode.
if !in_witness.get() {
TESTNET_CIRCUIT.with(|circuit| {
// Ensure that we do not surpass the constraint limit for the circuit.
CONSTRAINT_LIMIT.with(|constraint_limit| {
if let Some(limit) = constraint_limit.get() {
if circuit.borrow().num_constraints() > limit {
Self::halt(format!("Surpassed the constraint limit ({limit})"))
}
}
});
let (a, b, c) = constraint();
let (a, b, c) = (a.into(), b.into(), c.into());
// Ensure the constraint is not comprised of constants.
match a.is_constant() && b.is_constant() && c.is_constant() {
true => {
// Evaluate the constant constraint.
assert_eq!(
a.value() * b.value(),
c.value(),
"Constant constraint failed: ({a} * {b}) =?= {c}"
);
// match self.counter.scope().is_empty() {
// true => println!("Enforced constraint with constant terms: ({} * {}) =?= {}", a, b, c),
// false => println!(
// "Enforced constraint with constant terms ({}): ({} * {}) =?= {}",
// self.counter.scope(), a, b, c
// ),
// }
}
false => {
// Construct the constraint object.
let constraint = Constraint(circuit.borrow().scope(), a, b, c);
// Append the constraint.
circuit.borrow_mut().enforce(constraint)
}
}
});
} else {
Self::halt("Tried to add a new constraint in witness mode")
}
})
}
/// Returns `true` if all constraints in the environment are satisfied.
fn is_satisfied() -> bool {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().is_satisfied())
}
/// Returns `true` if all constraints in the current scope are satisfied.
fn is_satisfied_in_scope() -> bool {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().is_satisfied_in_scope())
}
/// Returns the number of constants in the entire circuit.
fn num_constants() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_constants())
}
/// Returns the number of public variables in the entire circuit.
fn num_public() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_public())
}
/// Returns the number of private variables in the entire circuit.
fn num_private() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_private())
}
/// Returns the number of constant, public, and private variables in the entire circuit.
fn num_variables() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_variables())
}
/// Returns the number of constraints in the entire circuit.
fn num_constraints() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_constraints())
}
/// Returns the number of nonzeros in the entire circuit.
fn num_nonzeros() -> (u64, u64, u64) {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_nonzeros())
}
/// Returns the number of constants for the current scope.
fn num_constants_in_scope() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_constants_in_scope())
}
/// Returns the number of public variables for the current scope.
fn num_public_in_scope() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_public_in_scope())
}
/// Returns the number of private variables for the current scope.
fn num_private_in_scope() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_private_in_scope())
}
/// Returns the number of constraints for the current scope.
fn num_constraints_in_scope() -> u64 {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_constraints_in_scope())
}
/// Returns the number of nonzeros for the current scope.
fn num_nonzeros_in_scope() -> (u64, u64, u64) {
TESTNET_CIRCUIT.with(|circuit| circuit.borrow().num_nonzeros_in_scope())
}
/// Returns the variable limit for the circuit, if one exists.
fn get_variable_limit() -> Option<u64> {
VARIABLE_LIMIT.with(|current_limit| current_limit.get())
}
/// Sets the variable limit for the circuit.
fn set_variable_limit(limit: Option<u64>) {
VARIABLE_LIMIT.with(|current_limit| current_limit.replace(limit));
}
/// Returns the constraint limit for the circuit, if one exists.
fn get_constraint_limit() -> Option<u64> {
CONSTRAINT_LIMIT.with(|current_limit| current_limit.get())
}
/// Sets the constraint limit for the circuit.
fn set_constraint_limit(limit: Option<u64>) {
CONSTRAINT_LIMIT.with(|current_limit| current_limit.replace(limit));
}
/// Halts the program from further synthesis, evaluation, and execution in the current environment.
fn halt<S: Into<String>, T>(message: S) -> T {
let error = message.into();
// eprintln!("{}", &error);
panic!("{}", &error)
}
/// Returns the R1CS circuit, resetting the circuit.
fn inject_r1cs(r1cs: R1CS<Self::BaseField>) {
TESTNET_CIRCUIT.with(|circuit| {
// Ensure the circuit is empty before injecting.
assert_eq!(0, circuit.borrow().num_constants());
assert_eq!(1, circuit.borrow().num_public());
assert_eq!(0, circuit.borrow().num_private());
assert_eq!(1, circuit.borrow().num_variables());
assert_eq!(0, circuit.borrow().num_constraints());
// Inject the R1CS instance.
let r1cs = circuit.replace(r1cs);
// Ensure the circuit that was replaced is empty.
assert_eq!(0, r1cs.num_constants());
assert_eq!(1, r1cs.num_public());
assert_eq!(0, r1cs.num_private());
assert_eq!(1, r1cs.num_variables());
assert_eq!(0, r1cs.num_constraints());
})
}
/// Returns the R1CS circuit, resetting the circuit.
fn eject_r1cs_and_reset() -> R1CS<Self::BaseField> {
TESTNET_CIRCUIT.with(|circuit| {
// Reset the witness mode.
IN_WITNESS.with(|in_witness| in_witness.replace(false));
// Reset the variable limit.
Self::set_variable_limit(None);
// Reset the constraint limit.
Self::set_constraint_limit(None);
// Eject the R1CS instance.
let r1cs = circuit.replace(R1CS::<<Self as Environment>::BaseField>::new());
// Ensure the circuit is now empty.
assert_eq!(0, circuit.borrow().num_constants());
assert_eq!(1, circuit.borrow().num_public());
assert_eq!(0, circuit.borrow().num_private());
assert_eq!(1, circuit.borrow().num_variables());
assert_eq!(0, circuit.borrow().num_constraints());
// Return the R1CS instance.
r1cs
})
}
/// Returns the R1CS assignment of the circuit, resetting the circuit.
fn eject_assignment_and_reset() -> Assignment<<Self::Network as console::Environment>::Field> {
TESTNET_CIRCUIT.with(|circuit| {
// Reset the witness mode.
IN_WITNESS.with(|in_witness| in_witness.replace(false));
// Reset the variable limit.
Self::set_variable_limit(None);
// Reset the constraint limit.
Self::set_constraint_limit(None);
// Eject the R1CS instance.
let r1cs = circuit.replace(R1CS::<<Self as Environment>::BaseField>::new());
assert_eq!(0, circuit.borrow().num_constants());
assert_eq!(1, circuit.borrow().num_public());
assert_eq!(0, circuit.borrow().num_private());
assert_eq!(1, circuit.borrow().num_variables());
assert_eq!(0, circuit.borrow().num_constraints());
// Convert the R1CS instance to an assignment.
Assignment::from(r1cs)
})
}
/// Clears the circuit and initializes an empty environment.
fn reset() {
TESTNET_CIRCUIT.with(|circuit| {
// Reset the witness mode.
IN_WITNESS.with(|in_witness| in_witness.replace(false));
// Reset the variable limit.
Self::set_variable_limit(None);
// Reset the constraint limit.
Self::set_constraint_limit(None);
// Reset the circuit.
*circuit.borrow_mut() = R1CS::<<Self as Environment>::BaseField>::new();
assert_eq!(0, circuit.borrow().num_constants());
assert_eq!(1, circuit.borrow().num_public());
assert_eq!(0, circuit.borrow().num_private());
assert_eq!(1, circuit.borrow().num_variables());
assert_eq!(0, circuit.borrow().num_constraints());
});
}
}
impl fmt::Display for TestnetCircuit {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
TESTNET_CIRCUIT.with(|circuit| write!(f, "{}", circuit.borrow()))
}
}
#[cfg(test)]
mod tests {
use snarkvm_circuit::prelude::*;
/// Compute 2^EXPONENT - 1, in a purposefully constraint-inefficient manner for testing.
fn create_example_circuit<E: Environment>() -> Field<E> {
let one = snarkvm_console_types::Field::<E::Network>::one();
let two = one + one;
const EXPONENT: u64 = 64;
// Compute 2^EXPONENT - 1, in a purposefully constraint-inefficient manner for testing.
let mut candidate = Field::<E>::new(Mode::Public, one);
let mut accumulator = Field::new(Mode::Private, two);
for _ in 0..EXPONENT {
candidate += &accumulator;
accumulator *= Field::new(Mode::Private, two);
}
assert_eq!((accumulator - Field::one()).eject_value(), candidate.eject_value());
assert_eq!(2, E::num_public());
assert_eq!(2 * EXPONENT + 1, E::num_private());
assert_eq!(EXPONENT, E::num_constraints());
assert!(E::is_satisfied());
candidate
}
#[test]
fn test_print_circuit() {
let _candidate = create_example_circuit::<TestnetCircuit>();
let output = format!("{TestnetCircuit}");
println!("{output}");
}
#[test]
fn test_circuit_scope() {
TestnetCircuit::scope("test_circuit_scope", || {
assert_eq!(0, TestnetCircuit::num_constants());
assert_eq!(1, TestnetCircuit::num_public());
assert_eq!(0, TestnetCircuit::num_private());
assert_eq!(0, TestnetCircuit::num_constraints());
assert_eq!(0, TestnetCircuit::num_constants_in_scope());
assert_eq!(0, TestnetCircuit::num_public_in_scope());
assert_eq!(0, TestnetCircuit::num_private_in_scope());
assert_eq!(0, TestnetCircuit::num_constraints_in_scope());
})
}
}