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
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct WycheproofSuite {
pub algorithm: String,
#[serde(rename = "generatorVersion")]
pub generator_version: String,
#[serde(rename = "numberOfTests")]
pub number_of_tests: i32,
pub notes: std::collections::HashMap<String, String>,
}
#[derive(Debug, Deserialize)]
pub struct WycheproofGroup {
#[serde(rename = "type")]
pub group_type: String,
}
#[derive(Debug, PartialEq, Eq)]
pub enum WycheproofResult {
Valid,
Invalid,
Acceptable,
}
impl std::fmt::Display for WycheproofResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
WycheproofResult::Valid => "valid",
WycheproofResult::Invalid => "invalid",
WycheproofResult::Acceptable => "acceptable",
}
)
}
}
#[derive(Debug, Deserialize)]
pub struct WycheproofCase {
#[serde(rename = "tcId")]
pub case_id: i32,
pub comment: String,
#[serde(with = "wycheproof_result")]
pub result: WycheproofResult,
#[serde(default)]
pub flags: Vec<String>,
}
pub fn wycheproof_data(filename: &str) -> Vec<u8> {
let wycheproof_dir = match std::env::var("WYCHEPROOF_DIR") {
Ok(d) => d,
Err(_) => concat!(env!("CARGO_MANIFEST_DIR"), "/../wycheproof").to_string(),
};
std::fs::read(std::path::Path::new(&wycheproof_dir).join(filename)).unwrap_or_else(|_| {
panic!(
"Test vector file {} not found under $WYCHEPROOF_DIR={}; `git submodule update --init` needed?",
filename, wycheproof_dir
)
})
}
pub mod hex_string {
use serde::Deserialize;
pub fn deserialize<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Vec<u8>, D::Error> {
let s = String::deserialize(deserializer)?;
::hex::decode(&s).map_err(|_e| {
serde::de::Error::invalid_value(serde::de::Unexpected::Str(&s), &"hex data expected")
})
}
}
pub mod wycheproof_result {
use serde::Deserialize;
pub fn deserialize<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<super::WycheproofResult, D::Error> {
let s = String::deserialize(deserializer)?;
match s.as_ref() {
"valid" => Ok(super::WycheproofResult::Valid),
"invalid" => Ok(super::WycheproofResult::Invalid),
"acceptable" => Ok(super::WycheproofResult::Acceptable),
_ => Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(&s),
&"unexpected result value",
)),
}
}
}