Skip to content

Commit 7a1ed14

Browse files
committed
Implement PoT verification optimized for AVX2+VAES and AES+SSE4.1
1 parent bb4d9af commit 7a1ed14

File tree

2 files changed

+275
-55
lines changed

2 files changed

+275
-55
lines changed

crates/subspace-proof-of-time/src/aes.rs

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,28 @@ pub(crate) fn verify_sequential(
5858
if has_avx512f_vaes::get() {
5959
// SAFETY: Checked `avx512f` and `vaes` features
6060
return unsafe {
61-
x86_64::verify_sequential_avx512f(&seed, &key, checkpoints, checkpoint_iterations)
61+
x86_64::verify_sequential_avx512f_vaes(
62+
&seed,
63+
&key,
64+
checkpoints,
65+
checkpoint_iterations,
66+
)
67+
};
68+
}
69+
70+
cpufeatures::new!(has_avx2_vaes, "avx2", "vaes");
71+
if has_avx2_vaes::get() {
72+
// SAFETY: Checked `avx2` and `vaes` features
73+
return unsafe {
74+
x86_64::verify_sequential_avx2_vaes(&seed, &key, checkpoints, checkpoint_iterations)
75+
};
76+
}
77+
78+
cpufeatures::new!(has_aes_sse41, "aes", "sse4.1");
79+
if has_aes_sse41::get() {
80+
// SAFETY: Checked `aes` and `sse4.1` features
81+
return unsafe {
82+
x86_64::verify_sequential_aes_sse41(&seed, &key, checkpoints, checkpoint_iterations)
6283
};
6384
}
6485
}
@@ -115,6 +136,65 @@ mod tests {
115136
];
116137
const BAD_CIPHER: [u8; 16] = [22; 16];
117138

139+
fn verify_test(
140+
seed: PotSeed,
141+
key: PotKey,
142+
checkpoints: &PotCheckpoints,
143+
checkpoint_iterations: u32,
144+
) -> bool {
145+
let sequential = verify_sequential(seed, key, checkpoints, checkpoint_iterations);
146+
let sequential_generic =
147+
verify_sequential_generic(seed, key, checkpoints, checkpoint_iterations);
148+
assert_eq!(sequential, sequential_generic);
149+
150+
#[cfg(target_arch = "x86_64")]
151+
{
152+
cpufeatures::new!(has_avx512f_vaes, "avx512f", "vaes");
153+
if has_avx512f_vaes::get() {
154+
// SAFETY: Checked `avx512f` and `vaes` features
155+
let avx512f_vaes = unsafe {
156+
x86_64::verify_sequential_avx512f_vaes(
157+
&seed,
158+
&key,
159+
checkpoints,
160+
checkpoint_iterations,
161+
)
162+
};
163+
assert_eq!(sequential, avx512f_vaes);
164+
}
165+
166+
cpufeatures::new!(has_avx2_vaes, "avx2", "vaes");
167+
if has_avx2_vaes::get() {
168+
// SAFETY: Checked `avx2` and `vaes` features
169+
let avx2_vaes = unsafe {
170+
x86_64::verify_sequential_avx2_vaes(
171+
&seed,
172+
&key,
173+
checkpoints,
174+
checkpoint_iterations,
175+
)
176+
};
177+
assert_eq!(sequential, avx2_vaes);
178+
}
179+
180+
cpufeatures::new!(has_aes_sse41, "aes", "sse4.1");
181+
if has_aes_sse41::get() {
182+
// SAFETY: Checked `aes` and `sse4.1` features
183+
let aes = unsafe {
184+
x86_64::verify_sequential_aes_sse41(
185+
&seed,
186+
&key,
187+
checkpoints,
188+
checkpoint_iterations,
189+
)
190+
};
191+
assert_eq!(sequential, aes);
192+
}
193+
}
194+
195+
sequential
196+
}
197+
118198
#[test]
119199
fn test_create_verify() {
120200
let seed = PotSeed::from(SEED);
@@ -128,83 +208,42 @@ mod tests {
128208
assert_eq!(checkpoints, generic_checkpoints);
129209
}
130210

131-
assert!(verify_sequential(
132-
seed,
133-
key,
134-
&checkpoints,
135-
checkpoint_iterations,
136-
));
137-
assert!(verify_sequential_generic(
138-
seed,
139-
key,
140-
&checkpoints,
141-
checkpoint_iterations,
142-
));
211+
assert!(verify_test(seed, key, &checkpoints, checkpoint_iterations,));
143212

144213
// Decryption of invalid cipher text fails.
145214
let mut checkpoints_1 = checkpoints;
146215
checkpoints_1[0] = PotOutput::from(BAD_CIPHER);
147-
assert!(!verify_sequential(
148-
seed,
149-
key,
150-
&checkpoints_1,
151-
checkpoint_iterations,
152-
));
153-
assert!(!verify_sequential_generic(
216+
assert!(!verify_test(
154217
seed,
155218
key,
156219
&checkpoints_1,
157220
checkpoint_iterations,
158221
));
159222

160223
// Decryption with wrong number of iterations fails.
161-
assert!(!verify_sequential(
162-
seed,
163-
key,
164-
&checkpoints,
165-
checkpoint_iterations + 2,
166-
));
167-
assert!(!verify_sequential_generic(
224+
assert!(!verify_test(
168225
seed,
169226
key,
170227
&checkpoints,
171228
checkpoint_iterations + 2,
172229
));
173-
assert!(!verify_sequential(
174-
seed,
175-
key,
176-
&checkpoints,
177-
checkpoint_iterations - 2,
178-
));
179-
assert!(!verify_sequential_generic(
230+
assert!(!verify_test(
180231
seed,
181232
key,
182233
&checkpoints,
183234
checkpoint_iterations - 2,
184235
));
185236

186237
// Decryption with wrong seed fails.
187-
assert!(!verify_sequential(
188-
PotSeed::from(SEED_1),
189-
key,
190-
&checkpoints,
191-
checkpoint_iterations,
192-
));
193-
assert!(!verify_sequential_generic(
238+
assert!(!verify_test(
194239
PotSeed::from(SEED_1),
195240
key,
196241
&checkpoints,
197242
checkpoint_iterations,
198243
));
199244

200245
// Decryption with wrong key fails.
201-
assert!(!verify_sequential(
202-
seed,
203-
PotKey::from(KEY_1),
204-
&checkpoints,
205-
checkpoint_iterations,
206-
));
207-
assert!(!verify_sequential_generic(
246+
assert!(!verify_test(
208247
seed,
209248
PotKey::from(KEY_1),
210249
&checkpoints,

0 commit comments

Comments
 (0)