Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSM optimizations #608

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 35 additions & 30 deletions halo2_proofs/src/poly/commitment/msm.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use super::Params;
use crate::arithmetic::{best_multiexp, parallelize, CurveAffine};
use crate::arithmetic::{best_multiexp, CurveAffine};
use ff::Field;
use group::Group;

use std::collections::BTreeMap;

/// A multiscalar multiplication in the polynomial commitment scheme
#[derive(Debug, Clone)]
pub struct MSM<'a, C: CurveAffine> {
pub(crate) params: &'a Params<C>,
g_scalars: Option<Vec<C::Scalar>>,
w_scalar: Option<C::Scalar>,
u_scalar: Option<C::Scalar>,
other_scalars: Vec<C::Scalar>,
other_bases: Vec<C>,
// TODO: we could make C: Ord somehow and use it as the key instead of this
// hacky approach that wastes ~64 bytes of memory for the pasta curves per
// entry.
other: BTreeMap<(C::Base, C::Base), (C::Scalar, C)>,
daira marked this conversation as resolved.
Show resolved Hide resolved
}

impl<'a, C: CurveAffine> MSM<'a, C> {
Expand All @@ -20,23 +24,25 @@ impl<'a, C: CurveAffine> MSM<'a, C> {
let g_scalars = None;
let w_scalar = None;
let u_scalar = None;
let other_scalars = vec![];
let other_bases = vec![];
let other = BTreeMap::new();

MSM {
params,
g_scalars,
w_scalar,
u_scalar,
other_scalars,
other_bases,
other,
}
}

/// Add another multiexp into this one
pub fn add_msm(&mut self, other: &Self) {
self.other_scalars.extend(other.other_scalars.iter());
self.other_bases.extend(other.other_bases.iter());
for (&point, entry) in other.other.iter() {
self.other
.entry(point)
.and_modify(|e| e.0 += entry.0)
.or_insert(*entry);
}

if let Some(g_scalars) = &other.g_scalars {
self.add_to_g_scalars(g_scalars);
Expand All @@ -53,8 +59,15 @@ impl<'a, C: CurveAffine> MSM<'a, C> {

/// Add arbitrary term (the scalar and the point)
pub fn append_term(&mut self, scalar: C::Scalar, point: C) {
self.other_scalars.push(scalar);
self.other_bases.push(point);
if !bool::from(point.is_identity()) {
let xy = point.coordinates().unwrap();
let xy = (*xy.x(), *xy.y());

self.other
.entry(xy)
.and_modify(|e| e.0 += scalar)
.or_insert((scalar, point));
}
}

/// Add a value to the first entry of `g_scalars`.
Expand All @@ -73,11 +86,9 @@ impl<'a, C: CurveAffine> MSM<'a, C> {
pub fn add_to_g_scalars(&mut self, scalars: &[C::Scalar]) {
assert_eq!(scalars.len(), self.params.n as usize);
if let Some(g_scalars) = &mut self.g_scalars {
parallelize(g_scalars, |g_scalars, start| {
for (g_scalar, scalar) in g_scalars.iter_mut().zip(scalars[start..].iter()) {
*g_scalar += scalar;
}
})
for (g_scalar, scalar) in g_scalars.iter_mut().zip(scalars.iter()) {
*g_scalar += scalar;
}
} else {
self.g_scalars = Some(scalars.to_vec());
}
Expand All @@ -96,19 +107,13 @@ impl<'a, C: CurveAffine> MSM<'a, C> {
/// Scale all scalars in the MSM by some scaling factor
pub fn scale(&mut self, factor: C::Scalar) {
if let Some(g_scalars) = &mut self.g_scalars {
parallelize(g_scalars, |g_scalars, _| {
for g_scalar in g_scalars {
*g_scalar *= &factor;
}
})
for g_scalar in g_scalars {
*g_scalar *= &factor;
}
}

if !self.other_scalars.is_empty() {
parallelize(&mut self.other_scalars, |other_scalars, _| {
for other_scalar in other_scalars {
*other_scalar *= &factor;
}
})
for other in self.other.values_mut() {
other.0 *= factor;
}

self.w_scalar = self.w_scalar.map(|a| a * &factor);
Expand All @@ -120,12 +125,12 @@ impl<'a, C: CurveAffine> MSM<'a, C> {
let len = self.g_scalars.as_ref().map(|v| v.len()).unwrap_or(0)
+ self.w_scalar.map(|_| 1).unwrap_or(0)
+ self.u_scalar.map(|_| 1).unwrap_or(0)
+ self.other_scalars.len();
+ self.other.len();
let mut scalars: Vec<C::Scalar> = Vec::with_capacity(len);
let mut bases: Vec<C> = Vec::with_capacity(len);

scalars.extend(&self.other_scalars);
bases.extend(&self.other_bases);
scalars.extend(self.other.values().map(|e| e.0));
bases.extend(self.other.values().map(|e| e.1));

if let Some(w_scalar) = self.w_scalar {
scalars.push(w_scalar);
Expand Down