Skip to main content

KyberCrypto

Struct KyberCrypto 

Source
pub struct KyberCrypto;
Expand description

Main Kyber1024 cryptographic operations

Implementations§

Source§

impl KyberCrypto

Source

pub fn generate_keypair() -> KyberKeyPair

Generate a new Kyber1024 key pair

Examples found in repository?
examples/benchmark.rs (line 72)
69fn benchmark_kyber() -> Result<()> {
70    // Benchmark key generation
71    let start = Instant::now();
72    let mut keypair = KyberCrypto::generate_keypair();
73    for _ in 1..BENCHMARK_ITERATIONS {
74        keypair = KyberCrypto::generate_keypair();
75    }
76    let keygen_time = start.elapsed();
77    
78    // Benchmark encapsulation
79    let start = Instant::now();
80    let mut result = KyberCrypto::encapsulate(&keypair.public_key);
81    for _ in 1..BENCHMARK_ITERATIONS {
82        result = KyberCrypto::encapsulate(&keypair.public_key);
83    }
84    let encapsulation_time = start.elapsed();
85    
86    // Benchmark decapsulation
87    let (_, ciphertext) = result;
88    let start = Instant::now();
89    for _ in 0..BENCHMARK_ITERATIONS {
90        let _shared_secret = KyberCrypto::decapsulate(&ciphertext, &keypair.secret_key);
91    }
92    let decapsulation_time = start.elapsed();
93    
94    println!("  Key Generation: {:.2} ms per operation", 
95             keygen_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
96    println!("  Encapsulation: {:.2} μs per operation", 
97             encapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
98    println!("  Decapsulation: {:.2} μs per operation", 
99             decapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
100    
101    Ok(())
102}
103
104fn benchmark_hybrid() -> Result<()> {
105    let plaintext = vec![0u8; MESSAGE_SIZE];
106    
107    // Benchmark full hybrid encryption
108    let start = Instant::now();
109    for _ in 0..BENCHMARK_ITERATIONS {
110        // Key establishment
111        let alice_keypair = KyberCrypto::generate_keypair();
112        let (shared_secret, _kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
113        
114        // Data encryption
115        let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
116        let (_nonce, _ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
117            &xchacha_key,
118            &plaintext,
119            None,
120        )?;
121    }
122    let hybrid_time = start.elapsed();
123    
124    let hybrid_throughput = (BENCHMARK_ITERATIONS * MESSAGE_SIZE) as f64 / hybrid_time.as_secs_f64() / 1_000_000.0;
125    
126    println!("  Hybrid Encryption: {:.2} MB/s ({:.2} ms per operation)", 
127             hybrid_throughput, 
128             hybrid_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
129    
130    Ok(())
131}
More examples
Hide additional examples
examples/basic_usage.rs (line 63)
61fn kyber_example() -> Result<()> {
62    // Alice generates a key pair
63    let alice_keypair = KyberCrypto::generate_keypair();
64    
65    println!("  Alice's public key size: {} bytes", alice_keypair.public_key.as_bytes().len());
66    println!("  Alice's secret key size: {} bytes", alice_keypair.secret_key.as_bytes().len());
67    
68    // Bob encapsulates a shared secret for Alice
69    let (bob_shared_secret, ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
70    
71    println!("  Encapsulated ciphertext size: {} bytes", ciphertext.as_bytes().len());
72    println!("  Bob's shared secret: {} bytes", bob_shared_secret.as_bytes().len());
73    
74    // Alice decapsulates the shared secret
75    let alice_shared_secret = KyberCrypto::decapsulate(&ciphertext, &alice_keypair.secret_key);
76    
77    println!("  Alice's shared secret: {} bytes", alice_shared_secret.as_bytes().len());
78    
79    // Verify they match
80    assert_eq!(bob_shared_secret.as_bytes(), alice_shared_secret.as_bytes());
81    println!("  ✓ Key encapsulation successful!");
82    
83    // Display key sizes
84    let sizes = KyberCrypto::sizes();
85    println!("  Component sizes:");
86    println!("    - Public key: {} bytes", sizes.public_key);
87    println!("    - Secret key: {} bytes", sizes.secret_key);
88    println!("    - Ciphertext: {} bytes", sizes.ciphertext);
89    println!("    - Shared secret: {} bytes", sizes.shared_secret);
90    
91    Ok(())
92}
93
94fn hybrid_example() -> Result<()> {
95    println!("  Setting up hybrid encryption...");
96    
97    // Step 1: Key establishment with Kyber1024
98    let alice_keypair = KyberCrypto::generate_keypair();
99    let (shared_secret, kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
100    
101    // Step 2: Use shared secret as XChaCha20Poly1305 key
102    let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
103    
104    // Step 3: Encrypt data with XChaCha20Poly1305
105    let plaintext = b"This is a secret message using hybrid post-quantum encryption!";
106    println!("  Message to encrypt: {:?}", std::str::from_utf8(plaintext).unwrap());
107    
108    let (nonce, ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
109        &xchacha_key,
110        plaintext,
111        None,
112    )?;
113    
114    println!("  Hybrid encryption complete!");
115    println!("  - Kyber ciphertext size: {} bytes", kyber_ciphertext.as_bytes().len());
116    println!("  - XChaCha nonce size: {} bytes", nonce.as_bytes().len());
117    println!("  - XChaCha ciphertext size: {} bytes", ciphertext.len());
118    
119    // Decryption process:
120    println!("  Decrypting...");
121    
122    // Step 1: Alice decapsulates the shared secret
123    let alice_shared_secret = KyberCrypto::decapsulate(&kyber_ciphertext, &alice_keypair.secret_key);
124    let alice_xchacha_key = XChaChaKey::from_bytes(alice_shared_secret.as_bytes())?;
125    
126    // Step 2: Decrypt the message
127    let decrypted = XChaChaCrypto::decrypt(&alice_xchacha_key, &nonce, &ciphertext, None)?;
128    
129    println!("  Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
130    
131    // Verify they match
132    assert_eq!(plaintext, &decrypted[..]);
133    println!("  ✓ Hybrid encryption/decryption successful!");
134    
135    Ok(())
136}
Source

pub fn encapsulate( public_key: &KyberPublicKey, ) -> (KyberSharedSecret, KyberCiphertext)

Encapsulate a shared secret for a public key

§Arguments
  • public_key - The recipient’s public key
§Returns

A tuple of (shared_secret, ciphertext)

Examples found in repository?
examples/benchmark.rs (line 80)
69fn benchmark_kyber() -> Result<()> {
70    // Benchmark key generation
71    let start = Instant::now();
72    let mut keypair = KyberCrypto::generate_keypair();
73    for _ in 1..BENCHMARK_ITERATIONS {
74        keypair = KyberCrypto::generate_keypair();
75    }
76    let keygen_time = start.elapsed();
77    
78    // Benchmark encapsulation
79    let start = Instant::now();
80    let mut result = KyberCrypto::encapsulate(&keypair.public_key);
81    for _ in 1..BENCHMARK_ITERATIONS {
82        result = KyberCrypto::encapsulate(&keypair.public_key);
83    }
84    let encapsulation_time = start.elapsed();
85    
86    // Benchmark decapsulation
87    let (_, ciphertext) = result;
88    let start = Instant::now();
89    for _ in 0..BENCHMARK_ITERATIONS {
90        let _shared_secret = KyberCrypto::decapsulate(&ciphertext, &keypair.secret_key);
91    }
92    let decapsulation_time = start.elapsed();
93    
94    println!("  Key Generation: {:.2} ms per operation", 
95             keygen_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
96    println!("  Encapsulation: {:.2} μs per operation", 
97             encapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
98    println!("  Decapsulation: {:.2} μs per operation", 
99             decapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
100    
101    Ok(())
102}
103
104fn benchmark_hybrid() -> Result<()> {
105    let plaintext = vec![0u8; MESSAGE_SIZE];
106    
107    // Benchmark full hybrid encryption
108    let start = Instant::now();
109    for _ in 0..BENCHMARK_ITERATIONS {
110        // Key establishment
111        let alice_keypair = KyberCrypto::generate_keypair();
112        let (shared_secret, _kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
113        
114        // Data encryption
115        let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
116        let (_nonce, _ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
117            &xchacha_key,
118            &plaintext,
119            None,
120        )?;
121    }
122    let hybrid_time = start.elapsed();
123    
124    let hybrid_throughput = (BENCHMARK_ITERATIONS * MESSAGE_SIZE) as f64 / hybrid_time.as_secs_f64() / 1_000_000.0;
125    
126    println!("  Hybrid Encryption: {:.2} MB/s ({:.2} ms per operation)", 
127             hybrid_throughput, 
128             hybrid_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
129    
130    Ok(())
131}
More examples
Hide additional examples
examples/basic_usage.rs (line 69)
61fn kyber_example() -> Result<()> {
62    // Alice generates a key pair
63    let alice_keypair = KyberCrypto::generate_keypair();
64    
65    println!("  Alice's public key size: {} bytes", alice_keypair.public_key.as_bytes().len());
66    println!("  Alice's secret key size: {} bytes", alice_keypair.secret_key.as_bytes().len());
67    
68    // Bob encapsulates a shared secret for Alice
69    let (bob_shared_secret, ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
70    
71    println!("  Encapsulated ciphertext size: {} bytes", ciphertext.as_bytes().len());
72    println!("  Bob's shared secret: {} bytes", bob_shared_secret.as_bytes().len());
73    
74    // Alice decapsulates the shared secret
75    let alice_shared_secret = KyberCrypto::decapsulate(&ciphertext, &alice_keypair.secret_key);
76    
77    println!("  Alice's shared secret: {} bytes", alice_shared_secret.as_bytes().len());
78    
79    // Verify they match
80    assert_eq!(bob_shared_secret.as_bytes(), alice_shared_secret.as_bytes());
81    println!("  ✓ Key encapsulation successful!");
82    
83    // Display key sizes
84    let sizes = KyberCrypto::sizes();
85    println!("  Component sizes:");
86    println!("    - Public key: {} bytes", sizes.public_key);
87    println!("    - Secret key: {} bytes", sizes.secret_key);
88    println!("    - Ciphertext: {} bytes", sizes.ciphertext);
89    println!("    - Shared secret: {} bytes", sizes.shared_secret);
90    
91    Ok(())
92}
93
94fn hybrid_example() -> Result<()> {
95    println!("  Setting up hybrid encryption...");
96    
97    // Step 1: Key establishment with Kyber1024
98    let alice_keypair = KyberCrypto::generate_keypair();
99    let (shared_secret, kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
100    
101    // Step 2: Use shared secret as XChaCha20Poly1305 key
102    let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
103    
104    // Step 3: Encrypt data with XChaCha20Poly1305
105    let plaintext = b"This is a secret message using hybrid post-quantum encryption!";
106    println!("  Message to encrypt: {:?}", std::str::from_utf8(plaintext).unwrap());
107    
108    let (nonce, ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
109        &xchacha_key,
110        plaintext,
111        None,
112    )?;
113    
114    println!("  Hybrid encryption complete!");
115    println!("  - Kyber ciphertext size: {} bytes", kyber_ciphertext.as_bytes().len());
116    println!("  - XChaCha nonce size: {} bytes", nonce.as_bytes().len());
117    println!("  - XChaCha ciphertext size: {} bytes", ciphertext.len());
118    
119    // Decryption process:
120    println!("  Decrypting...");
121    
122    // Step 1: Alice decapsulates the shared secret
123    let alice_shared_secret = KyberCrypto::decapsulate(&kyber_ciphertext, &alice_keypair.secret_key);
124    let alice_xchacha_key = XChaChaKey::from_bytes(alice_shared_secret.as_bytes())?;
125    
126    // Step 2: Decrypt the message
127    let decrypted = XChaChaCrypto::decrypt(&alice_xchacha_key, &nonce, &ciphertext, None)?;
128    
129    println!("  Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
130    
131    // Verify they match
132    assert_eq!(plaintext, &decrypted[..]);
133    println!("  ✓ Hybrid encryption/decryption successful!");
134    
135    Ok(())
136}
Source

pub fn decapsulate( ciphertext: &KyberCiphertext, secret_key: &KyberSecretKey, ) -> KyberSharedSecret

Decapsulate a shared secret from a ciphertext

§Arguments
  • ciphertext - The ciphertext to decapsulate
  • secret_key - The recipient’s secret key
§Returns

The shared secret

Examples found in repository?
examples/benchmark.rs (line 90)
69fn benchmark_kyber() -> Result<()> {
70    // Benchmark key generation
71    let start = Instant::now();
72    let mut keypair = KyberCrypto::generate_keypair();
73    for _ in 1..BENCHMARK_ITERATIONS {
74        keypair = KyberCrypto::generate_keypair();
75    }
76    let keygen_time = start.elapsed();
77    
78    // Benchmark encapsulation
79    let start = Instant::now();
80    let mut result = KyberCrypto::encapsulate(&keypair.public_key);
81    for _ in 1..BENCHMARK_ITERATIONS {
82        result = KyberCrypto::encapsulate(&keypair.public_key);
83    }
84    let encapsulation_time = start.elapsed();
85    
86    // Benchmark decapsulation
87    let (_, ciphertext) = result;
88    let start = Instant::now();
89    for _ in 0..BENCHMARK_ITERATIONS {
90        let _shared_secret = KyberCrypto::decapsulate(&ciphertext, &keypair.secret_key);
91    }
92    let decapsulation_time = start.elapsed();
93    
94    println!("  Key Generation: {:.2} ms per operation", 
95             keygen_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
96    println!("  Encapsulation: {:.2} μs per operation", 
97             encapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
98    println!("  Decapsulation: {:.2} μs per operation", 
99             decapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
100    
101    Ok(())
102}
More examples
Hide additional examples
examples/basic_usage.rs (line 75)
61fn kyber_example() -> Result<()> {
62    // Alice generates a key pair
63    let alice_keypair = KyberCrypto::generate_keypair();
64    
65    println!("  Alice's public key size: {} bytes", alice_keypair.public_key.as_bytes().len());
66    println!("  Alice's secret key size: {} bytes", alice_keypair.secret_key.as_bytes().len());
67    
68    // Bob encapsulates a shared secret for Alice
69    let (bob_shared_secret, ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
70    
71    println!("  Encapsulated ciphertext size: {} bytes", ciphertext.as_bytes().len());
72    println!("  Bob's shared secret: {} bytes", bob_shared_secret.as_bytes().len());
73    
74    // Alice decapsulates the shared secret
75    let alice_shared_secret = KyberCrypto::decapsulate(&ciphertext, &alice_keypair.secret_key);
76    
77    println!("  Alice's shared secret: {} bytes", alice_shared_secret.as_bytes().len());
78    
79    // Verify they match
80    assert_eq!(bob_shared_secret.as_bytes(), alice_shared_secret.as_bytes());
81    println!("  ✓ Key encapsulation successful!");
82    
83    // Display key sizes
84    let sizes = KyberCrypto::sizes();
85    println!("  Component sizes:");
86    println!("    - Public key: {} bytes", sizes.public_key);
87    println!("    - Secret key: {} bytes", sizes.secret_key);
88    println!("    - Ciphertext: {} bytes", sizes.ciphertext);
89    println!("    - Shared secret: {} bytes", sizes.shared_secret);
90    
91    Ok(())
92}
93
94fn hybrid_example() -> Result<()> {
95    println!("  Setting up hybrid encryption...");
96    
97    // Step 1: Key establishment with Kyber1024
98    let alice_keypair = KyberCrypto::generate_keypair();
99    let (shared_secret, kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
100    
101    // Step 2: Use shared secret as XChaCha20Poly1305 key
102    let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
103    
104    // Step 3: Encrypt data with XChaCha20Poly1305
105    let plaintext = b"This is a secret message using hybrid post-quantum encryption!";
106    println!("  Message to encrypt: {:?}", std::str::from_utf8(plaintext).unwrap());
107    
108    let (nonce, ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
109        &xchacha_key,
110        plaintext,
111        None,
112    )?;
113    
114    println!("  Hybrid encryption complete!");
115    println!("  - Kyber ciphertext size: {} bytes", kyber_ciphertext.as_bytes().len());
116    println!("  - XChaCha nonce size: {} bytes", nonce.as_bytes().len());
117    println!("  - XChaCha ciphertext size: {} bytes", ciphertext.len());
118    
119    // Decryption process:
120    println!("  Decrypting...");
121    
122    // Step 1: Alice decapsulates the shared secret
123    let alice_shared_secret = KyberCrypto::decapsulate(&kyber_ciphertext, &alice_keypair.secret_key);
124    let alice_xchacha_key = XChaChaKey::from_bytes(alice_shared_secret.as_bytes())?;
125    
126    // Step 2: Decrypt the message
127    let decrypted = XChaChaCrypto::decrypt(&alice_xchacha_key, &nonce, &ciphertext, None)?;
128    
129    println!("  Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
130    
131    // Verify they match
132    assert_eq!(plaintext, &decrypted[..]);
133    println!("  ✓ Hybrid encryption/decryption successful!");
134    
135    Ok(())
136}
Source

pub fn sizes() -> KyberSizes

Get the sizes of various Kyber1024 components

Examples found in repository?
examples/basic_usage.rs (line 84)
61fn kyber_example() -> Result<()> {
62    // Alice generates a key pair
63    let alice_keypair = KyberCrypto::generate_keypair();
64    
65    println!("  Alice's public key size: {} bytes", alice_keypair.public_key.as_bytes().len());
66    println!("  Alice's secret key size: {} bytes", alice_keypair.secret_key.as_bytes().len());
67    
68    // Bob encapsulates a shared secret for Alice
69    let (bob_shared_secret, ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
70    
71    println!("  Encapsulated ciphertext size: {} bytes", ciphertext.as_bytes().len());
72    println!("  Bob's shared secret: {} bytes", bob_shared_secret.as_bytes().len());
73    
74    // Alice decapsulates the shared secret
75    let alice_shared_secret = KyberCrypto::decapsulate(&ciphertext, &alice_keypair.secret_key);
76    
77    println!("  Alice's shared secret: {} bytes", alice_shared_secret.as_bytes().len());
78    
79    // Verify they match
80    assert_eq!(bob_shared_secret.as_bytes(), alice_shared_secret.as_bytes());
81    println!("  ✓ Key encapsulation successful!");
82    
83    // Display key sizes
84    let sizes = KyberCrypto::sizes();
85    println!("  Component sizes:");
86    println!("    - Public key: {} bytes", sizes.public_key);
87    println!("    - Secret key: {} bytes", sizes.secret_key);
88    println!("    - Ciphertext: {} bytes", sizes.ciphertext);
89    println!("    - Shared secret: {} bytes", sizes.shared_secret);
90    
91    Ok(())
92}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.