[zk-token-sdk] Update random `AeKey` generation to use `OsRng` internally (#31786)

update random `AeKey` generation to use `OsRng` internally
This commit is contained in:
samkim-crypto 2023-05-24 09:53:22 +09:00 committed by GitHub
parent 19a202873b
commit fa4c6aa015
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 6 deletions

View File

@ -7,7 +7,7 @@ use {
aead::{Aead, NewAead}, aead::{Aead, NewAead},
Aes128GcmSiv, Aes128GcmSiv,
}, },
rand::{rngs::OsRng, CryptoRng, Rng, RngCore}, rand::{rngs::OsRng, Rng},
thiserror::Error, thiserror::Error,
}; };
use { use {
@ -43,8 +43,8 @@ pub enum AuthenticatedEncryptionError {
struct AuthenticatedEncryption; struct AuthenticatedEncryption;
impl AuthenticatedEncryption { impl AuthenticatedEncryption {
#[cfg(not(target_os = "solana"))] #[cfg(not(target_os = "solana"))]
fn keygen<T: RngCore + CryptoRng>(rng: &mut T) -> AeKey { fn keygen() -> AeKey {
AeKey(rng.gen::<[u8; 16]>()) AeKey(OsRng.gen::<[u8; 16]>())
} }
#[cfg(not(target_os = "solana"))] #[cfg(not(target_os = "solana"))]
@ -104,8 +104,8 @@ impl AeKey {
Ok(result.to_vec()) Ok(result.to_vec())
} }
pub fn random<T: RngCore + CryptoRng>(rng: &mut T) -> Self { pub fn new_rand() -> Self {
AuthenticatedEncryption::keygen(rng) AuthenticatedEncryption::keygen()
} }
pub fn encrypt(&self, amount: u64) -> AeCiphertext { pub fn encrypt(&self, amount: u64) -> AeCiphertext {
@ -217,7 +217,7 @@ mod tests {
#[test] #[test]
fn test_aes_encrypt_decrypt_correctness() { fn test_aes_encrypt_decrypt_correctness() {
let key = AeKey::random(&mut OsRng); let key = AeKey::new_rand();
let amount = 55; let amount = 55;
let ct = key.encrypt(amount); let ct = key.encrypt(amount);