Skip to content

Commit 603f441

Browse files
committed
Add array constants
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.
1 parent 15a8c20 commit 603f441

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

src/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@ pub const GENERATOR_Y: [u8; 32] = [
8383
0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8
8484
];
8585

86+
/// The value zero as an array of bytes.
87+
pub const ZERO: [u8; 32] = [0; 32];
8688

89+
/// The value one as big-endian array of bytes.
90+
pub const ONE: [u8; 32] = [
91+
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,
92+
];

src/ecdsa/recovery.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ mod tests {
236236
use rand::{RngCore, thread_rng};
237237

238238
use crate::{Error, SecretKey, Secp256k1, Message};
239+
use crate::constants::ONE;
239240
use super::{RecoveryId, RecoverableSignature};
240241

241242
#[cfg(target_arch = "wasm32")]
@@ -280,13 +281,12 @@ mod tests {
280281
fn sign() {
281282
let mut s = Secp256k1::new();
282283
s.randomize(&mut thread_rng());
283-
let one: [u8; 32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
284-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
285284

286-
let sk = SecretKey::from_slice(&one).unwrap();
287-
let msg = Message::from_slice(&one).unwrap();
285+
let sk = SecretKey::from_slice(&ONE).unwrap();
286+
let msg = Message::from_slice(&ONE).unwrap();
288287

289288
let sig = s.sign_ecdsa_recoverable(&msg, &sk);
289+
290290
assert_eq!(Ok(sig), RecoverableSignature::from_compact(&[
291291
0x66, 0x73, 0xff, 0xad, 0x21, 0x47, 0x74, 0x1f,
292292
0x04, 0x77, 0x2b, 0x6f, 0x92, 0x1f, 0x0b, 0xa6,
@@ -305,14 +305,13 @@ mod tests {
305305
fn sign_with_noncedata() {
306306
let mut s = Secp256k1::new();
307307
s.randomize(&mut thread_rng());
308-
let one: [u8; 32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
309-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
310308

311-
let sk = SecretKey::from_slice(&one).unwrap();
312-
let msg = Message::from_slice(&one).unwrap();
309+
let sk = SecretKey::from_slice(&ONE).unwrap();
310+
let msg = Message::from_slice(&ONE).unwrap();
313311
let noncedata = [42u8; 32];
314312

315313
let sig = s.sign_ecdsa_recoverable_with_noncedata(&msg, &sk, &noncedata);
314+
316315
assert_eq!(Ok(sig), RecoverableSignature::from_compact(&[
317316
0xb5, 0x0b, 0xb6, 0x79, 0x5f, 0x31, 0x74, 0x8a,
318317
0x4d, 0x37, 0xc3, 0xa9, 0x7e, 0xbd, 0x06, 0xa2,

src/key.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ impl str::FromStr for SecretKey {
7272
}
7373

7474
/// The number 1 encoded as a secret key.
75-
pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0,
76-
0, 0, 0, 0, 0, 0, 0, 0,
77-
0, 0, 0, 0, 0, 0, 0, 0,
78-
0, 0, 0, 0, 0, 0, 0, 1]);
75+
pub const ONE_KEY: SecretKey = SecretKey(constants::ONE);
7976

8077
/// A Secp256k1 public key, used for verification of signatures.
8178
///

src/scalar.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
88
use core::fmt;
99

10+
use crate::constants;
11+
1012
/// Positive 256-bit integer guaranteed to be less than the secp256k1 curve order.
1113
///
1214
/// The difference between `PrivateKey` and `Scalar` is that `Scalar` doesn't guarantee being
@@ -28,9 +30,9 @@ const MAX_RAW: [u8; 32] = [
2830

2931
impl Scalar {
3032
/// Scalar representing `0`
31-
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]);
33+
pub const ZERO: Scalar = Scalar(constants::ZERO);
3234
/// Scalar representing `1`
33-
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]);
35+
pub const ONE: Scalar = Scalar(constants::ONE);
3436
/// Maximum valid value: `curve_order - 1`
3537
pub const MAX: Scalar = Scalar(MAX_RAW);
3638

0 commit comments

Comments
 (0)