Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
rust:
- nightly
steps:
- name: Checkout Crate
- name: Checkout Crate
uses: actions/checkout@v2
- name: Checkout Toolchain
uses: actions-rs/toolchain@v1
Expand All @@ -29,7 +29,7 @@ jobs:
DO_BENCH: true
run: ./contrib/test.sh

wasm:
wasm:
name: Stable - Docs / WebAssembly Build
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -63,6 +63,7 @@ jobs:
- 1.29.0
- beta
- stable
fail-fast: false
steps:
- name: Checkout Crate
uses: actions/checkout@v2
Expand All @@ -78,7 +79,7 @@ jobs:
- name: Running cargo
env:
DO_FEATURE_MATRIX: true
DO_SCHEMARS_TESTS: ${{matrix.rust != '1.29.0'}}
ON_1_29_0: ${{matrix.rust == '1.29.0'}}
run: ./contrib/test.sh

Embedded:
Expand All @@ -102,4 +103,3 @@ jobs:
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
run: cd embedded && cargo run --target thumbv7m-none-eabi


3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ default = [ "std" ]
std = []
serde-std = ["serde/std"]
unstable = [] # for benchmarking
use-core2 = ["core2"]
use-core2-std = ["use-core2", "core2/std"]

[dependencies]
serde = { version = "1.0", default-features = false, optional = true }
schemars = { version = "0.8.0", optional = true }
core2 = { version="0.3.0-alpha.1", optional= true, default-features = false }

[dev-dependencies]
serde_test = "1.0"
Expand Down
56 changes: 24 additions & 32 deletions contrib/test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/sh -ex
#!/bin/bash -ex

FEATURES="serde serde-std"
# Combination of features to test
# note std has a comma in the end so that following regex avoid matching serde-std
FEATURES=("" "std," "std,serde" "serde" "use-core2,serde" "use-core2" "use-core2-std" "std,use-core2" "std,serde-std" "use-core2,serde-std")

# Use toolchain if explicitly specified
if [ -n "$TOOLCHAIN" ]
then
if [[ -n "$TOOLCHAIN" ]]; then
alias cargo="cargo +$TOOLCHAIN"
fi

Expand All @@ -18,35 +19,27 @@ export CARGO_TERM_VERBOSE=true
cargo build --all
cargo test --all

if [ "$DO_FEATURE_MATRIX" = true ]; then
cargo build --all --no-default-features
cargo test --all --no-default-features

# All features
cargo build --all --no-default-features --features="$FEATURES"
cargo test --all --features="$FEATURES"
# Single features
for feature in ${FEATURES}
do
cargo build --all --no-default-features --features="$feature"
cargo test --all --features="$feature"
done

# Other combos
cargo test --all --features="serde-std"
if [[ "$DO_FEATURE_MATRIX" = true ]]; then
for feature in "${FEATURES[@]}"
do
# On rust 1.29.0 we are only testing with std lib and without use-core2
if [[ "$ON_1_29_0" = false || (${feature} =~ "std," && ! ${feature} =~ "use-core2") ]]; then
echo "--------------$feature----------------"
cargo build --no-default-features --features="$feature"
if [[ ${feature} =~ "std," ]] ; then
cargo test --no-default-features --features="$feature"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we only test with std and just build the rest of the time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because tests have std enabled

#![cfg_attr(all(not(test), not(feature = "std")), no_std)]

fi
cargo doc --no-default-features --features="$feature"
fi
done
fi

if [ "$DO_SCHEMARS_TESTS" = true ]; then
if [[ "$ON_1_29_0" = false ]]; then
(cd extended_tests/schemars && cargo test)
fi

# Docs
if [ "$DO_DOCS" = true ]; then
cargo doc --all --features="$FEATURES"
fi

# Webassembly stuff
if [ "$DO_WASM" = true ]; then
if [[ "$DO_WASM" = true ]]; then
clang --version &&
CARGO_TARGET_DIR=wasm cargo install --force wasm-pack &&
printf '\n[lib]\ncrate-type = ["cdylib", "rlib"]\n' >> Cargo.toml &&
Expand All @@ -55,20 +48,19 @@ if [ "$DO_WASM" = true ]; then
fi

# Address Sanitizer
if [ "$DO_ASAN" = true ]; then
if [[ "$DO_ASAN" = true ]]; then
cargo clean
CC='clang -fsanitize=address -fno-omit-frame-pointer' \
RUSTFLAGS='-Zsanitizer=address -Clinker=clang -Cforce-frame-pointers=yes' \
ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' \
cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu
cargo test --lib --all --all-features -Zbuild-std --target x86_64-unknown-linux-gnu
cargo clean
CC='clang -fsanitize=memory -fno-omit-frame-pointer' \
RUSTFLAGS='-Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes' \
cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be --all-features here and above, waiting to find a solution for defaulting to core2 if enabled (without conflicting with std)

cargo test --lib --all --all-features -Zbuild-std --target x86_64-unknown-linux-gnu
fi

# Bench
if [ "$DO_BENCH" = true ]; then
if [[ "$DO_BENCH" = true ]]; then
cargo bench --all --features="unstable"
fi

5 changes: 3 additions & 2 deletions embedded/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ extern crate bitcoin_hashes;
extern crate alloc;

use alloc_cortex_m::CortexMHeap;
use bitcoin_hashes::{sha256, Hash, HashEngine};
use bitcoin_hashes::{sha256, Hash};
use bitcoin_hashes::literacy::Write;
use core::alloc::Layout;
use core::str::FromStr;
use cortex_m::asm;
Expand All @@ -29,7 +30,7 @@ fn main() -> ! {
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }

let mut engine = TestType::engine();
engine.input(b"abc");
engine.write(b"abc").unwrap();
let hash = TestType::from_engine(engine);

let hash_check =
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use core::fmt;

/// [bitcoin_hashes] error.
/// This crate error.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Error {
/// Tried to create a fixed-length hash from a slice with the wrong size (expected, got).
Expand Down
96 changes: 47 additions & 49 deletions src/std_impls.rs → src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,70 @@
//! `std` Impls
//!
//! impls of traits defined in `std` and not `core`
//!
//! Note engines implementation cannot error, however, we use [crate::literacy::Error] type for
//! more ergonomic use inside other methods returning errors.

use std::{error, io};

use {hex, sha1, sha256, sha512, ripemd160, siphash24};
use HashEngine;
use Error;
use {sha1, sha256, sha512, ripemd160, siphash24};
use ::{HashEngine, literacy};

impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> { None }
fn description(&self) -> &str { "`std::error::description` is deprecated" }
}
macro_rules! write_impl {
($HashEngine: ty) => {
impl literacy::Write for $HashEngine {
type Error = literacy::Error;

impl error::Error for hex::Error {
fn cause(&self) -> Option<&error::Error> { None }
fn description(&self) -> &str { "`std::error::description` is deprecated" }
}
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, Self::Error> {
self.input(buf);
Ok(buf.len())
}

impl io::Write for sha1::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), Self::Error> {
self.write(buf)?;
Ok(())
}

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.input(buf);
Ok(buf.len())
}
}
fn flush(&mut self) -> ::core::result::Result<(), Self::Error> { Ok(()) }
}

impl io::Write for sha256::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl<'a> literacy::Write for &'a mut $HashEngine {
type Error = literacy::Error;

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.input(buf);
Ok(buf.len())
}
}
fn write<'b>(&'b mut self, buf: &'b [u8]) -> ::core::result::Result<usize, Self::Error> {
self.input(buf);
Ok(buf.len())
}

impl io::Write for sha512::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
fn write_all<'b>(&'b mut self, buf: &'b [u8]) -> ::core::result::Result<(), Self::Error> {
self.write(buf)?;
Ok(())
}

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.input(buf);
Ok(buf.len())
}
fn flush<'b>(&'b mut self) -> ::core::result::Result<(), Self::Error> { Ok(()) }
}
};
}

impl io::Write for ripemd160::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.input(buf);
Ok(buf.len())
}
#[cfg(any(test, feature = "std"))]
impl ::std::error::Error for ::Error {
fn cause(&self) -> Option<&::std::error::Error> { None }
fn description(&self) -> &str { "`std::error::description` is deprecated" }
}

impl io::Write for siphash24::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.input(buf);
Ok(buf.len())
}
#[cfg(any(test, feature = "std"))]
impl ::std::error::Error for ::hex::Error {
fn cause(&self) -> Option<&::std::error::Error> { None }
fn description(&self) -> &str { "`std::error::description` is deprecated" }
}

write_impl!(sha1::HashEngine);
write_impl!(sha256::HashEngine);
write_impl!(sha512::HashEngine);
write_impl!(ripemd160::HashEngine);
write_impl!(siphash24::HashEngine);

#[cfg(test)]
mod tests {
use std::io::Write;

use ::literacy::Write;
use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24};
use Hash;

Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#[cfg(any(test, feature="std"))] extern crate core;
#[cfg(feature="serde")] pub extern crate serde;
#[cfg(all(test,feature="serde"))] extern crate serde_test;
#[cfg(feature = "use-core2")] pub extern crate core2;

#[doc(hidden)]
pub mod _export {
Expand All @@ -51,9 +52,12 @@ pub mod _export {

#[cfg(feature = "schemars")] extern crate schemars;

#[cfg(not(feature = "std"))]
extern crate alloc;

#[macro_use] mod util;
#[macro_use] pub mod serde_macros;
#[cfg(any(test, feature = "std"))] mod std_impls;
mod impls;
pub mod error;
pub mod hex;
pub mod hash160;
Expand All @@ -66,6 +70,7 @@ pub mod sha256t;
pub mod siphash24;
pub mod sha512;
pub mod cmp;
pub mod literacy;

use core::{borrow, fmt, hash, ops};

Expand Down
Loading