Merge rust-bitcoin/rust-secp256k1#334: Use explicit u8 when assigning a byte slice

24d6f62603 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 24d6f62603

Tree-SHA512: f7796dcc3ae240983257bef0f25bd0df741943f75d86e9bca7c45076af179d96ce213bd9c339a01f721f7dc9b96a0a4a56ef2cf44339f4c91d208103b7659d9f
This commit is contained in:
Andrew Poelstra 2021-11-02 18:20:38 +00:00
commit 6a774bd47c
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
6 changed files with 30 additions and 30 deletions

View File

@ -932,7 +932,7 @@ mod fuzz_dummy {
let scalar_slice = slice::from_raw_parts(scalar, 32); let scalar_slice = slice::from_raw_parts(scalar, 32);
let pk_slice = &(*point).0[..32]; let pk_slice = &(*point).0[..32];
let mut res_arr = [0; 32]; let mut res_arr = [0u8; 32];
for i in 0..32 { for i in 0..32 {
res_arr[i] = scalar_slice[i] ^ pk_slice[i] ^ 1; res_arr[i] = scalar_slice[i] ^ pk_slice[i] ^ 1;
} }
@ -1123,7 +1123,7 @@ mod fuzz_dummy {
check_context_flags(cx, SECP256K1_START_VERIFY); check_context_flags(cx, SECP256K1_START_VERIFY);
let mut pk = PublicKey::new(); let mut pk = PublicKey::new();
pk.0.copy_from_slice(&(*keypair).0[32..]); pk.0.copy_from_slice(&(*keypair).0[32..]);
let mut sk = [0; 32]; let mut sk = [0u8; 32];
sk.copy_from_slice(&(*keypair).0[..32]); sk.copy_from_slice(&(*keypair).0[..32]);
assert_eq!(secp256k1_ec_pubkey_tweak_add(cx, &mut pk, tweak32), 1); assert_eq!(secp256k1_ec_pubkey_tweak_add(cx, &mut pk, tweak32), 1);
assert_eq!(secp256k1_ec_seckey_tweak_add(cx, (&mut sk[..]).as_mut_ptr(), tweak32), 1); assert_eq!(secp256k1_ec_seckey_tweak_add(cx, (&mut sk[..]).as_mut_ptr(), tweak32), 1);

View File

@ -123,7 +123,7 @@ mod fuzz_dummy {
return 0; return 0;
} }
// Pull the original pk out of the siganture // Pull the original pk out of the siganture
let mut pk_ser = [0; 33]; let mut pk_ser = [0u8; 33];
pk_ser.copy_from_slice(&sig_sl[32..]); pk_ser.copy_from_slice(&sig_sl[32..]);
pk_ser.swap(0, 32); pk_ser.swap(0, 32);
pk_ser[0] += 2; pk_ser[0] += 2;

View File

@ -34,7 +34,7 @@ impl_display_secret!(SecretKey);
impl str::FromStr for SecretKey { impl str::FromStr for SecretKey {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<SecretKey, Error> { fn from_str(s: &str) -> Result<SecretKey, Error> {
let mut res = [0; constants::SECRET_KEY_SIZE]; let mut res = [0u8; constants::SECRET_KEY_SIZE];
match from_hex(s, &mut res) { match from_hex(s, &mut res) {
Ok(constants::SECRET_KEY_SIZE) => SecretKey::from_slice(&res), Ok(constants::SECRET_KEY_SIZE) => SecretKey::from_slice(&res),
_ => Err(Error::InvalidSecretKey) _ => Err(Error::InvalidSecretKey)
@ -72,7 +72,7 @@ impl fmt::Display for PublicKey {
impl str::FromStr for PublicKey { impl str::FromStr for PublicKey {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<PublicKey, Error> { fn from_str(s: &str) -> Result<PublicKey, Error> {
let mut res = [0; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE]; let mut res = [0u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
match from_hex(s, &mut res) { match from_hex(s, &mut res) {
Ok(constants::PUBLIC_KEY_SIZE) => { Ok(constants::PUBLIC_KEY_SIZE) => {
PublicKey::from_slice( PublicKey::from_slice(
@ -117,7 +117,7 @@ impl SecretKey {
pub fn from_slice(data: &[u8])-> Result<SecretKey, Error> { pub fn from_slice(data: &[u8])-> Result<SecretKey, Error> {
match data.len() { match data.len() {
constants::SECRET_KEY_SIZE => { constants::SECRET_KEY_SIZE => {
let mut ret = [0; constants::SECRET_KEY_SIZE]; let mut ret = [0u8; constants::SECRET_KEY_SIZE];
unsafe { unsafe {
if ffi::secp256k1_ec_seckey_verify( if ffi::secp256k1_ec_seckey_verify(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,
@ -137,7 +137,7 @@ impl SecretKey {
/// Creates a new secret key using data from BIP-340 [`::schnorrsig::KeyPair`] /// Creates a new secret key using data from BIP-340 [`::schnorrsig::KeyPair`]
#[inline] #[inline]
pub fn from_keypair(keypair: &::schnorrsig::KeyPair) -> Self { pub fn from_keypair(keypair: &::schnorrsig::KeyPair) -> Self {
let mut sk = [0; constants::SECRET_KEY_SIZE]; let mut sk = [0u8; constants::SECRET_KEY_SIZE];
unsafe { unsafe {
let ret = ffi::secp256k1_keypair_sec( let ret = ffi::secp256k1_keypair_sec(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,
@ -317,7 +317,7 @@ impl PublicKey {
/// the y-coordinate is represented by only a single bit, as x determines /// the y-coordinate is represented by only a single bit, as x determines
/// it up to one bit. /// it up to one bit.
pub fn serialize(&self) -> [u8; constants::PUBLIC_KEY_SIZE] { pub fn serialize(&self) -> [u8; constants::PUBLIC_KEY_SIZE] {
let mut ret = [0; constants::PUBLIC_KEY_SIZE]; let mut ret = [0u8; constants::PUBLIC_KEY_SIZE];
unsafe { unsafe {
let mut ret_len = constants::PUBLIC_KEY_SIZE as usize; let mut ret_len = constants::PUBLIC_KEY_SIZE as usize;
@ -336,7 +336,7 @@ impl PublicKey {
/// Serialize the key as a byte-encoded pair of values, in uncompressed form /// Serialize the key as a byte-encoded pair of values, in uncompressed form
pub fn serialize_uncompressed(&self) -> [u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE] { pub fn serialize_uncompressed(&self) -> [u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE] {
let mut ret = [0; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE]; let mut ret = [0u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
unsafe { unsafe {
let mut ret_len = constants::UNCOMPRESSED_PUBLIC_KEY_SIZE as usize; let mut ret_len = constants::UNCOMPRESSED_PUBLIC_KEY_SIZE as usize;

View File

@ -196,7 +196,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl str::FromStr for Signature { impl str::FromStr for Signature {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<Signature, Error> { fn from_str(s: &str) -> Result<Signature, Error> {
let mut res = [0; 72]; let mut res = [0u8; 72];
match from_hex(s, &mut res) { match from_hex(s, &mut res) {
Ok(x) => Signature::from_der(&res[0..x]), Ok(x) => Signature::from_der(&res[0..x]),
_ => Err(Error::InvalidSignature), _ => Err(Error::InvalidSignature),
@ -398,7 +398,7 @@ impl Signature {
#[inline] #[inline]
/// Serializes the signature in compact format /// Serializes the signature in compact format
pub fn serialize_compact(&self) -> [u8; 64] { pub fn serialize_compact(&self) -> [u8; 64] {
let mut ret = [0; 64]; let mut ret = [0u8; 64];
unsafe { unsafe {
let err = ffi::secp256k1_ecdsa_signature_serialize_compact( let err = ffi::secp256k1_ecdsa_signature_serialize_compact(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,
@ -474,7 +474,7 @@ impl Message {
pub fn from_slice(data: &[u8]) -> Result<Message, Error> { pub fn from_slice(data: &[u8]) -> Result<Message, Error> {
match data.len() { match data.len() {
constants::MESSAGE_SIZE => { constants::MESSAGE_SIZE => {
let mut ret = [0; constants::MESSAGE_SIZE]; let mut ret = [0u8; constants::MESSAGE_SIZE];
ret[..].copy_from_slice(data); ret[..].copy_from_slice(data);
Ok(Message(ret)) Ok(Message(ret))
} }
@ -648,7 +648,7 @@ impl<C: Context> Secp256k1<C> {
/// compilation with "rand" feature. /// compilation with "rand" feature.
#[cfg(any(test, feature = "rand"))] #[cfg(any(test, feature = "rand"))]
pub fn randomize<R: Rng + ?Sized>(&mut self, rng: &mut R) { pub fn randomize<R: Rng + ?Sized>(&mut self, rng: &mut R) {
let mut seed = [0; 32]; let mut seed = [0u8; 32];
rng.fill_bytes(&mut seed); rng.fill_bytes(&mut seed);
self.seeded_randomize(&seed); self.seeded_randomize(&seed);
} }
@ -673,7 +673,7 @@ impl<C: Context> Secp256k1<C> {
} }
fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool { fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {
let mut ser_ret = [0; 72]; let mut ser_ret = [0u8; 72];
let mut len: usize = ser_ret.len(); let mut len: usize = ser_ret.len();
unsafe { unsafe {
let err = ffi::secp256k1_ecdsa_signature_serialize_der( let err = ffi::secp256k1_ecdsa_signature_serialize_der(
@ -688,7 +688,7 @@ fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {
} }
fn compact_sig_has_zero_first_bit(sig: &ffi::Signature) -> bool { fn compact_sig_has_zero_first_bit(sig: &ffi::Signature) -> bool {
let mut compact = [0; 64]; let mut compact = [0u8; 64];
unsafe { unsafe {
let err = ffi::secp256k1_ecdsa_signature_serialize_compact( let err = ffi::secp256k1_ecdsa_signature_serialize_compact(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,
@ -1030,7 +1030,7 @@ mod tests {
let mut s = Secp256k1::new(); let mut s = Secp256k1::new();
s.randomize(&mut thread_rng()); s.randomize(&mut thread_rng());
let mut msg = [0; 32]; let mut msg = [0u8; 32];
for _ in 0..100 { for _ in 0..100 {
thread_rng().fill_bytes(&mut msg); thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap(); let msg = Message::from_slice(&msg).unwrap();
@ -1116,7 +1116,7 @@ mod tests {
let mut s = Secp256k1::new(); let mut s = Secp256k1::new();
s.randomize(&mut thread_rng()); s.randomize(&mut thread_rng());
let mut msg = [0; 32]; let mut msg = [0u8; 32];
for _ in 0..100 { for _ in 0..100 {
thread_rng().fill_bytes(&mut msg); thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap(); let msg = Message::from_slice(&msg).unwrap();
@ -1149,8 +1149,8 @@ mod tests {
// Wild keys: 1, CURVE_ORDER - 1 // Wild keys: 1, CURVE_ORDER - 1
// Wild msgs: 1, CURVE_ORDER - 1 // Wild msgs: 1, CURVE_ORDER - 1
let mut wild_keys = [[0; 32]; 2]; let mut wild_keys = [[0u8; 32]; 2];
let mut wild_msgs = [[0; 32]; 2]; let mut wild_msgs = [[0u8; 32]; 2];
wild_keys[0][0] = 1; wild_keys[0][0] = 1;
wild_msgs[0][0] = 1; wild_msgs[0][0] = 1;

View File

@ -239,7 +239,7 @@ 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 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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]; 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();

View File

@ -65,7 +65,7 @@ impl fmt::Display for Signature {
impl str::FromStr for Signature { impl str::FromStr for Signature {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<Signature, Error> { fn from_str(s: &str) -> Result<Signature, Error> {
let mut res = [0; constants::SCHNORRSIG_SIGNATURE_SIZE]; let mut res = [0u8; constants::SCHNORRSIG_SIGNATURE_SIZE];
match from_hex(s, &mut res) { match from_hex(s, &mut res) {
Ok(constants::SCHNORRSIG_SIGNATURE_SIZE) => { Ok(constants::SCHNORRSIG_SIGNATURE_SIZE) => {
Signature::from_slice(&res[0..constants::SCHNORRSIG_SIGNATURE_SIZE]) Signature::from_slice(&res[0..constants::SCHNORRSIG_SIGNATURE_SIZE])
@ -103,7 +103,7 @@ impl fmt::Display for PublicKey {
impl str::FromStr for PublicKey { impl str::FromStr for PublicKey {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<PublicKey, Error> { fn from_str(s: &str) -> Result<PublicKey, Error> {
let mut res = [0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]; let mut res = [0u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];
match from_hex(s, &mut res) { match from_hex(s, &mut res) {
Ok(constants::SCHNORRSIG_PUBLIC_KEY_SIZE) => { Ok(constants::SCHNORRSIG_PUBLIC_KEY_SIZE) => {
PublicKey::from_slice(&res[0..constants::SCHNORRSIG_PUBLIC_KEY_SIZE]) PublicKey::from_slice(&res[0..constants::SCHNORRSIG_PUBLIC_KEY_SIZE])
@ -119,7 +119,7 @@ impl Signature {
pub fn from_slice(data: &[u8]) -> Result<Signature, Error> { pub fn from_slice(data: &[u8]) -> Result<Signature, Error> {
match data.len() { match data.len() {
constants::SCHNORRSIG_SIGNATURE_SIZE => { constants::SCHNORRSIG_SIGNATURE_SIZE => {
let mut ret = [0; constants::SCHNORRSIG_SIGNATURE_SIZE]; let mut ret = [0u8; constants::SCHNORRSIG_SIGNATURE_SIZE];
ret[..].copy_from_slice(data); ret[..].copy_from_slice(data);
Ok(Signature(ret)) Ok(Signature(ret))
} }
@ -184,7 +184,7 @@ impl KeyPair {
/// Creates a Schnorr KeyPair directly from a secret key string /// Creates a Schnorr KeyPair directly from a secret key string
#[inline] #[inline]
pub fn from_seckey_str<C: Signing>(secp: &Secp256k1<C>, s: &str) -> Result<KeyPair, Error> { pub fn from_seckey_str<C: Signing>(secp: &Secp256k1<C>, s: &str) -> Result<KeyPair, Error> {
let mut res = [0; constants::SECRET_KEY_SIZE]; let mut res = [0u8; constants::SECRET_KEY_SIZE];
match from_hex(s, &mut res) { match from_hex(s, &mut res) {
Ok(constants::SECRET_KEY_SIZE) => { Ok(constants::SECRET_KEY_SIZE) => {
KeyPair::from_seckey_slice(secp, &res[0..constants::SECRET_KEY_SIZE]) KeyPair::from_seckey_slice(secp, &res[0..constants::SECRET_KEY_SIZE])
@ -305,7 +305,7 @@ impl PublicKey {
/// the y-coordinate is represented by only a single bit, as x determines /// the y-coordinate is represented by only a single bit, as x determines
/// it up to one bit. /// it up to one bit.
pub fn serialize(&self) -> [u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE] { pub fn serialize(&self) -> [u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE] {
let mut ret = [0; constants::SCHNORRSIG_PUBLIC_KEY_SIZE]; let mut ret = [0u8; constants::SCHNORRSIG_PUBLIC_KEY_SIZE];
unsafe { unsafe {
let err = ffi::secp256k1_xonly_pubkey_serialize( let err = ffi::secp256k1_xonly_pubkey_serialize(
@ -613,7 +613,7 @@ mod tests {
macro_rules! hex_32 { macro_rules! hex_32 {
($hex:expr) => {{ ($hex:expr) => {{
let mut result = [0; 32]; let mut result = [0u8; 32];
from_hex($hex, &mut result).expect("valid hex string"); from_hex($hex, &mut result).expect("valid hex string");
result result
}}; }};
@ -626,7 +626,7 @@ mod tests {
let mut rng = thread_rng(); let mut rng = thread_rng();
let (seckey, pubkey) = secp.generate_schnorrsig_keypair(&mut rng); let (seckey, pubkey) = secp.generate_schnorrsig_keypair(&mut rng);
let mut msg = [0; 32]; let mut msg = [0u8; 32];
for _ in 0..100 { for _ in 0..100 {
rng.fill_bytes(&mut msg); rng.fill_bytes(&mut msg);
@ -641,7 +641,7 @@ mod tests {
#[test] #[test]
fn test_schnorrsig_sign_with_aux_rand_verify() { fn test_schnorrsig_sign_with_aux_rand_verify() {
test_schnorrsig_sign_helper(|secp, msg, seckey, rng| { test_schnorrsig_sign_helper(|secp, msg, seckey, rng| {
let mut aux_rand = [0; 32]; let mut aux_rand = [0u8; 32];
rng.fill_bytes(&mut aux_rand); rng.fill_bytes(&mut aux_rand);
secp.schnorrsig_sign_with_aux_rand(msg, seckey, &aux_rand) secp.schnorrsig_sign_with_aux_rand(msg, seckey, &aux_rand)
}) })
@ -864,7 +864,7 @@ mod tests {
let msg = Message::from_slice(&[1; 32]).unwrap(); let msg = Message::from_slice(&[1; 32]).unwrap();
let keypair = KeyPair::from_seckey_slice(&s, &[2; 32]).unwrap(); let keypair = KeyPair::from_seckey_slice(&s, &[2; 32]).unwrap();
let aux = [3; 32]; let aux = [3u8; 32];
let sig = s let sig = s
.schnorrsig_sign_with_aux_rand(&msg, &keypair, &aux); .schnorrsig_sign_with_aux_rand(&msg, &keypair, &aux);
static SIG_BYTES: [u8; constants::SCHNORRSIG_SIGNATURE_SIZE] = [ static SIG_BYTES: [u8; constants::SCHNORRSIG_SIGNATURE_SIZE] = [