Skip to content

Commit a37bf43

Browse files
committed
utils functionality moved to wrapper
1 parent 62e9262 commit a37bf43

File tree

7 files changed

+56
-74
lines changed

7 files changed

+56
-74
lines changed

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::anyhow;
22
use dusk_jubjub::{
3-
AffinePoint as JubjubAffinePoint, ExtendedPoint as JubjubExtended,
3+
AffinePoint as JubjubAffinePoint, EDWARDS_D, ExtendedPoint as JubjubExtended,
44
SubgroupPoint as JubjubSubgroup,
55
};
66
use group::{Group, GroupEncoding};
@@ -17,10 +17,10 @@ impl AffinePoint {
1717
}
1818

1919
pub(crate) fn from_prime_order_projective_point(
20-
prime_order_projective_point: PrimeOrderProjectivePoint,
20+
prime_order_projective_point: &PrimeOrderProjectivePoint,
2121
) -> Self {
2222
AffinePoint(JubjubAffinePoint::from(
23-
ProjectivePoint::from_prime_order_projective_point(prime_order_projective_point).0,
23+
ProjectivePoint::from_prime_order_projective_point(*prime_order_projective_point).0,
2424
))
2525
}
2626

@@ -49,6 +49,12 @@ impl ProjectivePoint {
4949
ProjectivePoint(self.0 * scalar.0)
5050
}
5151

52+
pub(crate) fn get_coordinates(&self) -> (BaseFieldElement, BaseFieldElement) {
53+
let affine_point = AffinePoint::from_projective_point(*self);
54+
55+
(affine_point.get_u(), affine_point.get_v())
56+
}
57+
5258
pub(crate) fn to_bytes(self) -> [u8; 32] {
5359
self.0.to_bytes()
5460
}
@@ -93,6 +99,29 @@ impl PrimeOrderProjectivePoint {
9399
PrimeOrderProjectivePoint(self.0 * scalar.0)
94100
}
95101

102+
/// Check if the given point is on the curve using its coordinates
103+
pub(crate) fn is_on_curve(&self) -> StmResult<PrimeOrderProjectivePoint> {
104+
let point_affine_representation = AffinePoint::from_prime_order_projective_point(self);
105+
let (x, y) = (
106+
point_affine_representation.get_u(),
107+
point_affine_representation.get_v(),
108+
);
109+
let x_square = x.square();
110+
let y_square = y.square();
111+
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());
116+
117+
if lhs != rhs {
118+
return Err(anyhow!(SchnorrSignatureError::PointIsNotOnCurve(Box::new(
119+
*self
120+
))));
121+
}
122+
Ok(*self)
123+
}
124+
96125
pub(crate) fn to_bytes(self) -> [u8; 32] {
97126
self.0.to_bytes()
98127
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use dusk_jubjub::{Fq as JubjubBase, Fr as JubjubScalar};
33
use ff::Field;
44
use rand_core::{CryptoRng, RngCore};
55

6+
use super::ProjectivePoint;
67
use crate::{StmResult, signature_scheme::SchnorrSignatureError};
78

89
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -28,6 +29,18 @@ impl BaseFieldElement {
2829
pub(crate) fn get_one() -> Self {
2930
BaseFieldElement(JubjubBase::ONE)
3031
}
32+
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
43+
}
3144
}
3245

3346
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ mod error;
22
mod jubjub_wrapper;
33
mod signature;
44
mod signing_key;
5-
mod utils;
65
mod verification_key;
76

87
pub use error::*;
98
pub(crate) use jubjub_wrapper::*;
109
pub use signature::*;
1110
pub use signing_key::*;
12-
pub(crate) use utils::*;
1311
pub use verification_key::*;
1412

1513
#[cfg(test)]
@@ -18,15 +16,14 @@ mod tests {
1816
use rand_chacha::ChaCha20Rng;
1917
use rand_core::SeedableRng;
2018

19+
use super::{SchnorrSigningKey, SchnorrVerificationKey};
2120
use crate::{
2221
SchnorrSignature,
2322
signature_scheme::{PrimeOrderProjectivePoint, ScalarFieldElement, SchnorrSignatureError},
2423
};
2524

26-
use super::{SchnorrSigningKey, SchnorrVerificationKey};
27-
2825
proptest! {
29-
#![proptest_config(ProptestConfig::with_cases(10))]
26+
#![proptest_config(ProptestConfig::with_cases(1000))]
3027

3128
#[test]
3229
fn verification_key(seed in any::<[u8;32]>()) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use anyhow::{Context, anyhow};
22

33
use super::{
4-
PrimeOrderProjectivePoint, ProjectivePoint, ScalarFieldElement, SchnorrSignatureError,
5-
SchnorrVerificationKey, collect_coordinates_of_list_of_points, compute_truncated_digest,
4+
BaseFieldElement, PrimeOrderProjectivePoint, ProjectivePoint, ScalarFieldElement,
5+
SchnorrSignatureError, SchnorrVerificationKey, compute_truncated_digest,
66
};
77
use crate::StmResult;
88

@@ -67,7 +67,7 @@ impl SchnorrSignature {
6767

6868
// Since the hash function takes as input scalar elements
6969
// We need to convert the EC points to their coordinates
70-
let points_coordinates = collect_coordinates_of_list_of_points(&[
70+
let points_coordinates = BaseFieldElement::collect_coordinates_of_list_of_points(&[
7171
msg_hash_point,
7272
ProjectivePoint::from_prime_order_projective_point(verification_key.0),
7373
self.commitment_point,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use anyhow::{Context, anyhow};
22
use rand_core::{CryptoRng, RngCore};
33

44
use super::{
5-
PrimeOrderProjectivePoint, ProjectivePoint, ScalarFieldElement, SchnorrSignature,
6-
SchnorrSignatureError, SchnorrVerificationKey, collect_coordinates_of_list_of_points,
7-
compute_truncated_digest,
5+
BaseFieldElement, PrimeOrderProjectivePoint, ProjectivePoint, ScalarFieldElement,
6+
SchnorrSignature, SchnorrSignatureError, SchnorrVerificationKey, compute_truncated_digest,
87
};
98
use crate::StmResult;
109

@@ -66,7 +65,7 @@ impl SchnorrSigningKey {
6665
// Since the hash function takes as input scalar elements
6766
// We need to convert the EC points to their coordinates
6867
// The order must be preserved
69-
let points_coordinates = collect_coordinates_of_list_of_points(&[
68+
let points_coordinates = BaseFieldElement::collect_coordinates_of_list_of_points(&[
7069
msg_hash_point,
7170
ProjectivePoint::from_prime_order_projective_point(verification_key.0),
7271
commitment_point,

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

Lines changed: 0 additions & 53 deletions
This file was deleted.

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use anyhow::{Context, Ok, anyhow};
22

3-
use super::{PrimeOrderProjectivePoint, SchnorrSignatureError, SchnorrSigningKey};
4-
use crate::{
5-
StmResult,
6-
signature_scheme::{ProjectivePoint, is_on_curve},
7-
};
3+
use super::{PrimeOrderProjectivePoint, ProjectivePoint, SchnorrSignatureError, SchnorrSigningKey};
4+
use crate::StmResult;
85

96
/// Schnorr verification key, it consists of a point on the Jubjub curve
107
/// vk = g * sk, where g is a generator
@@ -35,7 +32,7 @@ impl SchnorrVerificationKey {
3532
Box::new(self.0)
3633
)));
3734
}
38-
is_on_curve(self.0)?;
35+
self.0.is_on_curve()?;
3936

4037
Ok(*self)
4138
}

0 commit comments

Comments
 (0)