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
use arcium_program_macro;
use ;
use check_args_fn;
use ;
use TokenStream;
use ;
use quote;
use ;
use ;
/// Automatically generates the callback instruction for a computation. The callback function must
/// be named `<encrypted_ix>_callback` and take a single `SignedComputationOutputs<T>` argument in
/// addition to the `Context` parameter.
///
/// The generic type parameter for `SignedComputationOutputs<T>` is automatically generated from
/// your circuit's interface file (`build/<circuit_name>.idarc`). The generated type follows the
/// naming convention `<CircuitName>Output` (e.g., circuit "my_circuit" generates
/// `MyCircuitOutput`).
///
/// ```ignore
/// #[callback_accounts("my_circuit")]
/// #[derive(Accounts)]
/// pub struct Callback<'info> {
/// #[account(mut)]
/// pub payer: Signer<'info>,
/// pub arcium_program: Program<'info, Arcium>,
/// #[account(
/// address = derive_comp_def_pda!(COMP_DEF_OFFSET)
/// )]
/// pub comp_def_account: Account<'info, ComputationDefinitionAccount>,
/// #[account(address = ::anchor_lang::solana_program::sysvar::instructions::ID)]
/// /// CHECK: instructions_sysvar, checked by the account constraint
/// pub instructions_sysvar: AccountInfo<'info>,
/// }
///
/// #[arcium_program]
/// pub mod sample_program {
/// // Will be called when the computation with circuit "my_circuit" resolves
/// #[arcium_callback(encrypted_ix = "my_circuit")]
/// pub fn my_circuit_callback(
/// ctx: Context<Callback>,
/// output: SignedComputationOutputs<MyCircuitOutput>,
/// ) -> Result<()> {
/// // Destructure and handle Success/Failure
/// let result = match output {
/// SignedComputationOutputs::Success(MyCircuitOutput { field_0 }) => field_0,
/// SignedComputationOutputs::Failure => {
/// return Err(ErrorCode::ComputationFailed.into());
/// }
/// };
/// msg!("Computation succeeded with result: {:?}", result);
/// Ok(())
/// }
/// }
/// ```
/// Validates the structure for queuing computations by checking the encrypted instruction exists,
/// validating required account fields, and implementing the `QueueCompAccs` trait to make using it
/// with queuing computations easy:
///
/// ```ignore
/// #[queue_computation_accounts("add_together", payer)]
/// #[derive(Accounts)]
/// pub struct InitComputation<'info> {
/// #[account(mut)]
/// pub payer: Signer<'info>,
/// #[account(
/// address = derive_mxe_pda!()
/// )]
/// pub mxe_account: Account<'info, MXEAccount>,
/// #[account(
/// mut,
/// address = derive_mempool_pda!()
/// )]
/// pub mempool_account: Account<'info, Mempool>,
/// #[account(
/// mut,
/// address = derive_execpool_pda!()
/// )]
/// pub executing_pool: Account<'info, ExecutingPool>,
/// #[account(
/// address = derive_comp_def_pda!(COMP_DEF_OFFSET)
/// )]
/// pub comp_def_account: Account<'info, ComputationDefinitionAccount>,
/// #[account(
/// mut,
/// address = derive_cluster_pda!(mxe_account, ErrorCode::ClusterNotSet)
/// )]
/// pub cluster_account: Account<'info, Cluster>,
/// #[account(
/// mut,
/// address = ARCIUM_FEE_POOL_ACCOUNT_ADDRESS,
/// )]
/// pub pool_account: Account<'info, FeePool>,
/// #[account(
/// mut,
/// address = ARCIUM_CLOCK_ACCOUNT_ADDRESS
/// )]
/// pub clock_account: Account<'info, ClockAccount>,
/// pub system_program: Program<'info, System>,
/// pub arcium_program: Program<'info, Arcium>,
/// }
///
/// #[arcium_program]
/// pub mod sample_program {
/// pub fn submit_computation(
/// ctx: Context<InitComputation>,
/// x: [u8; 32],
/// y: [u8; 32],
/// computation_offset: u64,
/// ) -> Result<()> {
/// // This will queue a computation that will execute "add_together" circuit
/// let args = ArgBuilder::new()
/// .encrypted_u8(x)
/// .encrypted_u8(y)
/// .build();
/// // Parameters: accs, computation_offset, args, callback_url, callback_instructions, num_callback_txs, cu_price_micro
/// queue_computation(
/// &ctx.accounts,
/// computation_offset,
/// args,
/// None,
/// vec![AddTogetherCallback::callback_ix(&[])],
/// 1,
/// 0,
/// )?;
/// Ok(())
/// }
/// }
/// ```
/// Validates the structure for computation callbacks by checking the encrypted instruction exists,
/// validating required account fields, and ensuring the structure has the correct fields for
/// callbacks:
///
/// ```ignore
/// #[callback_accounts("my_circuit")]
/// #[derive(Accounts)]
/// pub struct Callback<'info> {
/// #[account(mut)]
/// pub payer: Signer<'info>,
/// pub arcium_program: Program<'info, Arcium>,
/// #[account(
/// address = derive_comp_def_pda!(COMP_DEF_OFFSET)
/// )]
/// pub comp_def_account: Account<'info, ComputationDefinitionAccount>,
/// #[account(address = ::anchor_lang::solana_program::sysvar::instructions::ID)]
/// /// CHECK: instructions_sysvar, checked by the account constraint
/// pub instructions_sysvar: AccountInfo<'info>,
/// }
///
/// #[arcium_program]
/// pub mod sample_program {
/// // Will be called when the computation with circuit "my_circuit" resolves
/// #[arcium_callback(encrypted_ix = "my_circuit")]
/// pub fn my_circuit_callback(
/// ctx: Context<Callback>,
/// output: SignedComputationOutputs<MyCircuitOutput>,
/// ) -> Result<()> {
/// // Destructure and handle Success/Failure
/// let result = match output {
/// SignedComputationOutputs::Success(MyCircuitOutput { field_0 }) => field_0,
/// SignedComputationOutputs::Failure => {
/// return Err(ErrorCode::ComputationFailed.into());
/// }
/// };
/// msg!("Computation succeeded with result: {:?}", result);
/// Ok(())
/// }
/// }
/// ```
/// The #[arcium_program] attribute defines the module
/// containing all instruction handlers defining all entries into a Solana program.
/// Under the hood, it gets expanded to Anchor's #[program] and some additional definitions needed
/// for Arcium.
/// Validates the structure for initializing computation definitions by checking the encrypted
/// instruction exists, validating required account fields, and implementing the `InitCompDefAccs`
/// trait to make using it with computation definitions easy:
///
/// ```ignore
/// #[init_computation_definition_accounts("my_circuit", payer)]
/// #[derive(Accounts)]
/// pub struct InitMyCircuitCompDef<'info> {
/// #[account(mut)]
/// pub payer: Signer<'info>,
/// #[account(
/// mut,
/// address = derive_mxe_pda!()
/// )]
/// pub mxe_account: Box<Account<'info, MXEAccount>>,
/// #[account(mut)]
/// /// CHECK: comp_def_account, checked by arcium program.
/// /// Can't check it here as it's not initialized yet.
/// pub comp_def_account: UncheckedAccount<'info>,
/// pub arcium_program: Program<'info, Arcium>,
/// pub system_program: Program<'info, System>,
/// }
/// ```
/// Compile-time validation of computation arguments against an interface definition.
///
/// This macro provides **compile-time** verification that your computation arguments match the
/// circuit interface, catching type mismatches and argument count errors before runtime.
///
/// ## Usage
/// 1. Attach `#[check_args]` to your function
/// 2. Attach `#[args("your_circuit_name")]` to your computation arguments (array literal or `vec!`
/// macro)
///
/// ## Checks Performed
/// - Correct number of arguments based on the interface file (`build/your_circuit_name.idarc`)
/// - Correct argument types (e.g., `PlaintextU32`, `EncryptedU8`, `X25519Pubkey`)
/// - Correct argument order matching the circuit interface
///
/// ## Compile-Time Errors
/// If arguments don't match the interface, you'll see a compilation error like:
/// ```text
/// error: mismatched types: expected Parameter::PlaintextU64, found Parameter::PlaintextU32
/// ```
///
/// ## Example
/// ```ignore
/// #[check_args]
/// pub fn submit_computation(ctx: Context<MyAccounts>, computation_offset: u64) -> Result<()> {
/// queue_computation(
/// &ctx.accounts,
/// computation_offset,
/// #[args("add_together")] // Validates against build/add_together.idarc
/// ArgBuilder::new()
/// .plaintext_u64(10)
/// .plaintext_u64(20)
/// .build(),
/// None,
/// vec![],
/// 1,
/// 0,
/// )?;
/// Ok(())
/// }
/// ```
/// Returns the SHA-256 hash of a compiled circuit as a `[u8; 32]` array at compile time.
///
/// The hash is computed over the serialized circuit bytecode and can be used for verifying
/// off-chain circuit sources. This is the same hash that ARX nodes verify when fetching circuits
/// from off-chain sources.
///
/// ## Example
/// ```ignore
/// use arcium_macros::circuit_hash;
///
/// let source = OffChainCircuitSource {
/// source: "https://ipfs.io/ipfs/Qm...".into(),
/// hash: circuit_hash!("my_circuit"),
/// };
/// ```