Skip to content

Commit a951505

Browse files
authored
Simplifications to trait structures (microsoft#219)
* refactor: Remove redundant/uneeded traits from various modules - Removed redundant trait bounds (e.g. PrimeFieldBits implies PrimeField, Group implies Serialize + for<'de> Deserialize), - Removed some uneeded trait bounds (e.g. no one serializes instances of the `CommitmentEngineTrait`), - this passes Nova tests of course, but Lurk tests built on this pass as well (relevant since many of these were introduced in microsoft#123) * chore: Refactor traits and integrate 'group' module - use traits from 'group' where they are a direct replacement * refactor: Refactor bn256_grumpkin module and update dependencies - Modified the `bn256_grumpkin.rs` imports, shifting the use of the `group` module out of `pasta_curves` scope. - annotated future simplifications for the provider traits * chore: Update `halo2curves` dependency - Updated the major dependency `halo2curves` to the latest version * refactor: Remove needless Phantom in CommitmentKey - Eliminated `_p: PhantomData<G>` from `CommitmentKey` structure within the `pedersen.rs` source file. - Purged `_p: Default::default(),` from every method under the `CommitmentEngine` trait implementation for `CommitmentKey<G>`. * refactor: Refactor CommitmentKeyExtTrait and adjust impls - The trait pedersen::CommitmentKeyExtTrait no longer requires an associated CommitmentEngine, - The struct pedersen::CommitmentKey only implements pedersen::CommitmentKeyExtTrait when the group's CommitmentEngineTrait implementation is pedersen::CommitmentEngine This amounts to shifting the following bounds in the code base: "we require a `Group` which associated type `CE` has an associated `CommitmentKey` which implements `pedersen::CommitmentKeyExtTrait` with the same choice of associated type `CE` as the group itself" and "we provide a `pedersen::CommitmentKeyExtTrait<G>` implementation for any `pedersen::CommitmentKey<G>`" to: "we require a `Group` which associated type `CE` has an associated `CommitmentKey` which implements `pedersen::CommitmentKeyExtTrait`" and "we provide a `pedersen::CommitmentKeyExtTrait<G>` implementation for those groups which associated type `CE` is `pedersen::CommitmentKeyExtTrait`. The claim is that the later is simpler. This is a breaking change only for those code bases that program generically over nova using the public `CommitmentKeyExtTrait` (since 0.23). At least for lurk, the adaptation is trivial. * refactor: Refactor InnerProductArgument struct in ipa_pc.rs - Removed unnecessary usage of `_p: PhantomData<G>` from the `InnerProductArgument` struct. - Correspondingly, eliminated `_p: Default::default()` from the struct's instantiation process. * refactor: Replace default values of `Default::default()` for phantom types with `PhantomData` throughout codebase * refactor: Refactor EvaluationEngineTrait Removes the uneeded CE associated type * Revert "chore: Refactor traits and integrate 'group' module" This reverts commit ae1440b.
1 parent 56eab20 commit a951505

17 files changed

+49
-97
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ bincode = "1.3"
3232
bitvec = "1.0"
3333
byteorder = "1.4.3"
3434
thiserror = "1.0"
35-
halo2curves = { version = "0.1.0", features = ["derive_serde"] }
35+
halo2curves = { version = "0.4.0", features = ["derive_serde"] }
36+
group = "0.13.0"
3637

3738
[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
3839
pasta-msm = { version = "0.1.4" }

benches/compressed-snark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ where
233233
pub fn new(num_cons: usize) -> Self {
234234
Self {
235235
num_cons,
236-
_p: Default::default(),
236+
_p: PhantomData,
237237
}
238238
}
239239
}

benches/compute-digest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ where
4545
pub fn new(num_cons: usize) -> Self {
4646
Self {
4747
num_cons,
48-
_p: Default::default(),
48+
_p: PhantomData,
4949
}
5050
}
5151
}

benches/recursive-snark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ where
136136
pub fn new(num_cons: usize) -> Self {
137137
Self {
138138
num_cons,
139-
_p: Default::default(),
139+
_p: PhantomData,
140140
}
141141
}
142142
}

benches/sha256.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<Scalar: PrimeField + PrimeFieldBits> Sha256Circuit<Scalar> {
3434
pub fn new(preimage: Vec<u8>) -> Self {
3535
Self {
3636
preimage,
37-
_p: Default::default(),
37+
_p: PhantomData,
3838
}
3939
}
4040
}

src/lib.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,10 +1087,8 @@ mod tests {
10871087
G1: Group<Base = <G2 as Group>::Scalar>,
10881088
G2: Group<Base = <G1 as Group>::Scalar>,
10891089
// this is due to the reliance on CommitmentKeyExtTrait as a bound in ipa_pc
1090-
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey:
1091-
CommitmentKeyExtTrait<G1, CE = <G1 as Group>::CE>,
1092-
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey:
1093-
CommitmentKeyExtTrait<G2, CE = <G2 as Group>::CE>,
1090+
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey: CommitmentKeyExtTrait<G1>,
1091+
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey: CommitmentKeyExtTrait<G2>,
10941092
{
10951093
let circuit_primary = TrivialTestCircuit::default();
10961094
let circuit_secondary = CubicCircuit::default();
@@ -1182,10 +1180,8 @@ mod tests {
11821180
G1: Group<Base = <G2 as Group>::Scalar>,
11831181
G2: Group<Base = <G1 as Group>::Scalar>,
11841182
// this is due to the reliance on CommitmentKeyExtTrait as a bound in ipa_pc
1185-
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey:
1186-
CommitmentKeyExtTrait<G1, CE = <G1 as Group>::CE>,
1187-
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey:
1188-
CommitmentKeyExtTrait<G2, CE = <G2 as Group>::CE>,
1183+
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey: CommitmentKeyExtTrait<G1>,
1184+
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey: CommitmentKeyExtTrait<G2>,
11891185
{
11901186
let circuit_primary = TrivialTestCircuit::default();
11911187
let circuit_secondary = CubicCircuit::default();
@@ -1280,10 +1276,8 @@ mod tests {
12801276
G1: Group<Base = <G2 as Group>::Scalar>,
12811277
G2: Group<Base = <G1 as Group>::Scalar>,
12821278
// this is due to the reliance on CommitmentKeyExtTrait as a bound in ipa_pc
1283-
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey:
1284-
CommitmentKeyExtTrait<G1, CE = <G1 as Group>::CE>,
1285-
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey:
1286-
CommitmentKeyExtTrait<G2, CE = <G2 as Group>::CE>,
1279+
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey: CommitmentKeyExtTrait<G1>,
1280+
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey: CommitmentKeyExtTrait<G2>,
12871281
{
12881282
// y is a non-deterministic advice representing the fifth root of the input at a step.
12891283
#[derive(Clone, Debug)]

src/nifs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<G: Group> NIFS<G> {
7272
Ok((
7373
Self {
7474
comm_T: comm_T.compress(),
75-
_p: Default::default(),
75+
_p: PhantomData,
7676
},
7777
(U, W),
7878
))

src/provider/bn256_grumpkin.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ use crate::{
1010
};
1111
use digest::{ExtendableOutput, Update};
1212
use ff::{FromUniformBytes, PrimeField};
13+
use group::{cofactor::CofactorCurveAffine, Curve, Group as AnotherGroup, GroupEncoding};
1314
use num_bigint::BigInt;
1415
use num_traits::Num;
15-
use pasta_curves::{
16-
self,
17-
arithmetic::{CurveAffine, CurveExt},
18-
group::{cofactor::CofactorCurveAffine, Curve, Group as AnotherGroup, GroupEncoding},
19-
};
16+
// Remove this when https://github.com/zcash/pasta_curves/issues/41 resolves
17+
use pasta_curves::arithmetic::{CurveAffine, CurveExt};
2018
use rayon::prelude::*;
2119
use sha3::Shake256;
2220
use std::io::Read;

src/provider/ipa_pc.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ pub struct EvaluationEngine<G: Group> {
4848
impl<G> EvaluationEngineTrait<G> for EvaluationEngine<G>
4949
where
5050
G: Group,
51-
CommitmentKey<G>: CommitmentKeyExtTrait<G, CE = G::CE>,
51+
CommitmentKey<G>: CommitmentKeyExtTrait<G>,
5252
{
53-
type CE = G::CE;
5453
type ProverKey = ProverKey<G>;
5554
type VerifierKey = VerifierKey<G>;
5655
type EvaluationArgument = EvaluationArgument<G>;
5756

5857
fn setup(
59-
ck: &<Self::CE as CommitmentEngineTrait<G>>::CommitmentKey,
58+
ck: &<<G as Group>::CE as CommitmentEngineTrait<G>>::CommitmentKey,
6059
) -> (Self::ProverKey, Self::VerifierKey) {
6160
let pk = ProverKey {
6261
ck_s: G::CE::setup(b"ipa", 1),
@@ -169,13 +168,12 @@ struct InnerProductArgument<G: Group> {
169168
L_vec: Vec<CompressedCommitment<G>>,
170169
R_vec: Vec<CompressedCommitment<G>>,
171170
a_hat: G::Scalar,
172-
_p: PhantomData<G>,
173171
}
174172

175173
impl<G> InnerProductArgument<G>
176174
where
177175
G: Group,
178-
CommitmentKey<G>: CommitmentKeyExtTrait<G, CE = G::CE>,
176+
CommitmentKey<G>: CommitmentKeyExtTrait<G>,
179177
{
180178
const fn protocol_name() -> &'static [u8] {
181179
b"IPA"
@@ -290,7 +288,6 @@ where
290288
L_vec,
291289
R_vec,
292290
a_hat: a_vec[0],
293-
_p: Default::default(),
294291
})
295292
}
296293

src/provider/keccak.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<G: Group> TranscriptEngineTrait<G> for Keccak256Transcript<G> {
5555
round: 0u16,
5656
state: output,
5757
transcript: keccak_instance,
58-
_p: Default::default(),
58+
_p: PhantomData,
5959
}
6060
}
6161

0 commit comments

Comments
 (0)