2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
2015-04-07 22:51:57 +00:00
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
2014-07-18 13:56:17 +00:00
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-08 21:38:50 +00:00
|
|
|
//! Miscellaneous functions
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
//! Various utility functions
|
|
|
|
|
2019-04-28 05:59:20 +00:00
|
|
|
use bitcoin_hashes::{sha256d, Hash};
|
2014-08-12 04:38:46 +00:00
|
|
|
use blockdata::opcodes;
|
Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
instead
- Move network::serialize::Simple{En,De}coder to
consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
should never fail
2018-09-20 10:15:45 +00:00
|
|
|
use consensus::encode;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2019-04-28 05:59:20 +00:00
|
|
|
static MSG_SIGN_PREFIX: &'static [u8] = b"\x18Bitcoin Signed Message:\n";
|
|
|
|
|
2019-01-04 01:24:18 +00:00
|
|
|
/// Helper function to convert hex nibble characters to their respective value
|
|
|
|
#[inline]
|
|
|
|
fn hex_val(c: u8) -> Result<u8, encode::Error> {
|
|
|
|
let res = match c {
|
|
|
|
b'0' ... b'9' => c - '0' as u8,
|
|
|
|
b'a' ... b'f' => c - 'a' as u8 + 10,
|
|
|
|
b'A' ... b'F' => c - 'A' as u8 + 10,
|
|
|
|
_ => return Err(encode::Error::UnexpectedHexDigit(c as char)),
|
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2014-07-18 13:56:17 +00:00
|
|
|
/// Convert a hexadecimal-encoded string to its corresponding bytes
|
2019-01-04 01:24:18 +00:00
|
|
|
pub fn hex_bytes(data: &str) -> Result<Vec<u8>, encode::Error> {
|
|
|
|
// This code is optimized to be as fast as possible without using unsafe or platform specific
|
|
|
|
// features. If you want to refactor it please make sure you don't introduce performance
|
2019-01-07 21:43:23 +00:00
|
|
|
// regressions (run the benchmark with `cargo bench --features unstable`).
|
2019-01-04 01:24:18 +00:00
|
|
|
|
|
|
|
// If the hex string has an uneven length fail early
|
|
|
|
if data.len() % 2 != 0 {
|
|
|
|
return Err(encode::Error::ParseFailed("hexstring of odd length"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preallocate the uninitialized memory for the byte array
|
|
|
|
let mut res = Vec::with_capacity(data.len() / 2);
|
|
|
|
|
|
|
|
let mut hex_it = data.bytes();
|
|
|
|
loop {
|
|
|
|
// Get most significant nibble of current byte or end iteration
|
|
|
|
let msn = match hex_it.next() {
|
|
|
|
None => break,
|
|
|
|
Some(x) => x,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get least significant nibble of current byte
|
|
|
|
let lsn = match hex_it.next() {
|
|
|
|
None => unreachable!("len % 2 == 0"),
|
|
|
|
Some(x) => x,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert bytes representing characters to their represented value and combine lsn and msn.
|
2019-01-23 18:30:32 +00:00
|
|
|
// The and_then and map are crucial for performance, in comparison to using ? and then
|
2019-01-04 01:24:18 +00:00
|
|
|
// using the results of that for the calculation it's nearly twice as fast. Using bit
|
|
|
|
// shifting and or instead of multiply and add on the other hand doesn't show a significant
|
|
|
|
// increase in performance.
|
|
|
|
match hex_val(msn).and_then(|msn_val| hex_val(lsn).map(|lsn_val| msn_val * 16 + lsn_val)) {
|
|
|
|
Ok(x) => res.push(x),
|
|
|
|
Err(e) => return Err(e),
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
2019-01-04 01:24:18 +00:00
|
|
|
Ok(res)
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-10 19:58:15 +00:00
|
|
|
/// Search for `needle` in the vector `haystack` and remove every
|
|
|
|
/// instance of it, returning the number of instances removed.
|
2014-08-12 04:38:46 +00:00
|
|
|
/// Loops through the vector opcode by opcode, skipping pushed data.
|
2015-01-18 18:16:01 +00:00
|
|
|
pub fn script_find_and_remove(haystack: &mut Vec<u8>, needle: &[u8]) -> usize {
|
2015-04-07 22:51:57 +00:00
|
|
|
if needle.len() > haystack.len() { return 0; }
|
|
|
|
if needle.len() == 0 { return 0; }
|
|
|
|
|
|
|
|
let mut top = haystack.len() - needle.len();
|
|
|
|
let mut n_deleted = 0;
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
while i <= top {
|
|
|
|
if &haystack[i..(i + needle.len())] == needle {
|
|
|
|
for j in i..top {
|
2015-04-10 23:15:57 +00:00
|
|
|
haystack.swap(j + needle.len(), j);
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
|
|
|
n_deleted += 1;
|
|
|
|
// This is ugly but prevents infinite loop in case of overflow
|
|
|
|
let overflow = top < needle.len();
|
2015-05-10 19:32:26 +00:00
|
|
|
top = top.wrapping_sub(needle.len());
|
2015-04-07 22:51:57 +00:00
|
|
|
if overflow { break; }
|
|
|
|
} else {
|
2015-10-14 13:56:48 +00:00
|
|
|
i += match opcodes::All::from((*haystack)[i]).classify() {
|
2015-04-07 22:51:57 +00:00
|
|
|
opcodes::Class::PushBytes(n) => n as usize + 1,
|
|
|
|
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA1) => 2,
|
|
|
|
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA2) => 3,
|
|
|
|
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA4) => 5,
|
|
|
|
_ => 1
|
|
|
|
};
|
|
|
|
}
|
2014-08-10 19:58:15 +00:00
|
|
|
}
|
2015-05-10 19:32:26 +00:00
|
|
|
haystack.truncate(top.wrapping_add(needle.len()));
|
2015-04-07 22:51:57 +00:00
|
|
|
n_deleted
|
2014-08-10 19:58:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-28 05:59:20 +00:00
|
|
|
/// Hash message for signature using Bitcoin's message signing format
|
|
|
|
pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
|
|
|
|
sha256d::Hash::hash(
|
|
|
|
&[
|
|
|
|
MSG_SIGN_PREFIX,
|
|
|
|
&encode::serialize(&encode::VarInt(msg.len() as u64)),
|
|
|
|
msg.as_bytes(),
|
|
|
|
]
|
|
|
|
.concat(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-07 21:43:23 +00:00
|
|
|
#[cfg(all(test, feature="unstable"))]
|
|
|
|
mod benches {
|
|
|
|
use rand::{Rng, thread_rng};
|
|
|
|
use super::hex_bytes;
|
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
fn join<I: Iterator<Item=IT>, IT: AsRef<str>>(iter: I, expected_len: usize) -> String {
|
|
|
|
let mut res = String::with_capacity(expected_len);
|
|
|
|
for s in iter {
|
|
|
|
res.push_str(s.as_ref());
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bench_from_hex(b: &mut Bencher, data_size: usize) {
|
|
|
|
let data_bytes = thread_rng()
|
|
|
|
.gen_iter()
|
|
|
|
.take(data_size)
|
|
|
|
.collect::<Vec<u8>>();
|
|
|
|
let data = join(data_bytes.iter().map(|x| format!("{:02x}", x)), data_size * 2);
|
|
|
|
|
|
|
|
assert_eq!(hex_bytes(&data).unwrap(), data_bytes);
|
|
|
|
|
2019-01-15 16:39:41 +00:00
|
|
|
b.iter(|| {
|
2019-01-07 21:43:23 +00:00
|
|
|
hex_bytes(&data).unwrap()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn from_hex_16_bytes(b: &mut Bencher) {
|
|
|
|
bench_from_hex(b, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn from_hex_64_bytes(b: &mut Bencher) {
|
|
|
|
bench_from_hex(b, 64);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn from_hex_256_bytes(b: &mut Bencher) {
|
|
|
|
bench_from_hex(b, 256);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn from_hex_4m_bytes(b: &mut Bencher) {
|
|
|
|
bench_from_hex(b, 1024 * 1024 * 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 13:56:17 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-04-28 05:59:20 +00:00
|
|
|
use bitcoin_hashes::hex::ToHex;
|
2015-04-07 22:51:57 +00:00
|
|
|
use super::script_find_and_remove;
|
|
|
|
use super::hex_bytes;
|
2019-04-28 05:59:20 +00:00
|
|
|
use super::signed_msg_hash;
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_script_find_and_remove() {
|
|
|
|
let mut v = vec![101u8, 102, 103, 104, 102, 103, 104, 102, 103, 104, 105, 106, 107, 108, 109];
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[]), 0);
|
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 105]), 0);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103, 104, 105, 106, 107, 108, 109]);
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[105, 106, 107]), 1);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103, 104, 108, 109]);
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[104, 108, 109]), 1);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert_eq!(v, vec![101, 102, 103, 104, 102, 103, 104, 102, 103]);
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[101]), 1);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert_eq!(v, vec![102, 103, 104, 102, 103, 104, 102, 103]);
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[102]), 3);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert_eq!(v, vec![103, 104, 103, 104, 103]);
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[103, 104]), 2);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert_eq!(v, vec![103]);
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 5]), 0);
|
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[105]), 0);
|
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[103]), 1);
|
2019-04-12 18:06:06 +00:00
|
|
|
assert_eq!(v, Vec::<u8>::new());
|
2014-08-10 19:58:15 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[105, 105, 5]), 0);
|
|
|
|
assert_eq!(script_find_and_remove(&mut v, &[105]), 0);
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_script_codesep_remove() {
|
|
|
|
let mut s = vec![33u8, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 171, 33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 171, 81];
|
2015-04-08 22:23:45 +00:00
|
|
|
assert_eq!(script_find_and_remove(&mut s, &[171]), 2);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert_eq!(s, vec![33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 33, 3, 132, 121, 160, 250, 153, 140, 211, 82, 89, 162, 239, 10, 122, 92, 104, 102, 44, 20, 116, 248, 140, 203, 109, 8, 167, 103, 123, 190, 199, 242, 32, 65, 173, 81]);
|
|
|
|
}
|
2014-08-13 03:56:16 +00:00
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_hex_bytes() {
|
|
|
|
assert_eq!(&hex_bytes("abcd").unwrap(), &[171u8, 205]);
|
|
|
|
assert!(hex_bytes("abcde").is_err());
|
|
|
|
assert!(hex_bytes("aBcDeF").is_ok());
|
|
|
|
assert!(hex_bytes("aBcD4eFL").is_err());
|
|
|
|
}
|
2019-04-28 05:59:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signed_msg_hash() {
|
|
|
|
let hash = signed_msg_hash("test");
|
|
|
|
assert_eq!(hash.to_hex(), "a6f87fe6d58a032c320ff8d1541656f0282c2c7bfcc69d61af4c8e8ed528e49c");
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|