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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::{
hazmat::{DigestPrimitive, VerifyPrimitive},
Error, Signature, SignatureSize,
};
use core::{fmt::Debug, ops::Add};
use elliptic_curve::{
consts::U1,
ff::PrimeField,
generic_array::ArrayLength,
point::AffinePoint,
sec1::{
EncodedPoint, FromEncodedPoint, ToEncodedPoint, UncompressedPointSize, UntaggedPointSize,
},
weierstrass::{point, Curve},
FieldBytes, FromDigest, ProjectiveArithmetic, Scalar,
};
use signature::{digest::Digest, DigestVerifier};
#[derive(Clone, Debug)]
pub struct VerifyKey<C>
where
C: Curve + ProjectiveArithmetic,
FieldBytes<C>: From<Scalar<C>> + for<'r> From<&'r Scalar<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>,
AffinePoint<C>: Clone + Debug,
{
pub(crate) public_key: AffinePoint<C>,
}
impl<C> VerifyKey<C>
where
C: Curve + ProjectiveArithmetic,
FieldBytes<C>: From<Scalar<C>> + for<'r> From<&'r Scalar<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>> + FromDigest<C>,
AffinePoint<C>: Clone + Debug + VerifyPrimitive<C> + FromEncodedPoint<C> + ToEncodedPoint<C>,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
SignatureSize<C>: ArrayLength<u8>,
{
pub fn new(bytes: &[u8]) -> Result<Self, Error> {
EncodedPoint::from_bytes(bytes)
.map_err(|_| Error::new())
.and_then(|point| Self::from_encoded_point(&point))
}
pub fn from_encoded_point(public_key: &EncodedPoint<C>) -> Result<Self, Error> {
let affine_point = AffinePoint::<C>::from_encoded_point(public_key);
if affine_point.is_some().into() {
Ok(Self {
public_key: affine_point.unwrap(),
})
} else {
Err(Error::new())
}
}
pub fn to_encoded_point(&self, compress: bool) -> EncodedPoint<C> {
self.public_key.to_encoded_point(compress)
}
}
impl<C, D> DigestVerifier<D, Signature<C>> for VerifyKey<C>
where
C: Curve + ProjectiveArithmetic,
D: Digest<OutputSize = C::FieldSize>,
FieldBytes<C>: From<Scalar<C>> + for<'r> From<&'r Scalar<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>> + FromDigest<C>,
AffinePoint<C>: Clone + Debug + VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
fn verify_digest(&self, digest: D, signature: &Signature<C>) -> Result<(), Error> {
self.public_key
.verify_prehashed(&Scalar::<C>::from_digest(digest), signature)
}
}
impl<C> signature::Verifier<Signature<C>> for VerifyKey<C>
where
C: Curve + ProjectiveArithmetic + DigestPrimitive,
C::Digest: Digest<OutputSize = C::FieldSize>,
FieldBytes<C>: From<Scalar<C>> + for<'r> From<&'r Scalar<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>> + FromDigest<C>,
AffinePoint<C>: Clone + Debug + VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
fn verify(&self, msg: &[u8], signature: &Signature<C>) -> Result<(), Error> {
self.verify_digest(C::Digest::new().chain(msg), signature)
}
}
impl<C> From<&VerifyKey<C>> for EncodedPoint<C>
where
C: Curve + ProjectiveArithmetic + point::Compression,
FieldBytes<C>: From<Scalar<C>> + for<'r> From<&'r Scalar<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>> + FromDigest<C>,
AffinePoint<C>: Clone + Debug + VerifyPrimitive<C> + FromEncodedPoint<C> + ToEncodedPoint<C>,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
SignatureSize<C>: ArrayLength<u8>,
{
fn from(verify_key: &VerifyKey<C>) -> EncodedPoint<C> {
verify_key.to_encoded_point(C::COMPRESS_POINTS)
}
}
impl<C> PartialEq for VerifyKey<C>
where
C: Curve + ProjectiveArithmetic,
FieldBytes<C>: From<Scalar<C>> + for<'r> From<&'r Scalar<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>> + FromDigest<C>,
AffinePoint<C>: Clone + Debug + VerifyPrimitive<C> + FromEncodedPoint<C> + ToEncodedPoint<C>,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
SignatureSize<C>: ArrayLength<u8>,
{
fn eq(&self, other: &Self) -> bool {
self.to_encoded_point(false) == other.to_encoded_point(false)
}
}
impl<C> Copy for VerifyKey<C>
where
C: Curve + ProjectiveArithmetic,
FieldBytes<C>: From<Scalar<C>> + for<'r> From<&'r Scalar<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>,
AffinePoint<C>: Copy + Clone + Debug,
{
}
impl<C> Eq for VerifyKey<C>
where
C: Curve + ProjectiveArithmetic,
FieldBytes<C>: From<Scalar<C>> + for<'r> From<&'r Scalar<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>> + FromDigest<C>,
AffinePoint<C>: Clone + Debug + VerifyPrimitive<C> + FromEncodedPoint<C> + ToEncodedPoint<C>,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
SignatureSize<C>: ArrayLength<u8>,
{
}