Commit Graph

154 Commits

Author SHA1 Message Date
Tobin C. Harding 53f68383b7
hashes: Bump version to 0.13.0
Add a changelog entry and bump the version to 0.13.0

Does not include changes to `bitcoin` to depend on the new version.
2023-08-24 12:25:42 +10:00
Andrew Poelstra 1b3a9d3580
Merge rust-bitcoin/rust-bitcoin#1990: Introduce the `small-hash` feature for `bitcoin_hashes`
f2c5f19557 Introduce the `small-hash` feature for `bitcoin_hashes` (Alekos Filini)

Pull request description:

  When enabled this feature swaps the hash implementation of sha512, sha256 and ripemd160 for a smaller (but also slower) one.

  On embedded processors (Cortex-M4) it can lead to up to a 52% size reduction, from around 37KiB for just the `process_block` methods of the three hash functions to 17.8KiB.

  The following numbers were collected on `aarch64-unknown-linux-gnu` with `cargo 1.72.0-nightly`.

  ## Original

  ```
  RUSTFLAGS='--cfg=bench -C opt-level=z' cargo bench
  ```

  ```
  test hash160::benches::hash160_10                 ... bench:          33 ns/iter (+/- 1) = 303 MB/s
  test hash160::benches::hash160_1k                 ... bench:       2,953 ns/iter (+/- 187) = 346 MB/s
  test hash160::benches::hash160_64k                ... bench:     188,480 ns/iter (+/- 11,595) = 347 MB/s
  test hmac::benches::hmac_sha256_10                ... bench:          33 ns/iter (+/- 2) = 303 MB/s
  test hmac::benches::hmac_sha256_1k                ... bench:       2,957 ns/iter (+/- 104) = 346 MB/s
  test hmac::benches::hmac_sha256_64k               ... bench:     192,022 ns/iter (+/- 6,407) = 341 MB/s
  test ripemd160::benches::ripemd160_10             ... bench:          25 ns/iter (+/- 1) = 400 MB/s
  test ripemd160::benches::ripemd160_1k             ... bench:       2,288 ns/iter (+/- 93) = 447 MB/s
  test ripemd160::benches::ripemd160_64k            ... bench:     146,823 ns/iter (+/- 1,102) = 446 MB/s
  test sha1::benches::sha1_10                       ... bench:          41 ns/iter (+/- 0) = 243 MB/s
  test sha1::benches::sha1_1k                       ... bench:       3,844 ns/iter (+/- 70) = 266 MB/s
  test sha1::benches::sha1_64k                      ... bench:     245,854 ns/iter (+/- 10,158) = 266 MB/s
  test sha256::benches::sha256_10                   ... bench:          35 ns/iter (+/- 0) = 285 MB/s
  test sha256::benches::sha256_1k                   ... bench:       3,063 ns/iter (+/- 15) = 334 MB/s
  test sha256::benches::sha256_64k                  ... bench:     195,729 ns/iter (+/- 2,880) = 334 MB/s
  test sha256d::benches::sha256d_10                 ... bench:          34 ns/iter (+/- 1) = 294 MB/s
  test sha256d::benches::sha256d_1k                 ... bench:       3,071 ns/iter (+/- 107) = 333 MB/s
  test sha256d::benches::sha256d_64k                ... bench:     188,614 ns/iter (+/- 8,101) = 347 MB/s
  test sha512::benches::sha512_10                   ... bench:          21 ns/iter (+/- 0) = 476 MB/s
  test sha512::benches::sha512_1k                   ... bench:       1,714 ns/iter (+/- 36) = 597 MB/s
  test sha512::benches::sha512_64k                  ... bench:     110,084 ns/iter (+/- 3,637) = 595 MB/s
  test sha512_256::benches::sha512_256_10           ... bench:          22 ns/iter (+/- 1) = 454 MB/s
  test sha512_256::benches::sha512_256_1k           ... bench:       1,822 ns/iter (+/- 70) = 562 MB/s
  test sha512_256::benches::sha512_256_64k          ... bench:     116,231 ns/iter (+/- 4,745) = 563 MB/s
  test siphash24::benches::siphash24_1ki            ... bench:       1,072 ns/iter (+/- 41) = 955 MB/s
  test siphash24::benches::siphash24_1ki_hash       ... bench:       1,102 ns/iter (+/- 42) = 929 MB/s
  test siphash24::benches::siphash24_1ki_hash_u64   ... bench:       1,064 ns/iter (+/- 41) = 962 MB/s
  test siphash24::benches::siphash24_64ki           ... bench:      69,957 ns/iter (+/- 2,712) = 936 MB/
  ```

  ```
  0000000000005872 t _ZN84_$LT$bitcoin_hashes..ripemd160..HashEngine$u20$as$u20$bitcoin_hashes..HashEngine$GT$5input17hc4800746a9da7ff4E
  0000000000007956 t _ZN81_$LT$bitcoin_hashes..sha256..HashEngine$u20$as$u20$bitcoin_hashes..HashEngine$GT$5input17hf49345f65130ce9bE
  0000000000008024 t _ZN14bitcoin_hashes6sha2568Midstate10const_hash17h57317bc8012004b4E.llvm.441255102889972912
  0000000000010528 t _ZN81_$LT$bitcoin_hashes..sha512..HashEngine$u20$as$u20$bitcoin_hashes..HashEngine$GT$5input17h9bc868d4392bd9acE
  ```

  Total size: 32380 bytes

  ## With `small-hash` enabled

  ```
  RUSTFLAGS='--cfg=bench -C opt-level=z' cargo bench --features small-hash
  ```

  ```
  test hash160::benches::hash160_10                 ... bench:          52 ns/iter (+/- 3) = 192 MB/s
  test hash160::benches::hash160_1k                 ... bench:       4,817 ns/iter (+/- 286) = 212 MB/s
  test hash160::benches::hash160_64k                ... bench:     319,572 ns/iter (+/- 11,031) = 205 MB/s
  test hmac::benches::hmac_sha256_10                ... bench:          54 ns/iter (+/- 2) = 185 MB/s
  test hmac::benches::hmac_sha256_1k                ... bench:       4,846 ns/iter (+/- 204) = 211 MB/s
  test hmac::benches::hmac_sha256_64k               ... bench:     319,114 ns/iter (+/- 4,451) = 205 MB/s
  test ripemd160::benches::ripemd160_10             ... bench:          27 ns/iter (+/- 0) = 370 MB/s
  test ripemd160::benches::ripemd160_1k             ... bench:       2,358 ns/iter (+/- 150) = 434 MB/s
  test ripemd160::benches::ripemd160_64k            ... bench:     154,573 ns/iter (+/- 3,954) = 423 MB/s
  test sha1::benches::sha1_10                       ... bench:          41 ns/iter (+/- 1) = 243 MB/s
  test sha1::benches::sha1_1k                       ... bench:       3,700 ns/iter (+/- 243) = 276 MB/s
  test sha1::benches::sha1_64k                      ... bench:     231,039 ns/iter (+/- 13,989) = 283 MB/s
  test sha256::benches::sha256_10                   ... bench:          51 ns/iter (+/- 3) = 196 MB/s
  test sha256::benches::sha256_1k                   ... bench:       4,823 ns/iter (+/- 182) = 212 MB/s
  test sha256::benches::sha256_64k                  ... bench:     299,960 ns/iter (+/- 17,545) = 218 MB/s
  test sha256d::benches::sha256d_10                 ... bench:          52 ns/iter (+/- 2) = 192 MB/s
  test sha256d::benches::sha256d_1k                 ... bench:       4,827 ns/iter (+/- 323) = 212 MB/s
  test sha256d::benches::sha256d_64k                ... bench:     302,844 ns/iter (+/- 15,796) = 216 MB/s
  test sha512::benches::sha512_10                   ... bench:          34 ns/iter (+/- 1) = 294 MB/s
  test sha512::benches::sha512_1k                   ... bench:       3,002 ns/iter (+/- 123) = 341 MB/s
  test sha512::benches::sha512_64k                  ... bench:     189,767 ns/iter (+/- 10,396) = 345 MB/s
  test sha512_256::benches::sha512_256_10           ... bench:          34 ns/iter (+/- 1) = 294 MB/s
  test sha512_256::benches::sha512_256_1k           ... bench:       2,996 ns/iter (+/- 198) = 341 MB/s
  test sha512_256::benches::sha512_256_64k          ... bench:     192,024 ns/iter (+/- 8,181) = 341 MB/s
  test siphash24::benches::siphash24_1ki            ... bench:       1,081 ns/iter (+/- 65) = 947 MB/s
  test siphash24::benches::siphash24_1ki_hash       ... bench:       1,083 ns/iter (+/- 63) = 945 MB/s
  test siphash24::benches::siphash24_1ki_hash_u64   ... bench:       1,084 ns/iter (+/- 63) = 944 MB/s
  test siphash24::benches::siphash24_64ki           ... bench:      67,237 ns/iter (+/- 4,185) = 974 MB/s
  ```

  ```
  0000000000005384 t _ZN81_$LT$bitcoin_hashes..sha256..HashEngine$u20$as$u20$bitcoin_hashes..HashEngine$GT$5input17hae341658cf9b880bE
  0000000000005608 t _ZN14bitcoin_hashes9ripemd16010HashEngine13process_block17h3276b13f1e9feef8E.llvm.13618235596061801146
  0000000000005616 t _ZN14bitcoin_hashes6sha2568Midstate10const_hash17h3e6fbef64c15ee00E.llvm.7326223909590351031
  0000000000005944 t _ZN81_$LT$bitcoin_hashes..sha512..HashEngine$u20$as$u20$bitcoin_hashes..HashEngine$GT$5input17h321a237bfbe5c0bbE
  ```

  Total size: 22552 bytes

  ## Conclusion

  On `aarch64` there's overall a ~30% improvement in size, although ripemd160 doesn't really shrink that much (and its performance also aren't impacted much with only a 6% slowdown). sha512 and sha256 instead are almost 40% slower with `small-hash` enabled.

  I don't have performance numbers for other architectures, but in terms of size there was an even larger improvements on `thumbv7em-none-eabihf`, with a 52% size reduction overall:

  ```
     Size          Crate Name
  25.3KiB bitcoin_hashes <bitcoin_hashes[fe467ef2aa3a1470]::sha512::HashEngine as bitcoin_hashes[fe467ef2aa3a1470]::HashEngine>::input
   6.9KiB bitcoin_hashes <bitcoin_hashes[fe467ef2aa3a1470]::sha256::HashEngine as bitcoin_hashes[fe467ef2aa3a1470]::HashEngine>::input
   4.8KiB bitcoin_hashes <bitcoin_hashes[fe467ef2aa3a1470]::ripemd160::HashEngine as bitcoin_hashes[fe467ef2aa3a1470]::HashEngine>::input
  ```

  vs

  ```
    Size          Crate Name
  9.5KiB bitcoin_hashes <bitcoin_hashes[974bb476ef905797]::sha512::HashEngine as bitcoin_hashes[974bb476ef905797]::HashEngine>::input
  4.5KiB bitcoin_hashes <bitcoin_hashes[974bb476ef905797]::ripemd160::HashEngine>::process_block
  3.8KiB bitcoin_hashes <bitcoin_hashes[974bb476ef905797]::sha256::HashEngine as bitcoin_hashes[974bb476ef905797]::HashEngine>::input
  ```

  I'm assuming this is because on more limited architectures the compiler needs to use more instructions to move data in and out of registers (especially for sha512 which ideally would benefit from 64-bit registers), so reusing the code by moving it into functions saves a lot of those instructions.

  Also note that the `const_hash` method on `sha256` causes the compiler to emit two independent implementations. I haven't looked into the code yet, maybe there's a way to merge them so that the non-const `process_block` calls into the const fn.

  -----

  Note: commits are unverified right now because I don't have the keys available, I will sign them after addressing the review comments.

ACKs for top commit:
  apoelstra:
    ACK f2c5f19557
  tcharding:
    ACK f2c5f19557

Tree-SHA512: 1d5eb56324c458660e2571e8cf59895dc31dae9c5427c7ed36f8a0e81ca2e9a0f39026f56b6803df03635cc8b66aee3bf5182d51ab8972d169d56bcfec33771c
2023-08-17 16:34:10 +00:00
Andrew Poelstra af85528428
Merge rust-bitcoin/rust-bitcoin#1962: Add simd sha256 intrinsics for x86 machines
546c0122d7 Add simd sha256 intrinsics for x86 machines (sanket1729)

Pull request description:

  This is my first time dabbling into architecture specific code and simd. The algorithm is a word to word translation of the C code from  4899efc81d/sha256-x86.c .

  Some benchmarks:

  With simd
  ```
  test sha256::benches::sha256_10                   ... bench:          11 ns/iter (+/- 0) = 909 MB/s
  test sha256::benches::sha256_1k                   ... bench:         712 ns/iter (+/- 2) = 1438 MB/s
  test sha256::benches::sha256_64k                  ... bench:      45,597 ns/iter (+/- 189) = 1437 MB/s
  ```
  Without simd
  ```
  test sha256::benches::sha256_10                   ... bench:          47 ns/iter (+/- 0) = 212 MB/s
  test sha256::benches::sha256_1k                   ... bench:       4,243 ns/iter (+/- 17) = 241 MB/s
  test sha256::benches::sha256_64k                  ... bench:     271,263 ns/iter (+/- 1,610) = 241 MB/s
  ```

ACKs for top commit:
  apoelstra:
    ACK 546c0122d7
  tcharding:
    ACK 546c0122d7

Tree-SHA512: 7167c900b77e63cf38135a3960cf9ac2615f73b2ef7020a12b5cc3f4c047910063ba9045217b9ecfa70f7de1eb0f02f2674f291bd023a853bad2b9162fae831e
2023-08-17 16:22:46 +00:00
Alekos Filini f2c5f19557
Introduce the `small-hash` feature for `bitcoin_hashes`
When enabled this feature swaps the hash implementation of sha512,
sha256 and ripemd160 for a smaller (but also slower) one.

On embedded processors (Cortex-M4) it can lead to up to a 52% size
reduction, from around 37KiB for just the `process_block` methods of the
three hash functions to 17.8KiB.
2023-08-16 14:19:17 +02:00
Riccardo Casatta ff5fa41975
Merge rust-bitcoin/rust-bitcoin#1933: Fix links in docs
154552e334 docs: Do not link to std::option::Option (Tobin C. Harding)
24843468c3 Remove rustdocs links to serde (Tobin C. Harding)

Pull request description:

  Two minor patches to fix up docs links. These were originally done as part of #1880 but are unrelated so pushing them up separately.

ACKs for top commit:
  apoelstra:
    ACK 154552e334
  RCasatta:
    utACK 154552e334

Tree-SHA512: e45e1538c66b59d63a66898896927bb6c1336fb4c8515bb9e2204c8035870ef8e4a6fd32dfc83db2938afda67feb27c48989e382410f9e7ea7a967132941c720
2023-08-02 16:37:13 +02:00
sanket1729 546c0122d7
Add simd sha256 intrinsics for x86 machines 2023-07-27 11:36:08 -07:00
Tobin C. Harding 2268b44911
Depend on hex-conservative
We have just released the `hex-conservative` crate, we can now use it.

Do the following:

- Depend on `hex-conservative` in `bitcoin` and `hashes`
- Re-export `hex-conservative` as `hex` from both crate roots.
- Remove all the old hex code from `hashes`
- Fix all the import statements (makes up the bulk of the lines changed
  in this patch)
2023-07-21 10:59:46 +10:00
Tobin C. Harding 7b402e930c
schemars: Add pinning docs
Not totally necessary but since I went to the trouble of working out the
last working version add it to the docs so the next guy can grep for
`cargo update` to find them.
2023-07-18 10:27:48 +10:00
Tobin C. Harding 5b1443a91c
hashes/embedded: Add script dir and README
To help folk work out how to run the `hashes/embedded` test crate copy
over the `script` directory and an updated version of the `README` from
`embedded/bitcoin`.
2023-07-18 10:27:48 +10:00
Tobin C. Harding 94732aecbf
Add patch section to test crates
In order to keep the embedded and schemacs test crates building when we
update their local transient dependencies we need to use a `patch`
section.

- For `bitcoin/embedded` add `patch` section for `internals`, `hashes`
already has an entry.
- For `hashes/embedded` add `patch` section for `internals`.
- For `hashes/extendend_tests/schemars` add `patch` section for
  `internals`.

FTR for direct local dependencies we use a `path` field when specifying
the dependency.
2023-07-18 10:27:48 +10:00
Tobin C. Harding 512d982275
Remove path field from internals dependency
We use two different methods for specifying local dependencies, `patch`
and also `path`. There does not seem to be a reason why we use both,
lets be uniform. Elect to use `patch` for all local crates.
2023-07-18 10:27:46 +10:00
Andrew Poelstra c7eb9e61ec
Merge rust-bitcoin/rust-bitcoin#1916: internals: Bump version to 0.2.0
8813a63ec9 internals: Bump version to 0.2.0 (Tobin C. Harding)

Pull request description:

  In preparation for release bump the version and add a changelog entry. Includes updating the dependency in `bitcoin` and `hashes`.

ACKs for top commit:
  apoelstra:
    ACK 8813a63ec9
  sanket1729:
    utACK 8813a63ec9

Tree-SHA512: a9bd9d4d69cba21329f3f63a9948afe566bb97c8c65f5d46c329a696a814e9eb31372d378de1ecf0f43f0cb42f11d53dc51bc467223b34629e61315d48b39a29
2023-07-17 16:27:22 +00:00
Tobin C. Harding 71c0043127
Remove docsrs attributes
Somehow when we started using `doc_auto_cfg` we forgot to remove a bunch
of docsrs attributes.
2023-07-14 14:59:11 +10:00
Tobin C. Harding 8813a63ec9
internals: Bump version to 0.2.0
In preparation for release bump the version and add a changelog entry.

Includes updating the dependency in `bitcoin` and `hashes` as well as
the minimal/recent lock files.
2023-07-14 14:31:21 +10:00
Tobin C. Harding c3a99c62ad
CI: Pin serde_json for MSRV build
Recent release of `serde_json` depends on `serde` 1.0.66 but we pin to
1.0.56

Pin `serde_json` for MSRV build to v1.0.99
2023-07-12 15:50:18 +10:00
Tobin C. Harding 24843468c3
Remove rustdocs links to serde
These links are broken just use code ticks instead since `serde` is
feature gated and these docs are private anyways.
2023-07-08 11:29:11 +10:00
Tobin C. Harding 96784b9cfa
Make sha512::HashEngine fields private
Recently we made the hash engine fields pub crate so that `sha512_256`
could construct a hash engine with different constants. We can make the
code slightly cleaner by adding a pub crate constructor and making the
fields private again.

Idea from Kixunil:

  https://github.com/rust-bitcoin/rust-bitcoin/pull/1413#pullrequestreview-1197207593
2023-06-20 10:11:11 +10:00
sanket1729 12e014e288
Merge rust-bitcoin/rust-bitcoin#1878: ci: Remove stal DO_ALLOC_TESTS variable
aa7b3a7d0c ci: Remove stal DO_ALLOC_TESTS variable (Tobin C. Harding)

Pull request description:

  We never enable the `DO_ALLOC_TESTS` variable, and hence never test the "alloc" feature in `hashes`.

  Remove the `DO_ALLOC_TESTS` variable and add "alloc" to the `FEATURES` array.

ACKs for top commit:
  yancyribbens:
    ACK aa7b3a7d0c
  apoelstra:
    ACK aa7b3a7d0c
  sanket1729:
    utACK aa7b3a7d0c

Tree-SHA512: 461735242ec94786624a3653c3ea108d1a8712d5a77943f2ce54b44298624f6d4a43c98a0079782211e9a4b61ac87fbadd9c98608ca0ad82574c4a63b921776f
2023-06-14 17:54:31 -07:00
sanket1729 39dae44d7b
Merge rust-bitcoin/rust-bitcoin#1877: hashes: Remove stale status badge
4dfaec952c hashes: Remove stale status badge (Tobin C. Harding)

Pull request description:

  We do not use travis in our CI pipeline anymore, remove the stale badge.

ACKs for top commit:
  yancyribbens:
    ACK 4dfaec952c
  apoelstra:
    ACK 4dfaec952c
  sanket1729:
    utACK 4dfaec952c

Tree-SHA512: 6865c8cc51f90161ea643922df3bbf890811777e3dcbff9e464d6092f0d79a6ae2798d593f6a27e7f18b05ba66a765c871d4d01086e18b8480916573288afc0e
2023-06-14 17:22:33 -07:00
Andrew Poelstra 80efdb065a
Merge rust-bitcoin/rust-bitcoin#1873: Improve `hashes::Error`
06afd52a12 Improve hashes::Error (Tobin C. Harding)

Pull request description:

  We are trying to make error types stable on the way to v1.0

  The current `hashes::Error` is a "general" enum error type with a single variant, better to use a struct and make the error usecase specific.

  Improve the `hashes::Error` by doing:

  - Make it a struct
  - Rename to `FromSliceError`
  - Move it to the crate root (remove `error` module)

  Includes usage in `bitcoin`.

ACKs for top commit:
  apoelstra:
    ACK 06afd52a12
  Kixunil:
    ACK 06afd52a12

Tree-SHA512: 20a517daaf3e9e09744e2a65cde6e238c8f2d1224899a6c04142a3a4e635d54112b0a2e846d25256071bb27cb70f7482380f98e9a535a5498aa4c7dc0d52cc54
2023-05-29 22:58:17 +00:00
Andrew Poelstra c041aea51a
Merge rust-bitcoin/rust-bitcoin#1876: hashes: Fix stale repository name
3717a549f9 hashes: Fix stale repository name (Tobin C. Harding)

Pull request description:

  The repository name is stale since we moved the `hashes` crate into the `rust-bitcoin` repo a while ago.

ACKs for top commit:
  apoelstra:
    ACK 3717a549f9
  Kixunil:
    ACK 3717a549f9

Tree-SHA512: ac4553547f1912c8242e3b87d3cc8951598999b5512ad1b49494b3c504449939e0e60905d96464b22c71b67ca975d18814a92af6e0aa66ff4f46effb97ac0733
2023-05-29 02:39:42 +00:00
Andrew Poelstra ca06d64455
rename bitcoin-private to bitcoin-internals
Since we now have control over the name on crates.io.
2023-05-26 19:49:52 +00:00
Tobin C. Harding aa7b3a7d0c
ci: Remove stal DO_ALLOC_TESTS variable
We never enable the `DO_ALLOC_TESTS` variable, and hence never test the
"alloc" feature in `hashes`.

Remove the `DO_ALLOC_TESTS` variable and add "alloc" to the `FEATURES`
array.
2023-05-25 18:16:26 +10:00
Tobin C. Harding 4dfaec952c
hashes: Remove stale status badge
We do not use travis in our CI pipeline anymore, remove the stale badge.
2023-05-25 14:34:28 +10:00
Tobin C. Harding 3717a549f9
hashes: Fix stale repository name
The repository name is stale since we moved the `hashes` crate into
the `rust-bitcoin` repo a while ago.
2023-05-25 14:12:06 +10:00
Tobin C. Harding 06afd52a12
Improve hashes::Error
We are trying to make error types stable on the way to v1.0

The current `hashes::Error` is a "general" enum error type with a single
variant, better to use a struct and make the error usecase specific.

Improve the `hashes::Error` by doing:

- Make it a struct
- Rename to `FromSliceError`
- Move it to the crate root (remove `error` module)

Includes usage in `bitcoin`.
2023-05-25 13:25:13 +10:00
Tobin C. Harding c2f33f60b8
Add changelo entry for removal of FromHex
During the last round of releases (bitcoin 0.30, hashes 0.12) we removed
the `FromHex` implementation from all types except vecs and arrays. We
added `FromStr` impls for types that roundtrip with `Display`.

We never added a changelog mention to either `bitcoin` or `hashes`, lets
retroactively add an entry.

Fix: #1747
2023-05-18 14:18:42 +10:00
yancy 639c548aed docs: Add doc comments for external crates 2023-05-08 10:57:49 +02:00
Andrew Poelstra 23d80bf1ab
Merge rust-bitcoin/rust-bitcoin#1764: Test with minimal dependency versions
c4c64c0dc5 Test with minimal dependency versions (Martin Habovstiak)
d5655d503a Bump core2 dependency from 0.3.0 -> 0.3.2 (Tobin C. Harding)

Pull request description:

  This is work originally done by Kixunil in #1272, I picked it up to help out. The only changes I made were rebasingg, updating the recent lock file, adding `--locked` to hashes contrib file,  and adding a co-developed-by tag for accountability.

  It could happen that we unknowingly depend on a new version of a crate without updating `Cargo.toml`. This could cause resolution issues for downstream users. It's also unclear for outsiders to know with which dependencies did we test the crate.

  This change commits two lock files: `minimal` and `recent`. `minimal` contains minimal dependency versions, while `recent` contains dependency versions at the time of making the change.

  Further, this adds CI jobs to test with both lock files, CI job for `internals` crate, removes old `serde` pinning and prints a warning if `recent` is no longer up to date. (We may have to override it somehow if any crate breaks MSRV.)

  The documentation is also updated accordingly.

  Closes #1230

ACKs for top commit:
  apoelstra:
    ACK c4c64c0dc5
  Kixunil:
    ACK c4c64c0dc5

Tree-SHA512: 7d386e96ab747f6a6bafeea828ac65bd8bb11975eaa3408acecac369cd2f235f6e9d4c57202be18a3dc2eeb2a2df532d73e4d35cd1f3fbf092eb6414c55b1524
2023-05-03 22:35:21 +00:00
Martin Habovstiak c4c64c0dc5
Test with minimal dependency versions
It could happen that we unknowingly depend on a new version of a crate
without updating `Cargo.toml`. This could cause resolution issues for
downstream users. It's also unclear for outsiders to see which
dependencies we tested the crate with.

This change commits two lock files: `minimal` and `recent`. `minimal`
contains minimal depdendency versions, while `recent` contains
dependency versions at the time of making the change.

Further, this adds CI jobs to test with both lock files, CI job for
`internals` crate, removes old `serde` pinning and prints a warning if
`recent` is no longer up to date. (We may have to override it somehow if
any crate breaks MSRV.)

The documentation is also updated accordingly.

Co-developed-by: Tobin C. Harding <me@tobin.cc>

Closes #1230
2023-05-03 08:06:46 +10:00
Andrew Poelstra 283b7d6e51
hashes: rename fuzzing cfg parameter to bitcoin_hashes_fuzz 2023-05-01 21:16:12 +00:00
Tobin C. Harding 99673ab5c4
hashes: Introduce SPDX license identifiers
Whether or not every file needs an explicit license comment is out of
scope for this patch; in the `bitcoin` crate we use SPDX identifiers
because they are a single line with no loss of "benefit" over any longer
form.

Use SPDX identifiers in `hashes`. Drop the mention of re-licensing code
from Apache to CC0-1 (because the original code was written by Andrew
as well as the copied code then if the argument ever comes up it can be
easily countered).
2023-05-01 09:26:36 +10:00
Andrew Poelstra 0be75f7edc
move hashes/fuzz into main fuzz/ directory 2023-04-27 00:24:52 +00:00
Tobin C. Harding d37845197f
embedded: Remove error handler
Seems we no longer need an explicit error handler, remove it.

I did not grok the reason (long thread link below) but just removed it
and checked that the embedded crates still ran correctly.

https://github.com/rust-lang/rust/issues/51540)
2023-04-26 14:29:39 +10:00
Tobin C. Harding 53d75c7b94
extended_tests: Remove stale docs
We no longer need to pin `syn`; remove stale docs.
2023-04-20 09:17:10 +10:00
Andrew Poelstra 8440d80a5a
Merge rust-bitcoin/rust-bitcoin#1696: Remove `schemars` pin from manifest
6c61e1019e Fix pinning (schemars and MSRV) (Tobin C. Harding)
c8e38d6a5a hashes: Implement JsonSchema for sha256t::Hash<T> (Tobin C. Harding)

Pull request description:

  This has grown due to now including pinning work also done in https://github.com/rust-bitcoin/rust-bitcoin/pull/1736, I decided to do this because the PRs conflict and doing it all here saves accidentally getting out of sync. And  https://github.com/rust-bitcoin/rust-bitcoin/pull/1764 requires this PR.

  - Patch 1 is unchanged
  - Patch 2 now fixes pinning in bitcoin and hashes CI scripts and in the docs of both as well as the manifest stuff relating to `schemars` - phew.

  Fix: #1687

ACKs for top commit:
  Kixunil:
    ACK 6c61e1019e
  apoelstra:
    ACK 6c61e1019e

Tree-SHA512: eae4aa9700817bab6ad444e07709e8b1a4ffb1625e08be6ba399abde38bf6f8e5ea216a0836e2e26dfaddc76c392802cd016738ea6e753a1bca2584d9d2a9796
2023-04-19 22:18:29 +00:00
Tobin C. Harding 6c61e1019e
Fix pinning (schemars and MSRV)
Done as is single patch to make sure all the docs and CI are in sync and
correct.

We currently pin the `schemars` dependency using `<=0.8.3` as well as a
the `dyn-clone` transient dependency in the manifest (`hashes` and the
extended test crate). This is incorrect because it makes usage of the
crate klunky (or possibly impossible) if downstream users wish to use a
later version of `schemars`.

Observe also that we do not have to pin `schemars`, we do however have to pin
the `serde` crate if either `serde` or `schemars` features are enabled.
Do so in CI and document in the readme file within hashes.

Currently we have a pin remaining from the old MSRV (`syn` due to use
of `matches!`).

Fix pinning by:

- Remove pin in manifest for `schemars`
- Fix pinning for MSRV in CI and docs (this includes documenting pinning
  requirements for `schemars` feature because it is related to the other
  pin of `serde`) in both `hashes` readme and main repo readme.
2023-04-19 10:17:18 +10:00
Tobin C. Harding 088fa24be8
hashes: Derive traits on the sha512::Hash type
For all the other hash types we use derive, by way of the `hash_type!`
macro, unless I'm missing something we can do the same for
`sha512::Hash`.
2023-04-09 13:36:38 +10:00
Martin Habovstiak 095b7958dd Make `sha256t_hash_newtype!` evocative of the output.
The Rust API guidelines state that macros should be evocative of the
output, which is a sensible recommendation. We already had this for
`hash_newtype!` macro but didn't for sha256t version.

This changes the macro to have this syntax:

```rust
sha256t_hash_newtype! {
    // Order of these structs is fixed.
    /// Optional documentation details here. Summary is auto-generated.
    /*pub*/ struct Tag = raw(MIDSTATE_BYTES, LEN);

    /// Documentation here
    #[hash_newtype(forward)] // optional, default is backward
    /*pub*/ struct HashType(/* attributes allowed here */ _);
}
```

Closes #1427
2023-04-02 17:00:52 +02:00
Martin Habovstiak 42d3ae05be Implement computing SHA256 in const context
Computing hashes in const fn is useful for esily creating tags for
`sha256t`. This adds `const fn` implementation for `sha256::Hash` and
the algorithm for computing midstate of tagged hash in `const` context.

Part of #1427
2023-04-02 01:35:12 +02:00
Tobin C. Harding c8e38d6a5a
hashes: Implement JsonSchema for sha256t::Hash<T>
The derived implementation of `JsonSchema` recently stopped working as
described here:

https://github.com/rust-bitcoin/rust-bitcoin/pull/1696#issuecomment-1458670415

Implement the suggestion.
2023-03-31 11:09:07 +11:00
Andrew Poelstra 895db71156
Merge rust-bitcoin/rust-bitcoin#1763: Use doc_auto_cfg
a189942c64 Use doc_auto_cfg (Tobin C. Harding)

Pull request description:

  If we use `#![cfg_attr(docsrs, feature(doc_auto_cfg))]` instead of `#![cfg_attr(docsrs, feature(doc_cfg))]` we no longer need to manually mark types with `#[cfg_attr(docsrs, doc(cfg(feature = "std")))]`.

  Sweeeeeet.

  Props to pezcore for the lesson :)

ACKs for top commit:
  apoelstra:
    ACK a189942c64
  Kixunil:
    ACK a189942c64

Tree-SHA512: 1ced1e09f5d1733b362b83ca650d3f52c89eb57e78e8437f74c496d89776548f8c50feab6750352342e2abe680434681de2c126ce36a81dda21397b9695d4d4e
2023-03-30 16:56:49 +00:00
Tobin C. Harding a189942c64
Use doc_auto_cfg
If we use `#![cfg_attr(docsrs, feature(doc_auto_cfg))]` instead of
`#![cfg_attr(docsrs, feature(doc_cfg))]` we no longer need to manually
mark types with `#[cfg_attr(docsrs, doc(cfg(feature = "std")))]`.

Sweeeeeet.
2023-03-29 14:50:33 +11:00
junderw 91863c8a78
Feature (hashes): Add from_bytes_ref and from_bytes_mut to all Hash types 2023-03-28 18:27:41 -07:00
Tobin C. Harding 71fa9e81e7
Bump MSRV to 1.48.1
As per discussion [0] bump our MSRV for all crates in `rust-bitcoin`
repo to 1.48.1 [1].

[0] https://github.com/rust-bitcoin/rust-bitcoin/discussions/1329
[1] https://blog.rust-lang.org/2020/11/19/Rust-1.48.html
2023-03-23 08:03:06 +11:00
Tobin C. Harding 913575ac91
hashes: Run the formatter
Run `cargo +nightly fmt`, no other manual changes.
2023-03-21 08:33:24 +11:00
Tobin C. Harding 52c4579057
Enable formatting for hashes
The `hashes` module contains a bunch of arrays, mostly formatted with 8
hex bytes on a line; add `rustfmt::skip` to keep the formatting of
arrays as is.

Remove the exclude for the `hashes` crate. Do not run the formatter,
that will be done as a separate patch to aid review.
2023-03-21 08:33:24 +11:00
Andrew Poelstra 51f2f8483c
rename bitcoin-internals to bitcoin-private 2023-03-13 16:02:12 +00:00
Andrew Poelstra ca3b4330e8
specify `internals` version in other Cargo.toml files 2023-03-08 15:02:31 +00:00
Andrew Poelstra 9ffbbb8efb
Merge rust-bitcoin/rust-bitcoin#1694: release bitcoin_hashes 0.12
bfd401c96e bitcoin_hashes: add CHANgELOG (Andrew Poelstra)
d1b7b54e3a bump bitcoin-hashes version to 0.12 (Andrew Poelstra)

Pull request description:

  It was a little tricky to bump the version number because of https://github.com/rust-bitcoin/rust-bitcoin/issues/1553. There are a couple other things I considered trying, which maybe we'll do for future releases, but I believe this works for now.

  Maybe should wait for #1111.

ACKs for top commit:
  tcharding:
    ACK bfd401c96e
  sanket1729:
    utACK bfd401c96e. ChangeLog looks good to me, did not review whether all noteworthy changes were included.

Tree-SHA512: d2104fc93e364415ae955e8123e6087c1eaa4c955aeaf4647ead051a223563326f66c0e278d68f64335e22c9d0af9b296dc3b744cd9d82d206844461fe7bf9c9
2023-03-08 02:13:45 +00:00
Tobin C. Harding c1360067e9
Enable formatting in CI
Enable formatting in CI by doing:

- Add a section to the `test.sh` scripts to run the formatter (guarded by
  the env variable `DO_FMT`) for all crates (bitcoin, hashes, internals).
- Add `DO_FMT` to the nightly `Tests` CI job.
2023-03-07 08:57:32 +11:00
Andrew Poelstra bfd401c96e
bitcoin_hashes: add CHANgELOG 2023-03-05 13:53:45 +00:00
Andrew Poelstra d1b7b54e3a
bump bitcoin-hashes version to 0.12
Because we have rust-secp in the loop, we need to update rust-secp, push
a new tag, and use that here, to ensure that the direct dependency on
bitcoin_hashes, and the rust-secp version, are compatible.
2023-03-05 13:40:20 +00:00
Andrew Poelstra ac8702f32f
Merge rust-bitcoin/rust-bitcoin#1670: `hash_newtype` cleanup
8ccfb412c1 Improve documentation of `hash_newtype!` (Martin Habovstiak)
58876e2be9 Remove unused macro (Martin Habovstiak)

Pull request description:

  Removed unused macro and improved documentation to address review of #1659 - see commits. I also added a note about recursion.

ACKs for top commit:
  apoelstra:
    ACK 8ccfb412c1
  tcharding:
    ACK 8ccfb412c1

Tree-SHA512: 3b4b0c4ffc8a5166619110d9dcb51affd5cafbb2af84a55dd540a815e4702514d99c71dc1c54aca27fb91970e7e7189d1dffb4f7da7951b0f71336ef6f32d30b
2023-02-28 01:34:38 +00:00
Martin Habovstiak 8ccfb412c1 Improve documentation of `hash_newtype!`
The macro is non-trivial, so documenting it well is very useful. This
change improves both user-facing and developer-facing code with
appropriate warnings about the limitations of the code and Rust macro
system.
2023-02-27 11:59:04 +01:00
Tobin C. Harding 161273b209
Re-name hash inner/byte methods
Currently we have an associated type on hash types `Inner` with
accompanying methods `into_inner`, `from_inner`, `as_inner`. Also, we
provide a way to create new wrapped hash types. The use of 'inner'
becomes ambiguous with the addition of wrapped types because the inner
could be the inner hash type or the `Inner` byte array of the inner
wrapped hash type.

In an effort to make the API more clear and uniform do the following:

- Rename `Inner` -> `Bytes`
- Rename `*_inner` -> `*_byte_array`
- Rename the inner hash to/from methods to `*_raw_hash`

Correct method prefix `into_` -> `to_` because theses methods convert
owned `Copy` types.

Add the trait Bound `Copy` to the `Bytes` type because we rely on this
trait bound for the conversion methods to be correctly named according
to convention.

Because of the dependency hole created by `secp256k1` this patch changes
the secp dependency to a git tag dependency that includes changes to the
hashes calls required so that we can get green lights on CI in this
repo.
2023-02-27 14:23:58 +11:00
Martin Habovstiak 58876e2be9 Remove unused macro
This macro was a part of previous failed design and is no longer used.
This change removes it.
2023-02-24 21:38:36 +01:00
Martin Habovstiak 06f1f027ab Make `hash_newtype` evocative of the output
The API guidelines say macro input should be evocative of the output.
`hash_newtype` didn't have this property.

This change makes it look exactly like the resulting struct, `$len`
parameter was removed since it's not needed, reversing is controlled
using an attribute. The macro is also better documented and ready to be
extended in the future.

The tagged SHA256 newtype is not yet modified because it has a more
complicated input parameters.

Closes #1648
2023-02-22 14:35:50 +01:00
Martin Habovstiak b018f3e90b Remove the `$len` argument from `hash_newtype`
Now that the `$len` argument is no longer used, remove it completely.
2023-02-22 14:35:14 +01:00
Martin Habovstiak 752817e20d Stop using `$len` in `hash_newtype`
We want to get rid of this argument since its value is implied by the
inner hash type. First we stop using it.
2023-02-22 01:12:56 +01:00
Andrew Poelstra 794ddde657
Merge rust-bitcoin/rust-bitcoin#1569: ci: Remove incorrect code comment and cargo clean
70bff0de8d ci: Remove incorrect code comment and cargo clean (Tobin C. Harding)

Pull request description:

  Recently while trying to fix CI I (Tobin) added a call to `cargo clean` and a code comment justifying it. This was merged because while not incorrect it is redundant since calling `cargo` with different `RUSTFLAGS` triggers a rebuild.

ACKs for top commit:
  apoelstra:
    ACK 70bff0de8d
  Kixunil:
    ACK 70bff0de8d

Tree-SHA512: 5bed107aedf9d6d240fa885335bfe04099436c0d30c045554644b43a06900c415708dfcedbd27037564d296c0ab12e4c4d4dc1e25ab64b4cc07ca7b46c46628a
2023-02-18 01:28:46 +00:00
Andrew Poelstra 562400a7bc
Merge rust-bitcoin/rust-bitcoin#1593: hashes: Implement AsRef for fixed size arrays
a121e19e94 hashes: Implement AsRef for fixed size arrays (Tobin C. Harding)

Pull request description:

  Implement `AsRef<[u8; X]>` for hash types including wrapped hash types. Doing so means at times the compiler can no longer infer the type because we have `AsRef<[u8]` implemented also but we can use `into_inner` and `as_inner` to get the inner array if needed.

  Fix: #1462

  ## Note

  This touches code that will likely be changed by #1577 and when we do #1491 but I believe its a step forward.

ACKs for top commit:
  arturomf94:
    ACK [`a121e19`](a121e19e94)
  apoelstra:
    ACK a121e19e94
  Kixunil:
    ACK a121e19e94

Tree-SHA512: 257c44826c7649db25bb3a6f023f68b2f17b70c546a056afad044bc8a16bf61f654c3846222505aaf5e6f9a0ad1d2113272d61317b407d0ac83702e41060a1ee
2023-02-17 13:24:57 +00:00
Andrew Poelstra 7930a9ba5c
Merge rust-bitcoin/rust-bitcoin#1641: Move deny lint checks to script
a1c3082319 Move deny lint checks to script (Harshil Jani)

Pull request description:

  Closes #1551
  Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>

ACKs for top commit:
  apoelstra:
    ACK a1c3082319
  Kixunil:
    ACK a1c3082319

Tree-SHA512: 68632fcff175fa7f035ea10346fc36c1d593ba797e18955b01b8f38932e10327dbb36d9cbf95f65c29f68c5106e8b3364708b75ec3fd94c41cee6c994300eca7
2023-02-14 19:05:45 +00:00
Harshil Jani a1c3082319 Move deny lint checks to script
Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>
2023-02-14 10:48:24 +05:30
Tobin C. Harding 9ae3285882
hashes: Improve feature gating
Currently we have a few things mixed up in the feature gating of
`hashes`.

Observe that:
- `io::Write` is not related to allocation.
- "std" should be able to enable "alloc".

Improve feature gating by doing:

- Enable "alloc" from "std" and simplify `cfg` codebase wide.
- Enable "internals/alloc" from "alloc".
- Fix feature gating to use the minimal requirement i.e., "alloc".
2023-02-14 06:23:56 +11:00
Andrew Poelstra 9615dd12b8
Merge rust-bitcoin/rust-bitcoin#1505: Improve test coverage for docs build
41f2dcf6ae Improve test coverage for docs build (Tobin C. Harding)
b4c14a4b7c hashes: Use automatic link (Tobin C. Harding)
96e8a080d1 ci: Remove redundant || exit (Tobin C. Harding)

Pull request description:

  Currently the docs build commands in `hashes` and `bitcoin` differ, they should be the same.

  Add a command `cargo doc` to improve coverage e.g., recently we botched the feature guarding but since CI only runs `cargo rustdoc` with custom compiler conditional set we didn't catch it.

  Done after seeing: https://github.com/rust-bitcoin/rust-bitcoin/pull/1504 and CI should fail on this PR until 1504 is in.

ACKs for top commit:
  apoelstra:
    ACK 41f2dcf6ae
  Kixunil:
    ACK 41f2dcf6ae

Tree-SHA512: 7cde68292cfc6f32b75d066e188e7c418ee251f9a5abc57fbd642ba33e9cd5bd8ef7c5ba7cffd206acae6ddec2f8c3db38c8c911a4319e979158666b8225953d
2023-02-10 23:20:57 +00:00
Tobin C. Harding 8596e402f2
Fix docs.rs to use all features
Many moon ago we were unable to build with `--all-features`, this is no
longer the case.

Instruct docs.rs to build the docs with `--all-features`.
2023-02-10 08:04:11 +11:00
Tobin C. Harding 89086d094d
hashes: Use angle braces to make hyperlinks
rustdoc build emits a few instances of:

 warning: this URL is not a hyperlink
2023-02-10 08:04:11 +11:00
Tobin C. Harding 9b5c2ad7af
hashes: Clean up optional dependencies
Clean up the optional dependencies by doing:

- Put the optional dependencies below the non-optional dependencies as
  is customary, separated by a line of whitespace.
- Put `optional = true` as the last item so as to be uniform
- Put `core2` at the top, aids reading because of how the comments are
  distributed (`core2` does not have a comment on it).
2023-02-10 08:04:11 +11:00
Tobin C. Harding 5b4f19c01f
hashes: Improve std/alloc features
Improve std/alloc features by doing:

- Fix incorrect feature enabling: "std" should enable "internals/std"
  not "internals/alloc".
- Put the "alloc" feature under the "std" feature, same as in other
  crates.
- Remove the comment, devs should understand what dependencies are for.
  We don't have comments everywhere else so we don't really need this one.
2023-02-10 08:03:40 +11:00
Tobin C. Harding aa62ca224a
hashes: Do not enable core2/alloc feature
`core2` is for Read/Write, nothing to do with allocation and we do not
use the "alloc" feature of `core2` in `hashes`.

Fix core2 dependency/features by doing:

- Explicitly enable "bitcoin_hashes/core2" in `bitcoin`.
- Do not enable "core2/alloc" in `hashes`
2023-02-08 08:24:49 +11:00
Tobin C. Harding c15f8dee29
Improve manifest package section
Improve all manifest package sections by doing:

- Order the list of options uniformly
- Remove unnecessary homepage option (currently same as repo)
- Add categories section
2023-02-08 08:24:49 +11:00
Tobin C. Harding 12f5e37ed9
Add excludes to manifests
We can check which files are included in the packaged release with
 `cargo package --list `.

Add an `exclude` section to each manifest that excludes `tests/` and
`contrib/`. Not all crates have a `tests/` directory yet but they should
so add the exclude anyway to future proof the crates.
2023-02-08 08:24:49 +11:00
Tobin C. Harding a308e1e2ea
Remove FromHex for all types except Vec and array
Remove `FromHex` from hash and script types

- Remove the `FromHex` implementation from hash types and `ScriptBuf`
- Remove the `FromStr` implementation from `ScriptBuf` because it does not
  roundtrip with `Display`.
- Implement a method `from_hex` on `ScriptBuf`.
- Implement `FromStr` on hash types using a fixed size array.

This leaves `FromHex` implementations only on `Vec` and fixed size arrays.
2023-02-01 08:26:46 +11:00
Tobin C. Harding 5ab5c264d2
Use fully qualified path in macro
It is easier to maintain code if macros use the fully qualified path to
types and functions instead of relying on code that uses the macro to
import said type or function.
2023-02-01 08:22:46 +11:00
Tobin C. Harding 7e85452cd9
hashes: Implement std::error::Error
We have old Rust 1.29 error handling code still in `hashes`. Implement
`std::error::Error` for the `hex::Error` and `error::Error` types in
line with "modern" Rust 1.41.1 error handling.
2023-02-01 08:22:46 +11:00
Tobin C. Harding 5e3abc5e11
Fix feature gating on unit tests
Currently we have incorrect feature gating on unit tests and imports
leading to unused import warnings.
2023-02-01 08:22:46 +11:00
Tobin C. Harding 3344cf6be2
Favour $reverse instead of $reversed
Currently we have two things that are the same but use different
identifiers, use `$reverse` instead of `$reversed`.
2023-02-01 08:22:46 +11:00
Tobin C. Harding a121e19e94
hashes: Implement AsRef for fixed size arrays
Implement `AsRef<[u8; X]>` for hash types including wrapped hash types.
Doing so means at times the compiler can no longer infer the type because we have
`AsRef<[u8]` implemented also but we can use `into_inner` and `as_inner`
to get the inner array if needed.
2023-01-26 11:00:57 +11:00
Tobin C. Harding e079524b2a
Remove hashes::hex::HexWriter
The `HexWriter` is not used any more since we added the new hex code in
internals for fast hex encoding.

While we are removing the benches for `HexWriter` also remove the last
remaining bench for writing using `Display` because this is not the
correct place for that code - its trivial to re add later in the correct
module.
2023-01-22 12:05:08 +11:00
Tobin C. Harding 70bff0de8d
ci: Remove incorrect code comment and cargo clean
Recently while trying to fix CI I (Tobin) added a call to `cargo clean`
and a code comment justifying it. This was merged because while not
incorrect it is redundant since calling `cargo` with different
`RUSTFLAGS` triggers a rebuild.
2023-01-21 09:37:16 +11:00
Tobin C. Harding 53ee42d7c1
Fix ASAN in CI
When we merged `hashes` into `rust-bitcoin` we ran the test script a
couple of times using `./hashes/contrib/test.sh` this causes the `cargo`
commands in the CI script to be run from the crate root which is not
what we want.

This showed up recently because `cargo test` was getting run in
`bitcoin` after building with address/memory sanitazation configured
into the build.

While we are at it run `cargo clean` after the last sanitizer build just
to future proof the CI script in case we later accidentally re-use the same
build with plain old `cargo` (without `-Zbuild-std etc.`).
2023-01-20 15:47:43 +11:00
Tobin C. Harding 41f2dcf6ae
Improve test coverage for docs build
Currently the docs build commands in `hashes` and `bitcoin` differ, they
should be the same.

Add a command `cargo doc` to improve coverage e.g., recently we botched
the feature guarding but since CI only runs `cargo rustdoc` with custom
compiler conditional set we didn't catch it.

Run docs in CI using nightly and stable toolchains as required.
2023-01-16 13:56:56 +11:00
Tobin C. Harding b4c14a4b7c
hashes: Use automatic link
Cargo emits various warnings when building the docs:

 warning: this URL is not a hyperlink

As suggested, add angle brackets to create automatic links.
2023-01-16 13:56:27 +11:00
Tobin C. Harding 7593f1f334
Use set -ex
Use the more typical form to enable bash features.

Refactor only, no logic changes.
2023-01-13 07:32:30 +11:00
Martin Habovstiak 1b0988833a Remove `ToHex`
The `ToHex` trait was replaced by either simple `Display`/`LowerHex`
where appropriate or `DisplayHex` from `bitcoin_internals` which is
faster.

This change replaces the usages and removes the trait.
2023-01-07 19:50:03 +01:00
Andrew Poelstra 0f98e179c3
Merge rust-bitcoin/rust-bitcoin#1413: Add sha512_256 to bitcoin_hashes
411174c391 Add fuzz target for sha512_256 (Calvin Kim)
31fc1f8638 Add support for sha512/256 (Calvin Kim)
15b5af1117 Export sha512::HashEngine fields/function within the crate (Calvin Kim)

Pull request description:

  Adds a new file named `sha512_256.rs` that implements the `sha512/256` hash. This was needed as a part of https://github.com/rust-bitcoin/rust-bitcoin/discussions/1318 to drop the `sha2` dependency.

  All the actual hashing code is exactly the same as `sha512.rs`, minus the initial constants and the use of `hash_type!` macro. Some unit tests were added from wikipedia (for the "" input) and the rest were from the Go standard library's tests for sha512_256.

  Benchmarks on my Ryzen 3600 machine show that it is faster than sha256.

  ```
  test sha256::benches::sha256_10                   ... bench:          37 ns/iter (+/- 0) = 270 MB/s
  test sha256::benches::sha256_1k                   ... bench:       3,338 ns/iter (+/- 24) = 306 MB/s
  test sha256::benches::sha256_64k                  ... bench:     213,605 ns/iter (+/- 1,806) = 306 MB/s
  test sha512_256::benches::sha512_256_10           ... bench:          27 ns/iter (+/- 1) = 370 MB/s
  test sha512_256::benches::sha512_256_1k           ... bench:       2,196 ns/iter (+/- 12) = 466 MB/s
  test sha512_256::benches::sha512_256_64k          ... bench:     140,552 ns/iter (+/- 777) = 466 MB/s
  ```

  One caveat is that I could not get hongfuzz to build locally so I couldn't test the fuzz on my machine. I ended up only testing through the CI for the fuzz tests.

  I thought adding a completely separate file was the easiest and the most straightforward way of implementing it. I'm very much open to changing the implementation if you guys don't think this is the right direction.

ACKs for top commit:
  sanket1729:
    ACK 411174c391. Reviwed range diff from 43feb9ea7b282d9119708a27fa7a1c7412d1386a that I had ACked
  apoelstra:
    ACK 411174c391

Tree-SHA512: 98298a7c177cbb616bfbc02cec5c5860f10204df8275cc9f1e4ea07333b901095e574fbc3fe0a03375e0d321a1579e2c2023a5c14addd863e10cc927f155710c
2022-12-31 19:38:02 +00:00
Andrew Poelstra d06bb226bf
Merge rust-bitcoin/rust-bitcoin#1476: Use hex from internals rather than hashes
3e520f9094 Use hex from internals rather than hashes (Martin Habovstiak)

Pull request description:

  `bitcoin-internals` contains a more performant implementation of hex encoding than what `bitcoin_hashes` uses internally. This switches the implementations for formatting trait implementations as a step towards moving over completely.

  The public macros are also changed to delegate to inner type which is technically a breaking change but we will break the API anyway and the consuers should only call the macro on the actual hash newtypes where the inner types already have the appropriate implementations.

  Apart from removing reliance on internal hex from public API this reduces duplicated code generated and compiled. E.g. if you created 10 hash newtypes of SHA256 the formatting implementation would be instantiated 11 times despite being the same.

  To do all this some other changes were required to the hex infrastructure. Mainly modifying `put_bytes` to accept iterator (so that `iter().rev()` can be used) and adding a new `DisplayArray` type. The iterator idea was invented by Tobin C. Harding, this commit just adds a bound check and generalizes over `u8` and `&u8` returning iterators.

  While it may seem that `DisplayByteSlice` would suffice it'd create and initialize a large array even for small arrays wasting performance. Knowing the exact length `DisplayArray` fixes this.

  Another part of refactoring is changing from returning `impl Display` to return `impl LowerHex + UpperHex`. This makes selecting casing less annoying since the consumer no longer needs to import `Case` without cluttering the API with convenience methods.

ACKs for top commit:
  tcharding:
    ACK 3e520f9094
  apoelstra:
    ACK 3e520f9094

Tree-SHA512: 62988cec17550ed35990386e572c0d32dc7107e1c36b7c9099080747e15167e6d66497fb300178afbd22481c0360a6b7a1228fd09402d4ce5d295a8594c02aa6
2022-12-31 19:17:32 +00:00
Andrew Poelstra 249a2cfa2a
Merge rust-bitcoin/rust-bitcoin#1468: hashes: ci: Remove --all
56e4e53357 hashes: ci: Remove --all (Tobin C. Harding)

Pull request description:

  Currently we are using the `--all` flag in `cargo` commands in the `hashes` CI script. This flag (the deprecated version of `--workspace`) causes cargo to run the command for the whole workspace, this is not what we want because we run test individually for each crate using a ci script per crate.

  The effect of this patch is to reduce re-runs of tests i.e., reduce machine usage during CI runs with no reduction of coverage - PROFIT!

ACKs for top commit:
  apoelstra:
    ACK 56e4e53357
  sanket1729:
    utACK 56e4e53357

Tree-SHA512: 13134ba2f6806a705f99af5b8d66b051e1e58da177a02ee46880f494e37c380fc4c28731cd42eabbd69ae884763dbc360902e0e8afa7f88e78483e8a37f614f5
2022-12-30 17:21:39 +00:00
Andrew Poelstra 4d7b8cd3b4
Merge rust-bitcoin/rust-bitcoin#1420: Allow dead_code/unused_imports when fuzzing
5a2a37d4be Allow dead_code/unused_imports when fuzzing (Tobin C. Harding)

Pull request description:

  Littering the codebase with `#[cfg(not(fuzzing))]` is a bit messy just to quieten the linter during fuzzing. Instead just globally allow.

  Done while debugging #1409

ACKs for top commit:
  sanket1729:
    ACK 5a2a37d4be
  apoelstra:
    ACK 5a2a37d4be

Tree-SHA512: fb84215a2b00ad6d3321b2781ba285af513ff8fd413c0997045a41c4f23028d2ef0fdf083839289d0c5108c990aa66bdae4430ad3ef32881eac5324b2e881b3b
2022-12-30 14:56:40 +00:00
Martin Habovstiak 3e520f9094 Use hex from internals rather than hashes
`bitcoin-internals` contains a more performant implementation of hex
encoding than what `bitcoin_hashes` uses internally. This switches the
implementations for formatting trait implementations as a step towards
moving over completely.

The public macros are also changed to delegate to inner type which is
technically a breaking change but we will break the API anyway and the
consuers should only call the macro on the actual hash newtypes where
the inner types already have the appropriate implementations.

Apart from removing reliance on internal hex from public API this
reduces duplicated code generated and compiled. E.g. if you created 10
hash newtypes of SHA256 the formatting implementation would be
instantiated 11 times despite being the same.

To do all this some other changes were required to the hex
infrastructure. Mainly modifying `put_bytes` to accept iterator (so that
`iter().rev()` can be used) and adding a new `DisplayArray` type. The
iterator idea was invented by Tobin C. Harding, this commit just adds a
bound check and generalizes over `u8` and `&u8` returning iterators.

While it may seem that `DisplayByteSlice` would suffice it'd create and
initialize a large array even for small arrays wasting performance.
Knowing the exact length `DisplayArray` fixes this.

Another part of refactoring is changing from returning `impl Display` to
return `impl LowerHex + UpperHex`. This makes selecting casing less
annoying since the consumer no longer needs to import `Case` without
cluttering the API with convenience methods.
2022-12-22 10:44:27 +01:00
Calvin Kim 411174c391 Add fuzz target for sha512_256 2022-12-21 16:36:35 +09:00
Calvin Kim 31fc1f8638 Add support for sha512/256
Adds a new file named sha512_256.rs that implements the sha512/256
hash. This was needed as a part of
https://github.com/rust-bitcoin/rust-bitcoin/discussions/1318 to
drop the sha2 dependency in current rustreexo code.
2022-12-21 16:36:31 +09:00
Andrew Poelstra 033d964b57
Merge rust-bitcoin/rust-bitcoin#1419: Run clippy in ci for `hashes`
b78ba730f2 hashes: Run clippy in ci (Tobin C. Harding)
5e67f7a7cb Remove the unnecessary explicit reference (Tobin C. Harding)

Pull request description:

  Currently we only run the linter in `bitcoin/contrib/test.sh`, we should do the same in the `hashes` ci script.

  - Patch 1: Fix current clippy issues in `hashes` crate
  - Patch 2: Run clippy in CI for `hashes` crate

ACKs for top commit:
  apoelstra:
    ACK b78ba730f2
  Kixunil:
    ACK b78ba730f2

Tree-SHA512: f9fedcd8c1a06c715396cf6c7b752e2c9e32dbfde48c8b4bcb9ac5e701abe109ddeadc2e7466f6926f7c3ab74fa26e68b70473b4a5b5009cb4644d634707d4ea
2022-12-15 20:34:13 +00:00
Tobin C. Harding 56e4e53357 hashes: ci: Remove --all
Currently we are using the `--all` flag in `cargo` commands in the
`hashes` CI script. This flag (the deprecated version of `--workspace`)
causes cargo to run the command for the whole workspace, this is not
what we want because we run test individually for each crate using a ci
script per crate.

The effect of this patch is to reduce re-runs of tests i.e., reduce
machine usage during CI runs with no reduction of coverage - PROFIT!
2022-12-14 08:24:59 +11:00
Tobin C. Harding b7a84d0c68 hashes: Do not implement Deref
Currently we implement `Deref` for hashes. From the docs [0]

 > Deref should only be implemented for smart pointers to avoid confusion

Furthermore because we implement `Deref` as well as implement
`internals::hex::display::DisplayHex` for slices hashes get coerced into
slices and `to_lower_hex_string` can be called on them, this is
incorrect because `DisplayHex` does not account for hashes that display
backwards so we end up with the wrong string.

[0] https://doc.rust-lang.org/std/ops/trait.Deref.html
2022-12-12 12:05:54 +11:00
sanket1729 5fc40baa73 Fix new clippy warnings 2022-12-01 15:59:08 -08:00
Tobin C. Harding 5a2a37d4be Allow dead_code/unused_imports when fuzzing
Littering the codebase with `#[cfg(not(fuzzing))]` is a bit messy just
to quieten the linter during fuzzing. Instead just globally allow.
2022-11-30 15:07:07 +11:00
Tobin C. Harding b78ba730f2 hashes: Run clippy in ci
Currently we only run the linter in `bitcoin/contrib/test.sh`, we should
do the same in the `hashes` ci script.
2022-11-30 14:43:56 +11:00
Tobin C. Harding 5e67f7a7cb Remove the unnecessary explicit reference
Clippy emits various warnings of form:

 warning: this expression creates a reference which is immediately
 dereferenced by the compiler

As suggested, remove the unnecessary explicit reference.
2022-11-30 14:41:14 +11:00