1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use rand_core::{CryptoRng, RngCore};
use crate::{random_nonzero, Ciphersuite, Error, Field, Group, Scalar, Signature, VerifyingKey};
#[derive(Copy, Clone)]
pub struct SigningKey<C>
where
C: Ciphersuite,
{
pub(crate) scalar: Scalar<C>,
}
impl<C> SigningKey<C>
where
C: Ciphersuite,
{
pub fn new<R: RngCore + CryptoRng>(mut rng: R) -> SigningKey<C> {
let scalar = random_nonzero::<C, R>(&mut rng);
SigningKey { scalar }
}
pub fn from_bytes(
bytes: <<C::Group as Group>::Field as Field>::Serialization,
) -> Result<SigningKey<C>, Error<C>> {
<<C::Group as Group>::Field as Field>::deserialize(&bytes)
.map(|scalar| SigningKey { scalar })
.map_err(|e| e.into())
}
pub fn to_bytes(&self) -> <<C::Group as Group>::Field as Field>::Serialization {
<<C::Group as Group>::Field as Field>::serialize(&self.scalar)
}
pub fn sign<R: RngCore + CryptoRng>(&self, mut rng: R, msg: &[u8]) -> Signature<C> {
let k = random_nonzero::<C, R>(&mut rng);
let R = <C::Group>::generator() * k;
let c = crate::challenge::<C>(&R, &VerifyingKey::<C>::from(*self).element, msg);
let z = k + (c.0 * self.scalar);
Signature { R, z }
}
}
impl<C> From<&SigningKey<C>> for VerifyingKey<C>
where
C: Ciphersuite,
{
fn from(signing_key: &SigningKey<C>) -> Self {
VerifyingKey {
element: C::Group::generator() * signing_key.scalar,
}
}
}
impl<C> From<SigningKey<C>> for VerifyingKey<C>
where
C: Ciphersuite,
{
fn from(signing_key: SigningKey<C>) -> Self {
VerifyingKey::<C>::from(&signing_key)
}
}