Struct Key
pub struct Key(/* private fields */);Expand description
A 256-bit encryption key for CLWW Order-Revealing Encryption.
The key is used to encrypt and decrypt values while preserving their ordering. All encryption operations are deterministic for a given key.
§Examples
use cllw_ore::Key;
// Create a key from a 32-byte array
let key = Key::from([0u8; 32]);
// Encrypt an integer
let plaintext: u32 = 42;
let ciphertext = key.encrypt(plaintext).unwrap();
// Decrypt back to the original value
let decrypted = key.decrypt(ciphertext).unwrap();
assert_eq!(plaintext, decrypted);Implementations§
§impl Key
impl Key
pub fn encrypt<T>(
&self,
input: T,
) -> Result<<T as CllwOreEncrypt>::Output, Error>where
T: CllwOreEncrypt,
pub fn encrypt<T>(
&self,
input: T,
) -> Result<<T as CllwOreEncrypt>::Output, Error>where
T: CllwOreEncrypt,
Encrypts a value using CLWW Order-Revealing Encryption.
The encryption is deterministic and preserves the ordering of values.
Supported types: u16, u32, u64, u128, &str, &[u8]
§Examples
use cllw_ore::Key;
use std::cmp::Ordering;
let key = Key::from([0u8; 32]);
// Order is preserved
let a = key.encrypt(10u32).unwrap();
let b = key.encrypt(20u32).unwrap();
assert_eq!(a.cmp(&b), Ordering::Less);
// Encryption is deterministic
let c = key.encrypt(10u32).unwrap();
assert_eq!(a, c);pub fn decrypt<T>(
&self,
input: T,
) -> Result<<T as CllwOreDecrypt>::Output, Error>where
T: CllwOreDecrypt,
pub fn decrypt<T>(
&self,
input: T,
) -> Result<<T as CllwOreDecrypt>::Output, Error>where
T: CllwOreDecrypt,
Decrypts a ciphertext back to its original value.
§Examples
use cllw_ore::Key;
let key = Key::from([0u8; 32]);
let plaintext = 12345u64;
let ciphertext = key.encrypt(plaintext).unwrap();
let decrypted = key.decrypt(ciphertext).unwrap();
assert_eq!(plaintext, decrypted);pub fn encrypt_ope<T>(
&self,
input: T,
) -> Result<<T as CllwOpeEncrypt>::Output, Error>where
T: CllwOpeEncrypt,
pub fn encrypt_ope<T>(
&self,
input: T,
) -> Result<<T as CllwOpeEncrypt>::Output, Error>where
T: CllwOpeEncrypt,
Encrypts a value using CLWW Order-Preserving Encryption.
Ciphertexts can be compared with standard lexicographic byte ordering, without
a custom comparison function. Encryption applies the CLWW keystream, places the
plaintext bit at the top bit of its byte (a +128 contribution rather than
+1), and propagates the resulting sum as a right-to-left carry into a
reserved leading byte.
Ordering is exact: lex compare on the ciphertext always matches the
plaintext order. At the first differing byte the +128 signal dominates any
backward-carry noise (±1 per byte), so there’s no PRF outcome that can flip
the comparison. See packages/cllw-ore/docs/backward-carry-ope.md for the
derivation and the naive-OPE / +1-variant history.
Encrypt-only: OPE ciphertexts cannot be decrypted. If round-trip is required, pair the OPE ciphertext with a standard authenticated symmetric cipher such as AES-GCM encrypting the same plaintext.
§Examples
use cllw_ore::Key;
let key = Key::from([0u8; 32]);
let a = key.encrypt_ope(10u32).unwrap();
let b = key.encrypt_ope(20u32).unwrap();
assert!(a < b);