Skip to content

Commit c93a51a

Browse files
committed
some suggestions resolved
1 parent e3d85d6 commit c93a51a

File tree

7 files changed

+150
-125
lines changed

7 files changed

+150
-125
lines changed

mithril-stm/src/signature_scheme/schnorr_signature/error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ pub enum SchnorrSignatureError {
1313
#[error("Invalid bytes")]
1414
SerializationError,
1515

16-
/// This error occurs when the serialization of the signing key bytes failed
16+
/// This error occurs when the serialization of the scalar field bytes failed
1717
#[error("Invalid scalar field element bytes")]
18-
ScalarFieldElementSerializationError,
18+
ScalarFieldElementSerialization,
1919

2020
/// This error occurs when the serialization of the projective point bytes failed
2121
#[error("Invalid projective point bytes")]
22-
ProjectivePointSerializationError,
22+
ProjectivePointSerialization,
2323

2424
/// This error occurs when the serialization of the prime order projective point bytes failed
2525
#[error("Invalid prime order projective point bytes")]
26-
PrimeOrderProjectivePointSerializationError,
26+
PrimeOrderProjectivePointSerialization,
2727

2828
/// This error occurs when the random scalar fails to generate during the signature
2929
#[error("Failed generation of the signature's random scalar")]
30-
RandomScalarGenerationError,
30+
RandomScalarGeneration,
3131

3232
/// This error occurs when signing key is zero or one.
3333
#[error("The signing key is invalid.")]

mithril-stm/src/signature_scheme/schnorr_signature/jubjub/curve_points.rs

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use dusk_jubjub::{
44
SubgroupPoint as JubjubSubgroup,
55
};
66
use group::{Group, GroupEncoding};
7+
use std::ops::{Add, Mul};
78

89
use super::{BaseFieldElement, ScalarFieldElement};
910
use crate::{StmResult, signature_scheme::SchnorrSignatureError};
@@ -16,14 +17,6 @@ impl AffinePoint {
1617
AffinePoint(JubjubAffinePoint::from(projective_point.0))
1718
}
1819

19-
pub(crate) fn from_prime_order_projective_point(
20-
prime_order_projective_point: &PrimeOrderProjectivePoint,
21-
) -> Self {
22-
AffinePoint(JubjubAffinePoint::from(
23-
ProjectivePoint::from_prime_order_projective_point(*prime_order_projective_point).0,
24-
))
25-
}
26-
2720
pub(crate) fn get_u(&self) -> BaseFieldElement {
2821
BaseFieldElement(self.0.get_u())
2922
}
@@ -33,6 +26,14 @@ impl AffinePoint {
3326
}
3427
}
3528

29+
impl From<&PrimeOrderProjectivePoint> for AffinePoint {
30+
fn from(prime_order_projective_point: &PrimeOrderProjectivePoint) -> Self {
31+
AffinePoint(JubjubAffinePoint::from(JubjubExtended::from(
32+
prime_order_projective_point.0,
33+
)))
34+
}
35+
}
36+
3637
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3738
pub(crate) struct ProjectivePoint(pub(crate) JubjubExtended);
3839

@@ -41,14 +42,6 @@ impl ProjectivePoint {
4142
ProjectivePoint(JubjubExtended::hash_to_point(input))
4243
}
4344

44-
pub(crate) fn add(&self, other: Self) -> Self {
45-
ProjectivePoint(self.0 + other.0)
46-
}
47-
48-
pub(crate) fn scalar_multiplication(&self, scalar: &ScalarFieldElement) -> Self {
49-
ProjectivePoint(self.0 * scalar.0)
50-
}
51-
5245
pub(crate) fn get_coordinates(&self) -> (BaseFieldElement, BaseFieldElement) {
5346
let affine_point = AffinePoint::from_projective_point(*self);
5447

@@ -66,23 +59,37 @@ impl ProjectivePoint {
6659

6760
match JubjubExtended::from_bytes(&projective_point_bytes).into_option() {
6861
Some(projective_point) => Ok(Self(projective_point)),
69-
None => Err(anyhow!(
70-
SchnorrSignatureError::ProjectivePointSerializationError
71-
)),
62+
None => Err(anyhow!(SchnorrSignatureError::ProjectivePointSerialization)),
7263
}
7364
}
7465

75-
pub(crate) fn from_prime_order_projective_point(
76-
prime_order_projective_point: PrimeOrderProjectivePoint,
77-
) -> Self {
78-
ProjectivePoint(JubjubExtended::from(prime_order_projective_point.0))
79-
}
80-
8166
pub(crate) fn is_prime_order(self) -> bool {
8267
self.0.is_prime_order().into()
8368
}
8469
}
8570

71+
impl Add for ProjectivePoint {
72+
type Output = ProjectivePoint;
73+
74+
fn add(self, other: ProjectivePoint) -> ProjectivePoint {
75+
ProjectivePoint(self.0 + other.0)
76+
}
77+
}
78+
79+
impl Mul<ProjectivePoint> for ScalarFieldElement {
80+
type Output = ProjectivePoint;
81+
82+
fn mul(self, point: ProjectivePoint) -> ProjectivePoint {
83+
ProjectivePoint(point.0 * self.0)
84+
}
85+
}
86+
87+
impl From<PrimeOrderProjectivePoint> for ProjectivePoint {
88+
fn from(prime_order_projective_point: PrimeOrderProjectivePoint) -> Self {
89+
ProjectivePoint(JubjubExtended::from(prime_order_projective_point.0))
90+
}
91+
}
92+
8693
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
8794
pub(crate) struct PrimeOrderProjectivePoint(pub(crate) JubjubSubgroup);
8895

@@ -91,28 +98,18 @@ impl PrimeOrderProjectivePoint {
9198
PrimeOrderProjectivePoint(JubjubSubgroup::generator())
9299
}
93100

94-
pub(crate) fn add(&self, other: Self) -> Self {
95-
PrimeOrderProjectivePoint(self.0 + other.0)
96-
}
97-
98-
pub(crate) fn scalar_multiplication(&self, scalar: &ScalarFieldElement) -> Self {
99-
PrimeOrderProjectivePoint(self.0 * scalar.0)
100-
}
101-
102101
/// Check if the given point is on the curve using its coordinates
103102
pub(crate) fn is_on_curve(&self) -> StmResult<PrimeOrderProjectivePoint> {
104-
let point_affine_representation = AffinePoint::from_prime_order_projective_point(self);
103+
let point_affine_representation = AffinePoint::from(self);
105104
let (x, y) = (
106105
point_affine_representation.get_u(),
107106
point_affine_representation.get_v(),
108107
);
109-
let x_square = x.square();
110-
let y_square = y.square();
108+
let x_square = &x * &x;
109+
let y_square = &y * &y;
111110

112-
let lhs = y_square.sub(&x_square);
113-
let mut rhs = x_square.mul(&y_square);
114-
rhs = rhs.mul(&BaseFieldElement(EDWARDS_D));
115-
rhs = rhs.add(&BaseFieldElement::get_one());
111+
let lhs = &y_square - &x_square;
112+
let rhs = (x_square * y_square) * BaseFieldElement(EDWARDS_D) + BaseFieldElement::get_one();
116113

117114
if lhs != rhs {
118115
return Err(anyhow!(SchnorrSignatureError::PointIsNotOnCurve(Box::new(
@@ -134,12 +131,28 @@ impl PrimeOrderProjectivePoint {
134131
match JubjubSubgroup::from_bytes(&prime_order_projective_point_bytes).into_option() {
135132
Some(prime_order_projective_point) => Ok(Self(prime_order_projective_point)),
136133
None => Err(anyhow!(
137-
SchnorrSignatureError::PrimeOrderProjectivePointSerializationError
134+
SchnorrSignatureError::PrimeOrderProjectivePointSerialization
138135
)),
139136
}
140137
}
141138
}
142139

140+
impl Add for PrimeOrderProjectivePoint {
141+
type Output = PrimeOrderProjectivePoint;
142+
143+
fn add(self, other: PrimeOrderProjectivePoint) -> PrimeOrderProjectivePoint {
144+
PrimeOrderProjectivePoint(self.0 + other.0)
145+
}
146+
}
147+
148+
impl Mul<PrimeOrderProjectivePoint> for ScalarFieldElement {
149+
type Output = PrimeOrderProjectivePoint;
150+
151+
fn mul(self, point: PrimeOrderProjectivePoint) -> PrimeOrderProjectivePoint {
152+
PrimeOrderProjectivePoint(point.0 * self.0)
153+
}
154+
}
155+
143156
#[cfg(test)]
144157
mod tests {
145158
use super::*;
@@ -156,7 +169,7 @@ mod tests {
156169
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
157170
let scalar = ScalarFieldElement::new_random_nonzero_scalar(&mut rng).unwrap();
158171
let point = PrimeOrderProjectivePoint::create_generator();
159-
point.scalar_multiplication(&scalar)
172+
PrimeOrderProjectivePoint(point.0 * scalar.0)
160173
}
161174

162175
#[test]

mithril-stm/src/signature_scheme/schnorr_signature/jubjub/field_elements.rs

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,48 @@ use anyhow::anyhow;
22
use dusk_jubjub::{Fq as JubjubBase, Fr as JubjubScalar};
33
use ff::Field;
44
use rand_core::{CryptoRng, RngCore};
5+
use std::ops::{Add, Mul, Sub};
56

6-
use super::ProjectivePoint;
77
use crate::{StmResult, signature_scheme::SchnorrSignatureError};
88

99
#[derive(Debug, Clone, PartialEq, Eq)]
1010
pub(crate) struct BaseFieldElement(pub(crate) JubjubBase);
1111

1212
impl BaseFieldElement {
13-
pub(crate) fn add(&self, other: &Self) -> Self {
13+
pub(crate) fn get_one() -> Self {
14+
BaseFieldElement(JubjubBase::ONE)
15+
}
16+
}
17+
18+
impl Add for BaseFieldElement {
19+
type Output = BaseFieldElement;
20+
21+
fn add(self, other: BaseFieldElement) -> BaseFieldElement {
1422
BaseFieldElement(self.0 + other.0)
1523
}
24+
}
1625

17-
pub(crate) fn sub(&self, other: &Self) -> Self {
26+
impl Sub for &BaseFieldElement {
27+
type Output = BaseFieldElement;
28+
29+
fn sub(self, other: &BaseFieldElement) -> BaseFieldElement {
1830
BaseFieldElement(self.0 - other.0)
1931
}
32+
}
2033

21-
pub(crate) fn mul(&self, other: &Self) -> Self {
22-
BaseFieldElement(self.0 * other.0)
23-
}
34+
impl Mul for BaseFieldElement {
35+
type Output = BaseFieldElement;
2436

25-
pub(crate) fn square(&self) -> Self {
26-
BaseFieldElement(self.0.square())
37+
fn mul(self, other: BaseFieldElement) -> BaseFieldElement {
38+
BaseFieldElement(self.0 * other.0)
2739
}
40+
}
2841

29-
pub(crate) fn get_one() -> Self {
30-
BaseFieldElement(JubjubBase::ONE)
31-
}
42+
impl Mul for &BaseFieldElement {
43+
type Output = BaseFieldElement;
3244

33-
pub(crate) fn collect_coordinates_of_list_of_points(
34-
point_list: &[ProjectivePoint],
35-
) -> Vec<Self> {
36-
let mut coordinates: Vec<BaseFieldElement> = Vec::new();
37-
for point in point_list {
38-
let (u, v) = point.get_coordinates();
39-
coordinates.push(u);
40-
coordinates.push(v);
41-
}
42-
coordinates
45+
fn mul(self, other: &BaseFieldElement) -> BaseFieldElement {
46+
BaseFieldElement(self.0 * other.0)
4347
}
4448
}
4549

@@ -74,15 +78,7 @@ impl ScalarFieldElement {
7478
return Ok(random_scalar);
7579
}
7680
}
77-
Err(anyhow!(SchnorrSignatureError::RandomScalarGenerationError))
78-
}
79-
80-
pub(crate) fn sub(&self, other: &Self) -> Self {
81-
ScalarFieldElement(self.0 - other.0)
82-
}
83-
84-
pub(crate) fn mul(&self, other: &Self) -> Self {
85-
ScalarFieldElement(self.0 * other.0)
81+
Err(anyhow!(SchnorrSignatureError::RandomScalarGeneration))
8682
}
8783

8884
pub(crate) fn to_bytes(self) -> [u8; 32] {
@@ -94,18 +90,34 @@ impl ScalarFieldElement {
9490
scalar_bytes.copy_from_slice(
9591
bytes
9692
.get(..32)
97-
.ok_or(SchnorrSignatureError::ScalarFieldElementSerializationError)?,
93+
.ok_or(SchnorrSignatureError::ScalarFieldElementSerialization)?,
9894
);
9995

10096
match JubjubScalar::from_bytes(&scalar_bytes).into_option() {
10197
Some(scalar_field_element) => Ok(Self(scalar_field_element)),
10298
None => Err(anyhow!(
103-
SchnorrSignatureError::ScalarFieldElementSerializationError
99+
SchnorrSignatureError::ScalarFieldElementSerialization
104100
)),
105101
}
106102
}
107103
}
108104

105+
impl Mul for ScalarFieldElement {
106+
type Output = ScalarFieldElement;
107+
108+
fn mul(self, other: ScalarFieldElement) -> ScalarFieldElement {
109+
ScalarFieldElement(self.0 * other.0)
110+
}
111+
}
112+
113+
impl Sub for ScalarFieldElement {
114+
type Output = ScalarFieldElement;
115+
116+
fn sub(self, other: ScalarFieldElement) -> ScalarFieldElement {
117+
ScalarFieldElement(self.0 - other.0)
118+
}
119+
}
120+
109121
#[cfg(test)]
110122
mod tests {
111123
use super::*;

mithril-stm/src/signature_scheme/schnorr_signature/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod tests {
2929
// Valid generation check
3030
let sk = SchnorrSigningKey::generate(&mut ChaCha20Rng::from_seed(seed)).unwrap();
3131
let g = PrimeOrderProjectivePoint::create_generator();
32-
let vk = g.scalar_multiplication(&sk.0);
32+
let vk = sk.0 * g;
3333
let vk_from_sk = SchnorrVerificationKey::new_from_signing_key(sk).unwrap();
3434
assert_eq!(vk, vk_from_sk.0);
3535

@@ -110,7 +110,7 @@ mod tests {
110110
assert!(
111111
matches!(
112112
result.downcast_ref::<SchnorrSignatureError>(),
113-
Some(SchnorrSignatureError::ScalarFieldElementSerializationError)
113+
Some(SchnorrSignatureError::ScalarFieldElementSerialization)
114114
),
115115
"Unexpected error: {result:?}"
116116
);
@@ -145,7 +145,7 @@ mod tests {
145145
assert!(
146146
matches!(
147147
result.downcast_ref::<SchnorrSignatureError>(),
148-
Some(SchnorrSignatureError::PrimeOrderProjectivePointSerializationError)
148+
Some(SchnorrSignatureError::PrimeOrderProjectivePointSerialization)
149149
),
150150
"Unexpected error: {result:?}"
151151
);
@@ -169,7 +169,7 @@ mod tests {
169169
assert!(
170170
matches!(
171171
result.downcast_ref::<SchnorrSignatureError>(),
172-
Some(SchnorrSignatureError::ProjectivePointSerializationError)
172+
Some(SchnorrSignatureError::ProjectivePointSerialization)
173173
),
174174
"Unexpected error: {result:?}"
175175
);
@@ -181,7 +181,7 @@ mod tests {
181181
assert!(
182182
matches!(
183183
result.downcast_ref::<SchnorrSignatureError>(),
184-
Some(SchnorrSignatureError::ScalarFieldElementSerializationError)
184+
Some(SchnorrSignatureError::ScalarFieldElementSerialization)
185185
),
186186
"Unexpected error: {result:?}"
187187
);
@@ -193,7 +193,7 @@ mod tests {
193193
assert!(
194194
matches!(
195195
result.downcast_ref::<SchnorrSignatureError>(),
196-
Some(SchnorrSignatureError::ScalarFieldElementSerializationError)
196+
Some(SchnorrSignatureError::ScalarFieldElementSerialization)
197197
),
198198
"Unexpected error: {result:?}"
199199
);

0 commit comments

Comments
 (0)