midnight-circuits 7.0.0

Circuit and gadget implementations for Midnight zero-knowledge proofs
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
517
518
519
520
521
522
523
524
// This file is part of MIDNIGHT-ZK.
// Copyright (C) Midnight Foundation
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A module for in-circuit partial MSMs and its off-circuit analog.
//! These MSM have a fixed-base part which is represented by the corresponding
//! scalars only.
//! (The bases are assumed to be fixed and globally known.)

use std::collections::{btree_map::Entry, BTreeMap};

use ff::Field;
use midnight_curves::msm::msm_best;
use midnight_proofs::{
    circuit::{Layouter, Value},
    plonk::Error,
};

use crate::{
    field::AssignedNative,
    instructions::PublicInputInstructions,
    types::{InnerValue, Instantiable},
    verifier::{
        types::SelfEmulation,
        utils::{
            add_bounded_scalars, assign_bounded_scalars, mul_bounded_scalars, AssignedBoundedScalar,
        },
    },
};

/// Type for off-circuit multi-scalar multiplications.
///
/// This structure represents the following computation:
/// `<scalars, bases> + <fixed_bases, fixed_base_scalars>`
///
/// Note that the `fixed_bases` are not part of this structure, they are
/// supposed to be globally known and will be provided when evaluating the MSM.
///
/// (`scalars` and `bases` are guaranteed to have the same length.)
#[derive(Clone, Debug)]
pub struct Msm<S: SelfEmulation> {
    bases: Vec<S::C>,
    scalars: Vec<S::F>,
    fixed_base_scalars: BTreeMap<String, S::F>,
}

/// Type for in-circuit multi-scalar multiplications.
///
/// This is the in-circuit analog of `Msm<C>`.
#[derive(Clone, Debug)]
pub struct AssignedMsm<S: SelfEmulation> {
    bases: Vec<S::AssignedPoint>,
    pub(crate) scalars: Vec<AssignedBoundedScalar<S::F>>,
    fixed_base_scalars: BTreeMap<String, AssignedBoundedScalar<S::F>>,
}

impl<S: SelfEmulation> PartialEq for AssignedMsm<S> {
    fn eq(&self, other: &Self) -> bool {
        self.bases == other.bases
            && self.scalars == other.scalars
            && self.fixed_base_scalars == other.fixed_base_scalars
    }
}

impl<S: SelfEmulation> Eq for AssignedMsm<S> {}

impl<S: SelfEmulation> Msm<S> {
    /// Creates a new MSM from the given slice of bases, scalars and a BTreeMap
    /// of fixed_base_scalars.
    ///
    /// # Panics
    ///
    /// If `|bases| != |scalars|`.
    pub fn new(
        bases: &[S::C],
        scalars: &[S::F],
        fixed_base_scalars: &BTreeMap<String, S::F>,
    ) -> Self {
        assert_eq!(bases.len(), scalars.len());
        Msm {
            bases: bases.to_vec(),
            scalars: scalars.to_vec(),
            fixed_base_scalars: fixed_base_scalars.clone(),
        }
    }

    /// The bases of this MSM.
    pub fn bases(&self) -> Vec<S::C> {
        self.bases.clone()
    }

    /// The (non-fixed-base) scalars of this MSM.
    pub fn scalars(&self) -> Vec<S::F> {
        self.scalars.clone()
    }

    /// The fixed-base scalars of this MSM.
    pub fn fixed_base_scalars(&self) -> BTreeMap<String, S::F> {
        self.fixed_base_scalars.clone()
    }

    /// Creates a new MSM from the given base-scalar pairs, with an empty tree
    /// of fixed_base_scalars.
    ///
    /// # Panics
    ///
    /// If `|bases| != |scalars|`.
    pub fn from_terms(bases: &[S::C], scalars: &[S::F]) -> Self {
        assert_eq!(bases.len(), scalars.len());
        Msm {
            bases: bases.to_vec(),
            scalars: scalars.to_vec(),
            fixed_base_scalars: BTreeMap::new(),
        }
    }

    /// Evaluates the variable part of the AssignedMsm (the scalar-base pairs)
    /// collapsing it to a single point (and a scalar of 1), leaving the
    /// fixed-base part intact.
    ///
    /// This function mutates self.
    pub fn collapse(&mut self) {
        let affine_bases: Vec<S::G1Affine> = self.bases.iter().map(|&b| b.into()).collect();
        let collapsed_base = msm_best(&self.scalars, &affine_bases);

        self.bases = vec![collapsed_base];
        self.scalars = vec![S::F::ONE];
    }

    /// Given the actual fixed bases, resolves the fixed-base part of the MSM
    /// by pairing each named scalar with its base and moving them to regular
    /// variable-base entries.
    ///
    /// After this call, `fixed_base_scalars` becomes empty.
    ///
    /// # Panics
    ///
    /// If some of the keys in `fixed_base_scalars` do not appear in the
    /// provided `fixed_bases` map.
    pub fn resolve_fixed_bases(&mut self, fixed_bases: &BTreeMap<String, S::C>) {
        for (name, scalar) in &self.fixed_base_scalars {
            let base = fixed_bases.get(name).unwrap_or_else(|| panic!("Base not provided: {name}"));
            self.bases.push(*base);
            self.scalars.push(*scalar);
        }
        self.fixed_base_scalars.clear();
    }

    /// Evaluates the MSM with the provided fixed_bases.
    /// I.e. it computes `<scalars, bases> + <fixed_bases, fixed_base_scalars>`.
    ///
    /// # Panics
    ///
    /// If some of the keys in the `fixed_base_scalars` of the MSM do not appear
    /// in the tree of `fixed_bases`.
    ///
    /// Note that the converse is not a problem, i.e., the keys of `fixed_bases`
    /// can be a superset of the keys of `fixed_base_scalars`.
    pub fn eval(&self, fixed_bases: &BTreeMap<String, S::C>) -> S::C {
        let mut bases = self.bases.clone();
        let mut scalars = self.scalars.clone();

        for (key, scalar) in self.fixed_base_scalars.iter() {
            let base = fixed_bases.get(key).unwrap_or_else(|| panic!("Base not provided: {key}"));
            bases.push(*base);
            scalars.push(*scalar);
        }

        let affine_bases: Vec<S::G1Affine> = bases.iter().map(|&b| b.into()).collect();
        msm_best(&scalars, &affine_bases)
    }

    /// Accumulates two MSMs with the given scalar r.
    /// The resulting MSM evaluates (on any `fixed_bases`) to
    /// `self.eval(fixed_bases) + r * other.eval(fixed_bases)`.
    pub fn accumulate_with_r(&self, other: &Self, r: S::F) -> Self {
        let mut acc = self.clone();

        acc.bases.extend(other.bases.clone());
        acc.scalars.extend(other.scalars.iter().map(|s| *s * r));

        for (key, value) in other.fixed_base_scalars.clone() {
            let r_times_value = r * value;
            acc.fixed_base_scalars
                .entry(key)
                .and_modify(|e| *e += r_times_value)
                .or_insert(r_times_value);
        }

        acc
    }
}

impl<S: SelfEmulation> InnerValue for AssignedMsm<S> {
    type Element = Msm<S>;

    fn value(&self) -> Value<Self::Element> {
        let bases: Value<Vec<S::C>> = Value::from_iter(self.bases.iter().map(|base| base.value()));

        let scalars: Value<Vec<S::F>> =
            Value::from_iter(self.scalars.iter().map(|s| s.scalar.value().copied()));

        let fixed_based_scalars: Value<BTreeMap<String, S::F>> = Value::from_iter(
            self.fixed_base_scalars
                .iter()
                .map(|(name, s)| s.scalar.value().map(|s| (name.clone(), *s))),
        );

        scalars
            .zip(bases)
            .zip(fixed_based_scalars)
            .map(|((scalars, bases), fixed_base_scalars)| Msm {
                bases,
                scalars,
                fixed_base_scalars,
            })
    }
}

impl<S: SelfEmulation> Instantiable<S::F> for AssignedMsm<S> {
    fn as_public_input(msm: &Msm<S>) -> Vec<S::F> {
        [
            msm.bases.iter().flat_map(S::AssignedPoint::as_public_input).collect::<Vec<_>>(),
            msm.scalars.clone(),
            msm.fixed_base_scalars.values().copied().collect::<Vec<_>>(),
        ]
        .into_iter()
        .flatten()
        .collect::<Vec<_>>()
    }
}

impl<S: SelfEmulation> AssignedMsm<S> {
    /// Converts the off-circuit MSM into two vectors of scalars. The first
    /// will be used as a normal instance, whereas the second will be plugged-in
    /// in as a committed instance.
    ///
    /// The committed instance part corresponds to the (fixed and non-fixed)
    /// scalars of the MSM.
    pub fn as_public_input_with_committed_scalars(msm: &Msm<S>) -> (Vec<S::F>, Vec<S::F>) {
        let normal_instance =
            msm.bases.iter().flat_map(S::AssignedPoint::as_public_input).collect();

        let committed_instance = [
            msm.scalars.clone(),
            msm.fixed_base_scalars.values().copied().collect(),
        ]
        .concat();

        (normal_instance, committed_instance)
    }
}

impl<S: SelfEmulation> AssignedMsm<S> {
    pub(crate) fn in_circuit_as_public_input(
        &self,
        layouter: &mut impl Layouter<S::F>,
        curve_chip: &S::CurveChip,
    ) -> Result<Vec<AssignedNative<S::F>>, Error> {
        Ok([
            self.bases
                .iter()
                .map(|base| curve_chip.as_public_input(layouter, base))
                .collect::<Result<Vec<_>, Error>>()?
                .into_iter()
                .flatten()
                .collect::<Vec<_>>(),
            self.scalars.iter().map(|s| s.clone().scalar).collect::<Vec<_>>(),
            self.fixed_base_scalars.values().map(|s| s.clone().scalar).collect::<Vec<_>>(),
        ]
        .into_iter()
        .flatten()
        .collect())
    }

    pub(crate) fn constrain_as_public_input(
        &self,
        layouter: &mut impl Layouter<S::F>,
        curve_chip: &S::CurveChip,
        scalar_chip: &S::ScalarChip,
    ) -> Result<(), Error> {
        self.bases
            .iter()
            .try_for_each(|base| curve_chip.constrain_as_public_input(layouter, base))?;

        self.scalars
            .iter()
            .try_for_each(|s| scalar_chip.constrain_as_public_input(layouter, &s.clone().scalar))?;

        self.fixed_base_scalars
            .values()
            .try_for_each(|s| scalar_chip.constrain_as_public_input(layouter, &s.clone().scalar))
    }

    pub(crate) fn constrain_as_public_input_with_committed_scalars(
        &self,
        layouter: &mut impl Layouter<S::F>,
        curve_chip: &S::CurveChip,
        scalar_chip: &S::ScalarChip,
    ) -> Result<(), Error> {
        self.bases
            .iter()
            .try_for_each(|base| curve_chip.constrain_as_public_input(layouter, base))?;

        self.scalars.iter().try_for_each(|s| {
            let mut a = S::F::ZERO;
            s.scalar.clone().value().map(|v| a = *v);
            S::constrain_scalar_as_committed_public_input(layouter, scalar_chip, &s.scalar)
        })?;

        self.fixed_base_scalars.values().try_for_each(|s| {
            S::constrain_scalar_as_committed_public_input(layouter, scalar_chip, &s.scalar)
        })
    }
}

impl<S: SelfEmulation> AssignedMsm<S> {
    /// Witnesses an MSM computation of `len` bases/scalars and a `BTreeMap` of
    /// fixed_base_scalars indexed by the given `fixed_base_names`.
    ///
    /// # Warning
    ///
    /// The points of the MSM are not enforced to be part of the relevant prime
    /// order subgroup.
    ///
    /// # Panics
    ///
    /// If `msm_value` is known and its number of variable bases differs from
    /// `len`, or its number of fixed-base scalars differs from
    /// `fixed_base_names.len()`.
    pub fn assign(
        layouter: &mut impl Layouter<S::F>,
        curve_chip: &S::CurveChip,
        scalar_chip: &S::ScalarChip,
        len: usize,
        fixed_base_names: &[String],
        msm_value: Value<Msm<S>>,
    ) -> Result<Self, Error> {
        let bases_val = msm_value.as_ref().map(|msm| msm.bases.clone()).transpose_vec(len);

        let scalars_val = msm_value.as_ref().map(|msm| msm.scalars.clone()).transpose_vec(len);

        let fixed_base_scalars_val = msm_value
            .as_ref()
            .map(|msm| {
                // We only use the keys inside the Value to iterate over it in the right order,
                // these are then discarded.
                msm.fixed_base_scalars.iter().map(|s| *s.1).collect::<Vec<_>>()
            })
            .transpose_vec(fixed_base_names.len());

        // Sort the fixed_base_names to ensure consistency with the BTreeMap.
        let mut fixed_base_names = fixed_base_names.to_vec();
        fixed_base_names.sort();

        let bases = bases_val
            .iter()
            .map(|p| S::assign_without_subgroup_check(layouter, curve_chip, *p))
            .collect::<Result<Vec<_>, Error>>()?;
        let scalars = assign_bounded_scalars(layouter, scalar_chip, &scalars_val)?;
        let fixed_base_scalars: BTreeMap<String, AssignedBoundedScalar<S::F>> = {
            let scalars = assign_bounded_scalars(layouter, scalar_chip, &fixed_base_scalars_val)?;
            fixed_base_names.iter().cloned().zip(scalars).collect()
        };

        Ok(AssignedMsm {
            scalars,
            bases,
            fixed_base_scalars,
        })
    }

    /// An empty AssignedMsm with no fixed base scalars, that evaluates to the
    /// identity point.
    pub fn empty() -> Self {
        Self {
            scalars: vec![],
            bases: vec![],
            fixed_base_scalars: BTreeMap::new(),
        }
    }

    /// Creates a new MSM from the given base (with a scalar of 1).
    pub fn from_term(scalar: &AssignedBoundedScalar<S::F>, base: &S::AssignedPoint) -> Self {
        Self {
            scalars: vec![scalar.clone()],
            bases: vec![base.clone()],
            fixed_base_scalars: BTreeMap::new(),
        }
    }

    /// Creates a new MSM from the given fixed base name (with a scalar of 1).
    pub fn from_fixed_term(scalar: &AssignedBoundedScalar<S::F>, base_name: &str) -> Self {
        Self {
            scalars: vec![],
            bases: vec![],
            fixed_base_scalars: [(base_name.to_string(), scalar.clone())].into_iter().collect(),
        }
    }

    /// Adds a `(scalar, base)` term to the AssignedMsm.
    pub fn add_term(&mut self, scalar: &AssignedBoundedScalar<S::F>, base: &S::AssignedPoint) {
        self.scalars.push(scalar.clone());
        self.bases.push(base.clone());
    }

    /// Adds two AssignedMsm.
    pub fn add_msm(
        &mut self,
        layouter: &mut impl Layouter<S::F>,
        scalar_chip: &S::ScalarChip,
        other: &Self,
    ) -> Result<(), Error> {
        self.scalars.extend(other.scalars.clone());
        self.bases.extend(other.bases.clone());

        for (key, value) in other.fixed_base_scalars.clone() {
            match self.fixed_base_scalars.entry(key) {
                Entry::Occupied(mut occ) => {
                    *occ.get_mut() = add_bounded_scalars(layouter, scalar_chip, occ.get(), &value)?;
                }
                Entry::Vacant(vac) => {
                    vac.insert(value);
                }
            }
        }

        Ok(())
    }

    /// Evaluates the variable part of the AssignedMsm (the scalar-base pairs)
    /// collapsing it to a single point (and a scalar of 1), leaving the
    /// fixed-base part intact.
    ///
    /// This function mutates self.
    pub fn collapse(
        &mut self,
        layouter: &mut impl Layouter<S::F>,
        curve_chip: &S::CurveChip,
        scalar_chip: &S::ScalarChip,
    ) -> Result<(), Error> {
        let scalars = self
            .scalars
            .iter()
            .map(|s| (s.scalar.clone(), s.bound.bits() as usize))
            .collect::<Vec<_>>();

        let collapsed_base = S::msm(layouter, curve_chip, &scalars, &self.bases)?;

        self.bases = vec![collapsed_base];
        self.scalars = vec![AssignedBoundedScalar::one(layouter, scalar_chip)?];

        Ok(())
    }

    /// Given the actual fixed bases, resolves the fixed-base part of the MSM
    /// by pairing each named scalar with its base and moving them to regular
    /// variable-base entries.
    ///
    /// After this call, `fixed_base_scalars` becomes empty.
    ///
    /// # Panics
    ///
    /// If some of the keys in `fixed_base_scalars` do not appear in the
    /// provided `fixed_bases` map.
    pub fn resolve_fixed_bases(&mut self, fixed_bases: &BTreeMap<String, S::AssignedPoint>) {
        for (name, scalar) in &self.fixed_base_scalars {
            let base = fixed_bases
                .get(name)
                .unwrap_or_else(|| panic!("Fixed base not provided: {name}"));
            self.bases.push(base.clone());
            self.scalars.push(scalar.clone());
        }
        self.fixed_base_scalars.clear();
    }

    /// Scales all the scalars of the AssignedMsm by the given factor r.
    ///
    /// This function mutates self.
    pub fn scale(
        &mut self,
        layouter: &mut impl Layouter<S::F>,
        scalar_chip: &S::ScalarChip,
        r: &AssignedBoundedScalar<S::F>,
    ) -> Result<(), Error> {
        self.scalars = (self.scalars.iter())
            .map(|s| mul_bounded_scalars(layouter, scalar_chip, s, r))
            .collect::<Result<Vec<_>, Error>>()?;

        for s in self.fixed_base_scalars.values_mut() {
            *s = mul_bounded_scalars(layouter, scalar_chip, s, r)?;
        }

        Ok(())
    }

    /// Accumulates two AssignedMSMs with the a given scalar r.
    /// The resulting AssignedMSMs evaluates (on `fixed_bases`) to
    /// `self.eval(fixed_bases) + r * other.eval(fixed_bases)`.
    pub fn accumulate_with_r(
        &self,
        layouter: &mut impl Layouter<S::F>,
        scalar_chip: &S::ScalarChip,
        other: &Self,
        r: &AssignedBoundedScalar<S::F>,
    ) -> Result<Self, Error> {
        let mut other = other.clone();
        other.scale(layouter, scalar_chip, r)?;

        let mut acc = self.clone();
        acc.add_msm(layouter, scalar_chip, &other)?;

        Ok(acc)
    }
}