Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce number of PoT checkpoints 2x #1708

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/subspace-proof-of-time/benches/pot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn criterion_benchmark(c: &mut Criterion) {
let slot_number = 1;
let mut injected_block_hash = BlockHash::default();
thread_rng().fill(injected_block_hash.as_mut());
let checkpoints = 16;
let checkpoints = 8;
// About 1s on 5.5 GHz Raptor Lake CPU
let iterations = 166_000_000;
let proof_of_time_sequential = ProofOfTime::new(1, iterations);
Expand Down
22 changes: 14 additions & 8 deletions crates/subspace-proof-of-time/src/pot_aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate alloc;

use aes::cipher::generic_array::GenericArray;
use aes::cipher::{BlockEncrypt, KeyInit};
use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit};
use aes::Aes128;
use alloc::vec::Vec;
use subspace_core_primitives::{PotBytes, PotCheckpoint, PotKey, PotSeed};
Expand Down Expand Up @@ -31,12 +31,16 @@ pub(crate) fn create(
}

/// Verifies the AES based proof sequentially.
///
/// Panics if `checkpoint_iterations` is not a multiple of `2`.
pub(crate) fn verify_sequential(
seed: &PotSeed,
key: &PotKey,
checkpoints: &[PotCheckpoint],
checkpoint_iterations: u32,
) -> bool {
assert_eq!(checkpoint_iterations % 2, 0);

let key = GenericArray::from(PotBytes::from(*key));
let cipher = Aes128::new(&key);

Expand All @@ -45,15 +49,17 @@ pub(crate) fn verify_sequential(
for checkpoint in checkpoints.iter().rev().skip(1).rev() {
inputs.push(GenericArray::from(PotBytes::from(*checkpoint)));
}
let mut outputs = checkpoints
.iter()
.map(|checkpoint| GenericArray::from(PotBytes::from(*checkpoint)))
.collect::<Vec<_>>();

for _ in 0..checkpoint_iterations {
for _ in 0..checkpoint_iterations / 2 {
cipher.encrypt_blocks(&mut inputs);
cipher.decrypt_blocks(&mut outputs);
}

inputs
.iter()
.zip(checkpoints)
.all(|(a, b)| a.as_slice() == b.as_ref())
inputs == outputs
}

#[cfg(test)]
Expand Down Expand Up @@ -111,13 +117,13 @@ mod tests {
&seed,
&key,
&checkpoints,
checkpoint_iterations + 1
checkpoint_iterations + 2
));
assert!(!verify_sequential(
&seed,
&key,
&checkpoints,
checkpoint_iterations - 1
checkpoint_iterations - 2
));

// Decryption with wrong seed fails.
Expand Down