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
#[cfg(all(feature = "digest", feature = "sha2"))]
#[macro_use]
mod macros;
pub mod nistp256;
pub mod nistp384;
pub mod point;
pub mod secp256k1;
pub use self::{nistp256::NistP256, nistp384::NistP384, secp256k1::Secp256k1};
use crate::Error;
use core::{fmt::Debug, hash::Hash, str::FromStr};
use generic_array::ArrayLength;
pub trait WeierstrassCurve:
Clone + Debug + Default + Hash + Eq + PartialEq + PartialOrd + Ord + Send + Sized + Sync
{
const CURVE_KIND: WeierstrassCurveKind;
type ScalarSize: ArrayLength<u8>;
type CompressedPointSize: ArrayLength<u8> + Eq + Hash + PartialEq + PartialOrd + Ord;
type UntaggedPointSize: ArrayLength<u8>;
type UncompressedPointSize: ArrayLength<u8> + Eq + Hash + PartialEq + PartialOrd + Ord;
type Asn1SignatureMaxSize: ArrayLength<u8>;
type FixedSignatureSize: ArrayLength<u8>;
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum WeierstrassCurveKind {
NistP256,
NistP384,
Secp256k1,
}
impl FromStr for WeierstrassCurveKind {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"nistp256" => Ok(WeierstrassCurveKind::NistP256),
"nistp384" => Ok(WeierstrassCurveKind::NistP384),
"secp256k1" => Ok(WeierstrassCurveKind::Secp256k1),
_ => Err(Error::new()),
}
}
}
impl WeierstrassCurveKind {
pub fn to_str(self) -> &'static str {
match self {
WeierstrassCurveKind::NistP256 => "nistp256",
WeierstrassCurveKind::NistP384 => "nistp384",
WeierstrassCurveKind::Secp256k1 => "secp256k1",
}
}
pub fn to_secg_name(self) -> Option<&'static str> {
match self {
WeierstrassCurveKind::NistP256 => Some("secp256r1"),
WeierstrassCurveKind::NistP384 => Some("secp384r1"),
WeierstrassCurveKind::Secp256k1 => Some("secp256k1"),
}
}
}