Skip to content

Commit 6a774bd

Browse files
committed
Merge #334: Use explicit u8 when assigning a byte slice
24d6f62 Use explicit u8 when assigning a byte slice (junderw) Pull request description: Is there a way to tell the compiler to not allow `[0; 64]` and require that either the type is explicitly given to the variable, or that each member uses explicit `0u8` notation? I noticed the usage was a mix of explicit and implicit, so I changed all to explicit. ACKs for top commit: apoelstra: ACK 24d6f62 Tree-SHA512: f7796dcc3ae240983257bef0f25bd0df741943f75d86e9bca7c45076af179d96ce213bd9c339a01f721f7dc9b96a0a4a56ef2cf44339f4c91d208103b7659d9f
2 parents 88196bd + 24d6f62 commit 6a774bd

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ mod fuzz_dummy {
932932
let scalar_slice = slice::from_raw_parts(scalar, 32);
933933
let pk_slice = &(*point).0[..32];
934934

935-
let mut res_arr = [0; 32];
935+
let mut res_arr = [0u8; 32];
936936
for i in 0..32 {
937937
res_arr[i] = scalar_slice[i] ^ pk_slice[i] ^ 1;
938938
}
@@ -1123,7 +1123,7 @@ mod fuzz_dummy {
11231123
check_context_flags(cx, SECP256K1_START_VERIFY);
11241124
let mut pk = PublicKey::new();
11251125
pk.0.copy_from_slice(&(*keypair).0[32..]);
1126-
let mut sk = [0; 32];
1126+
let mut sk = [0u8; 32];
11271127
sk.copy_from_slice(&(*keypair).0[..32]);
11281128
assert_eq!(secp256k1_ec_pubkey_tweak_add(cx, &mut pk, tweak32), 1);
11291129
assert_eq!(secp256k1_ec_seckey_tweak_add(cx, (&mut sk[..]).as_mut_ptr(), tweak32), 1);

secp256k1-sys/src/recovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ mod fuzz_dummy {
123123
return 0;
124124
}
125125
// Pull the original pk out of the siganture
126-
let mut pk_ser = [0; 33];
126+
let mut pk_ser = [0u8; 33];
127127
pk_ser.copy_from_slice(&sig_sl[32..]);
128128
pk_ser.swap(0, 32);
129129
pk_ser[0] += 2;

src/key.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl_display_secret!(SecretKey);
3434
impl str::FromStr for SecretKey {
3535
type Err = Error;
3636
fn from_str(s: &str) -> Result<SecretKey, Error> {
37-
let mut res = [0; constants::SECRET_KEY_SIZE];
37+
let mut res = [0u8; constants::SECRET_KEY_SIZE];
3838
match from_hex(s, &mut res) {
3939
Ok(constants::SECRET_KEY_SIZE) => SecretKey::from_slice(&res),
4040
_ => Err(Error::InvalidSecretKey)
@@ -72,7 +72,7 @@ impl fmt::Display for PublicKey {
7272
impl str::FromStr for PublicKey {
7373
type Err = Error;
7474
fn from_str(s: &str) -> Result<PublicKey, Error> {
75-
let mut res = [0; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
75+
let mut res = [0u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
7676
match from_hex(s, &mut res) {
7777
Ok(constants::PUBLIC_KEY_SIZE) => {
7878
PublicKey::from_slice(
@@ -117,7 +117,7 @@ impl SecretKey {
117117
pub fn from_slice(data: &[u8])-> Result<SecretKey, Error> {
118118
match data.len() {
119119
constants::SECRET_KEY_SIZE => {
120-
let mut ret = [0; constants::SECRET_KEY_SIZE];
120+
let mut ret = [0u8; constants::SECRET_KEY_SIZE];
121121
unsafe {
122122
if ffi::secp256k1_ec_seckey_verify(
123123
ffi::secp256k1_context_no_precomp,
@@ -137,7 +137,7 @@ impl SecretKey {
137137
/// Creates a new secret key using data from BIP-340 [`::schnorrsig::KeyPair`]
138138
#[inline]
139139
pub fn from_keypair(keypair: &::schnorrsig::KeyPair) -> Self {
140-
let mut sk = [0; constants::SECRET_KEY_SIZE];
140+
let mut sk = [0u8; constants::SECRET_KEY_SIZE];
141141
unsafe {
142142
let ret = ffi::secp256k1_keypair_sec(
143143
ffi::secp256k1_context_no_precomp,
@@ -317,7 +317,7 @@ impl PublicKey {
317317
/// the y-coordinate is represented by only a single bit, as x determines
318318
/// it up to one bit.
319319
pub fn serialize(&self) -> [u8; constants::PUBLIC_KEY_SIZE] {
320-
let mut ret = [0; constants::PUBLIC_KEY_SIZE];
320+
let mut ret = [0u8; constants::PUBLIC_KEY_SIZE];
321321

322322
unsafe {
323323
let mut ret_len = constants::PUBLIC_KEY_SIZE as usize;
@@ -336,7 +336,7 @@ impl PublicKey {
336336

337337
/// Serialize the key as a byte-encoded pair of values, in uncompressed form
338338
pub fn serialize_uncompressed(&self) -> [u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE] {
339-
let mut ret = [0; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
339+
let mut ret = [0u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
340340

341341
unsafe {
342342
let mut ret_len = constants::UNCOMPRESSED_PUBLIC_KEY_SIZE as usize;

src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196196
impl str::FromStr for Signature {
197197
type Err = Error;
198198
fn from_str(s: &str) -> Result<Signature, Error> {
199-
let mut res = [0; 72];
199+
let mut res = [0u8; 72];
200200
match from_hex(s, &mut res) {
201201
Ok(x) => Signature::from_der(&res[0..x]),
202202
_ => Err(Error::InvalidSignature),
@@ -398,7 +398,7 @@ impl Signature {
398398
#[inline]
399399
/// Serializes the signature in compact format
400400
pub fn serialize_compact(&self) -> [u8; 64] {
401-
let mut ret = [0; 64];
401+
let mut ret = [0u8; 64];
402402
unsafe {
403403
let err = ffi::secp256k1_ecdsa_signature_serialize_compact(
404404
ffi::secp256k1_context_no_precomp,
@@ -474,7 +474,7 @@ impl Message {
474474
pub fn from_slice(data: &[u8]) -> Result<Message, Error> {
475475
match data.len() {
476476
constants::MESSAGE_SIZE => {
477-
let mut ret = [0; constants::MESSAGE_SIZE];
477+
let mut ret = [0u8; constants::MESSAGE_SIZE];
478478
ret[..].copy_from_slice(data);
479479
Ok(Message(ret))
480480
}
@@ -648,7 +648,7 @@ impl<C: Context> Secp256k1<C> {
648648
/// compilation with "rand" feature.
649649
#[cfg(any(test, feature = "rand"))]
650650
pub fn randomize<R: Rng + ?Sized>(&mut self, rng: &mut R) {
651-
let mut seed = [0; 32];
651+
let mut seed = [0u8; 32];
652652
rng.fill_bytes(&mut seed);
653653
self.seeded_randomize(&seed);
654654
}
@@ -673,7 +673,7 @@ impl<C: Context> Secp256k1<C> {
673673
}
674674

675675
fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {
676-
let mut ser_ret = [0; 72];
676+
let mut ser_ret = [0u8; 72];
677677
let mut len: usize = ser_ret.len();
678678
unsafe {
679679
let err = ffi::secp256k1_ecdsa_signature_serialize_der(
@@ -688,7 +688,7 @@ fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {
688688
}
689689

690690
fn compact_sig_has_zero_first_bit(sig: &ffi::Signature) -> bool {
691-
let mut compact = [0; 64];
691+
let mut compact = [0u8; 64];
692692
unsafe {
693693
let err = ffi::secp256k1_ecdsa_signature_serialize_compact(
694694
ffi::secp256k1_context_no_precomp,
@@ -1030,7 +1030,7 @@ mod tests {
10301030
let mut s = Secp256k1::new();
10311031
s.randomize(&mut thread_rng());
10321032

1033-
let mut msg = [0; 32];
1033+
let mut msg = [0u8; 32];
10341034
for _ in 0..100 {
10351035
thread_rng().fill_bytes(&mut msg);
10361036
let msg = Message::from_slice(&msg).unwrap();
@@ -1116,7 +1116,7 @@ mod tests {
11161116
let mut s = Secp256k1::new();
11171117
s.randomize(&mut thread_rng());
11181118

1119-
let mut msg = [0; 32];
1119+
let mut msg = [0u8; 32];
11201120
for _ in 0..100 {
11211121
thread_rng().fill_bytes(&mut msg);
11221122
let msg = Message::from_slice(&msg).unwrap();
@@ -1149,8 +1149,8 @@ mod tests {
11491149

11501150
// Wild keys: 1, CURVE_ORDER - 1
11511151
// Wild msgs: 1, CURVE_ORDER - 1
1152-
let mut wild_keys = [[0; 32]; 2];
1153-
let mut wild_msgs = [[0; 32]; 2];
1152+
let mut wild_keys = [[0u8; 32]; 2];
1153+
let mut wild_msgs = [[0u8; 32]; 2];
11541154

11551155
wild_keys[0][0] = 1;
11561156
wild_msgs[0][0] = 1;

src/recovery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ mod tests {
239239
fn sign() {
240240
let mut s = Secp256k1::new();
241241
s.randomize(&mut thread_rng());
242-
let one = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
243-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
242+
let one: [u8; 32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
243+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
244244

245245
let sk = SecretKey::from_slice(&one).unwrap();
246246
let msg = Message::from_slice(&one).unwrap();

src/schnorrsig.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl fmt::Display for Signature {
6565
impl str::FromStr for Signature {
6666
type Err = Error;
6767
fn from_str(s: &str) -> Result<Signature, Error> {
68-
let mut res = [0; constants::SCHNORRSIG_SIGNATURE_SIZE];
68+
let mut res = [0u8; constants::SCHNORRSIG_SIGNATURE_SIZE];
6969
match from_hex(s, &mut res) {
7070
Ok(constants::SCHNORRSIG_SIGNATURE_SIZE) => {
7171
Signature::from_slice(&res[0..constants::SCHNORRSIG_SIGNATURE_SIZE])
@@ -103,7 +103,7 @@ impl fmt::Display for PublicKey {
103103
impl str::FromStr for PublicKey {
104104
type Err = Error;
105105
fn from_str(s: &str) -> Result<PublicKey, Error> {
106-
let mut res = [0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];
106+
let mut res = [0u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];
107107
match from_hex(s, &mut res) {
108108
Ok(constants::SCHNORRSIG_PUBLIC_KEY_SIZE) => {
109109
PublicKey::from_slice(&res[0..constants::SCHNORRSIG_PUBLIC_KEY_SIZE])
@@ -119,7 +119,7 @@ impl Signature {
119119
pub fn from_slice(data: &[u8]) -> Result<Signature, Error> {
120120
match data.len() {
121121
constants::SCHNORRSIG_SIGNATURE_SIZE => {
122-
let mut ret = [0; constants::SCHNORRSIG_SIGNATURE_SIZE];
122+
let mut ret = [0u8; constants::SCHNORRSIG_SIGNATURE_SIZE];
123123
ret[..].copy_from_slice(data);
124124
Ok(Signature(ret))
125125
}
@@ -184,7 +184,7 @@ impl KeyPair {
184184
/// Creates a Schnorr KeyPair directly from a secret key string
185185
#[inline]
186186
pub fn from_seckey_str<C: Signing>(secp: &Secp256k1<C>, s: &str) -> Result<KeyPair, Error> {
187-
let mut res = [0; constants::SECRET_KEY_SIZE];
187+
let mut res = [0u8; constants::SECRET_KEY_SIZE];
188188
match from_hex(s, &mut res) {
189189
Ok(constants::SECRET_KEY_SIZE) => {
190190
KeyPair::from_seckey_slice(secp, &res[0..constants::SECRET_KEY_SIZE])
@@ -305,7 +305,7 @@ impl PublicKey {
305305
/// the y-coordinate is represented by only a single bit, as x determines
306306
/// it up to one bit.
307307
pub fn serialize(&self) -> [u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE] {
308-
let mut ret = [0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];
308+
let mut ret = [0u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];
309309

310310
unsafe {
311311
let err = ffi::secp256k1_xonly_pubkey_serialize(
@@ -613,7 +613,7 @@ mod tests {
613613

614614
macro_rules! hex_32 {
615615
($hex:expr) => {{
616-
let mut result = [0; 32];
616+
let mut result = [0u8; 32];
617617
from_hex($hex, &mut result).expect("valid hex string");
618618
result
619619
}};
@@ -626,7 +626,7 @@ mod tests {
626626

627627
let mut rng = thread_rng();
628628
let (seckey, pubkey) = secp.generate_schnorrsig_keypair(&mut rng);
629-
let mut msg = [0; 32];
629+
let mut msg = [0u8; 32];
630630

631631
for _ in 0..100 {
632632
rng.fill_bytes(&mut msg);
@@ -641,7 +641,7 @@ mod tests {
641641
#[test]
642642
fn test_schnorrsig_sign_with_aux_rand_verify() {
643643
test_schnorrsig_sign_helper(|secp, msg, seckey, rng| {
644-
let mut aux_rand = [0; 32];
644+
let mut aux_rand = [0u8; 32];
645645
rng.fill_bytes(&mut aux_rand);
646646
secp.schnorrsig_sign_with_aux_rand(msg, seckey, &aux_rand)
647647
})
@@ -864,7 +864,7 @@ mod tests {
864864

865865
let msg = Message::from_slice(&[1; 32]).unwrap();
866866
let keypair = KeyPair::from_seckey_slice(&s, &[2; 32]).unwrap();
867-
let aux = [3; 32];
867+
let aux = [3u8; 32];
868868
let sig = s
869869
.schnorrsig_sign_with_aux_rand(&msg, &keypair, &aux);
870870
static SIG_BYTES: [u8; constants::SCHNORRSIG_SIGNATURE_SIZE] = [

0 commit comments

Comments
 (0)