Skip to content

Commit a6fc158

Browse files
committed
dusk jubjub wrapper created
1 parent 8f12c05 commit a6fc158

File tree

18 files changed

+661
-471
lines changed

18 files changed

+661
-471
lines changed

mithril-stm/benches/schnorr_sig.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ fn sign_and_verify(c: &mut Criterion, nr_sigs: usize) {
4545
let mut msks = Vec::new();
4646
let mut sigs = Vec::new();
4747
for _ in 0..nr_sigs {
48-
let sk = SchnorrSigningKey::try_generate(&mut rng).unwrap();
49-
let vk = SchnorrVerificationKey::from(&sk);
48+
let sk = SchnorrSigningKey::generate(&mut rng).unwrap();
49+
let vk = SchnorrVerificationKey::new_from_signing_key(sk.clone()).unwrap();
5050
let sig = sk.sign(&msg, &mut rng_sig).unwrap();
5151
sigs.push(sig);
5252
mvks.push(vk);

mithril-stm/src/membership_commitment/merkle_tree/leaf.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::cmp::Ordering;
2-
31
use serde::{Deserialize, Serialize};
2+
use std::cmp::Ordering;
43

54
use crate::{
65
MerkleTreeError, Stake, StmResult, VerificationKey, signature_scheme::BlsVerificationKey,

mithril-stm/src/membership_commitment/merkle_tree/tree.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,8 @@ mod tests {
271271
use proptest::{collection::vec, prelude::*};
272272
use rand::{rng, seq::IteratorRandom};
273273

274-
use crate::signature_scheme::BlsVerificationKey;
275-
276274
use super::*;
275+
use crate::signature_scheme::BlsVerificationKey;
277276

278277
fn pow2_plus1(h: usize) -> usize {
279278
1 + 2_usize.pow(h as u32)

mithril-stm/src/protocol/aggregate_signature/signature.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::{collections::HashMap, fmt::Display, hash::Hash};
2-
31
use anyhow::anyhow;
42
use blake2::digest::{Digest, FixedOutput};
53
use serde::{Deserialize, Serialize};
4+
use std::{collections::HashMap, fmt::Display, hash::Hash};
65

76
use super::AggregateVerificationKey;
87
use crate::{

mithril-stm/src/protocol/key_registration.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//! Key registration functionality.
2+
use anyhow::anyhow;
3+
use blake2::digest::{Digest, FixedOutput};
24
use std::{
35
collections::{HashMap, hash_map::Entry},
46
sync::Arc,
57
};
68

7-
use anyhow::anyhow;
8-
use blake2::digest::{Digest, FixedOutput};
9-
109
use crate::{
1110
RegisterError, Stake, StmResult,
1211
membership_commitment::{MerkleTree, MerkleTreeLeaf},
@@ -95,9 +94,8 @@ mod tests {
9594
use rand_chacha::ChaCha20Rng;
9695
use rand_core::SeedableRng;
9796

98-
use crate::signature_scheme::BlsSigningKey;
99-
10097
use super::*;
98+
use crate::signature_scheme::BlsSigningKey;
10199

102100
proptest! {
103101
#[test]

mithril-stm/src/protocol/single_signature/signature.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
use anyhow::{Context, anyhow};
2+
use blake2::digest::{Digest, FixedOutput};
3+
use serde::{Deserialize, Serialize};
14
use std::{
25
cmp::Ordering,
36
hash::{Hash, Hasher},
47
};
58

6-
use anyhow::{Context, anyhow};
7-
use blake2::digest::{Digest, FixedOutput};
8-
use serde::{Deserialize, Serialize};
9-
109
use crate::{
1110
AggregateVerificationKey, Index, Parameters, SignatureError, Stake, StmResult, VerificationKey,
1211
is_lottery_won, signature_scheme::BlsSignature,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ mod tests {
9595
use rand_core::{RngCore, SeedableRng};
9696

9797
use super::helper::unsafe_helpers::{p1_affine_to_sig, p2_affine_to_vk};
98-
use crate::{KeyRegistration, MultiSignatureError, RegisterError};
99-
10098
use super::*;
99+
use crate::{KeyRegistration, MultiSignatureError, RegisterError};
101100

102101
impl PartialEq for BlsSigningKey {
103102
fn eq(&self, other: &Self) -> bool {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use anyhow::anyhow;
2+
use blst::{
3+
BLST_ERROR,
4+
min_sig::{AggregatePublicKey, PublicKey as BlstVk},
5+
};
6+
use serde::{Deserialize, Serialize};
27
use std::{
38
cmp::Ordering,
49
fmt::{Display, Formatter},
510
hash::{Hash, Hasher},
611
iter::Sum,
712
};
813

9-
use blst::{
10-
BLST_ERROR,
11-
min_sig::{AggregatePublicKey, PublicKey as BlstVk},
12-
};
13-
use serde::{Deserialize, Serialize};
14-
1514
use super::{BlsProofOfPossession, BlsSigningKey, POP, helper::unsafe_helpers::verify_pairing};
1615
use crate::{MultiSignatureError, StmResult, blst_error_to_stm_error};
1716

Lines changed: 24 additions & 8 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,19 +9,35 @@ 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

20-
/// This error occurs when the signing key fails to generate
21-
#[error("Failed generation of the signing key")]
22-
SigningKeyGenerationError,
16+
/// This error occurs when the serialization of the signing key bytes failed
17+
#[error("Invalid scalar field element bytes")]
18+
ScalarFieldElementSerializationError,
19+
20+
/// This error occurs when the serialization of the projective point bytes failed
21+
#[error("Invalid projective point bytes")]
22+
ProjectivePointSerializationError,
23+
24+
/// This error occurs when the serialization of the prime order projective point bytes failed
25+
#[error("Invalid prime order projective point bytes")]
26+
PrimeOrderProjectivePointSerializationError,
2327

2428
/// This error occurs when the random scalar fails to generate during the signature
2529
#[error("Failed generation of the signature's random scalar")]
2630
RandomScalarGenerationError,
31+
32+
/// This error occurs when signing key is zero or one.
33+
#[error("The signing key is invalid.")]
34+
InvalidSigningKey,
35+
36+
/// Given point is not on the curve
37+
#[error("Given point is not on the curve")]
38+
PointIsNotOnCurve(Box<PrimeOrderProjectivePoint>),
39+
40+
/// Given point is not prime order
41+
#[error("Given point is not prime order")]
42+
PointIsNotPrimeOrder(Box<PrimeOrderProjectivePoint>),
2743
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
use anyhow::anyhow;
2+
use dusk_jubjub::{
3+
AffinePoint as JubjubAffinePoint, EDWARDS_D, ExtendedPoint as JubjubExtended,
4+
SubgroupPoint as JubjubSubgroup,
5+
};
6+
use group::{Group, GroupEncoding};
7+
8+
use super::{BaseFieldElement, ScalarFieldElement};
9+
use crate::{StmResult, signature_scheme::SchnorrSignatureError};
10+
11+
#[derive(Clone)]
12+
pub(crate) struct AffinePoint(JubjubAffinePoint);
13+
14+
impl AffinePoint {
15+
pub(crate) fn from_projective_point(projective_point: ProjectivePoint) -> Self {
16+
AffinePoint(JubjubAffinePoint::from(projective_point.0))
17+
}
18+
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+
27+
pub(crate) fn get_u(&self) -> BaseFieldElement {
28+
BaseFieldElement(self.0.get_u())
29+
}
30+
31+
pub(crate) fn get_v(&self) -> BaseFieldElement {
32+
BaseFieldElement(self.0.get_v())
33+
}
34+
}
35+
36+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37+
pub(crate) struct ProjectivePoint(pub(crate) JubjubExtended);
38+
39+
impl ProjectivePoint {
40+
pub(crate) fn hash_to_projective_point(input: &[u8]) -> Self {
41+
ProjectivePoint(JubjubExtended::hash_to_point(input))
42+
}
43+
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+
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+
58+
pub(crate) fn to_bytes(self) -> [u8; 32] {
59+
self.0.to_bytes()
60+
}
61+
62+
pub(crate) fn from_bytes(bytes: &[u8]) -> StmResult<Self> {
63+
let mut projective_point_bytes = [0u8; 32];
64+
projective_point_bytes
65+
.copy_from_slice(bytes.get(..32).ok_or(SchnorrSignatureError::SerializationError)?);
66+
67+
match JubjubExtended::from_bytes(&projective_point_bytes).into_option() {
68+
Some(projective_point) => Ok(Self(projective_point)),
69+
None => Err(anyhow!(
70+
SchnorrSignatureError::ProjectivePointSerializationError
71+
)),
72+
}
73+
}
74+
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+
81+
pub(crate) fn is_prime_order(self) -> bool {
82+
self.0.is_prime_order().into()
83+
}
84+
}
85+
86+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
87+
pub(crate) struct PrimeOrderProjectivePoint(pub(crate) JubjubSubgroup);
88+
89+
impl PrimeOrderProjectivePoint {
90+
pub(crate) fn create_generator() -> Self {
91+
PrimeOrderProjectivePoint(JubjubSubgroup::generator())
92+
}
93+
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+
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+
125+
pub(crate) fn to_bytes(self) -> [u8; 32] {
126+
self.0.to_bytes()
127+
}
128+
129+
pub(crate) fn from_bytes(bytes: &[u8]) -> StmResult<Self> {
130+
let mut prime_order_projective_point_bytes = [0u8; 32];
131+
prime_order_projective_point_bytes
132+
.copy_from_slice(bytes.get(..32).ok_or(SchnorrSignatureError::SerializationError)?);
133+
134+
match JubjubSubgroup::from_bytes(&prime_order_projective_point_bytes).into_option() {
135+
Some(prime_order_projective_point) => Ok(Self(prime_order_projective_point)),
136+
None => Err(anyhow!(
137+
SchnorrSignatureError::PrimeOrderProjectivePointSerializationError
138+
)),
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)