midnight-aggregation 0.1.0

Toolkit for proof aggregation of midnight-proofs
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
//! Single-circuit proof aggregation via IVC.
//!
//! This example demonstrates how to aggregate multiple proofs of a single
//! circuit (a SHA-256 preimage circuit) using Incrementally Verifiable
//! Computation (IVC). All aggregated proofs share the same verifying key
//! since they originate from the same inner circuit.
//!
//! The IVC state tracks:
//! - A list of aggregated statements (off-circuit),
//! - A Poseidon hash of those statements (constant-size),
//! - An accumulator for deferred inner-proof verification (decider check).
//!
//! At each IVC step the transition function verifies one inner proof
//! in-circuit and folds the result into the running accumulator.
//!
//! DO NOT add this example to the CI as it is slow.

#[path = "circuits/sha_preimage.rs"]
mod sha_preimage;

use std::{collections::BTreeMap, time::Instant};

use ff::Field;
use group::Group;
use midnight_aggregation::ivc::{self, IvcContext, IvcIO, IvcState, IvcTransition};
use midnight_circuits::{
    hash::poseidon::{PoseidonChip, PoseidonState},
    instructions::{hash::HashCPU, *},
    types::{AssignedNative, Instantiable},
    verifier::{self, Accumulator, AssignedAccumulator, BlstrsEmulation, SelfEmulation},
};
use midnight_proofs::{
    circuit::{Layouter, Value},
    plonk::{self, ConstraintSystem, Error},
    poly::{
        kzg::{
            params::{ParamsKZG, ParamsVerifierKZG},
            KZGCommitmentScheme,
        },
        EvaluationDomain,
    },
    transcript::{CircuitTranscript, Transcript},
};
use midnight_zk_stdlib::{prove, setup_pk, setup_vk, MidnightVK, Relation, ZkStdLib, ZkStdLibArch};
use rand::rngs::OsRng;
use sha_preimage::ShaPreimageCircuit;

type S = BlstrsEmulation;
type F = <S as SelfEmulation>::F;
type C = <S as SelfEmulation>::C;
type E = <S as SelfEmulation>::Engine;

type InnerCircuit = ShaPreimageCircuit;

/// Setup data for the inner circuit, threaded as IVC context.
#[derive(Clone, Debug)]
pub struct InnerCircuitContext {
    /// Constraint system used by the inner proofs (to be aggregated).
    cs: ConstraintSystem<F>,
    /// Evaluation domain for the inner circuit.
    domain: EvaluationDomain<F>,
    /// Verifying key.
    vk: MidnightVK,
    /// SRS verifier parameters (for off-circuit proof preparation).
    params_verifier: ParamsVerifierKZG<E>,
}

impl InnerCircuitContext {
    fn fixed_bases(&self) -> BTreeMap<String, C> {
        verifier::fixed_bases::<S>("inner_vk", self.vk.vk())
    }
}

/// Off-circuit IVC state for proof aggregation.
#[derive(Clone, Debug)]
pub struct State {
    /// All aggregated inner-circuit statements.
    statements: Vec<<InnerCircuit as Relation>::Instance>,
    /// Poseidon hash chain: H(h_n, H(h_{n-1}, H(... H(h_1, 0)))),
    /// where h_i = Poseidon(statement_i) is the digest of the i-th statement.
    statements_hash: F,
    /// Running accumulator over inner-proof verifications.
    inner_acc: Accumulator<S>,
}

/// In-circuit counterpart of [`State`] (constant size).
#[derive(Clone, Debug)]
pub struct AssignedState {
    statements_hash: AssignedNative<F>,
    inner_acc: AssignedAccumulator<S>,
}

/// Witness for a single aggregation step: an inner statement and its proof.
#[derive(Clone, Debug)]
pub struct AggregationWitness {
    pub inner_statement: <InnerCircuit as Relation>::Instance,
    pub inner_proof: Vec<u8>,
}

/// IVC transition that aggregates one inner proof per step.
#[derive(Clone, Debug)]
pub struct ProofAggregation {
    std_lib: ZkStdLib,
    inner_ctx: InnerCircuitContext,
}

impl IvcContext for ProofAggregation {
    type Context = InnerCircuitContext;

    fn new(std_lib: ZkStdLib, ctx: &InnerCircuitContext) -> Self {
        ProofAggregation {
            std_lib,
            inner_ctx: ctx.clone(),
        }
    }

    fn write_context<W: std::io::Write>(
        _ctx: &InnerCircuitContext,
        _writer: &mut W,
    ) -> std::io::Result<()> {
        // InnerCircuitContext serialization is not needed for this example.
        unimplemented!("InnerCircuitContext serialization not implemented")
    }

    fn read_context<R: std::io::Read>(_reader: &mut R) -> std::io::Result<InnerCircuitContext> {
        unimplemented!("InnerCircuitContext deserialization not implemented")
    }
}

impl IvcState for ProofAggregation {
    type State = State;
    type AssignedState = AssignedState;

    fn genesis(ctx: &InnerCircuitContext) -> Self::State {
        State {
            statements: vec![],
            statements_hash: F::ZERO,
            inner_acc: Accumulator::<S>::trivial(
                &ctx.fixed_bases().keys().cloned().collect::<Vec<_>>(),
            ),
        }
    }

    fn decider(ctx: &InnerCircuitContext, state: &State) -> bool {
        // Hash all collected statements and check against the claimed hash.
        let expected_hash = state.statements.iter().fold(F::ZERO, |h_acc, x| {
            let pis = ShaPreimageCircuit::format_instance(x).expect("valid instance");
            let h = <PoseidonChip<F> as HashCPU<F, F>>::hash(&pis);
            <PoseidonChip<F> as HashCPU<F, F>>::hash(&[h, h_acc])
        });

        if expected_hash != state.statements_hash {
            return false;
        }

        // Check the inner accumulator.
        state.inner_acc.check(&ctx.params_verifier, &ctx.fixed_bases())
    }
}

impl IvcIO for ProofAggregation {
    fn assign(
        &self,
        layouter: &mut impl Layouter<F>,
        value: Value<State>,
    ) -> Result<AssignedState, Error> {
        let statements_hash =
            self.std_lib.assign(layouter, value.as_ref().map(|s| s.statements_hash))?;

        let inner_acc = self.std_lib.verifier().assign_collapsed_accumulator(
            layouter,
            &self.inner_ctx.fixed_bases().keys().cloned().collect::<Vec<_>>(),
            value.as_ref().map(|s| s.inner_acc.clone()),
        )?;

        Ok(AssignedState {
            statements_hash,
            inner_acc,
        })
    }

    fn constrain_as_public_input(
        &self,
        layouter: &mut impl Layouter<F>,
        state: &AssignedState,
    ) -> Result<(), Error> {
        self.std_lib.constrain_as_public_input(layouter, &state.statements_hash)?;
        self.std_lib.verifier().constrain_as_public_input(layouter, &state.inner_acc)
    }

    fn as_public_input(
        &self,
        layouter: &mut impl Layouter<F>,
        state: &AssignedState,
    ) -> Result<Vec<AssignedNative<F>>, Error> {
        Ok([
            self.std_lib.as_public_input(layouter, &state.statements_hash)?,
            self.std_lib.verifier().as_public_input(layouter, &state.inner_acc)?,
        ]
        .concat())
    }

    fn format_public_input(state: &State) -> Vec<F> {
        [
            vec![state.statements_hash],
            AssignedAccumulator::<S>::as_public_input(&state.inner_acc),
        ]
        .concat()
    }
}

impl IvcTransition for ProofAggregation {
    type Witness = AggregationWitness;

    fn arch() -> ZkStdLibArch {
        ZkStdLibArch {
            poseidon: true,
            nr_pow2range_cols: 4,
            ..ZkStdLibArch::default()
        }
    }

    fn transition(
        ctx: &InnerCircuitContext,
        state: &Self::State,
        witness: Self::Witness,
    ) -> Self::State {
        // Format inner statement as field elements.
        let statement_pis =
            ShaPreimageCircuit::format_instance(&witness.inner_statement).expect("valid instance");

        // Off-circuit: prepare the inner proof to obtain the proof accumulator.
        let inner_proof_acc = {
            let mut transcript =
                CircuitTranscript::<PoseidonState<F>>::init_from_bytes(&witness.inner_proof);
            let dual_msm =
                plonk::prepare::<F, KZGCommitmentScheme<E>, CircuitTranscript<PoseidonState<F>>>(
                    ctx.vk.vk(),
                    &[&[C::identity()]],
                    &[&[&statement_pis]],
                    &mut transcript,
                )
                .expect("off-circuit prepare should succeed");

            // Sanity check.
            assert!(
                dual_msm.clone().check(&ctx.params_verifier),
                "invalid inner proof"
            );

            Accumulator::from_dual_msm(dual_msm, "inner_vk", &ctx.fixed_bases())
        };

        // Accumulate and collapse.
        let inner_acc = {
            let mut acc = Accumulator::accumulate(&[inner_proof_acc, state.inner_acc.clone()]);
            acc.collapse();
            acc
        };

        // Hash: H(h_statement, prev_hash).
        let statements_hash = {
            let h_statement = <PoseidonChip<F> as HashCPU<F, F>>::hash(&statement_pis);
            <PoseidonChip<F> as HashCPU<F, F>>::hash(&[h_statement, state.statements_hash])
        };

        let mut statements = state.statements.clone();
        statements.push(witness.inner_statement);

        State {
            statements,
            statements_hash,
            inner_acc,
        }
    }

    fn circuit_transition(
        &self,
        layouter: &mut impl Layouter<F>,
        state: &Self::AssignedState,
        witness: Value<Self::Witness>,
    ) -> Result<Self::AssignedState, Error> {
        // Assign inner VK as a hard-coded constant.
        let inner_vk = self.std_lib.verifier().assign_fixed_vk(
            layouter,
            "inner_vk",
            &self.inner_ctx.domain,
            &self.inner_ctx.cs,
            self.inner_ctx.vk.vk().transcript_repr(),
        )?;

        // Assign the inner statement as a witness.
        let statement_pis = self.std_lib.assign_many(
            layouter,
            &witness
                .as_ref()
                .map(|w| ShaPreimageCircuit::format_instance(&w.inner_statement).unwrap())
                .transpose_vec(sha_preimage::NB_PUBLIC_INPUTS),
        )?;

        // Verify the inner proof in-circuit.
        let id_point = self.std_lib.bls12_381().assign_fixed(layouter, C::identity())?;

        let inner_proof_acc = self.std_lib.verifier().prepare(
            layouter,
            &inner_vk,
            &[id_point],
            &[&statement_pis],
            witness.map(|w| w.inner_proof),
        )?;

        // Accumulate and collapse.
        let inner_acc = {
            let mut acc = self
                .std_lib
                .verifier()
                .accumulate(layouter, &[inner_proof_acc, state.inner_acc.clone()])?;

            acc.collapse(
                layouter,
                self.std_lib.bls12_381(),
                self.std_lib.bls12_381().scalar_field_chip(),
            )?;
            acc
        };

        // Hash: H(h_statement, prev_hash).
        let statements_hash = {
            let h_statement = self.std_lib.poseidon(layouter, &statement_pis)?;
            self.std_lib.poseidon(layouter, &[h_statement, state.statements_hash.clone()])?
        };

        Ok(AssignedState {
            statements_hash,
            inner_acc,
        })
    }
}

fn main() {
    // Circuit size parameter for the IVC circuit (log2 of rows).
    const IVC_K: u32 = 19;
    const STEPS: usize = 3;

    // The inner circuit can use a different SRS than the IVC circuit.
    let inner_srs = ParamsKZG::unsafe_setup(sha_preimage::K, OsRng);
    let inner_vk = setup_vk(&inner_srs, &ShaPreimageCircuit);
    let inner_pk = setup_pk(&ShaPreimageCircuit, &inner_vk);
    let inner_ctx = {
        let arch = ShaPreimageCircuit.used_chips();
        let k = sha_preimage::K;
        let mut cs = midnight_proofs::plonk::ConstraintSystem::default();
        ZkStdLib::configure(&mut cs, (arch, (k - 1) as u8));
        let domain = midnight_proofs::poly::EvaluationDomain::new(cs.degree() as u32, k);

        InnerCircuitContext {
            cs,
            domain,
            vk: inner_vk,
            params_verifier: inner_srs.verifier_params(),
        }
    };

    // Generate random inner statements and prove them.
    let start = Instant::now();
    let inner_statements_with_witnesses: [_; STEPS] =
        std::array::from_fn(|_| sha_preimage::random_instance());
    let inner_proofs: [_; STEPS] = std::array::from_fn(|i| {
        let (digest, preimage) = &inner_statements_with_witnesses[i];
        prove::<ShaPreimageCircuit, PoseidonState<F>>(
            &inner_srs,
            &inner_pk,
            &ShaPreimageCircuit,
            digest,
            *preimage,
            OsRng,
        )
        .expect("proof generation should not fail")
    });
    let inner_statements = inner_statements_with_witnesses.map(|(x, _)| x);
    println!("{STEPS} inner proofs generated in {:.2?}", start.elapsed());

    // IVC setup.
    let ivc_srs = midnight_zk_stdlib::utils::plonk_api::filecoin_srs(IVC_K);
    let start = Instant::now();
    let (mut prover, verifier) = ivc::setup::<ProofAggregation>(ivc_srs, IVC_K, inner_ctx);
    println!("IVC setup completed in {:.2?}", start.elapsed());

    // Aggregation steps.
    for i in 0..STEPS {
        let ivc_witness = AggregationWitness {
            inner_statement: inner_statements[i],
            inner_proof: inner_proofs[i].clone(),
        };

        let start = Instant::now();
        let ivc_proof = prover.prove_step(ivc_witness).unwrap();
        let prove_time = start.elapsed();

        let ivc_instance = prover.instance();
        let start = Instant::now();
        verifier.verify(&ivc_instance, &ivc_proof).unwrap();
        let verify_time = start.elapsed();

        println!("Step {i}: IVC prove {prove_time:.2?}, verify {verify_time:.2?}");
    }

    let final_state = prover.instance().state().clone();
    println!("\nAggregated {STEPS} SHA-256 proofs.");
    for (i, stmt) in final_state.statements.iter().enumerate() {
        let hex: String = stmt.iter().map(|b| format!("{b:02x}")).collect();
        println!("  {i}: {hex}");
    }
    println!("Statements hash: {:?}", final_state.statements_hash);
}