Skip to content

Commit 8173170

Browse files
committed
Merge #200: A few cleanup commits
b2252b7 ci: upgrade upload-artifact and download-artifact to v4 (Andrew Poelstra) 207c6dd field: add Sum trait (Andrew Poelstra) 53664cf fuzz: remove `afl` feature, clean up fuzztests a bit (Andrew Poelstra) 665f864 clippy: shorten a doccomment's first line (Andrew Poelstra) Pull request description: This extracts 3 (hopefully) simple and non-controversial commits from my error-correction branch. The first is just a clippy fix. The second cleans up and simplifies the fuzztests (it does *not* bring over the new fuzztesting scripts from rust-bitcoin, use the bitcoin-maintainer-tools repo, introduce libfuzzer, etc.; it just does some basic cleanups to make things more useful). And the third adds a few new bounds to the `Field` trait, specifically `Neg<Output = Self>` and `Sum`. (This is a breaking change.) ACKs for top commit: tcharding: ACK b2252b7 clarkmoody: ACK b2252b7 Tree-SHA512: cad04dcf67f90acd5dcac1cf4a1ce0cd23e6171270e95c425b591b7b41bd1b6611b7c2147082ff68f0dd0a5ad2c483f2ec1a92bcc4aad3eb31c1289e02f4cc98
2 parents 2052ec1 + b2252b7 commit 8173170

File tree

9 files changed

+61
-90
lines changed

9 files changed

+61
-90
lines changed

.github/workflows/fuzz.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: fuzz
2929
run: cd fuzz && ./fuzz.sh "${{ matrix.fuzz_target }}"
3030
- run: echo "${{ matrix.fuzz_target }}.rs" >executed_${{ matrix.fuzz_target }}
31-
- uses: actions/upload-artifact@v2
31+
- uses: actions/upload-artifact@v4
3232
with:
3333
name: executed_${{ matrix.fuzz_target }}
3434
path: executed_${{ matrix.fuzz_target }}
@@ -39,7 +39,7 @@ jobs:
3939
runs-on: ubuntu-latest
4040
steps:
4141
- uses: actions/checkout@v2
42-
- uses: actions/download-artifact@v2
42+
- uses: actions/download-artifact@v4
4343
- name: Display structure of downloaded files
4444
run: ls -R
4545
- run: find executed_* -type f -exec cat {} + | sort > executed

fuzz/Cargo.toml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
[package]
22
name = "bech32-fuzz"
3+
edition = "2021"
4+
rust-version = "1.56.1"
35
version = "0.0.1"
46
authors = ["Automatically generated"]
57
publish = false
68

79
[package.metadata]
810
cargo-fuzz = true
911

10-
[features]
11-
afl_fuzz = ["afl"]
12-
honggfuzz_fuzz = ["honggfuzz"]
13-
1412
[dependencies]
15-
honggfuzz = { version = "0.5", default-features = false, optional = true }
16-
afl = { version = "0.3", optional = true }
17-
libc = "0.2"
13+
honggfuzz = { version = "0.5.55", default-features = false }
1814
bech32 = { path = ".." }
1915

2016
# Prevent this from interfering with workspaces

fuzz/fuzz.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
set -e
33
cargo install --force honggfuzz --no-default-features
44
for TARGET in fuzz_targets/*; do
5-
FILENAME=$(basename $TARGET)
5+
FILENAME=$(basename "$TARGET")
66
FILE="${FILENAME%.*}"
7-
if [ -d hfuzz_input/$FILE ]; then
7+
if [ -d "hfuzz_input/$FILE" ]; then
88
HFUZZ_INPUT_ARGS="-f hfuzz_input/$FILE/input"
99
fi
10-
HFUZZ_BUILD_ARGS="--features honggfuzz_fuzz" HFUZZ_RUN_ARGS="-N1000000 --exit_upon_crash -v $HFUZZ_INPUT_ARGS" cargo hfuzz run $FILE
10+
HFUZZ_RUN_ARGS="--run_time 10 --exit_upon_crash -v $HFUZZ_INPUT_ARGS" cargo hfuzz run "$FILE"
1111

12-
if [ -f hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT ]; then
13-
cat hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT
14-
for CASE in hfuzz_workspace/$FILE/SIG*; do
15-
cat $CASE | xxd -p
12+
if [ -f "hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT" ]; then
13+
cat "hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT"
14+
for CASE in "hfuzz_workspace/$FILE/SIG"*; do
15+
xxd -p < "$CASE"
1616
done
1717
exit 1
1818
fi

fuzz/fuzz_targets/decode_rnd.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
extern crate bech32;
2-
31
use bech32::primitives::decode::{CheckedHrpstring, SegwitHrpstring, UncheckedHrpstring};
42
use bech32::Bech32m;
3+
use honggfuzz::fuzz;
54

65
// Checks that we do not crash if passed random data while decoding.
76
fn do_test(data: &[u8]) {
@@ -11,19 +10,6 @@ fn do_test(data: &[u8]) {
1110
let _ = SegwitHrpstring::new(&data_str);
1211
}
1312

14-
#[cfg(feature = "afl")]
15-
extern crate afl;
16-
#[cfg(feature = "afl")]
17-
fn main() {
18-
afl::read_stdio_bytes(|data| {
19-
do_test(&data);
20-
});
21-
}
22-
23-
#[cfg(feature = "honggfuzz")]
24-
#[macro_use]
25-
extern crate honggfuzz;
26-
#[cfg(feature = "honggfuzz")]
2713
fn main() {
2814
loop {
2915
fuzz!(|data| {
@@ -39,9 +25,9 @@ mod tests {
3925
for (idx, c) in hex.as_bytes().iter().enumerate() {
4026
b <<= 4;
4127
match *c {
42-
b'A'...b'F' => b |= c - b'A' + 10,
43-
b'a'...b'f' => b |= c - b'a' + 10,
44-
b'0'...b'9' => b |= c - b'0',
28+
b'A'..=b'F' => b |= c - b'A' + 10,
29+
b'a'..=b'f' => b |= c - b'a' + 10,
30+
b'0'..=b'9' => b |= c - b'0',
4531
_ => panic!("Bad hex"),
4632
}
4733
if (idx & 1) == 1 {

fuzz/fuzz_targets/encode_decode.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
extern crate bech32;
2-
31
use std::str;
42

53
use bech32::{Bech32m, Hrp};
4+
use honggfuzz::fuzz;
65

76
fn do_test(data: &[u8]) {
8-
if data.len() < 1 {
7+
if data.is_empty() {
98
return;
109
}
1110

@@ -17,35 +16,23 @@ fn do_test(data: &[u8]) {
1716

1817
let dp = &data[hrp_end..];
1918

20-
match str::from_utf8(&data[1..hrp_end]) {
19+
let s = match str::from_utf8(&data[1..hrp_end]) {
20+
Ok(s) => s,
2121
Err(_) => return,
22-
Ok(s) => {
23-
match Hrp::parse(&s) {
24-
Err(_) => return,
25-
Ok(hrp) => {
26-
if let Ok(address) = bech32::encode::<Bech32m>(hrp, dp) {
27-
let (hrp, data) = bech32::decode(&address).expect("should be able to decode own encoding");
28-
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), address);
29-
}
30-
}
31-
}
32-
}
33-
}
34-
}
22+
};
23+
let hrp = match Hrp::parse(s) {
24+
Ok(hrp) => hrp,
25+
Err(_) => return,
26+
};
27+
let address = match bech32::encode::<Bech32m>(hrp, dp) {
28+
Ok(addr) => addr,
29+
Err(_) => return,
30+
};
3531

36-
#[cfg(feature = "afl")]
37-
extern crate afl;
38-
#[cfg(feature = "afl")]
39-
fn main() {
40-
afl::read_stdio_bytes(|data| {
41-
do_test(&data);
42-
});
32+
let (hrp, data) = bech32::decode(&address).expect("should be able to decode own encoding");
33+
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), address);
4334
}
4435

45-
#[cfg(feature = "honggfuzz")]
46-
#[macro_use]
47-
extern crate honggfuzz;
48-
#[cfg(feature = "honggfuzz")]
4936
fn main() {
5037
loop {
5138
fuzz!(|data| {
@@ -61,9 +48,9 @@ mod tests {
6148
for (idx, c) in hex.as_bytes().iter().filter(|&&c| c != b'\n').enumerate() {
6249
b <<= 4;
6350
match *c {
64-
b'A'...b'F' => b |= c - b'A' + 10,
65-
b'a'...b'f' => b |= c - b'a' + 10,
66-
b'0'...b'9' => b |= c - b'0',
51+
b'A'..=b'F' => b |= c - b'A' + 10,
52+
b'a'..=b'f' => b |= c - b'a' + 10,
53+
b'0'..=b'9' => b |= c - b'0',
6754
_ => panic!("Bad hex"),
6855
}
6956
if (idx & 1) == 1 {

fuzz/fuzz_targets/parse_hrp.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
extern crate bech32;
2-
31
use bech32::Hrp;
2+
use honggfuzz::fuzz;
43

54
fn do_test(data: &[u8]) {
65
let s = String::from_utf8_lossy(data);
@@ -10,19 +9,6 @@ fn do_test(data: &[u8]) {
109
let _ = Hrp::parse(&s);
1110
}
1211

13-
#[cfg(feature = "afl")]
14-
extern crate afl;
15-
#[cfg(feature = "afl")]
16-
fn main() {
17-
afl::read_stdio_bytes(|data| {
18-
do_test(&data);
19-
});
20-
}
21-
22-
#[cfg(feature = "honggfuzz")]
23-
#[macro_use]
24-
extern crate honggfuzz;
25-
#[cfg(feature = "honggfuzz")]
2612
fn main() {
2713
loop {
2814
fuzz!(|data| {
@@ -38,9 +24,9 @@ mod tests {
3824
for (idx, c) in hex.as_bytes().iter().filter(|&&c| c != b'\n').enumerate() {
3925
b <<= 4;
4026
match *c {
41-
b'A'...b'F' => b |= c - b'A' + 10,
42-
b'a'...b'f' => b |= c - b'a' + 10,
43-
b'0'...b'9' => b |= c - b'0',
27+
b'A'..=b'F' => b |= c - b'A' + 10,
28+
b'a'..=b'f' => b |= c - b'a' + 10,
29+
b'0'..=b'9' => b |= c - b'0',
4430
_ => panic!("Bad hex"),
4531
}
4632
if (idx & 1) == 1 {

src/primitives/encode.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@ where
169169
}
170170
}
171171

172-
/// Iterator adaptor which takes a stream of field elements, converts it to characters prefixed by
173-
/// an HRP (and separator), and suffixed by the checksum i.e., converts the data in a stream of
174-
/// field elements into stream of characters representing the encoded bech32 string.
172+
/// Iterator adaptor which converts a stream of field elements to an iterator over the
173+
/// characters of an HRP-string.
174+
///
175+
/// Does so by converting the field elements to characters, prefixing an HRP, and suffixing
176+
/// a checksum.
175177
pub struct CharIter<'hrp, I, Ck>
176178
where
177179
I: Iterator<Item = Fe32>,

src/primitives/field.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//! Generic Field Traits
44
5-
use core::{fmt, hash, ops};
5+
use core::{fmt, hash, iter, ops};
66

77
/// A generic field.
88
pub trait Field:
@@ -13,6 +13,8 @@ pub trait Field:
1313
+ hash::Hash
1414
+ fmt::Debug
1515
+ fmt::Display
16+
+ iter::Sum
17+
+ for<'a> iter::Sum<&'a Self>
1618
+ ops::Add<Self, Output = Self>
1719
+ ops::Sub<Self, Output = Self>
1820
+ ops::AddAssign
@@ -29,7 +31,7 @@ pub trait Field:
2931
+ for<'a> ops::MulAssign<&'a Self>
3032
+ for<'a> ops::Div<&'a Self, Output = Self>
3133
+ for<'a> ops::DivAssign<&'a Self>
32-
+ ops::Neg
34+
+ ops::Neg<Output = Self>
3335
{
3436
/// The zero constant of the field.
3537
const ZERO: Self;
@@ -362,6 +364,19 @@ macro_rules! impl_ops_for_fe {
362364
self._neg()
363365
}
364366
}
367+
368+
// sum
369+
impl core::iter::Sum for $op {
370+
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
371+
iter.fold(crate::primitives::Field::ZERO, |i, acc| i + acc)
372+
}
373+
}
374+
375+
impl<'s> core::iter::Sum<&'s Self> for $op {
376+
fn sum<I: Iterator<Item = &'s Self>>(iter: I) -> Self {
377+
iter.fold(crate::primitives::Field::ZERO, |i, acc| i + acc)
378+
}
379+
}
365380
};
366381
}
367382
pub(super) use impl_ops_for_fe;

src/primitives/polynomial.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ impl<F: Field> Polynomial<F> {
9090
let mut cand = F::ONE;
9191
let mut eval = self.clone();
9292
for _ in 0..F::MULTIPLICATIVE_ORDER {
93-
let sum = eval.inner.iter().cloned().fold(F::ZERO, F::add);
94-
if sum == F::ZERO {
93+
if eval.inner.iter().sum::<F>() == F::ZERO {
9594
ret.push(cand.clone());
9695
}
9796

0 commit comments

Comments
 (0)