From 35b535008871250cbea9ee79864b75166d7a8c03 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 18 Jan 2024 08:42:56 +1100 Subject: [PATCH 1/2] Remove usage of Cursor in multi key read We do not need to know the position of the reader when reading multiple keys, usage of `Cursor` is unnecessary. --- bitcoin/src/crypto/key.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index f5ac68d7..4cc99097 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -1096,16 +1096,15 @@ mod tests { k.write_into(&mut v).expect("writing into vec"); } + let mut reader = v.as_slice(); let mut dec_keys = vec![]; - let mut cursor = io::Cursor::new(&v); for _ in 0..N_KEYS { - dec_keys.push(PublicKey::read_from(&mut cursor).expect("reading from vec")); + dec_keys.push(PublicKey::read_from(&mut reader).expect("reading from vec")); } - assert_eq!(keys, dec_keys); + assert!(PublicKey::read_from(&mut reader).is_err()); // sanity checks - assert!(PublicKey::read_from(&mut cursor).is_err()); assert!(PublicKey::read_from(&mut io::Cursor::new(&[])).is_err()); assert!(PublicKey::read_from(&mut io::Cursor::new(&[0; 33][..])).is_err()); assert!(PublicKey::read_from(&mut io::Cursor::new(&[2; 32][..])).is_err()); From 3ea44a166b013048a5e165c6d46058868dea974a Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 18 Jan 2024 08:45:25 +1100 Subject: [PATCH 2/2] Remove usage of Cursor in pubkey sanity checks We do not need to use `Cursor`, `io::Read` is implemented for slices of `u8`s. --- bitcoin/src/crypto/key.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 4cc99097..eb3af41a 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -1105,11 +1105,12 @@ mod tests { assert!(PublicKey::read_from(&mut reader).is_err()); // sanity checks - assert!(PublicKey::read_from(&mut io::Cursor::new(&[])).is_err()); - assert!(PublicKey::read_from(&mut io::Cursor::new(&[0; 33][..])).is_err()); - assert!(PublicKey::read_from(&mut io::Cursor::new(&[2; 32][..])).is_err()); - assert!(PublicKey::read_from(&mut io::Cursor::new(&[0; 65][..])).is_err()); - assert!(PublicKey::read_from(&mut io::Cursor::new(&[4; 64][..])).is_err()); + let mut empty: &[u8] = &[]; + assert!(PublicKey::read_from(&mut empty).is_err()); + assert!(PublicKey::read_from(&mut &[0; 33][..]).is_err()); + assert!(PublicKey::read_from(&mut &[2; 32][..]).is_err()); + assert!(PublicKey::read_from(&mut &[0; 65][..]).is_err()); + assert!(PublicKey::read_from(&mut &[4; 64][..]).is_err()); } #[test]