Skip to content

Commit 3b0d759

Browse files
committed
errors updated
1 parent 37dfe38 commit 3b0d759

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[cfg(feature = "future_snark")]
2-
use super::{SchnorrSignature, SchnorrVerificationKey};
2+
use super::{PrimeOrderProjectivePoint, SchnorrSignature};
33

44
/// Error types for Schnorr signatures.
55
#[cfg(feature = "future_snark")]
@@ -9,15 +9,15 @@ pub enum SchnorrSignatureError {
99
#[error("Invalid Schnorr single signature")]
1010
SignatureInvalid(Box<SchnorrSignature>),
1111

12-
/// Invalid Verification key
13-
#[error("Invalid Schnorr Verification key")]
14-
VerificationKeyInvalid(Box<SchnorrVerificationKey>),
15-
1612
/// This error occurs when the serialization of the raw bytes failed
1713
#[error("Invalid bytes")]
1814
SerializationError,
1915

2016
/// This error occurs when the random scalar fails to generate during the signature
2117
#[error("Failed generation of the signature's random scalar")]
2218
RandomScalarGenerationError,
19+
20+
/// Given point is not on the curve
21+
#[error("Given point is not on the curve")]
22+
PointIsNotOnCurve(Box<PrimeOrderProjectivePoint>),
2323
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ impl AffinePoint {
1616
AffinePoint(JubjubAffinePoint::from(projective_point.0))
1717
}
1818

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+
1927
pub(crate) fn get_u(&self) -> BaseFieldElement {
2028
BaseFieldElement(self.0.get_u())
2129
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,7 @@ impl SchnorrSignature {
4444
///
4545
pub fn verify(&self, msg: &[u8], verification_key: &SchnorrVerificationKey) -> StmResult<()> {
4646
// Check that the verification key is on the curve
47-
if !is_on_curve(ProjectivePoint::from_prime_order_projective_point(
48-
verification_key.0,
49-
)) {
50-
return Err(anyhow!(SchnorrSignatureError::VerificationKeyInvalid(
51-
Box::new(*verification_key)
52-
)));
53-
}
47+
is_on_curve(verification_key.0).with_context(|| "The verification key is invalid.")?;
5448

5549
let generator = PrimeOrderProjectivePoint::create_generator();
5650

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ impl SchnorrSigningKey {
5757
let sigma = msg_hash.scalar_multiplication(&self.0);
5858

5959
// r1 = H(msg) * r, r2 = g * r
60-
let random_scalar = ScalarFieldElement::new_random_nonzero_scalar(rng)?;
60+
let random_scalar = ScalarFieldElement::new_random_nonzero_scalar(rng)
61+
.with_context(|| "Random scalar generation failed during signing.")?;
6162

6263
let random_point_1 = msg_hash.scalar_multiplication(&random_scalar);
6364
let random_point_2 = generator.scalar_multiplication(&random_scalar);
@@ -94,10 +95,12 @@ impl SchnorrSigningKey {
9495
/// The bytes must represent a Jubjub scalar or the conversion will fail
9596
pub fn from_bytes(bytes: &[u8]) -> StmResult<Self> {
9697
if bytes.len() < 32 {
97-
return Err(anyhow!(SchnorrSignatureError::SerializationError))
98-
.with_context(|| "Not enough bytes provided to create a Schnorr signing key.");
98+
return Err(anyhow!(SchnorrSignatureError::SerializationError)).with_context(
99+
|| "Not enough bytes provided to re-construct a Schnorr signing key.",
100+
);
99101
}
100-
let scalar_field_element = ScalarFieldElement::from_bytes(bytes)?;
102+
let scalar_field_element = ScalarFieldElement::from_bytes(bytes)
103+
.with_context(|| "Could not construct Schnorr signing key from given bytes.")?;
101104
Ok(SchnorrSigningKey(scalar_field_element))
102105
}
103106
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
use anyhow::anyhow;
12
use dusk_jubjub::EDWARDS_D;
23

3-
use super::{AffinePoint, BaseFieldElement, ProjectivePoint};
4+
use super::{
5+
AffinePoint, BaseFieldElement, PrimeOrderProjectivePoint, ProjectivePoint,
6+
SchnorrSignatureError,
7+
};
8+
use crate::StmResult;
49

510
/// Check if the given point is on the curve using its coordinates
6-
pub fn is_on_curve(point: ProjectivePoint) -> bool {
7-
let point_affine_representation = AffinePoint::from_projective_point(point);
11+
pub fn is_on_curve(point: PrimeOrderProjectivePoint) -> StmResult<PrimeOrderProjectivePoint> {
12+
let point_affine_representation = AffinePoint::from_prime_order_projective_point(point);
813
let (x, y) = (
914
point_affine_representation.get_u(),
1015
point_affine_representation.get_v(),
@@ -17,7 +22,12 @@ pub fn is_on_curve(point: ProjectivePoint) -> bool {
1722
rhs = rhs.mul(&BaseFieldElement(EDWARDS_D));
1823
rhs = rhs.add(&BaseFieldElement::get_one());
1924

20-
lhs == rhs
25+
if lhs != rhs {
26+
return Err(anyhow!(SchnorrSignatureError::PointIsNotOnCurve(Box::new(
27+
point
28+
))));
29+
}
30+
Ok(point)
2131
}
2232

2333
/// Extract the coordinates of given points in an Extended form

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ impl SchnorrVerificationKey {
2020
pub fn from_bytes(bytes: &[u8]) -> StmResult<Self> {
2121
if bytes.len() < 32 {
2222
return Err(anyhow!(SchnorrSignatureError::SerializationError)).with_context(
23-
|| "Not enough bytes provided to create a Schnorr verification key.",
23+
|| "Not enough bytes provided to construct a Schnorr verification key.",
2424
);
2525
}
26-
let prime_order_projective_point = PrimeOrderProjectivePoint::from_bytes(bytes)?;
26+
let prime_order_projective_point = PrimeOrderProjectivePoint::from_bytes(bytes)
27+
.with_context(|| "Cannot construct Schnorr verification key from given bytes.")?;
2728

2829
Ok(SchnorrVerificationKey(prime_order_projective_point))
2930
}

0 commit comments

Comments
 (0)