Merge rust-bitcoin/rust-secp256k1#578: Implement `Debug` trait for `Scalar` type

8ed8cac2fe Implement `Debug` trait for `Scalar` type. (Arik Sosman)

Pull request description:

  Currently, `Scalar` types do not implement the `Debug` trait, whereas most other types in the library do. Besides that being an upstream requirement for us, I believe it would also be quite useful for users of that type.

  Also implements the `Index` traits for `Scalar`.

ACKs for top commit:
  apoelstra:
    ACK 8ed8cac2fe

Tree-SHA512: f254859144850e40badf6ace2b2a1b231e5ed224ec60861586cd5f2042167d89c759dc16a1075702bce90d810ac60db924ea8cb20d82099a42fddb2718da12db
This commit is contained in:
Andrew Poelstra 2023-02-02 18:33:55 +00:00
commit 8603719a93
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 12 additions and 1 deletions

View File

@ -6,7 +6,7 @@
//! provides the `Scalar` type and related. //! provides the `Scalar` type and related.
//! //!
use core::fmt; use core::{fmt, ops};
use crate::constants; use crate::constants;
@ -23,6 +23,7 @@ use crate::constants;
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Scalar([u8; 32]); pub struct Scalar([u8; 32]);
impl_pretty_debug!(Scalar);
const MAX_RAW: [u8; 32] = [ const MAX_RAW: [u8; 32] = [
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
@ -107,6 +108,16 @@ impl Scalar {
} }
} }
impl<I> ops::Index<I> for Scalar
where
[u8]: ops::Index<I>,
{
type Output = <[u8] as ops::Index<I>>::Output;
#[inline]
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
}
impl From<crate::SecretKey> for Scalar { impl From<crate::SecretKey> for Scalar {
fn from(value: crate::SecretKey) -> Self { Scalar(value.secret_bytes()) } fn from(value: crate::SecretKey) -> Self { Scalar(value.secret_bytes()) }
} }