Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check the key byte length generically #245

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/compressed_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ impl<T: PublicKey> CompressedKey<T> {
}

impl<T> CompressedKey<T> {
/// Create a new compressed key
pub fn new(key: &[u8]) -> CompressedKey<T> {
/// Create a new compressed key. NOTE: does not check the key length for T
fn new(key: &[u8]) -> CompressedKey<T> {
Self {
key: key.to_vec(),
public_key: OnceLock::new(),
Expand Down Expand Up @@ -198,15 +198,15 @@ impl<T> Ord for CompressedKey<T> {
}
}

impl<T> ByteArray for CompressedKey<T> {
impl<T: PublicKey> ByteArray for CompressedKey<T> {
/// Create a new `RistrettoPublicKey` instance form the given byte array. The constructor returns errors under
/// the following circumstances:
/// * The byte array is not exactly 32 bytes
/// * The byte array does not represent a valid (compressed) point on the ristretto255 curve
fn from_canonical_bytes(bytes: &[u8]) -> Result<CompressedKey<T>, ByteArrayError>
where Self: Sized {
// Check the length here, because The Ristretto constructor panics rather than returning an error
if bytes.len() != 32 {
// Check the length here, because the new function does not
if bytes.len() != T::KEY_LEN {
return Err(ByteArrayError::IncorrectLength {});
}
Ok(Self::new(bytes))
Expand Down
2 changes: 1 addition & 1 deletion src/ristretto/ristretto_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ impl ByteArray for RistrettoPublicKey {
fn from_canonical_bytes(bytes: &[u8]) -> Result<RistrettoPublicKey, ByteArrayError>
where Self: Sized {
// Check the length here, because The Ristretto constructor panics rather than returning an error
if bytes.len() != 32 {
if bytes.len() != Self::KEY_LEN {
return Err(ByteArrayError::IncorrectLength {});
}
let compressed = CompressedRistretto::from_slice(bytes).map_err(|_| ByteArrayError::ConversionError {
Expand Down
Loading