Skip to content

Commit c4e101b

Browse files
committed
refactor: GroupMorphismPreimage -> LinearRelation.
1 parent 29dfd20 commit c4e101b

File tree

9 files changed

+35
-40
lines changed

9 files changed

+35
-40
lines changed

src/composition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::traits::CompactProtocol;
1616
use crate::{
1717
errors::Error,
1818
fiat_shamir::{FiatShamir, HasGroupMorphism},
19-
group_morphism::GroupMorphismPreimage,
19+
group_morphism::LinearRelation,
2020
group_serialization::{deserialize_scalar, serialize_scalar},
2121
schnorr_protocol::SchnorrProtocol,
2222
traits::{SigmaProtocol, SigmaProtocolSimulator},
@@ -44,11 +44,11 @@ where
4444
}
4545
}
4646

47-
impl<G> From<GroupMorphismPreimage<G>> for Protocol<G>
47+
impl<G> From<LinearRelation<G>> for Protocol<G>
4848
where
4949
G: Group + GroupEncoding,
5050
{
51-
fn from(value: GroupMorphismPreimage<G>) -> Self {
51+
fn from(value: LinearRelation<G>) -> Self {
5252
Self::from(SchnorrProtocol::from(value))
5353
}
5454
}

src/group_morphism.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! It includes:
77
//! - [`LinearCombination`]: a sparse representation of scalar multiplication relations
88
//! - [`Morphism`]: a collection of linear combinations acting on group elements
9-
//! - [`GroupMorphismPreimage`]: a higher-level structure managing morphisms and their associated images
9+
//! - [`LinearRelation`]: a higher-level structure managing morphisms and their associated images
1010
1111
use crate::errors::Error;
1212
use group::{Group, GroupEncoding};
@@ -275,7 +275,7 @@ impl<G: Group> Morphism<G> {
275275
/// - A list of group elements and linear equations (held in the [`Morphism`] field),
276276
/// - A list of [`GroupVar`] indices (`image`) that specify the expected output for each constraint.
277277
#[derive(Clone, Default, Debug)]
278-
pub struct GroupMorphismPreimage<G>
278+
pub struct LinearRelation<G>
279279
where
280280
G: Group + GroupEncoding,
281281
{
@@ -285,11 +285,11 @@ where
285285
pub image: Vec<GroupVar>,
286286
}
287287

288-
impl<G> GroupMorphismPreimage<G>
288+
impl<G> LinearRelation<G>
289289
where
290290
G: Group + GroupEncoding,
291291
{
292-
/// Create a new empty GroupMorphismPreimage.
292+
/// Create a new empty [`LinearRelation`].
293293
pub fn new() -> Self {
294294
Self {
295295
morphism: Morphism::new(),
@@ -339,10 +339,10 @@ where
339339
///
340340
/// # Example
341341
/// ```
342-
/// # use sigma_rs::group_morphism::GroupMorphismPreimage;
342+
/// # use sigma_rs::group_morphism::LinearRelation;
343343
/// use curve25519_dalek::RistrettoPoint as G;
344344
///
345-
/// let mut morphism = GroupMorphismPreimage::<G>::new();
345+
/// let mut morphism = LinearRelation::<G>::new();
346346
/// let [var_x, var_y] = morphism.allocate_scalars();
347347
/// let vars = morphism.allocate_scalars::<10>();
348348
/// ```
@@ -367,10 +367,10 @@ where
367367
///
368368
/// # Example
369369
/// ```
370-
/// # use sigma_rs::group_morphism::GroupMorphismPreimage;
370+
/// # use sigma_rs::group_morphism::LinearRelation;
371371
/// use curve25519_dalek::RistrettoPoint as G;
372372
///
373-
/// let mut morphism = GroupMorphismPreimage::<G>::new();
373+
/// let mut morphism = LinearRelation::<G>::new();
374374
/// let [var_g, var_h] = morphism.allocate_elements();
375375
/// let vars = morphism.allocate_elements::<10>();
376376
/// ```
@@ -421,12 +421,10 @@ where
421421
/// # Returns
422422
///
423423
/// Return `Ok` on success, and an error if unassigned elements prevent the image from being
424-
/// computed. Modifies the group elements assigned in the [GroupMorphismPreimage].
424+
/// computed. Modifies the group elements assigned in the [LinearRelation].
425425
pub fn compute_image(&mut self, scalars: &[<G as Group>::Scalar]) -> Result<(), Error> {
426426
if self.morphism.constraints.len() != self.image.len() {
427-
panic!(
428-
"invalid GroupMorphismPreimage: different number of constraints and image variables"
429-
);
427+
panic!("invalid LinearRelation: different number of constraints and image variables");
430428
}
431429

432430
for (lc, lhs) in iter::zip(self.morphism.constraints.as_slice(), self.image.as_slice()) {

src/schnorr_protocol.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::codec::Codec;
88
use crate::errors::Error;
99
use crate::fiat_shamir::{FiatShamir, HasGroupMorphism};
10-
use crate::group_morphism::GroupMorphismPreimage;
10+
use crate::group_morphism::LinearRelation;
1111
use crate::{
1212
group_serialization::*,
1313
traits::{CompactProtocol, SigmaProtocol, SigmaProtocolSimulator},
@@ -20,12 +20,12 @@ use rand::{CryptoRng, RngCore};
2020
/// A Schnorr protocol proving knowledge of a witness for a linear group relation.
2121
///
2222
/// This implementation generalizes Schnorr’s discrete logarithm proof by using
23-
/// a [`GroupMorphismPreimage`], representing an abstract linear relation over the group.
23+
/// a [`LinearRelation`], representing an abstract linear relation over the group.
2424
///
2525
/// # Type Parameters
2626
/// - `G`: A cryptographic group implementing [`Group`] and [`GroupEncoding`].
2727
#[derive(Clone, Default, Debug)]
28-
pub struct SchnorrProtocol<G: Group + GroupEncoding>(pub GroupMorphismPreimage<G>);
28+
pub struct SchnorrProtocol<G: Group + GroupEncoding>(pub LinearRelation<G>);
2929

3030
impl<G: Group + GroupEncoding> SchnorrProtocol<G> {
3131
pub fn scalars_nb(&self) -> usize {
@@ -37,11 +37,11 @@ impl<G: Group + GroupEncoding> SchnorrProtocol<G> {
3737
}
3838
}
3939

40-
impl<G> From<GroupMorphismPreimage<G>> for SchnorrProtocol<G>
40+
impl<G> From<LinearRelation<G>> for SchnorrProtocol<G>
4141
where
4242
G: Group + GroupEncoding,
4343
{
44-
fn from(value: GroupMorphismPreimage<G>) -> Self {
44+
fn from(value: LinearRelation<G>) -> Self {
4545
Self(value)
4646
}
4747
}
File renamed without changes.

src/tests/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mod composition_protocol;
2-
mod morphism_preimage;
1+
mod composition;
2+
mod relations;
33
mod spec;
44
pub mod test_utils;

src/tests/morphism_preimage.rs renamed to src/tests/relations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::tests::test_utils::{
1010
use crate::{codec::ShakeCodec, schnorr_protocol::SchnorrProtocol};
1111

1212
/// This part tests the functioning of morphisms
13-
/// as well as the implementation of GroupMorphismPreimage
13+
/// as well as the implementation of LinearRelation
1414
#[test]
1515
fn test_discrete_logarithm() {
1616
let mut rng = OsRng;

src/tests/spec/custom_schnorr_protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use rand::{CryptoRng, Rng};
55
use crate::codec::Codec;
66
use crate::errors::Error;
77
use crate::fiat_shamir::FiatShamir;
8-
use crate::group_morphism::GroupMorphismPreimage;
8+
use crate::group_morphism::LinearRelation;
99
use crate::group_serialization::*;
1010
use crate::tests::spec::random::SRandom;
1111
use crate::traits::SigmaProtocol;
1212

13-
pub struct SchnorrProtocolCustom<G: SRandom + GroupEncoding>(pub GroupMorphismPreimage<G>);
13+
pub struct SchnorrProtocolCustom<G: SRandom + GroupEncoding>(pub LinearRelation<G>);
1414

1515
impl<G: SRandom + GroupEncoding> SchnorrProtocolCustom<G> {
1616
pub fn witness_len(&self) -> usize {

src/tests/spec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod bls12_381;
22
mod custom_schnorr_protocol;
33
mod random;
4-
mod test_vectors;
54
mod rng;
5+
mod test_vectors;

src/tests/test_utils.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
33
use group::{Group, GroupEncoding};
44

5-
use crate::group_morphism::{GroupMorphismPreimage, msm_pr};
5+
use crate::group_morphism::{LinearRelation, msm_pr};
66

77
/// Morphism for knowledge of a discrete logarithm relative to a fixed basepoint.
88
#[allow(non_snake_case)]
99
pub fn discrete_logarithm<G: Group + GroupEncoding>(
1010
x: G::Scalar,
11-
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
12-
let mut morphismp: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
11+
) -> (LinearRelation<G>, Vec<G::Scalar>) {
12+
let mut morphismp: LinearRelation<G> = LinearRelation::new();
1313

1414
let var_x = morphismp.allocate_scalar();
1515
let var_G = morphismp.allocate_element();
@@ -27,11 +27,8 @@ pub fn discrete_logarithm<G: Group + GroupEncoding>(
2727

2828
/// Morphism for knowledge of a discrete logarithm equality between two pairs.
2929
#[allow(non_snake_case)]
30-
pub fn dleq<G: Group + GroupEncoding>(
31-
x: G::Scalar,
32-
H: G,
33-
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
34-
let mut morphismp: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
30+
pub fn dleq<G: Group + GroupEncoding>(x: G::Scalar, H: G) -> (LinearRelation<G>, Vec<G::Scalar>) {
31+
let mut morphismp: LinearRelation<G> = LinearRelation::new();
3532

3633
let var_x = morphismp.allocate_scalar();
3734
let [var_G, var_H] = morphismp.allocate_elements();
@@ -56,8 +53,8 @@ pub fn pedersen_commitment<G: Group + GroupEncoding>(
5653
H: G,
5754
x: G::Scalar,
5855
r: G::Scalar,
59-
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
60-
let mut cs: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
56+
) -> (LinearRelation<G>, Vec<G::Scalar>) {
57+
let mut cs: LinearRelation<G> = LinearRelation::new();
6158

6259
let [var_x, var_r] = cs.allocate_scalars();
6360
let [var_G, var_H] = cs.allocate_elements();
@@ -79,8 +76,8 @@ pub fn pedersen_commitment<G: Group + GroupEncoding>(
7976
pub fn pedersen_commitment_dleq<G: Group + GroupEncoding>(
8077
generators: [G; 4],
8178
witness: [G::Scalar; 2],
82-
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
83-
let mut morphismp: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
79+
) -> (LinearRelation<G>, Vec<G::Scalar>) {
80+
let mut morphismp: LinearRelation<G> = LinearRelation::new();
8481

8582
let X = msm_pr::<G>(&witness, &[generators[0], generators[1]]);
8683
let Y = msm_pr::<G>(&witness, &[generators[2], generators[3]]);
@@ -112,8 +109,8 @@ pub fn bbs_blind_commitment_computation<G: Group + GroupEncoding>(
112109
[Q_2, J_1, J_2, J_3]: [G; 4],
113110
[msg_1, msg_2, msg_3]: [G::Scalar; 3],
114111
secret_prover_blind: G::Scalar,
115-
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
116-
let mut morphismp = GroupMorphismPreimage::new();
112+
) -> (LinearRelation<G>, Vec<G::Scalar>) {
113+
let mut morphismp = LinearRelation::new();
117114

118115
// these are computed before the proof in the specification
119116
let C = Q_2 * secret_prover_blind + J_1 * msg_1 + J_2 * msg_2 + J_3 * msg_3;

0 commit comments

Comments
 (0)