Skip to content

Commit

Permalink
feat(fuzzing): fuzzing for felt addition
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Nov 8, 2023
1 parent 01b222e commit 9bc5015
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ Cargo.lock
# Added by cargo

/target
.DS_Store

corpus/
artifacts/
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [
"crates/starknet-types-rpc",
]

exclude = ["ensure_no_std"]
exclude = ["ensure_no_std", "fuzz"]
resolver = "2"

[workspace.package]
Expand Down
15 changes: 15 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[workspace]
members = ["felt"]

resolver = "2"

[workspace.package]
authors = [
"Lucas Levy <@LucasLvy>",
]
edition = "2021"
repository = "https://github.com/starknet-io/types-rs"

[workspace.dependencies]
starknet-types-core = { path = "../crates/starknet-types-core", default-features = false, features = ["arbitrary"] }
libfuzzer-sys = "0.4"
25 changes: 25 additions & 0 deletions fuzz/felt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "fuzzing_felt"
version = "0.0.1"
edition = "2021"
license = "MIT"
homepage = "https://github.com/starknet-io/types-rs"
repository = "https://github.com/starknet-io/types-rs"
categories = ["types", "math", "crypto", "fuzzing"]
keywords = ["stark", "zkp", "cairo", "fuzzing"]
description = "Fuzzing crate for the starknet common felt type"
readme = "README.md"

[package.metadata]
cargo-fuzz = true

[dependencies]
starknet-types-core.workspace = true
libfuzzer-sys.workspace = true

[[bin]]
name = "add_fuzzer"
path = "fuzz_targets/add.rs"
test = false
doc = false

Empty file added fuzz/felt/README.md
Empty file.
22 changes: 22 additions & 0 deletions fuzz/felt/fuzz_targets/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use starknet_types_core::felt::Felt;

fuzz_target!(|data: (Felt, Felt)| {
let zero = Felt::ZERO;
// Check a + 0 = a
assert_eq!(data.0 + zero, data.0, "Zero addition failed");
assert_eq!(data.1 + zero, data.1, "Zero addition failed");

// Check a + (-a) = 0
assert_eq!(data.0 + (-data.0), zero, "Unary addition failed");
assert_eq!(data.1 + (-data.1), zero, "Unary addition failed");

// Check a + b = a - (-b)
assert_eq!(data.0 + data.1, data.0 - (-data.1), "addition failed");
// Check a + a = a - (-a)
assert_eq!(data.0 + data.0, data.0 - (-data.0), "addition failed");
assert_eq!(data.1 + data.1, data.1 - (-data.1), "addition failed");
// Check a + a = 2 * a
assert_eq!(data.0 + data.0, Felt::TWO * data.0, "Doubling failed");
});

0 comments on commit 9bc5015

Please sign in to comment.