Skip to content

Commit bd01455

Browse files
authored
split DlogGroup so we have a separate trait for MSM methods (#380)
1 parent dee92f8 commit bd01455

File tree

6 files changed

+47
-54
lines changed

6 files changed

+47
-54
lines changed

src/provider/bn256_grumpkin.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
impl_traits,
44
provider::{
55
msm::{msm, msm_small},
6-
traits::{DlogGroup, PairingGroup},
6+
traits::{DlogGroup, DlogGroupExt, PairingGroup},
77
},
88
traits::{Group, PrimeFieldExt, TranscriptReprTrait},
99
};
@@ -83,17 +83,6 @@ impl Group for G2 {
8383
impl DlogGroup for G2 {
8484
type AffineGroupElement = G2Affine;
8585

86-
fn vartime_multiscalar_mul(scalars: &[Self::Scalar], bases: &[Self::AffineGroupElement]) -> Self {
87-
msm(scalars, bases)
88-
}
89-
90-
fn vartime_multiscalar_mul_small<T: Integer + Into<u64> + Copy + Sync + ToPrimitive>(
91-
scalars: &[T],
92-
bases: &[Self::AffineGroupElement],
93-
) -> Self {
94-
msm_small(scalars, bases)
95-
}
96-
9786
fn affine(&self) -> Self::AffineGroupElement {
9887
self.to_affine()
9988
}

src/provider/hyperkzg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
provider::{
1313
ptau::PtauFileError,
1414
read_ptau,
15-
traits::{DlogGroup, PairingGroup},
15+
traits::{DlogGroup, DlogGroupExt, PairingGroup},
1616
write_ptau,
1717
},
1818
traits::{

src/provider/pasta.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
impl_traits,
44
provider::{
55
msm::{msm, msm_small},
6-
traits::DlogGroup,
6+
traits::{DlogGroup, DlogGroupExt},
77
},
88
traits::{Group, PrimeFieldExt, TranscriptReprTrait},
99
};

src/provider/pedersen.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
gadgets::utils::to_bignat_repr,
55
provider::{
66
ptau::{read_points, write_points, PtauFileError},
7-
traits::DlogGroup,
7+
traits::{DlogGroup, DlogGroupExt},
88
},
99
traits::{
1010
commitment::{CommitmentEngineTrait, CommitmentTrait, Len},
@@ -203,7 +203,7 @@ where
203203

204204
impl<E: Engine> CommitmentEngineTrait<E> for CommitmentEngine<E>
205205
where
206-
E::GE: DlogGroup,
206+
E::GE: DlogGroupExt,
207207
{
208208
type CommitmentKey = CommitmentKey<E>;
209209
type Commitment = Commitment<E>;
@@ -307,10 +307,9 @@ where
307307
Self: Sized;
308308
}
309309

310-
impl<E> CommitmentKeyExtTrait<E> for CommitmentKey<E>
310+
impl<E: Engine<CE = CommitmentEngine<E>>> CommitmentKeyExtTrait<E> for CommitmentKey<E>
311311
where
312-
E: Engine<CE = CommitmentEngine<E>>,
313-
E::GE: DlogGroup,
312+
E::GE: DlogGroupExt,
314313
{
315314
fn split_at(&self, n: usize) -> (CommitmentKey<E>, CommitmentKey<E>) {
316315
(

src/provider/secp_secq.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
impl_traits,
44
provider::{
55
msm::{msm, msm_small},
6-
traits::DlogGroup,
6+
traits::{DlogGroup, DlogGroupExt},
77
},
88
traits::{Group, PrimeFieldExt, TranscriptReprTrait},
99
};

src/provider/traits.rs

+39-34
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for T where T: for<'r> GroupOps<
2828
pub trait ScalarMulOwned<Rhs, Output = Self>: for<'r> ScalarMul<&'r Rhs, Output> {}
2929
impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T where T: for<'r> ScalarMul<&'r Rhs, Output> {}
3030

31-
/// A trait that defines extensions to the Group trait
31+
/// A trait that defines the core discrete logarithm group functionality
3232
pub trait DlogGroup:
3333
Group
3434
+ Serialize
@@ -51,6 +51,27 @@ pub trait DlogGroup:
5151
+ CurveAffine
5252
+ SerdeObject;
5353

54+
/// Produce a vector of group elements using a static label
55+
fn from_label(label: &'static [u8], n: usize) -> Vec<Self::AffineGroupElement>;
56+
57+
/// Produces a preprocessed element
58+
fn affine(&self) -> Self::AffineGroupElement;
59+
60+
/// Returns a group element from a preprocessed group element
61+
fn group(p: &Self::AffineGroupElement) -> Self;
62+
63+
/// Returns an element that is the additive identity of the group
64+
fn zero() -> Self;
65+
66+
/// Returns the generator of the group
67+
fn gen() -> Self;
68+
69+
/// Returns the affine coordinates (x, y, infinity) for the point
70+
fn to_coordinates(&self) -> (<Self as Group>::Base, <Self as Group>::Base, bool);
71+
}
72+
73+
/// Extension trait for DlogGroup that provides multi-scalar multiplication operations
74+
pub trait DlogGroupExt: DlogGroup {
5475
/// A method to compute a multiexponentation
5576
fn vartime_multiscalar_mul(scalars: &[Self::Scalar], bases: &[Self::AffineGroupElement]) -> Self;
5677

@@ -81,29 +102,11 @@ pub trait DlogGroup:
81102
.map(|scalar| Self::vartime_multiscalar_mul_small(scalar, &bases[..scalar.len()]))
82103
.collect::<Vec<_>>()
83104
}
84-
85-
/// Produce a vector of group elements using a static label
86-
fn from_label(label: &'static [u8], n: usize) -> Vec<Self::AffineGroupElement>;
87-
88-
/// Produces a preprocessed element
89-
fn affine(&self) -> Self::AffineGroupElement;
90-
91-
/// Returns a group element from a preprocessed group element
92-
fn group(p: &Self::AffineGroupElement) -> Self;
93-
94-
/// Returns an element that is the additive identity of the group
95-
fn zero() -> Self;
96-
97-
/// Returns the generator of the group
98-
fn gen() -> Self;
99-
100-
/// Returns the affine coordinates (x, y, infinity) for the point
101-
fn to_coordinates(&self) -> (<Self as Group>::Base, <Self as Group>::Base, bool);
102105
}
103106

104107
/// A trait that defines extensions to the DlogGroup trait, to be implemented for
105108
/// elliptic curve groups that are pairing friendly
106-
pub trait PairingGroup: DlogGroup {
109+
pub trait PairingGroup: DlogGroupExt {
107110
/// A type representing the second group
108111
type G2: DlogGroup<Scalar = Self::Scalar, Base = Self::Base>;
109112

@@ -141,20 +144,6 @@ macro_rules! impl_traits {
141144
impl DlogGroup for $name::Point {
142145
type AffineGroupElement = $name::Affine;
143146

144-
fn vartime_multiscalar_mul(
145-
scalars: &[Self::Scalar],
146-
bases: &[Self::AffineGroupElement],
147-
) -> Self {
148-
msm(scalars, bases)
149-
}
150-
151-
fn vartime_multiscalar_mul_small<T: Integer + Into<u64> + Copy + Sync + ToPrimitive>(
152-
scalars: &[T],
153-
bases: &[Self::AffineGroupElement],
154-
) -> Self {
155-
msm_small(scalars, bases)
156-
}
157-
158147
fn affine(&self) -> Self::AffineGroupElement {
159148
self.to_affine()
160149
}
@@ -229,6 +218,22 @@ macro_rules! impl_traits {
229218
}
230219
}
231220

221+
impl DlogGroupExt for $name::Point {
222+
fn vartime_multiscalar_mul(
223+
scalars: &[Self::Scalar],
224+
bases: &[Self::AffineGroupElement],
225+
) -> Self {
226+
msm(scalars, bases)
227+
}
228+
229+
fn vartime_multiscalar_mul_small<T: Integer + Into<u64> + Copy + Sync + ToPrimitive>(
230+
scalars: &[T],
231+
bases: &[Self::AffineGroupElement],
232+
) -> Self {
233+
msm_small(scalars, bases)
234+
}
235+
}
236+
232237
impl PrimeFieldExt for $name::Scalar {
233238
fn from_uniform(bytes: &[u8]) -> Self {
234239
let bytes_arr: [u8; 64] = bytes.try_into().unwrap();

0 commit comments

Comments
 (0)