Skip to content

Commit

Permalink
Merge #578: Implement Debug trait for Scalar type
Browse files Browse the repository at this point in the history
8ed8cac 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 8ed8cac

Tree-SHA512: f254859144850e40badf6ace2b2a1b231e5ed224ec60861586cd5f2042167d89c759dc16a1075702bce90d810ac60db924ea8cb20d82099a42fddb2718da12db
  • Loading branch information
apoelstra committed Feb 2, 2023
2 parents 65eb166 + 8ed8cac commit 8603719
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! provides the `Scalar` type and related.
//!
use core::fmt;
use core::{fmt, ops};

use crate::constants;

Expand All @@ -23,6 +23,7 @@ use crate::constants;
#[allow(missing_debug_implementations)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Scalar([u8; 32]);
impl_pretty_debug!(Scalar);

const MAX_RAW: [u8; 32] = [
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
Expand Down Expand Up @@ -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 {
fn from(value: crate::SecretKey) -> Self { Scalar(value.secret_bytes()) }
}
Expand Down

0 comments on commit 8603719

Please sign in to comment.