arcium-core-utils 0.7.3

Arcium core utils
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/// A serialization optimized representation of a circuit.
use primitives::algebra::elliptic_curve::{Point, Scalar};
use serde::{Deserialize, Serialize};

use super::{errors::CircuitError, Circuit, Gate, GateIndex};
use crate::config::{name_tag, tag_name, MpcConfig};

/// Version of the serialized circuit layout. Bump on any change to the byte format so stale
/// blobs fail with [`CircuitError::UnsupportedFormatVersion`] instead of a parse error.
const CIRCUIT_FORMAT_VERSION: u8 = 1;

/// Serialization/deserialization optimized representation of a circuit.
#[derive(Serialize, Deserialize, Default)]
#[serde(bound(
    serialize = "Scalar<C::Curve>: Serialize, Point<C::Curve>: Serialize",
    deserialize = "Scalar<C::Curve>: Deserialize<'de>, Point<C::Curve>: Deserialize<'de>"
))]
#[repr(C)]
struct CompressedCircuit<C: MpcConfig> {
    /// Serialization format version (see [`CIRCUIT_FORMAT_VERSION`]). First in the layout so a
    /// stale blob fails fast.
    pub format_version: u8,
    /// Identifies the curve the circuit was built for (see [`name_tag`]).
    pub curve_tag: [u8; 32],
    /// Identifies the MPC field the circuit was built for (see [`name_tag`]). The tags precede
    /// the gates so a config mismatch fails before they are parsed.
    pub mpc_field_tag: [u8; 32],
    /// The circuit operations.
    pub ops: Vec<Gate<C>>,
    /// The output gates in order of definition
    pub output_gates: Vec<GateIndex>,
}

impl<C: MpcConfig> From<&Circuit<C>> for CompressedCircuit<C> {
    fn from(value: &Circuit<C>) -> Self {
        CompressedCircuit {
            format_version: CIRCUIT_FORMAT_VERSION,
            curve_tag: name_tag::<C::Curve>(),
            mpc_field_tag: name_tag::<C::Field>(),
            ops: value.iter_gates().cloned().collect(),
            output_gates: value.iter_output_indices().copied().collect(),
        }
    }
}

impl<C: MpcConfig> TryFrom<CompressedCircuit<C>> for Circuit<C> {
    type Error = CircuitError<C>;

    fn try_from(circuit: CompressedCircuit<C>) -> Result<Self, Self::Error> {
        if circuit.format_version != CIRCUIT_FORMAT_VERSION {
            return Err(CircuitError::UnsupportedFormatVersion {
                expected: CIRCUIT_FORMAT_VERSION,
                found: circuit.format_version,
            });
        }
        let expected_curve = name_tag::<C::Curve>();
        if circuit.curve_tag != expected_curve {
            return Err(CircuitError::CurveMismatch {
                expected: tag_name(&expected_curve),
                found: tag_name(&circuit.curve_tag),
            });
        }
        let expected_field = name_tag::<C::Field>();
        if circuit.mpc_field_tag != expected_field {
            return Err(CircuitError::MpcFieldMismatch {
                expected: tag_name(&expected_field),
                found: tag_name(&circuit.mpc_field_tag),
            });
        }

        let mut res = Self {
            gates: Vec::with_capacity(circuit.ops.len()),
            inputs: Vec::new(),
            outputs: Vec::with_capacity(circuit.output_gates.len()),
        };

        for gate in circuit.ops.into_iter() {
            res.add_gate(gate)?;
        }

        for index in circuit.output_gates.into_iter() {
            res.add_output(index)?;
        }

        Ok(res)
    }
}

mod bincode {

    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    use super::{Circuit, CompressedCircuit};
    use crate::config::MpcConfig;

    impl<C: MpcConfig> Serialize for Circuit<C> {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            let circuit_serde: CompressedCircuit<C> = self.into();
            circuit_serde.serialize(serializer)
        }
    }

    impl<'de, C: MpcConfig> Deserialize<'de> for Circuit<C> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let circuit_serde = CompressedCircuit::<C>::deserialize(deserializer)
                .map_err(serde::de::Error::custom)?;
            let circuit = circuit_serde.try_into();
            circuit.map_err(serde::de::Error::custom)
        }
    }
}

mod wincode {
    use core::{
        mem::{self, MaybeUninit},
        ptr,
    };

    use ::wincode::{
        containers,
        io::{Reader, Writer},
        len::BincodeLen,
        ReadResult,
        SchemaRead,
        SchemaWrite,
        TypeMeta,
        WriteResult,
    };

    use super::*;
    pub type BincodeLenU32 = BincodeLen<{ 2 << 32 }>;

    // TODO: optimize so that we dont need to cast into CircuitSerde twice
    impl<C: MpcConfig> SchemaWrite for Circuit<C> {
        type Src = Self;

        const TYPE_META: TypeMeta = <CompressedCircuit<C> as SchemaWrite>::TYPE_META;

        fn size_of(src: &Self::Src) -> WriteResult<usize> {
            let circuit_serde: CompressedCircuit<C> = src.into();
            <CompressedCircuit<C> as SchemaWrite>::size_of(&circuit_serde)
        }

        fn write(writer: &mut impl Writer, src: &Self::Src) -> WriteResult<()> {
            let circuit_serde: CompressedCircuit<C> = src.into();
            <CompressedCircuit<C> as SchemaWrite>::write(writer, &circuit_serde)
        }
    }

    impl<C: MpcConfig> ::wincode::SchemaWrite for CompressedCircuit<C> {
        type Src = Self;
        #[allow(clippy::arithmetic_side_effects)]
        const TYPE_META: TypeMeta = if let (
            TypeMeta::Static {
                size: v,
                zero_copy: zc_v,
            },
            TypeMeta::Static {
                size: t,
                zero_copy: zc_t,
            },
            TypeMeta::Static {
                size: a,
                zero_copy: zc_a,
            },
            TypeMeta::Static {
                size: b,
                zero_copy: zc_b,
            },
        ) = (
            <u8 as SchemaWrite>::TYPE_META,
            <[u8; 32] as SchemaWrite>::TYPE_META,
            <containers::Vec<Gate<C>, BincodeLenU32> as SchemaWrite>::TYPE_META,
            <Vec<GateIndex> as SchemaWrite>::TYPE_META,
        ) {
            let serialized_size = v + 2 * t + a + b;
            let no_padding = serialized_size == size_of::<Self>();
            TypeMeta::Static {
                size: serialized_size,
                zero_copy: no_padding && zc_v && zc_t && zc_a && zc_b,
            }
        } else {
            TypeMeta::Dynamic
        };
        #[inline]
        fn size_of(src: &Self::Src) -> WriteResult<usize> {
            if let TypeMeta::Static { size, .. } = <Self as SchemaWrite>::TYPE_META {
                return Ok(size);
            }
            let mut total = 0usize;
            total += <u8 as SchemaWrite>::size_of(&src.format_version)?;
            total += <[u8; 32] as SchemaWrite>::size_of(&src.curve_tag)?;
            total += <[u8; 32] as SchemaWrite>::size_of(&src.mpc_field_tag)?;
            total += <containers::Vec<Gate<C>, BincodeLenU32> as SchemaWrite>::size_of(&src.ops)?;
            total += <Vec<GateIndex> as SchemaWrite>::size_of(&src.output_gates)?;
            Ok(total)
        }
        #[inline]
        fn write(writer: &mut impl Writer, src: &Self::Src) -> WriteResult<()> {
            // Macro to reduce duplication in field writing
            macro_rules! write_fields {
                ($writer:expr) => {{
                    <u8 as SchemaWrite>::write($writer, &src.format_version)?;
                    <[u8; 32] as SchemaWrite>::write($writer, &src.curve_tag)?;
                    <[u8; 32] as SchemaWrite>::write($writer, &src.mpc_field_tag)?;
                    <containers::Vec<Gate<C>, BincodeLenU32> as SchemaWrite>::write(
                        $writer, &src.ops,
                    )?;
                    <Vec<GateIndex> as SchemaWrite>::write($writer, &src.output_gates)?;
                }};
            }

            match <Self as SchemaWrite>::TYPE_META {
                TypeMeta::Static { size, .. } => {
                    let writer = &mut unsafe { writer.as_trusted_for(size) }?;
                    write_fields!(writer);
                    writer.finish()?;
                }
                TypeMeta::Dynamic => {
                    write_fields!(writer);
                }
            }
            Ok(())
        }
    }

    impl<'de, C: MpcConfig> SchemaRead<'de> for Circuit<C> {
        type Dst = Self;
        const TYPE_META: TypeMeta = <CompressedCircuit<C> as SchemaRead>::TYPE_META;

        fn read(reader: &mut impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>) -> ReadResult<()> {
            let mut circuit_serde = MaybeUninit::new(CompressedCircuit::<C>::default());
            <CompressedCircuit<C> as SchemaRead>::read(reader, &mut circuit_serde)?;
            let circuit_serde = unsafe { circuit_serde.assume_init() };
            let circuit: Self::Dst = circuit_serde.try_into().map_err(|e| match e {
                CircuitError::UnsupportedFormatVersion { .. } => ::wincode::ReadError::Custom(
                    "Circuit was serialized with an unsupported format version",
                ),
                CircuitError::CurveMismatch { .. } => ::wincode::ReadError::Custom(
                    "Circuit was built for a different curve than this runtime's config",
                ),
                CircuitError::MpcFieldMismatch { .. } => ::wincode::ReadError::Custom(
                    "Circuit was built for a different MPC field than this runtime's config",
                ),
                _ => {
                    ::wincode::ReadError::Custom("Invalid cast from CircuitSerde to Circuit struct")
                }
            })?;
            dst.write(circuit);
            Ok(())
        }
    }

    impl<'de, C: MpcConfig> SchemaRead<'de> for CompressedCircuit<C> {
        type Dst = Self;
        #[allow(clippy::arithmetic_side_effects)]
        const TYPE_META: TypeMeta = if let (
            TypeMeta::Static {
                size: v,
                zero_copy: zc_v,
            },
            TypeMeta::Static {
                size: t,
                zero_copy: zc_t,
            },
            TypeMeta::Static {
                size: a,
                zero_copy: zc_a,
            },
            TypeMeta::Static {
                size: b,
                zero_copy: zc_b,
            },
        ) = (
            <u8 as SchemaRead<'de>>::TYPE_META,
            <[u8; 32] as SchemaRead<'de>>::TYPE_META,
            <containers::Vec<Gate<C>, BincodeLenU32> as SchemaRead<'de>>::TYPE_META,
            <Vec<GateIndex> as SchemaRead<'de>>::TYPE_META,
        ) {
            let serialized_size = v + 2 * t + a + b;
            let no_padding = serialized_size == size_of::<Self>();
            TypeMeta::Static {
                size: serialized_size,
                zero_copy: no_padding && zc_v && zc_t && zc_a && zc_b,
            }
        } else {
            TypeMeta::Dynamic
        };
        #[inline]
        fn read(reader: &mut impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>) -> ReadResult<()> {
            struct DropGuard<C: MpcConfig> {
                init_count: u8,
                dst_ptr: *mut CompressedCircuit<C>,
            }
            impl<C: MpcConfig> Drop for DropGuard<C> {
                #[cold]
                fn drop(&mut self) {
                    let dst_ptr = self.dst_ptr;
                    let init_count = self.init_count;
                    match init_count {
                        0 => {}
                        1u8 => unsafe {
                            ptr::drop_in_place(&raw mut (*dst_ptr).ops);
                        },
                        _ => unreachable!("init_count out of bounds"),
                    }
                }
            }
            // Macro to reduce duplication in field reading
            macro_rules! read_fields {
                ($reader:expr, $dst_ptr:expr, $guard:expr) => {{
                    let init_count = &mut $guard.init_count;
                    // The version byte and tags are Copy: no drop tracking needed.
                    <u8 as SchemaRead<'de>>::read($reader, unsafe {
                        &mut *(&raw mut (*$dst_ptr).format_version).cast::<MaybeUninit<_>>()
                    })?;
                    <[u8; 32] as SchemaRead<'de>>::read($reader, unsafe {
                        &mut *(&raw mut (*$dst_ptr).curve_tag).cast::<MaybeUninit<_>>()
                    })?;
                    <[u8; 32] as SchemaRead<'de>>::read($reader, unsafe {
                        &mut *(&raw mut (*$dst_ptr).mpc_field_tag).cast::<MaybeUninit<_>>()
                    })?;
                    <wincode::containers::Vec<Gate<C>, BincodeLenU32> as SchemaRead<'de>>::read(
                        $reader,
                        unsafe { &mut *(&raw mut (*$dst_ptr).ops).cast::<MaybeUninit<_>>() },
                    )?;
                    *init_count += 1;
                    <Vec<GateIndex> as SchemaRead<'de>>::read($reader, unsafe {
                        &mut *(&raw mut (*$dst_ptr).output_gates).cast::<MaybeUninit<_>>()
                    })?;
                    mem::forget($guard);
                }};
            }

            let dst_ptr = dst.as_mut_ptr();
            let mut guard = DropGuard {
                init_count: 0,
                dst_ptr,
            };

            match <Self as SchemaRead<'de>>::TYPE_META {
                TypeMeta::Static { size, .. } => {
                    read_fields!(&mut unsafe { reader.as_trusted_for(size) }?, dst_ptr, guard);
                }
                TypeMeta::Dynamic => {
                    read_fields!(reader, dst_ptr, guard);
                }
            }
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        circuit::v2::{tests::create_add_tree_circuit, AlgebraicType, FieldShareBinaryOp, Input},
        config::DefaultConfig as C,
    };

    #[test]
    fn valid_circuit() {
        let mut circuit = Circuit::<C>::new();
        let input_gate1 = circuit
            .add_gate(Gate::Input(Input::SecretPlaintext {
                inputer: 0,
                algebraic_type: AlgebraicType::ScalarField,
                batch_size: 1,
            }))
            .unwrap();
        assert_eq!(input_gate1, 0);
        let input_gate2 = circuit
            .add_gate(Gate::Input(Input::SecretPlaintext {
                inputer: 1,
                algebraic_type: AlgebraicType::ScalarField,
                batch_size: 1,
            }))
            .unwrap();
        assert_eq!(input_gate2, 1);
        let add_gate = circuit
            .add_gate(Gate::FieldShareBinaryOp {
                x: input_gate1,
                y: input_gate2,
                op: FieldShareBinaryOp::Add,
            })
            .unwrap();
        assert_eq!(add_gate, 2);
        circuit.add_output(add_gate).unwrap();
        assert_eq!(
            circuit.iter_output_indices().copied().collect::<Vec<_>>(),
            vec![2]
        );
    }

    #[test]
    fn test_ser_circuit_bincode() {
        let circuit = create_add_tree_circuit(18);
        let serialized = ::bincode::serialize(&circuit).unwrap();
        let circuit_de: Circuit<C> = ::bincode::deserialize(&serialized).unwrap();

        assert_eq!(circuit, circuit_de);
    }

    #[test]
    fn test_ser_circuit_wincode() {
        let circuit = create_add_tree_circuit(18);

        // Wincode roundtrip
        let serialized = ::wincode::serialize(&circuit).expect("Serialization failed");
        let deserialized: Circuit<C> =
            ::wincode::deserialize(&serialized).expect("Deserialization failed");

        assert_eq!(circuit, deserialized);
    }

    /// A circuit serialized for one MPC field must not deserialize under a config with another,
    /// in either format.
    #[test]
    fn test_cross_field_rejection() {
        use crate::config::Gf2_128Config;

        let circuit = create_add_tree_circuit::<C>(2);

        let bin = ::bincode::serialize(&circuit).unwrap();
        let err = ::bincode::deserialize::<Circuit<Gf2_128Config>>(&bin).unwrap_err();
        assert!(
            err.to_string().contains("MPC field"),
            "unexpected error: {err}"
        );

        let win = ::wincode::serialize(&circuit).unwrap();
        assert!(::wincode::deserialize::<Circuit<Gf2_128Config>>(&win).is_err());

        // Same-config roundtrips still pass the tag check.
        assert_eq!(::bincode::deserialize::<Circuit<C>>(&bin).unwrap(), circuit);
    }

    /// A blob with an unknown format version is rejected before anything else is parsed, and a
    /// corrupted curve tag is rejected before the field tag.
    #[test]
    fn test_version_and_curve_rejection() {
        let circuit = create_add_tree_circuit::<C>(2);
        let bin = ::bincode::serialize(&circuit).unwrap();

        // Layout: format_version (1 byte) | curve_tag (32) | mpc_field_tag (32) | gates...
        let mut wrong_version = bin.clone();
        wrong_version[0] ^= 0xFF;
        let err = ::bincode::deserialize::<Circuit<C>>(&wrong_version).unwrap_err();
        assert!(
            err.to_string().contains("format version"),
            "unexpected error: {err}"
        );

        let mut wrong_curve = bin;
        wrong_curve[1] ^= 0xFF;
        let err = ::bincode::deserialize::<Circuit<C>>(&wrong_curve).unwrap_err();
        assert!(err.to_string().contains("curve"), "unexpected error: {err}");
    }

    /// The circuit id commits to the MPC field: identical shapes under different configs get
    /// different ids.
    #[test]
    fn test_circuit_id_commits_to_field() {
        use crate::{circuit::circuit_id::CircuitId, config::Gf2_128Config};

        let id_default = CircuitId::of(&create_add_tree_circuit::<C>(2));
        let id_gf2_128 = CircuitId::of(&create_add_tree_circuit::<Gf2_128Config>(2));
        assert_ne!(id_default.as_bytes(), id_gf2_128.as_bytes());
    }
}