Skip to content

Commit

Permalink
resovle some conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
heliannuuthus committed Aug 27, 2024
1 parent 0f943a3 commit da3ec59
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions sm2/src/pke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
//! 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)?;
//! // Encrypting ASN.1 DER
//! let ciphertext = encrypting_key.encrypt_der(plaintext)?;
//!
//! // Decrypting asn.1
//! assert_eq!(decrypting_key.decrypt_asna1(&ciphertext)?, plaintext);
//! // Decrypting ASN.1 DER
//! assert_eq!(decrypting_key.decrypt_der(&ciphertext)?, plaintext);
//!
//! Ok(())
//! # }
Expand Down Expand Up @@ -121,8 +121,8 @@ impl<'a> DecodeValue<'a> for Cipher<'a> {
let digest = OctetStringRef::decode(nr)?.into();
let cipher = OctetStringRef::decode(nr)?.into();
Ok(Cipher {
x: Uint::from_be_bytes(zero_byte_slice(x)?),
y: Uint::from_be_bytes(zero_byte_slice(y)?),
x: Uint::from_be_bytes(zero_pad_byte_slice(x)?),
y: Uint::from_be_bytes(zero_pad_byte_slice(y)?),
digest,
cipher,
})
Expand Down Expand Up @@ -164,7 +164,7 @@ fn xor(c2: &mut [u8], ha: &[u8], offset: usize, xor_len: usize) {
}

/// Converts a byte slice to a fixed-size array, padding with leading zeroes if necessary.
pub(crate) fn zero_byte_slice<const N: usize>(
pub(crate) fn zero_pad_byte_slice<const N: usize>(
bytes: &[u8],
) -> elliptic_curve::pkcs8::der::Result<[u8; N]> {
let num_zeroes = N
Expand Down
2 changes: 1 addition & 1 deletion sm2/src/pke/encrypting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn encrypt(
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 {
if !<elliptic_curve::subtle::Choice as Into<bool>>::into(k.is_zero()) && k < Sm2::ORDER {
return k;
}
}
Expand Down

0 comments on commit da3ec59

Please sign in to comment.