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
pub use sec1::point::{Coordinates, ModulusSize, Tag};
use crate::{Curve, FieldSize, Result, SecretKey};
use generic_array::GenericArray;
use subtle::CtOption;
#[cfg(feature = "arithmetic")]
use crate::{AffinePoint, Error, ProjectiveArithmetic};
pub type CompressedPoint<C> = GenericArray<u8, CompressedPointSize<C>>;
pub type CompressedPointSize<C> = <FieldSize<C> as ModulusSize>::CompressedPointSize;
pub type EncodedPoint<C> = sec1::point::EncodedPoint<FieldSize<C>>;
pub type UncompressedPoint<C> = GenericArray<u8, UncompressedPointSize<C>>;
pub type UncompressedPointSize<C> = <FieldSize<C> as ModulusSize>::UncompressedPointSize;
pub trait FromEncodedPoint<C>
where
Self: Sized,
C: Curve,
FieldSize<C>: ModulusSize,
{
fn from_encoded_point(point: &EncodedPoint<C>) -> CtOption<Self>;
}
pub trait ToEncodedPoint<C>
where
C: Curve,
FieldSize<C>: ModulusSize,
{
fn to_encoded_point(&self, compress: bool) -> EncodedPoint<C>;
}
pub trait ToCompactEncodedPoint<C>
where
C: Curve,
FieldSize<C>: ModulusSize,
{
fn to_compact_encoded_point(&self) -> CtOption<EncodedPoint<C>>;
}
pub trait ValidatePublicKey
where
Self: Curve,
FieldSize<Self>: ModulusSize,
{
#[allow(unused_variables)]
fn validate_public_key(
secret_key: &SecretKey<Self>,
public_key: &EncodedPoint<Self>,
) -> Result<()> {
Ok(())
}
}
#[cfg(all(feature = "arithmetic"))]
impl<C> ValidatePublicKey for C
where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
fn validate_public_key(secret_key: &SecretKey<C>, public_key: &EncodedPoint<C>) -> Result<()> {
let pk = secret_key
.public_key()
.to_encoded_point(public_key.is_compressed());
if public_key == &pk {
Ok(())
} else {
Err(Error)
}
}
}