arcis_interface/types.rs
1/// Represents the kind of scalar value (signed vs unsigned integer).
2///
3/// This enum distinguishes between signed and unsigned integer types in circuit interfaces,
4/// allowing the system to properly serialize/deserialize values and generate correct Rust types.
5///
6/// # Examples
7///
8/// ```
9/// use arcis_interface::{Value, ScalarKind};
10///
11/// // Unsigned 32-bit integer (u32)
12/// let unsigned = Value::Scalar {
13/// size_in_bits: 32,
14/// kind: ScalarKind::Unsigned
15/// };
16///
17/// // Signed 32-bit integer (i32)
18/// let signed = Value::Scalar {
19/// size_in_bits: 32,
20/// kind: ScalarKind::Signed
21/// };
22/// ```
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub enum ScalarKind {
25 /// Unsigned integer type (u8, u16, u32, u64, u128).
26 ///
27 /// This is the default for backward compatibility with existing circuits
28 /// that were created before signed integer support was added.
29 #[default]
30 Unsigned,
31
32 /// Signed integer type (i8, i16, i32, i64, i128).
33 ///
34 /// Signed integers use two's complement representation in the MPC protocol.
35 Signed,
36}
37
38/// Circuit interface value types.
39///
40/// Represents all possible types that can be used as inputs or outputs in Arcium circuits.
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum Value {
43 MScalar {
44 size_in_bits: usize,
45 },
46 MFloat {
47 size_in_bits: usize,
48 },
49 MBool,
50 /// Plaintext scalar value (integer).
51 ///
52 /// # Fields
53 /// * `size_in_bits` - Bit width of the scalar (8, 16, 32, 64, or 128)
54 /// * `kind` - Whether the scalar is signed or unsigned
55 ///
56 /// # Examples
57 /// * `Scalar { size_in_bits: 32, kind: ScalarKind::Unsigned }` represents u32
58 /// * `Scalar { size_in_bits: 64, kind: ScalarKind::Signed }` represents i64
59 Scalar {
60 size_in_bits: usize,
61 kind: ScalarKind,
62 },
63 Float {
64 size_in_bits: usize,
65 },
66 Bool,
67 Ciphertext {
68 size_in_bits: usize,
69 },
70 ArcisX25519Pubkey,
71 Point,
72 /// Bits supplied per node rather than read from the agreed public inputs -- fetched from a
73 /// URL, say. Nodes may hold different values for these until a `ConstrainPlaintextBits` gate
74 /// reconciles them, which is why they are a distinct type rather than `[Bool; N]`: keeping it
75 /// in the interface is what stops unsynchronized data being handed to a circuit that assumed
76 /// every node reads the same bytes.
77 ///
78 /// `count` is the number of bits, and maps to one
79 /// `Parameter::UnsynchronizedPlaintextBools(count)` on chain.
80 UnsynchronizedBools {
81 count: usize,
82 },
83 Array(Vec<Value>),
84 Tuple(Vec<Value>),
85 Struct(Vec<Value>),
86}
87
88impl Value {
89 /// Returns the number of scalar field elements this value occupies.
90 ///
91 /// For primitive types (Scalar, Bool, Float, etc.), this returns 1.
92 /// For composite types (Array, Tuple, Struct), this recursively counts all scalar fields.
93 ///
94 /// # Examples
95 ///
96 /// ```
97 /// use arcis_interface::{Value, ScalarKind};
98 ///
99 /// let scalar = Value::Scalar { size_in_bits: 32, kind: ScalarKind::Unsigned };
100 /// assert_eq!(scalar.size_in_scalars(), 1);
101 ///
102 /// let tuple = Value::Tuple(vec![
103 /// Value::Scalar { size_in_bits: 32, kind: ScalarKind::Unsigned },
104 /// Value::Bool,
105 /// ]);
106 /// assert_eq!(tuple.size_in_scalars(), 2);
107 /// ```
108 pub fn size_in_scalars(&self) -> usize {
109 match self {
110 Self::MScalar { .. } => 1,
111 Self::MFloat { .. } => 1,
112 Self::Scalar { .. } => 1,
113 Self::Float { .. } => 1,
114 Self::MBool { .. } => 1,
115 Self::Bool { .. } => 1,
116 Self::Ciphertext { .. } => 1,
117 Self::ArcisX25519Pubkey => 1,
118 Self::Point => 1,
119 // One value, however many bits it carries: it maps to a single
120 // `Parameter::UnsynchronizedPlaintextBools(count)` and a single `ArgumentRef::Url`.
121 Self::UnsynchronizedBools { .. } => 1,
122 Self::Array(c) | Self::Tuple(c) | Self::Struct(c) => {
123 c.iter().fold(0, |acc, x| acc + x.size_in_scalars())
124 }
125 }
126 }
127
128 /// Flattens composite types into a vector of primitive values.
129 ///
130 /// For primitive types, returns a single-element vector containing self.
131 /// For composite types (Array, Tuple, Struct), recursively flattens all nested values
132 /// into a flat vector of primitives.
133 ///
134 /// # Examples
135 ///
136 /// ```
137 /// use arcis_interface::{Value, ScalarKind};
138 ///
139 /// let scalar = Value::Scalar { size_in_bits: 32, kind: ScalarKind::Unsigned };
140 /// assert_eq!(scalar.flatten().len(), 1);
141 ///
142 /// let tuple = Value::Tuple(vec![
143 /// Value::Scalar { size_in_bits: 32, kind: ScalarKind::Unsigned },
144 /// Value::Tuple(vec![Value::Bool, Value::Bool]),
145 /// ]);
146 /// assert_eq!(tuple.flatten().len(), 3); // u32, bool, bool
147 /// ```
148 pub fn flatten(&self) -> Vec<Value> {
149 match self {
150 Self::Array(c) | Self::Tuple(c) | Self::Struct(c) => {
151 let mut v = Vec::new();
152 for a in c {
153 v.extend(a.flatten());
154 }
155 v
156 }
157 _ => vec![self.clone()],
158 }
159 }
160}
161
162#[derive(Debug, serde::Serialize)]
163pub struct CircuitInterface {
164 pub name: String,
165 pub inputs: Vec<Value>,
166 pub outputs: Vec<Value>,
167}
168
169impl CircuitInterface {
170 pub fn new(name: String, inputs: Vec<Value>, outputs: Vec<Value>) -> Self {
171 Self {
172 name,
173 inputs,
174 outputs,
175 }
176 }
177
178 pub fn serialize(&self) -> Result<String, serde_json::Error> {
179 serde_json::to_string(self)
180 }
181
182 pub fn from_json(input: &str) -> Result<Self, serde_json::Error> {
183 serde_json::from_str(input)
184 }
185}