rsnark_core/
variable.rs

1//! Variable types for circuit construction.
2//!
3//! These types can used in API to define circuit.
4
5use ruint::aliases::U256;
6
7use crate::types::VariableType;
8
9/// A trait that defines types that can be used as API parameters in circuit construction.
10///
11/// This trait enables various types to be used interchangeably in circuit operations,
12/// including public variables, private variables, local variables, and constant values.
13/// Each implementor must provide its variable type representation.
14pub trait Variable {
15    /// Returns the variable type representation for this variable.
16    /// This is used internally by the circuit builder to track variable usage.
17    fn ty(&self) -> VariableType;
18}
19
20/// Represents a variable within a circuit during construction.
21///
22/// A `CircuitVariable` is created during circuit building and encapsulates a specific
23/// variable type (public, private, local, or constant). It serves as a handle to reference
24/// variables in circuit operations and maintains the variable's type information internally.
25///
26/// # Usage
27///
28/// Circuit variables are typically created through:
29/// - [`VariableIniter`](crate::VariableIniter) methods for creating new variables
30/// - API operations that return intermediate results
31/// - Circuit builder operations that allocate local variables
32///
33/// # Implementation Details
34///
35/// The struct contains a [`VariableType`] that specifies whether
36/// the variable is public input, private witness, local intermediate value, or a constant.
37/// This type information is used by the circuit builder to generate proper constraints.
38#[derive(Debug, Clone)]
39pub struct CircuitVariable {
40    pub(crate) ty: VariableType,
41}
42
43impl Variable for CircuitVariable {
44    fn ty(&self) -> VariableType {
45        self.ty.clone()
46    }
47}
48
49macro_rules! define_variable_for_from_u256 {
50    ($t:ident) => {
51        impl Variable for $t {
52            fn ty(&self) -> VariableType {
53                let x = U256::from(*self);
54                VariableType::Constant(x)
55            }
56        }
57    };
58}
59
60define_variable_for_from_u256!(U256);
61define_variable_for_from_u256!(u128);
62define_variable_for_from_u256!(u64);
63define_variable_for_from_u256!(u32);
64define_variable_for_from_u256!(u16);
65define_variable_for_from_u256!(u8);
66define_variable_for_from_u256!(i128);
67define_variable_for_from_u256!(i64);
68define_variable_for_from_u256!(i32);
69define_variable_for_from_u256!(i16);
70define_variable_for_from_u256!(i8);
71define_variable_for_from_u256!(bool);