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
327
328
329
330
331
332
333
334
335
336
337
338
339
/*!
# Async-MPC Macros
This crate provides procedural macros for the async-mpc library.
*/
use TokenStream;
/// Generates async-mpc task implementations with automatic dependency management.
///
/// This macro takes a struct definition and a compute function, and generates:
/// - An internal unresolved struct to hold task dependencies
/// - A public type alias using the `TaskWrapper` type
/// - A Task trait implementation with async execution logic
/// - A `new` constructor for creating new task instances
/// - A standalone `compute` method for direct calls
///
/// # Examples
///
/// ```rust
/// define_task! {
/// pub struct FieldAddTask<F: FieldExtension> {
/// x: Arc<dyn Task<Output = Arc<FieldShare<F>>>>,
/// y: Arc<dyn Task<Output = Arc<FieldShare<F>>>>,
/// }
///
/// async fn compute(x: FieldShare<F>, y: FieldShare<F>) -> Result<FieldShare<F>, AbortError> {
/// Ok(x + &y)
/// }
/// }
/// ```
/// Derives `try_get_*_task` functions for enum variants containing task types.
///
/// This macro automatically generates getter functions for each variant in a `TaskType` enum,
/// eliminating the need for repetitive boilerplate code. These functions retrieve tasks based on
/// their index (encoded in the Label's first field) and return the appropriate task type.
///
/// # Examples
///
/// ```rust
/// #[derive(TaskGetters)]
/// pub enum TaskType<C: Curve> {
/// ScalarShareTask(Arc<dyn Task<Output = Arc<ScalarShare<C>>>>),
/// BaseFieldShareTask(Arc<dyn Task<Output = Arc<BaseFieldShare<C>>>>),
/// // ... more variants
/// }
/// ```
///
/// This generates functions like:
/// - `try_get_scalar_share_task`
/// - `try_get_base_field_share_task`
/// - `try_get_point_share_task`
///
/// Each function has the signature:
/// ```rust
/// pub fn try_get_<variant_name>_task<C: Curve>(
/// label: Label,
/// task_map: &[TaskType<C>],
/// ) -> Result<Arc<dyn Task<Output = Arc<T>>>, ProtocolError>
/// ```
///
/// ## Requirements
///
/// - The enum must have variants with the pattern `*Task(Arc<dyn Task<Output = Arc<T>>>)`
/// - The macro extracts the inner type `T` from `Arc<dyn Task<Output = Arc<T>>>`
/// - Each variant must have a corresponding `try_as_*` method (typically generated by
/// `EnumTryAsInner`)
/// Derives `Gate::map_labels` and `Gate::for_each_label`
/// Macro to ensure protocol tag uniqueness at compile time.
/// This macro generates a unique tag for a given protocol name,
/// and checks against a registry to prevent collisions.
/// If a collision is detected, it finds the next available tag.
/// Macro to dump the current state of the protocol tag registry at compile time (for debugging)
/// Procedural macro to automatically implement the `Party` trait for a struct.
///
/// # Requirements
/// - The struct must have either:
/// - A field marked with `#[session_id]` or a field of type `SessionId`, or
/// - At least one field annotated with `#[party]` (which must itself implement `Party`).
///
/// # Behavior
/// - `session_id()`: Returns a reference to the `SessionId` field, or delegates to the first
/// `#[party]` field.
/// - `protocol_name()`: Uses the `PROTOCOL_INFO` constant if a direct `SessionId` field is present,
/// otherwise formats as `"<TypeName> - <InnerProtocolName>"`.
/// - `refresh()`: Calls `refresh_from` or `refresh_with` on the session id, and `refresh()` on all
/// `#[party]` fields. If a `hasher` field is present, it is reset.
///
/// # Usage
/// ```rust
/// #[derive(Party)]
/// pub struct MyParty {
/// #[session_id]
/// session_id: SessionId,
/// #[party]
/// inner: InnerPartyType,
/// // ...
/// }
/// // or, with type/name matching fallback:
/// #[derive(Party)]
/// pub struct MyParty { session_id: SessionId, #[party] inner: InnerPartyType, ... }
/// ```
/// Procedural macro to automatically implement the `Probabilistic` trait for a struct.
///
/// # Requirements
/// - The struct must have a field marked with `#[rng]` or, if not present, a field named `rng` that
/// implements `CryptoRngCore`.
///
/// # Behavior
/// - Implements the `rng(&mut self)` method, returning a mutable reference to the selected field.
///
/// # Usage
/// ```rust
/// #[derive(Probabilistic)]
/// pub struct MyParty {
/// #[rng]
/// my_rng: MyRngType,
/// // ...
/// }
/// // or, fallback:
/// #[derive(Probabilistic)]
/// pub struct MyParty { rng: MyRngType, ... }
/// ```
/// Procedural macro to automatically implement the `Scribe` trait for a struct.
///
/// # Requirements
/// - The struct must have a field marked with `#[transcript]` or, if not present, a field named
/// `transcript` that implements `Transcript`.
///
/// # Behavior
/// - Implements the `transcript(&mut self)` method, returning a mutable reference to the selected
/// field.
///
/// # Usage
/// ```rust
/// #[derive(Scribe)]
/// pub struct MyParty {
/// #[transcript]
/// my_transcript: MyTranscriptType,
/// // ...
/// }
/// // or, fallback:
/// #[derive(Scribe)]
/// pub struct MyParty { transcript: MyTranscriptType, ... }
/// ```
/// Procedural macro to automatically implement the `HasTweakableHasher` trait for a struct.
///
/// # Requirements
/// - The struct must have a field marked with `#[hasher]` or, if not present, a field named
/// `hasher` whose type implements `TweakableHasher`.
///
/// # Behavior
/// - Implements the `get_hasher(&mut self)` method, returning a mutable reference to the selected
/// field.
/// - Sets the associated type `Hasher` to the type of the selected field.
///
/// # Usage
/// ```rust
/// #[derive(HasTweakableHasher)]
/// pub struct MyParty {
/// #[hasher]
/// my_hasher: MyHasherType,
/// // ...
/// }
/// // or, fallback:
/// #[derive(HasTweakableHasher)]
/// pub struct MyParty { hasher: MyHasherType, ... }
/// ```
/// Procedural macro to automatically implement the `Peer` trait for a struct.
///
/// # Requirements
/// - The struct must have a field marked with `#[peer_ctx]` or, if not present, a field named
/// `peer_ctx` whose type is PeerContext.
///
/// # Behavior
/// - Implements the `peer_context(&self)` method, returning a reference to the selected field.
///
/// # Usage
/// ```rust
/// #[derive(Peer)]
/// pub struct MyParty {
/// #[peer_ctx]
/// peer_ctx: PeerContext,
/// // ...
/// }
/// // or, fallback to type matching:
/// #[derive(Peer)]
/// pub struct MyParty { peer_ctx: PeerContext, ... }
/// ```
/// Procedural macro to expose a private member function for given cfg tags.
///
/// This macro creates a public wrapper function with an underscore prefix that
/// calls the original private function. The wrapper is only compiled when the
/// specified cfg condition is met (e.g., feature flags or test mode).
///
/// # Usage
/// ```rust
/// use macros::public;
///
/// struct MyStruct;
///
/// impl MyStruct {
/// #[public(feature = "dev")]
/// fn private_method(&mut self, x: i32) -> i32 {
/// x * 2
/// }
/// }
///
/// // Generates:
/// // #[cfg(feature = "dev")]
/// // pub fn _private_method(&mut self, x: i32) -> i32 {
/// // self.private_method(x)
/// // }
/// ```
/// Procedural macro to generate variants for arithmetic operations.
///
/// Supports binary operations (like `Add`), binary assign operations (like
/// `AddAssign`), and unary operations (like `Neg`).
///
/// For **binary operations** (impl with `-> Self::Output`), it requires `Self op &RHS`;
/// for **unary operations** it assumes `op Self`.
///
/// There are three supported variants:
/// - `owned`: All operands owned - implemented by referencing rhs.
/// - `borrowed`: All operands borrowed - implemented by cloning Self.
/// - `flipped`: `&Self op RHS` - implemented by cloning lhs and referencing rhs (binary ops only).
/// - `flipped_commutative`: `&Self op RHS` - implemented as `rhs.op(lhs)`. To be used only in
/// commutative ops (e.g., Add & Mul, but not Sub | Div).
///
/// # Usage
/// ```rust
/// #[derive(Clone, Debug)]
/// struct Point {
/// x: i32,
/// y: i32,
/// }
///
/// // Binary operation: Point + &Point (base impl)
/// #[macros::arithmetic_variants(owned, borrowed, flipped)]
/// impl std::ops::Add<&Point> for Point {
/// type Output = Point;
/// fn add(self, other: &Point) -> Point {
/// Point {
/// x: self.x + other.x,
/// y: self.y + other.y,
/// }
/// }
/// }
/// // Generates: Point + Point, &Point + &Point, &Point + Point
///
/// // Binary assign operation: Point += &Point (base impl)
/// #[macros::arithmetic_variants(owned)]
/// impl std::ops::AddAssign<&Point> for Point {
/// fn add_assign(&mut self, other: &Point) {
/// self.x += other.x;
/// self.y += other.y;
/// }
/// }
/// // Generates: Point += Point
///
/// // Unary operation: -&Point (base impl)
/// #[macros::arithmetic_variants(owned)]
/// impl std::ops::Neg for &Point {
/// type Output = Point;
/// fn neg(self) -> Point {
/// Point {
/// x: -self.x,
/// y: -self.y,
/// }
/// }
/// }
/// // Generates: -Point
/// ```