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
use std::fmt::Debug;
use super::commitment::{KZGCommitmentScheme, ParamsKZG};
use crate::{
arithmetic::{best_multiexp, parallelize, CurveAffine},
poly::commitment::MSM,
};
use group::{Curve, Group};
use halo2curves::pairing::{Engine, MillerLoopResult, MultiMillerLoop};
#[derive(Clone, Default, Debug)]
pub struct MSMKZG<E: Engine> {
pub(crate) scalars: Vec<E::Scalar>,
pub(crate) bases: Vec<E::G1>,
}
impl<E: Engine> MSMKZG<E> {
pub fn new() -> Self {
MSMKZG {
scalars: vec![],
bases: vec![],
}
}
pub fn combine_with_base(&mut self, base: E::Scalar) {
use ff::Field;
let mut acc = E::Scalar::one();
if !self.scalars.is_empty() {
for scalar in self.scalars.iter_mut().rev() {
*scalar *= &acc;
acc *= base;
}
}
}
}
impl<E: Engine + Debug> MSM<E::G1Affine> for MSMKZG<E> {
fn append_term(&mut self, scalar: E::Scalar, point: E::G1) {
self.scalars.push(scalar);
self.bases.push(point);
}
fn add_msm(&mut self, other: &Self) {
self.scalars.extend(other.scalars().iter());
self.bases.extend(other.bases().iter());
}
fn scale(&mut self, factor: E::Scalar) {
if !self.scalars.is_empty() {
parallelize(&mut self.scalars, |scalars, _| {
for other_scalar in scalars {
*other_scalar *= &factor;
}
})
}
}
fn check(&self) -> bool {
bool::from(self.eval().is_identity())
}
fn eval(&self) -> E::G1 {
use group::prime::PrimeCurveAffine;
let mut bases = vec![E::G1Affine::identity(); self.scalars.len()];
E::G1::batch_normalize(&self.bases, &mut bases);
best_multiexp(&self.scalars, &bases)
}
fn bases(&self) -> Vec<E::G1> {
self.bases.clone()
}
fn scalars(&self) -> Vec<E::Scalar> {
self.scalars.clone()
}
}
#[derive(Debug, Clone)]
pub(crate) struct PreMSM<E: Engine> {
projectives_msms: Vec<MSMKZG<E>>,
}
impl<E: Engine + Debug> PreMSM<E> {
pub(crate) fn new() -> Self {
PreMSM {
projectives_msms: vec![],
}
}
pub(crate) fn normalize(self) -> MSMKZG<E> {
use group::prime::PrimeCurveAffine;
let (scalars, bases) = self
.projectives_msms
.into_iter()
.map(|msm| (msm.scalars, msm.bases))
.unzip::<_, _, Vec<_>, Vec<_>>();
MSMKZG {
scalars: scalars.into_iter().flatten().collect(),
bases: bases.into_iter().flatten().collect(),
}
}
pub(crate) fn add_msm(&mut self, other: MSMKZG<E>) {
self.projectives_msms.push(other);
}
}
impl<'params, E: MultiMillerLoop + Debug> From<&'params ParamsKZG<E>> for DualMSM<'params, E> {
fn from(params: &'params ParamsKZG<E>) -> Self {
DualMSM::new(params)
}
}
#[derive(Debug, Clone)]
pub struct DualMSM<'a, E: Engine> {
pub(crate) params: &'a ParamsKZG<E>,
pub(crate) left: MSMKZG<E>,
pub(crate) right: MSMKZG<E>,
}
impl<'a, E: MultiMillerLoop + Debug> DualMSM<'a, E> {
pub fn new(params: &'a ParamsKZG<E>) -> Self {
Self {
params,
left: MSMKZG::new(),
right: MSMKZG::new(),
}
}
pub fn scale(&mut self, e: E::Scalar) {
self.left.scale(e);
self.right.scale(e);
}
pub fn add_msm(&mut self, other: Self) {
self.left.add_msm(&other.left);
self.right.add_msm(&other.right);
}
pub fn check(self) -> bool {
let s_g2_prepared = E::G2Prepared::from(self.params.s_g2);
let n_g2_prepared = E::G2Prepared::from(-self.params.g2);
let left = self.left.eval();
let right = self.right.eval();
let (term_1, term_2) = (
(&left.into(), &s_g2_prepared),
(&right.into(), &n_g2_prepared),
);
let terms = &[term_1, term_2];
bool::from(
E::multi_miller_loop(&terms[..])
.final_exponentiation()
.is_identity(),
)
}
}