snarkvm_circuit_environment/helpers/
constraint.rs1use crate::{prelude::*, *};
17use snarkvm_fields::PrimeField;
18
19#[derive(Clone, Debug, Hash)]
20pub struct Constraint<F: PrimeField>(
21 pub(crate) Scope,
22 pub(crate) LinearCombination<F>,
23 pub(crate) LinearCombination<F>,
24 pub(crate) LinearCombination<F>,
25);
26
27impl<F: PrimeField> Constraint<F> {
28 pub(crate) fn num_nonzeros(&self) -> (u64, u64, u64) {
30 let (a, b, c) = (&self.1, &self.2, &self.3);
31 (a.num_nonzeros(), b.num_nonzeros(), c.num_nonzeros())
32 }
33
34 pub(crate) fn is_satisfied(&self) -> bool {
36 let (scope, a, b, c) = (&self.0, &self.1, &self.2, &self.3);
37 let a = a.value();
38 let b = b.value();
39 let c = c.value();
40
41 match a * b == c {
42 true => true,
43 false => {
44 eprintln!("Failed constraint at {scope}:\n\t({a} * {b}) != {c}");
45 false
46 }
47 }
48 }
49
50 pub fn to_terms(&self) -> (&LinearCombination<F>, &LinearCombination<F>, &LinearCombination<F>) {
52 (&self.1, &self.2, &self.3)
53 }
54}
55
56impl<F: PrimeField> Display for Constraint<F> {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 let (scope, a, b, c) = (&self.0, &self.1, &self.2, &self.3);
59 let a = a.value();
60 let b = b.value();
61 let c = c.value();
62
63 match (a * b) == c {
64 true => write!(f, "Constraint {scope}:\n\t{a} * {b} == {c}\n"),
65 false => write!(f, "Constraint {scope}:\n\t{a} * {b} != {c} (Unsatisfied)\n"),
66 }
67 }
68}