Skip to content

Commit

Permalink
clippy polish etc
Browse files Browse the repository at this point in the history
  • Loading branch information
integritychain committed Jan 16, 2025
1 parent dd8472e commit 5aab380
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 96 deletions.
4 changes: 2 additions & 2 deletions fuzz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ The `scalars.rs` harness has fairly complete coverage of deserializors and arith
$ cargo +nightly fuzz run scalars -j 4
#722833: cov: 2025 ft: 3282 corp: 309 exec/s: 1941 oom/timeout/crash: 0/0/0 time: 106s job: 26 dft_time: 0
#775578: cov: 2025 ft: 3282 corp: 309 exec/s: 1883 oom/timeout/crash: 0/0/0 time: 113s job: 27 dft_time: 0
#16419246: cov: 2038 ft: 3305 corp: 292 exec/s: 1763 oom/timeout/crash: 0/0/0 time: 2350s job: 134 dft_time: 0
#16650547: cov: 2038 ft: 3305 corp: 292 exec/s: 1700 oom/timeout/crash: 0/0/0 time: 2384s job: 135 dft_time: 0
~~~


Expand Down
105 changes: 33 additions & 72 deletions fuzz/fuzz_targets/points.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#![no_main]
use bign256;
// Targets: bign256, k256, p192, p224, p256, p384, p521, sm2
// bp256 and bp384 are under construction
use ciborium::de;
use elliptic_curve::{group::GroupEncoding, Field, Group};
use k256;
use libfuzzer_sys::fuzz_target;
use p192;
use p224;
use p256;
use p384;
use p521;
use rand_chacha::{rand_core::SeedableRng, ChaChaRng};
use sm2;

static mut I: u64 = 0;

Expand Down Expand Up @@ -55,27 +48,23 @@ fuzz_target!(|data: &[u8]| {
let ap2 = bign256::AffinePoint::from_bytes(&data[32..32 + len].try_into().unwrap())
.unwrap_or_else(|| bign256::AffinePoint::GENERATOR);
let ap3 = de::from_reader(&data[48..48 + usize::from(data[3] & 0x3f)])
.unwrap_or_else(|_| bign256::AffinePoint::GENERATOR);
.unwrap_or(bign256::AffinePoint::GENERATOR);
let ap4 = bign256::AffinePoint::try_from(bign256::EncodedPoint::from_affine_coordinates(
&data[24..24 + len - 1].try_into().unwrap(),
&data[40..40 + len - 1].try_into().unwrap(),
true,
))
.unwrap_or_else(|_| bign256::AffinePoint::GENERATOR);
.unwrap_or(bign256::AffinePoint::GENERATOR);
let ap5 = bign256::AffinePoint::try_from(
bign256::EncodedPoint::from_bytes(&data[64..64 + usize::from(data[4] & 0x3f)])
.unwrap_or_else(|_| bign256::EncodedPoint::identity()),
)
.unwrap_or_else(|_| bign256::AffinePoint::GENERATOR);
.unwrap_or(bign256::AffinePoint::GENERATOR);
let pp2 = bign256::ProjectivePoint::from(ap2);
let scalar = bign256::Scalar::from_slice(&data[96..96 + len])
.unwrap_or_else(|_| bign256::Scalar::random(&mut rng));

test_group(
pp1 + pp2,
(pp1 + ap1 + ap2 + ap3 + ap4 + ap5).into(),
scalar,
);
test_group(pp1 + pp2, pp1 + ap1 + ap2 + ap3 + ap4 + ap5, scalar);

//
// Test k256 (secp256k1)
Expand All @@ -87,27 +76,23 @@ fuzz_target!(|data: &[u8]| {
let ap2 = k256::AffinePoint::from_bytes(&data[32..32 + len].try_into().unwrap())
.unwrap_or_else(|| k256::AffinePoint::GENERATOR);
let ap3 = de::from_reader(&data[48..48 + usize::from(data[3] & 0x3f)])
.unwrap_or_else(|_| k256::AffinePoint::GENERATOR);
.unwrap_or(k256::AffinePoint::GENERATOR);
let ap4 = k256::AffinePoint::try_from(k256::EncodedPoint::from_affine_coordinates(
&data[24..24 + len - 1].try_into().unwrap(),
&data[40..40 + len - 1].try_into().unwrap(),
true,
))
.unwrap_or_else(|_| k256::AffinePoint::GENERATOR);
.unwrap_or(k256::AffinePoint::GENERATOR);
let ap5 = k256::AffinePoint::try_from(
k256::EncodedPoint::from_bytes(&data[64..64 + usize::from(data[4] & 0x3f)])
.unwrap_or_else(|_| k256::EncodedPoint::identity()),
)
.unwrap_or_else(|_| k256::AffinePoint::GENERATOR);
.unwrap_or(k256::AffinePoint::GENERATOR);
let pp2 = k256::ProjectivePoint::from(ap2);
let scalar = de::from_reader(&data[96..96 + usize::from(data[5] & 0x3f)])
.unwrap_or(k256::Scalar::random(&mut rng));

test_group(
pp1 + pp2,
(pp1 + ap1 + ap2 + ap3 + ap4 + ap5).into(),
scalar,
);
test_group(pp1 + pp2, pp1 + ap1 + ap2 + ap3 + ap4 + ap5, scalar);

//
// Test p192
Expand All @@ -119,27 +104,23 @@ fuzz_target!(|data: &[u8]| {
let ap2 = p192::AffinePoint::from_bytes(&data[32..32 + len].try_into().unwrap())
.unwrap_or_else(|| p192::AffinePoint::GENERATOR);
let ap3 = de::from_reader(&data[48..48 + usize::from(data[3] & 0x3f)])
.unwrap_or_else(|_| p192::AffinePoint::GENERATOR);
.unwrap_or(p192::AffinePoint::GENERATOR);
let ap4 = p192::AffinePoint::try_from(p192::EncodedPoint::from_affine_coordinates(
&data[24..24 + len - 1].try_into().unwrap(),
&data[40..40 + len - 1].try_into().unwrap(),
true,
))
.unwrap_or_else(|_| p192::AffinePoint::GENERATOR);
.unwrap_or(p192::AffinePoint::GENERATOR);
let ap5 = p192::AffinePoint::try_from(
p192::EncodedPoint::from_bytes(&data[64..64 + usize::from(data[4] & 0x3f)])
.unwrap_or_else(|_| p192::EncodedPoint::identity()),
)
.unwrap_or_else(|_| p192::AffinePoint::GENERATOR);
.unwrap_or(p192::AffinePoint::GENERATOR);
let pp2 = p192::ProjectivePoint::from(ap2);
let scalar = de::from_reader(&data[96..96 + usize::from(data[5] & 0x3f)])
.unwrap_or(p192::Scalar::random(&mut rng));

test_group(
pp1 + pp2,
(pp1 + ap1 + ap2 + ap3 + ap4 + ap5).into(),
scalar,
);
test_group(pp1 + pp2, pp1 + ap1 + ap2 + ap3 + ap4 + ap5, scalar);

//
// Test p224
Expand All @@ -151,27 +132,23 @@ fuzz_target!(|data: &[u8]| {
let ap2 = p224::AffinePoint::from_bytes(&data[32..32 + len].try_into().unwrap())
.unwrap_or_else(|| p224::AffinePoint::GENERATOR);
let ap3 = de::from_reader(&data[48..48 + usize::from(data[3] & 0x3f)])
.unwrap_or_else(|_| p224::AffinePoint::GENERATOR);
.unwrap_or(p224::AffinePoint::GENERATOR);
let ap4 = p224::AffinePoint::try_from(p224::EncodedPoint::from_affine_coordinates(
&data[24..24 + len - 1].try_into().unwrap(),
&data[40..40 + len - 1].try_into().unwrap(),
true,
))
.unwrap_or_else(|_| p224::AffinePoint::GENERATOR);
.unwrap_or(p224::AffinePoint::GENERATOR);
let ap5 = p224::AffinePoint::try_from(
p224::EncodedPoint::from_bytes(&data[64..64 + usize::from(data[4] & 0x3f)])
.unwrap_or_else(|_| p224::EncodedPoint::identity()),
)
.unwrap_or_else(|_| p224::AffinePoint::GENERATOR);
.unwrap_or(p224::AffinePoint::GENERATOR);
let pp2 = p224::ProjectivePoint::from(ap2);
let scalar = de::from_reader(&data[96..96 + usize::from(data[5] & 0x3f)])
.unwrap_or(p224::Scalar::random(&mut rng));

test_group(
pp1 + pp2,
(pp1 + ap1 + ap2 + ap3 + ap4 + ap5).into(),
scalar,
);
test_group(pp1 + pp2, pp1 + ap1 + ap2 + ap3 + ap4 + ap5, scalar);

//
// Test p256
Expand All @@ -182,27 +159,23 @@ fuzz_target!(|data: &[u8]| {
let ap2 = p256::AffinePoint::from_bytes(&data[32..32 + len].try_into().unwrap())
.unwrap_or_else(|| p256::AffinePoint::GENERATOR);
let ap3 = de::from_reader(&data[48..48 + usize::from(data[3] & 0x3f)])
.unwrap_or_else(|_| p256::AffinePoint::GENERATOR);
.unwrap_or(p256::AffinePoint::GENERATOR);
let ap4 = p256::AffinePoint::try_from(p256::EncodedPoint::from_affine_coordinates(
&data[24..24 + len - 1].try_into().unwrap(),
&data[40..40 + len - 1].try_into().unwrap(),
true,
))
.unwrap_or_else(|_| p256::AffinePoint::GENERATOR);
.unwrap_or(p256::AffinePoint::GENERATOR);
let ap5 = p256::AffinePoint::try_from(
p256::EncodedPoint::from_bytes(&data[64..64 + usize::from(data[4] & 0x3f)])
.unwrap_or_else(|_| p256::EncodedPoint::identity()),
)
.unwrap_or_else(|_| p256::AffinePoint::GENERATOR);
.unwrap_or(p256::AffinePoint::GENERATOR);
let pp2 = p256::ProjectivePoint::from(ap2);
let scalar = de::from_reader(&data[96..96 + usize::from(data[5] & 0x3f)])
.unwrap_or(p256::Scalar::random(&mut rng));

test_group(
pp1 + pp2,
(pp1 + ap1 + ap2 + ap3 + ap4 + ap5).into(),
scalar,
);
test_group(pp1 + pp2, pp1 + ap1 + ap2 + ap3 + ap4 + ap5, scalar);

// Test p384
let len = p384::ProjectivePoint::random(&mut rng).to_bytes().len(); //affine and projective
Expand All @@ -212,27 +185,23 @@ fuzz_target!(|data: &[u8]| {
let ap2 = p384::AffinePoint::from_bytes(&data[32..32 + len].try_into().unwrap())
.unwrap_or_else(|| p384::AffinePoint::GENERATOR);
let ap3 = de::from_reader(&data[48..48 + usize::from(data[3] & 0x3f)])
.unwrap_or_else(|_| p384::AffinePoint::GENERATOR);
.unwrap_or(p384::AffinePoint::GENERATOR);
let ap4 = p384::AffinePoint::try_from(p384::EncodedPoint::from_affine_coordinates(
&data[24..24 + len - 1].try_into().unwrap(),
&data[40..40 + len - 1].try_into().unwrap(),
true,
))
.unwrap_or_else(|_| p384::AffinePoint::GENERATOR);
.unwrap_or(p384::AffinePoint::GENERATOR);
let ap5 = p384::AffinePoint::try_from(
p384::EncodedPoint::from_bytes(&data[64..64 + usize::from(data[4] & 0x3f)])
.unwrap_or_else(|_| p384::EncodedPoint::identity()),
)
.unwrap_or_else(|_| p384::AffinePoint::GENERATOR);
.unwrap_or(p384::AffinePoint::GENERATOR);
let pp2 = p384::ProjectivePoint::from(ap2);
let scalar = de::from_reader(&data[96..96 + usize::from(data[5] & 0x3f)])
.unwrap_or(p384::Scalar::random(&mut rng));

test_group(
pp1 + pp2,
(pp1 + ap1 + ap2 + ap3 + ap4 + ap5).into(),
scalar,
);
test_group(pp1 + pp2, pp1 + ap1 + ap2 + ap3 + ap4 + ap5, scalar);

// Test p521
let len = p521::ProjectivePoint::random(&mut rng).to_bytes().len(); //affine and projective
Expand All @@ -242,27 +211,23 @@ fuzz_target!(|data: &[u8]| {
let ap2 = p521::AffinePoint::from_bytes(&data[32..32 + len].try_into().unwrap())
.unwrap_or_else(|| p521::AffinePoint::GENERATOR);
let ap3 = de::from_reader(&data[48..48 + usize::from(data[3] & 0x3f)])
.unwrap_or_else(|_| p521::AffinePoint::GENERATOR);
.unwrap_or(p521::AffinePoint::GENERATOR);
let ap4 = p521::AffinePoint::try_from(p521::EncodedPoint::from_affine_coordinates(
&data[24..24 + len - 1].try_into().unwrap(),
&data[40..40 + len - 1].try_into().unwrap(),
true,
))
.unwrap_or_else(|_| p521::AffinePoint::GENERATOR);
.unwrap_or(p521::AffinePoint::GENERATOR);
let ap5 = p521::AffinePoint::try_from(
p521::EncodedPoint::from_bytes(&data[64..64 + usize::from(data[4] & 0x3f)])
.unwrap_or_else(|_| p521::EncodedPoint::identity()),
)
.unwrap_or_else(|_| p521::AffinePoint::GENERATOR);
.unwrap_or(p521::AffinePoint::GENERATOR);
let pp2 = p521::ProjectivePoint::from(ap2);
let scalar = de::from_reader(&data[96..96 + usize::from(data[5] & 0x3f)])
.unwrap_or(p521::Scalar::random(&mut rng));

test_group(
pp1 + pp2,
(pp1 + ap1 + ap2 + ap3 + ap4 + ap5).into(),
scalar,
);
test_group(pp1 + pp2, pp1 + ap1 + ap2 + ap3 + ap4 + ap5, scalar);

// Test sm2
let len = sm2::ProjectivePoint::random(&mut rng).to_bytes().len(); //affine and projective
Expand All @@ -272,25 +237,21 @@ fuzz_target!(|data: &[u8]| {
let ap2 = sm2::AffinePoint::from_bytes(&data[32..32 + len].try_into().unwrap())
.unwrap_or_else(|| sm2::AffinePoint::GENERATOR);
let ap3 = de::from_reader(&data[48..48 + usize::from(data[3] & 0x3f)])
.unwrap_or_else(|_| sm2::AffinePoint::GENERATOR);
.unwrap_or(sm2::AffinePoint::GENERATOR);
let ap4 = sm2::AffinePoint::try_from(sm2::EncodedPoint::from_affine_coordinates(
&data[24..24 + len - 1].try_into().unwrap(),
&data[40..40 + len - 1].try_into().unwrap(),
true,
))
.unwrap_or_else(|_| sm2::AffinePoint::GENERATOR);
.unwrap_or(sm2::AffinePoint::GENERATOR);
let ap5 = sm2::AffinePoint::try_from(
sm2::EncodedPoint::from_bytes(&data[64..64 + usize::from(data[4] & 0x3f)])
.unwrap_or_else(|_| sm2::EncodedPoint::identity()),
)
.unwrap_or_else(|_| sm2::AffinePoint::GENERATOR);
.unwrap_or(sm2::AffinePoint::GENERATOR);
let pp2 = sm2::ProjectivePoint::from(ap2);
let scalar = de::from_reader(&data[96..96 + usize::from(data[5] & 0x3f)])
.unwrap_or(sm2::Scalar::random(&mut rng));

test_group(
pp1 + pp2,
(pp1 + ap1 + ap2 + ap3 + ap4 + ap5).into(),
scalar,
);
test_group(pp1 + pp2, pp1 + ap1 + ap2 + ap3 + ap4 + ap5, scalar);
});
Loading

0 comments on commit 5aab380

Please sign in to comment.