rust-bitcoin-unsafe-fast/src/util/mod.rs

114 lines
2.7 KiB
Rust
Raw Normal View History

// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
// SPDX-License-Identifier: CC0-1.0
2014-07-18 13:56:17 +00:00
//! Utility functions.
//!
//! Functions needed by all parts of the Bitcoin library.
2014-07-18 13:56:17 +00:00
//!
2022-01-09 06:46:24 +00:00
pub mod key;
2022-01-09 06:50:02 +00:00
pub mod ecdsa;
pub mod schnorr;
pub mod address;
2018-11-10 00:45:00 +00:00
pub mod amount;
2014-08-25 06:03:47 +00:00
pub mod base58;
2016-06-24 19:15:57 +00:00
pub mod bip32;
pub mod bip143;
pub mod bip152;
2014-07-18 13:56:17 +00:00
pub mod hash;
pub mod merkleblock;
2014-07-18 13:56:17 +00:00
pub mod misc;
pub mod psbt;
2019-05-14 17:07:53 +00:00
pub mod taproot;
2014-07-18 13:56:17 +00:00
pub mod uint;
pub mod bip158;
pub mod sighash;
2014-07-18 13:56:17 +00:00
pub(crate) mod endian;
use crate::prelude::*;
use crate::io;
2021-06-09 10:40:41 +00:00
use core::fmt;
use crate::consensus::encode;
use crate::internal_macros::write_err;
2014-07-18 13:56:17 +00:00
/// A trait which allows numbers to act as fixed-size bit arrays
pub trait BitArray {
/// Is bit set?
fn bit(&self, idx: usize) -> bool;
/// Returns an array which is just the bits from start to end
fn bit_slice(&self, start: usize, end: usize) -> Self;
/// Bitwise and with `n` ones
fn mask(&self, n: usize) -> Self;
2014-07-18 13:56:17 +00:00
/// Trailing zeros
fn trailing_zeros(&self) -> usize;
2018-02-18 15:21:05 +00:00
/// Create all-zeros value
fn zero() -> Self;
2019-01-23 18:30:32 +00:00
/// Create value representing one
2018-02-18 15:21:05 +00:00
fn one() -> Self;
}
2014-07-18 13:56:17 +00:00
/// A general error code, other errors should implement conversions to/from this
/// if appropriate.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Encoding error
Encode(encode::Error),
/// The header hash is not below the target
2019-06-07 13:12:07 +00:00
BlockBadProofOfWork,
/// The `target` field of a block header did not match the expected difficulty
2019-06-07 13:12:07 +00:00
BlockBadTarget,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Encode(ref e) => write_err!(f, "encoding error"; e),
2020-03-29 13:49:48 +00:00
Error::BlockBadProofOfWork => f.write_str("block target correct but not attained"),
Error::BlockBadTarget => f.write_str("block target incorrect"),
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::Error::*;
match self {
Encode(e) => Some(e),
BlockBadProofOfWork | BlockBadTarget => None
}
}
}
2014-07-18 13:56:17 +00:00
#[doc(hidden)]
impl From<encode::Error> for Error {
fn from(e: encode::Error) -> Error {
Error::Encode(e)
2018-03-09 20:27:13 +00:00
}
}
// core2 doesn't have read_to_end
pub(crate) fn read_to_end<D: io::Read>(mut d: D) -> Result<Vec<u8>, io::Error> {
let mut result = vec![];
let mut buf = [0u8; 64];
loop {
match d.read(&mut buf) {
Ok(0) => break,
Ok(n) => result.extend_from_slice(&buf[0..n]),
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {},
Err(e) => return Err(e),
};
}
Ok(result)
}