arcium-core-utils 0.8.0

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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! `old::v1` is a frozen snapshot of the very first circuit format; real historical circuits may
//! depend on this shape, so it must never change. `EXPECTED_FINGERPRINT` pins v1's current
//! `bincode` encoding of `(sample_circuit(), all_gates())` (this tooling postdates v1's freeze, so
//! it can't prove day-one fidelity, only that nothing has changed since). A failure means
//! something modified v1 and broke a format real data depends on -- revert it, don't touch the
//! constant.
//!
//! `testdata/circuit.bin` freezes those same bytes as a fixture; `fixture_round_trips` decodes
//! them with v1's production `Deserialize` impl and re-encodes, proving real bytes still decode
//! correctly. Never regenerate it.
//!
//! `old::v1` only derives `Deserialize` in production (it's read-only). To compute this
//! fingerprint we add `#[cfg_attr(test, derive(Serialize))]` on `Gate`, `Input`, `Batched`,
//! `Plaintext`, `GateIndex`, plus a `#[cfg(test)] GateIndex::new` (its field is private) --
//! test-only, no production impact.
//!
//! See `old::v2::format_fingerprint` for the freeze recipe.

use primitives::{
    algebra::{
        elliptic_curve::{BaseFieldElement, Curve25519Ristretto as EC, Point, Scalar},
        field::{subfield_element::Mersenne107Element, Bit},
        BoxedUint,
    },
    utils::codec::bincode_io,
};

use crate::circuit::old::v1::{
    circuit::{Circuit, GateIndex},
    constants::{
        BaseFieldPlaintext,
        BaseFieldPlaintextBatch,
        BitPlaintext,
        BitPlaintextBatch,
        Mersenne107Plaintext,
        Mersenne107PlaintextBatch,
        PointPlaintext,
        PointPlaintextBatch,
        ScalarPlaintext,
        ScalarPlaintextBatch,
    },
    gate::Gate,
    ops::{
        Batched,
        BitShareBinaryOp,
        BitShareUnaryOp,
        FieldPlaintextBinaryOp,
        FieldPlaintextUnaryOp,
        FieldShareBinaryOp,
        FieldShareUnaryOp,
        Input,
        PointPlaintextBinaryOp,
        PointPlaintextUnaryOp,
        PointShareBinaryOp,
        PointShareUnaryOp,
    },
    AlgebraicType,
    FieldType,
    ShareOrPlaintext,
};

/// One instance of every [`Gate`] variant, every `Input`/`Plaintext`/`Batched` variant, and every
/// op variant for v1-specific op fields. Op enums shared with `latest` (via type alias) get one
/// representative variant each -- already covered by `latest`'s own fingerprint.
fn all_gates() -> Vec<Gate<EC>> {
    let scalar = || Scalar::<EC>::from(1u64);
    let base_field = || BaseFieldElement::<EC>::from(1u64);
    let mersenne107 = || Mersenne107Element::from(1u64);
    let bit = || Bit::from(false);
    let point = || Point::<EC>::identity();
    let exp = || BoxedUint::from(vec![1u64]);
    let gi = GateIndex::new;

    vec![
        // Input - share-like variants
        Gate::Input {
            input_type: Input::SecretPlaintext {
                inputer: 0,
                algebraic_type: AlgebraicType::ScalarField,
                batched: Batched::No,
            },
        },
        Gate::Input {
            input_type: Input::SecretPlaintext {
                inputer: 0,
                algebraic_type: AlgebraicType::ScalarField,
                batched: Batched::Yes(1),
            },
        },
        Gate::Input {
            input_type: Input::Share {
                algebraic_type: AlgebraicType::Point,
                batched: Batched::No,
            },
        },
        Gate::Input {
            input_type: Input::Share {
                algebraic_type: AlgebraicType::Point,
                batched: Batched::Yes(1),
            },
        },
        Gate::Input {
            input_type: Input::RandomShare {
                algebraic_type: AlgebraicType::Bit,
                batched: Batched::No,
            },
        },
        Gate::Input {
            input_type: Input::RandomShare {
                algebraic_type: AlgebraicType::Bit,
                batched: Batched::Yes(1),
            },
        },
        // Input - Plaintext-wrapped constants
        Gate::Input {
            input_type: Input::Scalar(ScalarPlaintext::<EC>::Fixed(scalar())),
        },
        Gate::Input {
            input_type: Input::Scalar(ScalarPlaintext::<EC>::Input(1)),
        },
        Gate::Input {
            input_type: Input::ScalarBatch(ScalarPlaintextBatch::<EC>::Fixed(vec![scalar()])),
        },
        Gate::Input {
            input_type: Input::ScalarBatch(ScalarPlaintextBatch::<EC>::Input(1)),
        },
        Gate::Input {
            input_type: Input::BaseField(BaseFieldPlaintext::<EC>::Fixed(base_field())),
        },
        Gate::Input {
            input_type: Input::BaseField(BaseFieldPlaintext::<EC>::Input(1)),
        },
        Gate::Input {
            input_type: Input::BaseFieldBatch(BaseFieldPlaintextBatch::<EC>::Fixed(vec![
                base_field(),
            ])),
        },
        Gate::Input {
            input_type: Input::BaseFieldBatch(BaseFieldPlaintextBatch::<EC>::Input(1)),
        },
        Gate::Input {
            input_type: Input::Mersenne107(Mersenne107Plaintext::Fixed(mersenne107())),
        },
        Gate::Input {
            input_type: Input::Mersenne107(Mersenne107Plaintext::Input(1)),
        },
        Gate::Input {
            input_type: Input::Mersenne107Batch(Mersenne107PlaintextBatch::Fixed(vec![
                mersenne107(),
            ])),
        },
        Gate::Input {
            input_type: Input::Mersenne107Batch(Mersenne107PlaintextBatch::Input(1)),
        },
        Gate::Input {
            input_type: Input::Bit(BitPlaintext::Fixed(bit())),
        },
        Gate::Input {
            input_type: Input::Bit(BitPlaintext::Input(1)),
        },
        Gate::Input {
            input_type: Input::BitBatch(BitPlaintextBatch::Fixed(vec![bit()])),
        },
        Gate::Input {
            input_type: Input::BitBatch(BitPlaintextBatch::Input(1)),
        },
        Gate::Input {
            input_type: Input::Point(PointPlaintext::<EC>::Fixed(point())),
        },
        Gate::Input {
            input_type: Input::Point(PointPlaintext::<EC>::Input(1)),
        },
        Gate::Input {
            input_type: Input::PointBatch(PointPlaintextBatch::<EC>::Fixed(vec![point()])),
        },
        Gate::Input {
            input_type: Input::PointBatch(PointPlaintextBatch::<EC>::Input(1)),
        },
        // FieldShareUnaryOp
        Gate::FieldShareUnaryOp {
            x: gi(0),
            op: FieldShareUnaryOp::Neg,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldShareUnaryOp {
            x: gi(0),
            op: FieldShareUnaryOp::MulInverse,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldShareUnaryOp {
            x: gi(0),
            op: FieldShareUnaryOp::Open,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldShareUnaryOp {
            x: gi(0),
            op: FieldShareUnaryOp::IsZero,
            field_type: FieldType::ScalarField,
        },
        // FieldShareBinaryOp
        Gate::FieldShareBinaryOp {
            x: gi(0),
            y: gi(1),
            y_form: ShareOrPlaintext::Share,
            op: FieldShareBinaryOp::Add,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldShareBinaryOp {
            x: gi(0),
            y: gi(1),
            y_form: ShareOrPlaintext::Plaintext,
            op: FieldShareBinaryOp::Mul,
            field_type: FieldType::ScalarField,
        },
        // BatchSummation
        Gate::BatchSummation {
            x: gi(0),
            x_form: ShareOrPlaintext::Share,
            algebraic_type: AlgebraicType::ScalarField,
        },
        // BitShareUnaryOp
        Gate::BitShareUnaryOp {
            x: gi(0),
            op: BitShareUnaryOp::Not,
        },
        Gate::BitShareUnaryOp {
            x: gi(0),
            op: BitShareUnaryOp::Open,
        },
        // BitShareBinaryOp
        Gate::BitShareBinaryOp {
            x: gi(0),
            y: gi(1),
            y_form: ShareOrPlaintext::Share,
            op: BitShareBinaryOp::Xor,
        },
        Gate::BitShareBinaryOp {
            x: gi(0),
            y: gi(1),
            y_form: ShareOrPlaintext::Plaintext,
            op: BitShareBinaryOp::Or,
        },
        Gate::BitShareBinaryOp {
            x: gi(0),
            y: gi(1),
            y_form: ShareOrPlaintext::Share,
            op: BitShareBinaryOp::And,
        },
        // PointShareUnaryOp
        Gate::PointShareUnaryOp {
            p: gi(0),
            op: PointShareUnaryOp::Neg,
        },
        Gate::PointShareUnaryOp {
            p: gi(0),
            op: PointShareUnaryOp::Open,
        },
        Gate::PointShareUnaryOp {
            p: gi(0),
            op: PointShareUnaryOp::IsZero,
        },
        // PointShareBinaryOp
        Gate::PointShareBinaryOp {
            p: gi(0),
            y: gi(1),
            p_form: ShareOrPlaintext::Share,
            y_form: ShareOrPlaintext::Share,
            op: PointShareBinaryOp::Add,
        },
        Gate::PointShareBinaryOp {
            p: gi(0),
            y: gi(1),
            p_form: ShareOrPlaintext::Share,
            y_form: ShareOrPlaintext::Plaintext,
            op: PointShareBinaryOp::ScalarMul,
        },
        // FieldPlaintextUnaryOp
        Gate::FieldPlaintextUnaryOp {
            x: gi(0),
            op: FieldPlaintextUnaryOp::Neg,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextUnaryOp {
            x: gi(0),
            op: FieldPlaintextUnaryOp::MulInverse,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextUnaryOp {
            x: gi(0),
            op: FieldPlaintextUnaryOp::BitExtract {
                little_endian_bit_idx: 0,
                signed: false,
            },
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextUnaryOp {
            x: gi(0),
            op: FieldPlaintextUnaryOp::Sqrt,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextUnaryOp {
            x: gi(0),
            op: FieldPlaintextUnaryOp::Pow { exp: exp() },
            field_type: FieldType::ScalarField,
        },
        // FieldPlaintextBinaryOp
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Add,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Mul,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::EuclDiv,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Mod,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Gt,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Ge,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Eq,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Xor,
            field_type: FieldType::ScalarField,
        },
        Gate::FieldPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Or,
            field_type: FieldType::ScalarField,
        },
        // BitPlaintextUnaryOp (op: FieldPlaintextUnaryOp)
        Gate::BitPlaintextUnaryOp {
            x: gi(0),
            op: FieldPlaintextUnaryOp::Neg,
        },
        // BitPlaintextBinaryOp (op: FieldPlaintextBinaryOp)
        Gate::BitPlaintextBinaryOp {
            x: gi(0),
            y: gi(1),
            op: FieldPlaintextBinaryOp::Mul,
        },
        // PointPlaintextUnaryOp
        Gate::PointPlaintextUnaryOp {
            p: gi(0),
            op: PointPlaintextUnaryOp::Neg,
        },
        // PointPlaintextBinaryOp
        Gate::PointPlaintextBinaryOp {
            p: gi(0),
            y: gi(1),
            op: PointPlaintextBinaryOp::Add,
        },
        Gate::PointPlaintextBinaryOp {
            p: gi(0),
            y: gi(1),
            op: PointPlaintextBinaryOp::ScalarMul,
        },
        // DaBit
        Gate::DaBit {
            field_type: FieldType::BaseField,
            batched: Batched::No,
        },
        Gate::DaBit {
            field_type: FieldType::ScalarField,
            batched: Batched::Yes(1),
        },
        Gate::DaBit {
            field_type: FieldType::MpcField,
            batched: Batched::No,
        },
        Gate::GetDaBitFieldShare {
            x: gi(0),
            field_type: FieldType::ScalarField,
        },
        Gate::GetDaBitSharedBit {
            x: gi(0),
            field_type: FieldType::ScalarField,
        },
        Gate::BaseFieldPow {
            x: gi(0),
            exp: exp(),
        },
        // BitPlaintextToField
        Gate::BitPlaintextToField {
            x: gi(0),
            field_type: FieldType::BaseField,
        },
        Gate::BitPlaintextToField {
            x: gi(0),
            field_type: FieldType::ScalarField,
        },
        Gate::BitPlaintextToField {
            x: gi(0),
            field_type: FieldType::MpcField,
        },
        Gate::FieldPlaintextToBit {
            x: gi(0),
            field_type: FieldType::ScalarField,
        },
        Gate::BatchGetIndex {
            x: gi(0),
            x_type: AlgebraicType::ScalarField,
            x_form: ShareOrPlaintext::Share,
            index: 0,
        },
        Gate::CollectToBatch {
            wires: vec![gi(0), gi(1)],
            x_type: AlgebraicType::ScalarField,
            x_form: ShareOrPlaintext::Share,
        },
        Gate::PointFromPlaintextCoordinates {
            wires: vec![gi(0), gi(1)],
        },
        Gate::PlaintextPointToCoordinates { point: gi(0) },
        Gate::PlaintextKeccakF1600 {
            wires: vec![gi(0), gi(1)],
        },
        Gate::CompressPlaintextPoint { point: gi(0) },
        Gate::KeyRecoveryPlaintextComputeErrors {
            d_minus_one: gi(0),
            syndromes: gi(1),
        },
    ]
}

/// A small circuit (two inputs, no ops, one output) exercising `Circuit`'s own struct shape.
fn sample_circuit() -> Circuit<EC> {
    Circuit::new_for_test(
        vec![
            Gate::Input {
                input_type: Input::SecretPlaintext {
                    inputer: 0,
                    algebraic_type: AlgebraicType::ScalarField,
                    batched: Batched::No,
                },
            },
            Gate::Input {
                input_type: Input::SecretPlaintext {
                    inputer: 1,
                    algebraic_type: AlgebraicType::ScalarField,
                    batched: Batched::No,
                },
            },
        ],
        vec![GateIndex::new(0), GateIndex::new(1)],
        vec![GateIndex::new(1)],
    )
}

/// Frozen historical anchor -- never update. See module docs.
const EXPECTED_FINGERPRINT: &str =
    "861b86ca2b29c0618706023addae0e0cfd763531472e7b8afe24fa908f9352a9";

#[test]
fn wire_format_matches_frozen_v1() {
    let bytes = bincode_io::serialize(&(sample_circuit(), all_gates()))
        .expect("circuit/gate bincode serialization");
    let fingerprint = blake3::hash(&bytes).to_hex().to_string();

    assert_eq!(
        fingerprint, EXPECTED_FINGERPRINT,
        "\n\n`old::v1` wire format changed -- it must never change. Revert whatever touched \
         `old::v1::gate`/`ops`/`constants`/`circuit::Circuit`; note that v1's `FieldType`, \
         `AlgebraicType`, `ShareOrPlaintext`, and all of its op enums are type aliases to \
         `latest::mod`/`latest::ops`, so a variant change there breaks v1 too. Do not update \
         EXPECTED_FINGERPRINT.\n"
    );
}

/// Decodes `testdata/circuit.bin` with today's `old::v1` code and re-encodes it -- proves
/// `Deserialize` still works on real bytes, not just that a freshly-built sample still hashes the
/// same.
#[test]
fn fixture_round_trips() {
    let fixture = include_bytes!("testdata/circuit.bin");
    let (circuit, gates): (Circuit<EC>, Vec<Gate<EC>>) =
        bincode_io::deserialize(fixture).expect("fixture must still deserialize");
    let bytes =
        bincode_io::serialize(&(circuit, gates)).expect("circuit/gate bincode serialization");

    assert_eq!(
        bytes, fixture,
        "\n\nRe-encoding the decoded `testdata/circuit.bin` fixture didn't reproduce the original \
         bytes -- `old::v1` must never change. Revert whatever touched it.\n"
    );
}