Skip to content

Commit 61f8270

Browse files
committed
fix: move more trait bounds to PrimeGroup
1 parent ebfaabc commit 61f8270

File tree

5 files changed

+49
-54
lines changed

5 files changed

+49
-54
lines changed

src/codec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pub use crate::duplex_sponge::keccak::KeccakDuplexSponge;
44
use crate::duplex_sponge::{shake::ShakeDuplexSponge, DuplexSpongeInterface};
55
use ff::PrimeField;
6-
use group::{Group, GroupEncoding};
6+
use group::prime::PrimeGroup;
77
use num_bigint::BigUint;
88
use num_traits::identities::One;
99

@@ -46,7 +46,7 @@ fn cardinal<F: PrimeField>() -> BigUint {
4646
#[derive(Clone)]
4747
pub struct ByteSchnorrCodec<G, H>
4848
where
49-
G: Group + GroupEncoding,
49+
G: PrimeGroup,
5050
H: DuplexSpongeInterface,
5151
{
5252
hasher: H,
@@ -61,10 +61,10 @@ fn length_to_bytes(x: usize) -> [u8; WORD_SIZE] {
6161

6262
impl<G, H> Codec for ByteSchnorrCodec<G, H>
6363
where
64-
G: Group + GroupEncoding,
64+
G: PrimeGroup,
6565
H: DuplexSpongeInterface,
6666
{
67-
type Challenge = <G as Group>::Scalar;
67+
type Challenge = G::Scalar;
6868

6969
fn new(protocol_id: &[u8], session_id: &[u8], instance_label: &[u8]) -> Self {
7070
let iv = {

src/linear_relation/mod.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::iter;
1414
use std::marker::PhantomData;
1515

1616
use ff::Field;
17-
use group::{Group, GroupEncoding};
17+
use group::prime::PrimeGroup;
1818

1919
use crate::codec::Shake128DuplexSponge;
2020
use crate::errors::Error;
@@ -68,7 +68,7 @@ pub enum ScalarTerm<G> {
6868
Unit,
6969
}
7070

71-
impl<G: Group> ScalarTerm<G> {
71+
impl<G: PrimeGroup> ScalarTerm<G> {
7272
// NOTE: This function is private intentionally as it would be replaced if a ScalarMap struct
7373
// were to be added.
7474
fn value(self, scalars: &[G::Scalar]) -> G::Scalar {
@@ -112,13 +112,13 @@ impl<T> Sum<T> {
112112
/// - `w_i` are the constant weight scalars
113113
///
114114
/// The indices refer to external lists managed by the containing LinearMap.
115-
pub type LinearCombination<G> = Sum<Weighted<Term<G>, <G as Group>::Scalar>>;
115+
pub type LinearCombination<G> = Sum<Weighted<Term<G>, <G as group::Group>::Scalar>>;
116116

117117
/// Ordered mapping of [GroupVar] to group elements assignments.
118118
#[derive(Clone, Debug)]
119119
pub struct GroupMap<G>(Vec<Option<G>>);
120120

121-
impl<G: Group> GroupMap<G> {
121+
impl<G: PrimeGroup> GroupMap<G> {
122122
/// Assign a group element value to a point variable.
123123
///
124124
/// # Parameters
@@ -207,7 +207,7 @@ impl<G> Default for GroupMap<G> {
207207
}
208208
}
209209

210-
impl<G: Group> FromIterator<(GroupVar<G>, G)> for GroupMap<G> {
210+
impl<G: PrimeGroup> FromIterator<(GroupVar<G>, G)> for GroupMap<G> {
211211
fn from_iter<T: IntoIterator<Item = (GroupVar<G>, G)>>(iter: T) -> Self {
212212
iter.into_iter()
213213
.fold(Self::default(), |mut instance, (var, val)| {
@@ -222,7 +222,7 @@ impl<G: Group> FromIterator<(GroupVar<G>, G)> for GroupMap<G> {
222222
/// It supports dynamic allocation of scalars and elements,
223223
/// and evaluates by performing multi-scalar multiplications.
224224
#[derive(Clone, Default, Debug)]
225-
pub struct LinearMap<G: Group> {
225+
pub struct LinearMap<G: PrimeGroup> {
226226
/// The set of linear combination constraints (equations).
227227
pub linear_combinations: Vec<LinearCombination<G>>,
228228
/// The list of group elements referenced in the linear map.
@@ -246,15 +246,15 @@ pub struct LinearMap<G: Group> {
246246
///
247247
/// # Returns
248248
/// The group element result of the MSM.
249-
pub fn msm_pr<G: Group>(scalars: &[G::Scalar], bases: &[G]) -> G {
249+
pub fn msm_pr<G: PrimeGroup>(scalars: &[G::Scalar], bases: &[G]) -> G {
250250
let mut acc = G::identity();
251251
for (s, p) in scalars.iter().zip(bases.iter()) {
252252
acc += *p * s;
253253
}
254254
acc
255255
}
256256

257-
impl<G: Group> LinearMap<G> {
257+
impl<G: PrimeGroup> LinearMap<G> {
258258
/// Creates a new empty [`LinearMap`].
259259
///
260260
/// # Returns
@@ -291,7 +291,7 @@ impl<G: Group> LinearMap<G> {
291291
/// # Returns
292292
///
293293
/// A vector of group elements, each being the result of evaluating one linear combination with the scalars.
294-
pub fn evaluate(&self, scalars: &[<G as Group>::Scalar]) -> Result<Vec<G>, Error> {
294+
pub fn evaluate(&self, scalars: &[G::Scalar]) -> Result<Vec<G>, Error> {
295295
self.linear_combinations
296296
.iter()
297297
.map(|lc| {
@@ -320,9 +320,7 @@ impl<G: Group> LinearMap<G> {
320320
/// - A list of group elements and linear equations (held in the [`LinearMap`] field),
321321
/// - A list of [`GroupVar`] indices (`image`) that specify the expected output for each constraint.
322322
#[derive(Clone, Default, Debug)]
323-
pub struct LinearRelation<G>
324-
where
325-
G: Group + GroupEncoding,
323+
pub struct LinearRelation<G: PrimeGroup>
326324
{
327325
/// The underlying linear map describing the structure of the statement.
328326
pub linear_map: LinearMap<G>,
@@ -338,7 +336,7 @@ where
338336
/// This struct represents a normalized form of a linear relation where each
339337
/// constraint is of the form: image[i] = Σ (scalar_j * group_element_k)
340338
#[derive(Clone, Debug, Default)]
341-
pub struct CanonicalLinearRelation<G: Group + GroupEncoding> {
339+
pub struct CanonicalLinearRelation<G: PrimeGroup> {
342340
/// The image group elements (left-hand side of equations)
343341
pub image: Vec<G>,
344342
/// The constraints, where each constraint is a vector of (scalar_var, group_var) pairs
@@ -350,7 +348,7 @@ pub struct CanonicalLinearRelation<G: Group + GroupEncoding> {
350348
pub num_scalars: usize,
351349
}
352350

353-
impl<G: Group + GroupEncoding> CanonicalLinearRelation<G> {
351+
impl<G: PrimeGroup> CanonicalLinearRelation<G> {
354352
/// Create a new empty canonical linear relation
355353
pub fn new() -> Self {
356354
Self {
@@ -514,7 +512,7 @@ impl<G: Group + GroupEncoding> CanonicalLinearRelation<G> {
514512
}
515513
}
516514

517-
impl<G: Group + GroupEncoding> TryFrom<LinearRelation<G>> for CanonicalLinearRelation<G> {
515+
impl<G: PrimeGroup> TryFrom<LinearRelation<G>> for CanonicalLinearRelation<G> {
518516
type Error = Error;
519517

520518
fn try_from(relation: LinearRelation<G>) -> Result<Self, Self::Error> {
@@ -545,9 +543,7 @@ impl<G: Group + GroupEncoding> TryFrom<LinearRelation<G>> for CanonicalLinearRel
545543
}
546544
}
547545

548-
impl<G> LinearRelation<G>
549-
where
550-
G: Group + GroupEncoding,
546+
impl<G: PrimeGroup> LinearRelation<G>
551547
{
552548
/// Create a new empty [`LinearRelation`].
553549
pub fn new() -> Self {
@@ -675,7 +671,7 @@ where
675671
///
676672
/// Return `Ok` on success, and an error if unassigned elements prevent the image from being
677673
/// computed. Modifies the group elements assigned in the [LinearRelation].
678-
pub fn compute_image(&mut self, scalars: &[<G as Group>::Scalar]) -> Result<(), Error> {
674+
pub fn compute_image(&mut self, scalars: &[G::Scalar]) -> Result<(), Error> {
679675
if self.linear_map.num_constraints() != self.image.len() {
680676
// NOTE: This is a panic, rather than a returned error, because this can only happen if
681677
// this implementation has a bug.
@@ -761,9 +757,10 @@ where
761757
/// let proof = nizk.prove_batchable(&vec![x], &mut OsRng).unwrap();
762758
/// assert!(nizk.verify_batchable(&proof).is_ok());
763759
/// ```
764-
pub fn into_nizk(self, session_identifier: &[u8]) -> Nizk<SchnorrProof<G>, Shake128DuplexSponge<G>>
765-
where
766-
G: group::GroupEncoding,
760+
pub fn into_nizk(
761+
self,
762+
session_identifier: &[u8],
763+
) -> Nizk<SchnorrProof<G>, Shake128DuplexSponge<G>>
767764
{
768765
let schnorr = SchnorrProof::from(self);
769766
Nizk::new(session_identifier, schnorr)

src/schnorr_protocol.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
};
1515

1616
use ff::Field;
17-
use group::{Group, GroupEncoding};
17+
use group::prime::PrimeGroup;
1818
use rand::{CryptoRng, Rng, RngCore};
1919

2020
/// A Schnorr protocol proving knowledge of a witness for a linear group relation.
@@ -23,11 +23,11 @@ use rand::{CryptoRng, Rng, RngCore};
2323
/// a [`LinearRelation`], representing an abstract linear relation over the group.
2424
///
2525
/// # Type Parameters
26-
/// - `G`: A cryptographic group implementing [`Group`] and [`GroupEncoding`].
26+
/// - `G`: A [`PrimeGroup`] instance.
2727
#[derive(Clone, Default, Debug)]
28-
pub struct SchnorrProof<G: Group + GroupEncoding>(pub CanonicalLinearRelation<G>);
28+
pub struct SchnorrProof<G: PrimeGroup>(pub CanonicalLinearRelation<G>);
2929

30-
impl<G: Group + GroupEncoding> SchnorrProof<G> {
30+
impl<G: PrimeGroup> SchnorrProof<G> {
3131
pub fn witness_length(&self) -> usize {
3232
self.0.num_scalars
3333
}
@@ -54,9 +54,7 @@ impl<G: Group + GroupEncoding> SchnorrProof<G> {
5454
}
5555
}
5656

57-
impl<G> From<LinearRelation<G>> for SchnorrProof<G>
58-
where
59-
G: Group + GroupEncoding,
57+
impl<G: PrimeGroup> From<LinearRelation<G>> for SchnorrProof<G>
6058
{
6159
fn from(value: LinearRelation<G>) -> Self {
6260
Self(
@@ -69,13 +67,13 @@ where
6967

7068
impl<G> SigmaProtocol for SchnorrProof<G>
7169
where
72-
G: Group + GroupEncoding,
70+
G: PrimeGroup,
7371
{
7472
type Commitment = Vec<G>;
75-
type ProverState = (Vec<<G as Group>::Scalar>, Vec<<G as Group>::Scalar>);
76-
type Response = Vec<<G as Group>::Scalar>;
77-
type Witness = Vec<<G as Group>::Scalar>;
78-
type Challenge = <G as Group>::Scalar;
73+
type ProverState = (Vec<G::Scalar>, Vec<G::Scalar>);
74+
type Response = Vec<G::Scalar>;
75+
type Witness = Vec<G::Scalar>;
76+
type Challenge = G::Scalar;
7977

8078
/// Prover's first message: generates a commitment using random nonces.
8179
///
@@ -293,7 +291,7 @@ where
293291

294292
impl<G> SigmaProtocolSimulator for SchnorrProof<G>
295293
where
296-
G: Group + GroupEncoding,
294+
G: PrimeGroup,
297295
{
298296
/// Simulates a valid transcript for a given challenge without a witness.
299297
///

src/serialization.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! byte representations using canonical encodings.
55
66
use ff::PrimeField;
7-
use group::{Group, GroupEncoding};
7+
use group::prime::PrimeGroup;
88

99
/// Serialize a slice of group elements into a byte vector.
1010
///
@@ -13,7 +13,7 @@ use group::{Group, GroupEncoding};
1313
///
1414
/// # Returns
1515
/// - A `Vec<u8>` containing the concatenated canonical compressed byte representations.
16-
pub fn serialize_elements<G: Group + GroupEncoding>(elements: &[G]) -> Vec<u8> {
16+
pub fn serialize_elements<G: PrimeGroup>(elements: &[G]) -> Vec<u8> {
1717
let mut bytes = Vec::new();
1818
for element in elements {
1919
bytes.extend_from_slice(element.to_bytes().as_ref());
@@ -30,7 +30,7 @@ pub fn serialize_elements<G: Group + GroupEncoding>(elements: &[G]) -> Vec<u8> {
3030
/// # Returns
3131
/// - `Some(Vec<G>)`: The deserialized group elements if all are valid.
3232
/// - `None`: If the byte slice length is incorrect or any element is invalid.
33-
pub fn deserialize_elements<G: Group + GroupEncoding>(data: &[u8], count: usize) -> Option<Vec<G>> {
33+
pub fn deserialize_elements<G: PrimeGroup>(data: &[u8], count: usize) -> Option<Vec<G>> {
3434
let element_len = G::Repr::default().as_ref().len();
3535
let expected_len = count * element_len;
3636

@@ -69,7 +69,7 @@ pub fn deserialize_elements<G: Group + GroupEncoding>(data: &[u8], count: usize)
6969
///
7070
/// # Returns
7171
/// - A `Vec<u8>` containing the scalar bytes in big-endian order.
72-
pub fn serialize_scalars<G: Group>(scalars: &[G::Scalar]) -> Vec<u8> {
72+
pub fn serialize_scalars<G: PrimeGroup>(scalars: &[G::Scalar]) -> Vec<u8> {
7373
let mut bytes = Vec::new();
7474
for scalar in scalars {
7575
let mut scalar_bytes = scalar.to_repr().as_ref().to_vec();
@@ -88,7 +88,7 @@ pub fn serialize_scalars<G: Group>(scalars: &[G::Scalar]) -> Vec<u8> {
8888
/// # Returns
8989
/// - `Some(Vec<G::Scalar>)`: The deserialized scalars if all are valid.
9090
/// - `None`: If the byte slice length is incorrect or any scalar is invalid.
91-
pub fn deserialize_scalars<G: Group>(data: &[u8], count: usize) -> Option<Vec<G::Scalar>> {
91+
pub fn deserialize_scalars<G: PrimeGroup>(data: &[u8], count: usize) -> Option<Vec<G::Scalar>> {
9292
#[allow(clippy::manual_div_ceil)]
9393
let scalar_len = (G::Scalar::NUM_BITS as usize + 7) / 8;
9494
let expected_len = count * scalar_len;

src/tests/spec/custom_schnorr_protocol.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use group::{Group, GroupEncoding};
1+
use group::prime::PrimeGroup;
22
use rand::{CryptoRng, Rng};
33

44
use crate::errors::Error;
@@ -9,32 +9,32 @@ use crate::serialization::{
99
use crate::tests::spec::random::SRandom;
1010
use crate::traits::{SigmaProtocol, SigmaProtocolSimulator};
1111

12-
pub struct SchnorrProtocolCustom<G: SRandom + GroupEncoding>(pub LinearRelation<G>);
12+
pub struct SchnorrProtocolCustom<G: SRandom + PrimeGroup>(pub LinearRelation<G>);
1313

1414
impl<G> From<LinearRelation<G>> for SchnorrProtocolCustom<G>
1515
where
16-
G: SRandom + GroupEncoding,
16+
G: SRandom + PrimeGroup,
1717
{
1818
fn from(value: LinearRelation<G>) -> Self {
1919
Self(value)
2020
}
2121
}
2222

23-
impl<G: SRandom + GroupEncoding> SchnorrProtocolCustom<G> {
23+
impl<G: SRandom + PrimeGroup> SchnorrProtocolCustom<G> {
2424
pub fn witness_len(&self) -> usize {
2525
self.0.linear_map.num_scalars
2626
}
2727
}
2828

2929
impl<G> SigmaProtocol for SchnorrProtocolCustom<G>
3030
where
31-
G: SRandom + GroupEncoding,
31+
G: SRandom + PrimeGroup,
3232
{
3333
type Commitment = Vec<G>;
34-
type ProverState = (Vec<<G as Group>::Scalar>, Vec<<G as Group>::Scalar>);
35-
type Response = Vec<<G as Group>::Scalar>;
36-
type Witness = Vec<<G as Group>::Scalar>;
37-
type Challenge = <G as Group>::Scalar;
34+
type ProverState = (Vec<G::Scalar>, Vec<G::Scalar>);
35+
type Response = Vec<G::Scalar>;
36+
type Witness = Vec<G::Scalar>;
37+
type Challenge = G::Scalar;
3838

3939
fn prover_commit(
4040
&self,
@@ -131,7 +131,7 @@ where
131131
}
132132
}
133133

134-
impl<G: SRandom + GroupEncoding> SigmaProtocolSimulator for SchnorrProtocolCustom<G> {
134+
impl<G: SRandom + PrimeGroup> SigmaProtocolSimulator for SchnorrProtocolCustom<G> {
135135
fn simulate_commitment(
136136
&self,
137137
challenge: &Self::Challenge,

0 commit comments

Comments
 (0)