interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-kv-store / tests / seal_bench.rs

104 lines · 3664 bytes Raw Blame History
1#![cfg(feature = "std")]
2// SPDX-License-Identifier: Apache-2.0
3// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Host micro-benchmark behind the cipher choice of ADR 0003 section 20:
18//! AES-256-GCM against ChaCha20-Poly1305 (both from `ring`), sealing and
19//! opening 1 MiB and 16 MiB buffers in place, as the store does.
20//!
21//! Ignored by default (timing, not correctness). Run on the host with:
22//! `cargo test -p aien-kv-store --release --test seal_bench -- --ignored --nocapture`
23//! Numbers are host-only; the restore cost on the Spark is measured there.
24
25use ring::aead::{Aad, LessSafeKey, Nonce, UnboundKey, AES_256_GCM, CHACHA20_POLY1305};
26use std::time::Instant;
27
28fn rate(alg: &'static ring::aead::Algorithm, len: usize, reps: u32) -> (f64, f64) {
29 let key = LessSafeKey::new(UnboundKey::new(alg, &[0x42; 32]).unwrap());
30 let mut buf = vec![0x5Au8; len];
31 let aad = [7u8; 64];
32 // Warm up once (page faults, CPU feature detection).
33 let tag = key
34 .seal_in_place_separate_tag(
35 Nonce::assume_unique_for_key([0; 12]),
36 Aad::from(&aad),
37 &mut buf,
38 )
39 .unwrap();
40 key.open_in_place_separate_tag(
41 Nonce::assume_unique_for_key([0; 12]),
42 Aad::from(&aad),
43 tag,
44 &mut buf,
45 0..,
46 )
47 .unwrap();
48
49 let mut tags = Vec::with_capacity(reps as usize);
50 let t0 = Instant::now();
51 for i in 0..reps {
52 let mut n = [0u8; 12];
53 n[..4].copy_from_slice(&i.to_le_bytes());
54 tags.push(
55 key.seal_in_place_separate_tag(
56 Nonce::assume_unique_for_key(n),
57 Aad::from(&aad),
58 &mut buf,
59 )
60 .unwrap(),
61 );
62 // Reopen at once so every pass works on the same ciphertext state.
63 key.open_in_place_separate_tag(
64 Nonce::assume_unique_for_key(n),
65 Aad::from(&aad),
66 tags[i as usize],
67 &mut buf,
68 0..,
69 )
70 .unwrap();
71 }
72 let both = t0.elapsed().as_secs_f64();
73
74 let mut n = [0u8; 12];
75 n[11] = 1;
76 let t1 = Instant::now();
77 for _ in 0..reps {
78 let tag = key
79 .seal_in_place_separate_tag(Nonce::assume_unique_for_key(n), Aad::from(&aad), &mut buf)
80 .unwrap();
81 let _ = std::hint::black_box(tag);
82 }
83 let seal = t1.elapsed().as_secs_f64();
84 let open = (both - seal).max(f64::MIN_POSITIVE);
85 let mib = (len as f64 * reps as f64) / (1024.0 * 1024.0);
86 (mib / seal, mib / open)
87}
88
89#[test]
90#[ignore = "host timing benchmark; run with --release -- --ignored --nocapture"]
91fn seal_cipher_throughput_aes256gcm_vs_chacha20poly1305() {
92 for (len, reps) in [(1usize << 20, 200u32), (16 << 20, 20)] {
93 for (name, alg) in [
94 ("AES-256-GCM", &AES_256_GCM),
95 ("ChaCha20-Poly1305", &CHACHA20_POLY1305),
96 ] {
97 let (s, o) = rate(alg, len, reps);
98 println!(
99 "{name:18} {:>3} MiB seal {s:8.0} MiB/s open {o:8.0} MiB/s",
100 len >> 20
101 );
102 }
103 }
104}