Skip to content

Commit

Permalink
test and doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
heliannuuthus committed Aug 13, 2024
1 parent 397e783 commit a6274c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions sm2/src/pke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@
//!
//! // Encrypting
//! let secret_key = SecretKey::random(&mut OsRng); // serialize with `::to_bytes()`
//! let encrypting_key = EncryptingKey::new_with_mode(secret_key, Mode::C1C2C3);
//! let public_key = secret_key.public_key();
//! let encrypting_key = EncryptingKey::new_with_mode(public_key, Mode::C1C2C3);
//! let plaintext = b"plaintext";
//! let ciphertext = encrypting_key.encrypt(plaintext)?;
//!
//! use sm2::pke::DecryptingKey;
//! // Decrypting
//! let decrypting_key = DecryptingKey::new_with_mode(secret_key, Mode::C1C2C3);
//! let decrypting_key = DecryptingKey::new_with_mode(secret_key.to_nonzero_scalar(), Mode::C1C2C3);
//! assert_eq!(decrypting_key.decrypt(&ciphertext)?, plaintext);
//!
//! // Encrypting asn.1
//! let ciphertext = encrypting_key.encrypt_asna1(plaintext)?;
//!
//! // Decrypting asn.1
//! assert_eq!(decrypting_key.decrypt_asna1(&ciphertext)?, plaintext);
//!
//! Ok(())
//! # }
//! ```
//!
Expand Down Expand Up @@ -173,3 +176,4 @@ pub(crate) fn zero_byte_slice<const N: usize>(
output[num_zeroes..].copy_from_slice(bytes);
Ok(output)
}

3 changes: 2 additions & 1 deletion sm2/src/pke/encrypting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ fn encrypt(
})
}

fn next_k(bit_length: u32) -> Uint<4> {
fn next_k(bit_length: u32) -> U256 {
loop {
let k = U256::random_bits(&mut rand_core::OsRng, bit_length);
if k.is_zero().unwrap_u8() == 0 && k <= Sm2::ORDER {
return k;
}
}
}

0 comments on commit a6274c3

Please sign in to comment.