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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
macro_rules! instantiate {
(
$platform:ident, // name for the module
$simdunit:path, // paths to the platform specific implementations ...
$shake128:path,
$shake128x4:path,
$shake256:path,
$shake256xof:path,
$shake256x4:path,
$sampler:path
) => {
pub mod $platform {
use crate::{
constants::*,
pre_hash::SHAKE128_PH,
types::*,
types::{SigningError, VerificationError},
};
macro_rules! parameter_set {
($parameter_module:ident, $feature:literal) => {
#[cfg(feature = $feature)]
pub(crate) mod $parameter_module {
use super::*;
use crate::ml_dsa_generic::$parameter_module::{
SIGNATURE_SIZE, SIGNING_KEY_SIZE, VERIFICATION_KEY_SIZE,
};
/// Generate key pair.
pub fn generate_key_pair(
randomness: [u8; KEY_GENERATION_RANDOMNESS_SIZE],
signing_key: &mut [u8; SIGNING_KEY_SIZE],
verification_key: &mut [u8; VERIFICATION_KEY_SIZE],
) {
crate::ml_dsa_generic::$parameter_module::generate_key_pair::<
$simdunit,
$sampler,
$shake128x4,
$shake256,
$shake256xof,
$shake256x4,
>(randomness, signing_key, verification_key)
}
/// Sign.
pub fn sign(
signing_key: &[u8; SIGNING_KEY_SIZE],
message: &[u8],
context: &[u8],
randomness: [u8; SIGNING_RANDOMNESS_SIZE],
) -> Result<MLDSASignature<SIGNATURE_SIZE>, SigningError> {
crate::ml_dsa_generic::$parameter_module::sign::<
$simdunit,
$sampler,
$shake128x4,
$shake256,
$shake256xof,
$shake256x4,
>(signing_key, message, context, randomness)
}
/// Sign.
pub fn sign_mut(
signing_key: &[u8; SIGNING_KEY_SIZE],
message: &[u8],
context: &[u8],
randomness: [u8; SIGNING_RANDOMNESS_SIZE],
signature: &mut [u8; SIGNATURE_SIZE],
) -> Result<(), SigningError> {
crate::ml_dsa_generic::$parameter_module::sign_mut::<
$simdunit,
$sampler,
$shake128x4,
$shake256,
$shake256xof,
$shake256x4,
>(signing_key, message, context, randomness, signature)
}
#[cfg(feature = "acvp")]
pub fn sign_internal(
signing_key: &[u8; SIGNING_KEY_SIZE],
message: &[u8],
randomness: [u8; SIGNING_RANDOMNESS_SIZE],
) -> Result<MLDSASignature<SIGNATURE_SIZE>, SigningError> {
let mut signature = MLDSASignature::zero();
crate::ml_dsa_generic::$parameter_module::sign_internal::<
$simdunit,
$sampler,
$shake128x4,
$shake256,
$shake256xof,
$shake256x4,
>(signing_key, message, None, randomness, &mut signature.value)?;
Ok(signature)
}
/// Sign (pre-hashed).
pub(crate) fn sign_pre_hashed_shake128(
signing_key: &[u8; SIGNING_KEY_SIZE],
message: &[u8],
context: &[u8],
pre_hash_buffer: &mut [u8],
randomness: [u8; SIGNING_RANDOMNESS_SIZE],
) -> Result<MLDSASignature<SIGNATURE_SIZE>, SigningError> {
crate::ml_dsa_generic::$parameter_module::sign_pre_hashed::<
$simdunit,
$sampler,
$shake128,
$shake128x4,
$shake256,
$shake256xof,
$shake256x4,
SHAKE128_PH,
>(signing_key, message, context, pre_hash_buffer, randomness)
}
/// Verify.
pub(crate) fn verify(
verification_key: &[u8; VERIFICATION_KEY_SIZE],
message: &[u8],
context: &[u8],
signature: &[u8; SIGNATURE_SIZE],
) -> Result<(), VerificationError> {
crate::ml_dsa_generic::$parameter_module::verify::<
$simdunit,
$sampler,
$shake128x4,
$shake256,
$shake256xof,
>(verification_key, message, context, signature)
}
/// Verify (internal API).
#[cfg(feature = "acvp")]
pub(crate) fn verify_internal(
verification_key: &[u8; VERIFICATION_KEY_SIZE],
message: &[u8],
signature: &[u8; SIGNATURE_SIZE],
) -> Result<(), VerificationError> {
crate::ml_dsa_generic::$parameter_module::verify_internal::<
$simdunit,
$sampler,
$shake128x4,
$shake256,
$shake256xof,
>(verification_key, message, None, signature)
}
/// Verify (pre-hashed with SHAKE-128).
pub(crate) fn verify_pre_hashed_shake128(
verification_key: &[u8; VERIFICATION_KEY_SIZE],
message: &[u8],
context: &[u8],
pre_hash_buffer: &mut [u8],
signature: &[u8; SIGNATURE_SIZE],
) -> Result<(), VerificationError> {
crate::ml_dsa_generic::$parameter_module::verify_pre_hashed::<
$simdunit,
$sampler,
$shake128,
$shake128x4,
$shake256,
$shake256xof,
SHAKE128_PH,
>(
verification_key,
message,
context,
pre_hash_buffer,
signature,
)
}
}
};
}
parameter_set!(ml_dsa_44, "mldsa44");
parameter_set!(ml_dsa_65, "mldsa65");
parameter_set!(ml_dsa_87, "mldsa87");
}
};
}
// Portable generic implementations.
instantiate! {portable,
crate::simd::portable::PortableSIMDUnit,
crate::hash_functions::portable::Shake128,
crate::hash_functions::portable::Shake128X4,
crate::hash_functions::portable::Shake256,
crate::hash_functions::portable::Shake256Xof,
crate::hash_functions::portable::Shake256X4,
crate::samplex4::portable::PortableSampler
}
// AVX2 generic implementation.
#[cfg(feature = "simd256")]
pub mod avx2;
// NEON generic implementation.
#[cfg(feature = "simd128")]
instantiate! {neon,
crate::simd::portable::PortableSIMDUnit,
crate::hash_functions::portable::Shake128,
crate::hash_functions::neon::Shake128x4,
crate::hash_functions::portable::Shake256,
crate::hash_functions::portable::Shake256Xof,
crate::hash_functions::neon::Shake256x4,
crate::samplex4::neon::NeonSampler
}