603f441548 Add array constants (Tobin C. Harding)

Pull request description:

  In multiple places we use array constants for zero and one. Add two constants and use them throughout the codebase. Note the endian-ness of `ONE` in the docs.

ACKs for top commit:
  apoelstra:
    ACK 603f441548

Tree-SHA512: 70c455ee42f8a04feec37c3963b030c0f2c07b83801caf818dbb1661b7a0f65c4b92ff6a5df496a4dd6a917d13af4d60624a072c6f8a083293db9cd80d194232
This commit is contained in:
Andrew Poelstra 2022-11-06 15:11:52 +00:00
commit 497654ea23
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 18 additions and 14 deletions

View File

@ -83,4 +83,10 @@ pub const GENERATOR_Y: [u8; 32] = [
0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8 0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8
]; ];
/// The value zero as an array of bytes.
pub const ZERO: [u8; 32] = [0; 32];
/// The value one as big-endian array of bytes.
pub const ONE: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
];

View File

@ -236,6 +236,7 @@ mod tests {
use rand::{RngCore, thread_rng}; use rand::{RngCore, thread_rng};
use crate::{Error, SecretKey, Secp256k1, Message}; use crate::{Error, SecretKey, Secp256k1, Message};
use crate::constants::ONE;
use super::{RecoveryId, RecoverableSignature}; use super::{RecoveryId, RecoverableSignature};
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
@ -280,13 +281,12 @@ mod tests {
fn sign() { fn sign() {
let mut s = Secp256k1::new(); let mut s = Secp256k1::new();
s.randomize(&mut thread_rng()); s.randomize(&mut thread_rng());
let one: [u8; 32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
let sk = SecretKey::from_slice(&one).unwrap(); let sk = SecretKey::from_slice(&ONE).unwrap();
let msg = Message::from_slice(&one).unwrap(); let msg = Message::from_slice(&ONE).unwrap();
let sig = s.sign_ecdsa_recoverable(&msg, &sk); let sig = s.sign_ecdsa_recoverable(&msg, &sk);
assert_eq!(Ok(sig), RecoverableSignature::from_compact(&[ assert_eq!(Ok(sig), RecoverableSignature::from_compact(&[
0x66, 0x73, 0xff, 0xad, 0x21, 0x47, 0x74, 0x1f, 0x66, 0x73, 0xff, 0xad, 0x21, 0x47, 0x74, 0x1f,
0x04, 0x77, 0x2b, 0x6f, 0x92, 0x1f, 0x0b, 0xa6, 0x04, 0x77, 0x2b, 0x6f, 0x92, 0x1f, 0x0b, 0xa6,
@ -305,14 +305,13 @@ mod tests {
fn sign_with_noncedata() { fn sign_with_noncedata() {
let mut s = Secp256k1::new(); let mut s = Secp256k1::new();
s.randomize(&mut thread_rng()); s.randomize(&mut thread_rng());
let one: [u8; 32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
let sk = SecretKey::from_slice(&one).unwrap(); let sk = SecretKey::from_slice(&ONE).unwrap();
let msg = Message::from_slice(&one).unwrap(); let msg = Message::from_slice(&ONE).unwrap();
let noncedata = [42u8; 32]; let noncedata = [42u8; 32];
let sig = s.sign_ecdsa_recoverable_with_noncedata(&msg, &sk, &noncedata); let sig = s.sign_ecdsa_recoverable_with_noncedata(&msg, &sk, &noncedata);
assert_eq!(Ok(sig), RecoverableSignature::from_compact(&[ assert_eq!(Ok(sig), RecoverableSignature::from_compact(&[
0xb5, 0x0b, 0xb6, 0x79, 0x5f, 0x31, 0x74, 0x8a, 0xb5, 0x0b, 0xb6, 0x79, 0x5f, 0x31, 0x74, 0x8a,
0x4d, 0x37, 0xc3, 0xa9, 0x7e, 0xbd, 0x06, 0xa2, 0x4d, 0x37, 0xc3, 0xa9, 0x7e, 0xbd, 0x06, 0xa2,

View File

@ -75,10 +75,7 @@ impl str::FromStr for SecretKey {
} }
/// The number 1 encoded as a secret key. /// The number 1 encoded as a secret key.
pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0, pub const ONE_KEY: SecretKey = SecretKey(constants::ONE);
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1]);
/// A Secp256k1 public key, used for verification of signatures. /// A Secp256k1 public key, used for verification of signatures.
/// ///

View File

@ -7,6 +7,8 @@
use core::fmt; use core::fmt;
use crate::constants;
/// Positive 256-bit integer guaranteed to be less than the secp256k1 curve order. /// Positive 256-bit integer guaranteed to be less than the secp256k1 curve order.
/// ///
/// The difference between `PrivateKey` and `Scalar` is that `Scalar` doesn't guarantee being /// The difference between `PrivateKey` and `Scalar` is that `Scalar` doesn't guarantee being
@ -28,9 +30,9 @@ const MAX_RAW: [u8; 32] = [
impl Scalar { impl Scalar {
/// Scalar representing `0` /// Scalar representing `0`
pub const ZERO: Scalar = Scalar([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); pub const ZERO: Scalar = Scalar(constants::ZERO);
/// Scalar representing `1` /// Scalar representing `1`
pub const ONE: Scalar = Scalar([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); pub const ONE: Scalar = Scalar(constants::ONE);
/// Maximum valid value: `curve_order - 1` /// Maximum valid value: `curve_order - 1`
pub const MAX: Scalar = Scalar(MAX_RAW); pub const MAX: Scalar = Scalar(MAX_RAW);