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
use ConstraintSystemRef;
use Field;
/// A namespaced `ConstraintSystemRef`.
/// Creates namespaces for different parts of a circuit when generating
/// constraints. Here, a namespace is equivalent to having a unique span for
/// each part of the circuit. For more information on spans, see the [tracing](https://docs.rs/tracing) crate.
///
/// Takes in a reference to a Constraint System and a string slice representing
/// the name of the namespace. The name is used to identify the namespace.
///
/// # Simple Example of using namespaces
///
/// ```rust,ignore
/// use ark_ff::Field;
/// use ark_r1cs_std::prelude::*;
/// use ark_relations::gr1cs::{ConstraintSystemRef, SynthesisError};
///
/// // Define the circuit structure
/// pub struct SimpleAdditionCircuit<F: Field> {
/// pub a: F, // Public input 1
/// pub b: F, // Public input 2
/// pub c: F, // Witness (private input)
/// }
///
/// impl<F: Field> SimpleAdditionCircuit<F> {
/// pub fn generate_constraints(self, cs: ConstraintSystemRef<F>) -> Result<(), SynthesisError> {
/// // Create a namespace for public inputs
/// let cs = ns!(cs, "public_inputs");
/// let a_var = FpVar::new_input(cs.clone(), || Ok(self.a))?;
/// let b_var = FpVar::new_input(cs.clone(), || Ok(self.b))?;
///
/// // Create another namespace for the witness
/// let cs = ns!(cs, "witness");
/// let c_var = FpVar::new_witness(cs.clone(), || Ok(self.c))?;
///
/// // Create another namespace for the addition constraint
/// let cs = ns!(cs, "addition_constraint");
/// let sum = &a_var + &b_var;
///
/// // Enforce that a + b = c
/// sum.enforce_equal(&c_var)?;
///
/// Ok(())
/// }
/// }
/// ```