diff --git a/.clippy.toml b/.clippy.toml new file mode 100644 index 0000000..961117a --- /dev/null +++ b/.clippy.toml @@ -0,0 +1,2 @@ +type-complexity-threshold = 9999 +too-many-arguments-threshold = 20 \ No newline at end of file diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..a1b7b0b --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,16 @@ +#!/bin/sh + +# Format the code +make fmt + +# Apply fixes +make fix + +# Run checks +make check + +# If checks fail, prevent the commit +if [ $? -ne 0 ]; then + echo "Checks failed. Commit aborted." + exit 1 +fi \ No newline at end of file diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..64bfc3a --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,45 @@ +name: Rust CI + +on: + push: + branches: "main" + pull_request: + branches: "main" + +env: + CARGO_TERM_COLOR: always + +jobs: + fmt-and-clippy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: Swatinem/rust-cache@v2.7.0 + - name: Version info + run: | + rustc --version + cargo clippy --version + - name: Fmt check + run: | + if ! cargo fmt -- --check ; then + echo "Formatting errors detected, apply make fmt." + exit 1 + fi + - name: Clippy check + run: | + if ! cargo clippy -- -D warnings ; then + echo "Clippy warnings detected, apply proper fixes." + exit 1 + fi + + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: Swatinem/rust-cache@v2.7.0 + - name: Run Tests + run: | + if ! cargo test ; then + echo "Tests failed." + exit 1 + fi \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d949082 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +.PHONY: set-hook fmt fix check + +set-hook: + chmod u+x .githooks/* + git config core.hooksPath .githooks + +fmt: + cargo fmt + +fix: + cargo clippy --fix --allow-dirty --allow-staged + +check: + cargo fmt -- --check + cargo clippy -- -D warnings \ No newline at end of file diff --git a/src/circuit.rs b/src/circuit.rs index ae29384..795e73d 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -14,14 +14,20 @@ pub struct Circuit { pub wires: Vec, } +impl Default for Circuit { + fn default() -> Self { + Self::new() + } +} + impl Circuit { pub fn new() -> Self { - return Circuit { + Circuit { input_sizes: vec![32, 32], output_sizes: vec![32], gates: vec![Box::new(NotGate::new(vec![], vec![]))], wires: vec![], - }; + } } } @@ -54,7 +60,7 @@ impl CircuitTrait for Circuit { let x: usize = words.next().unwrap().parse().unwrap(); output_sizes.push(x); } - } else if line_str != "" { + } else if !line_str.is_empty() { let mut words = line_str.split_whitespace(); let noi = words.next().unwrap().parse().unwrap(); // number of inputs let noo = words.next().unwrap().parse().unwrap(); // number of outputs diff --git a/src/gates.rs b/src/gates.rs index 6f9a7d9..b7a4fa0 100644 --- a/src/gates.rs +++ b/src/gates.rs @@ -9,16 +9,16 @@ pub struct NotGate { impl NotGate { pub fn new(input_wires: Vec, output_wires: Vec) -> Self { - return NotGate { + NotGate { input_wires, output_wires, - }; + } } } impl GateTrait for NotGate { fn create_challenge_script(&self) -> String { - return "NotGate".to_string(); + "NotGate".to_string() } } @@ -29,16 +29,16 @@ pub struct AndGate { impl AndGate { pub fn new(input_wires: Vec, output_wires: Vec) -> Self { - return AndGate { + AndGate { input_wires, output_wires, - }; + } } } impl GateTrait for AndGate { fn create_challenge_script(&self) -> String { - return "NotGate".to_string(); + "NotGate".to_string() } } @@ -49,15 +49,15 @@ pub struct XorGate { impl XorGate { pub fn new(input_wires: Vec, output_wires: Vec) -> Self { - return XorGate { + XorGate { input_wires, output_wires, - }; + } } } impl GateTrait for XorGate { fn create_challenge_script(&self) -> String { - return "NotGate".to_string(); + "NotGate".to_string() } } diff --git a/src/main.rs b/src/main.rs index f803547..c042d8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,5 +3,6 @@ use bitvmrs::{circuit::Circuit, traits::circuit::CircuitTrait}; fn main() { println!("Hello, world!"); let circuit = Circuit::from_bristol("bristol/add.txt"); + println!("{}", circuit.input_sizes[0]); } diff --git a/src/traits/bit_commitment.rs b/src/traits/bit_commitment.rs index a283ec3..e6e336d 100644 --- a/src/traits/bit_commitment.rs +++ b/src/traits/bit_commitment.rs @@ -1,2 +1 @@ -pub trait BitCommitmentTrait { -} \ No newline at end of file +pub trait BitCommitmentTrait {} diff --git a/src/traits/circuit.rs b/src/traits/circuit.rs index b0442ec..0302429 100644 --- a/src/traits/circuit.rs +++ b/src/traits/circuit.rs @@ -1,4 +1,3 @@ - // This trait defines the behavior of a circuit. pub trait CircuitTrait { fn evaluate(&self); @@ -6,4 +5,4 @@ pub trait CircuitTrait { fn from_bristol(file: &str) -> Self; fn generate_commitment_tree(&self); -} \ No newline at end of file +} diff --git a/src/traits/gate.rs b/src/traits/gate.rs index 90a5d48..d487b26 100644 --- a/src/traits/gate.rs +++ b/src/traits/gate.rs @@ -1,3 +1,3 @@ pub trait GateTrait { fn create_challenge_script(&self) -> String; -} \ No newline at end of file +} diff --git a/src/traits/mod.rs b/src/traits/mod.rs index 1e74085..311a838 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -1,4 +1,4 @@ pub mod bit_commitment; -pub mod gate; pub mod circuit; -pub mod wire; \ No newline at end of file +pub mod gate; +pub mod wire; diff --git a/src/utils.rs b/src/utils.rs index 6e47346..43efcbc 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,7 +3,9 @@ use std::io::{self, BufRead}; use std::path::Path; pub fn read_lines

(filename: P) -> io::Result>> -where P: AsRef, { +where + P: AsRef, +{ let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } diff --git a/src/wire.rs b/src/wire.rs index 228a3da..714b9a8 100644 --- a/src/wire.rs +++ b/src/wire.rs @@ -14,6 +14,12 @@ pub struct Wire { pub selector: Option, } +impl Default for Wire { + fn default() -> Self { + Self::new() + } +} + impl Wire { pub fn new() -> Self { let mut rng = rand::thread_rng(); @@ -26,11 +32,11 @@ impl Wire { let hash2 = Target::from_le_bytes(sha256::Hash::hash(&preimage2.to_le_bytes()).to_byte_array()); - return Wire { + Wire { preimages: Some([preimage1, preimage2]), hashes: [hash1, hash2], selector: None, - }; + } } } @@ -38,10 +44,10 @@ impl WireTrait for Wire { fn generate_anti_contradiction_script(&self) -> ScriptBuf { Builder::new() .push_opcode(OP_SHA256) - .push_slice(&self.hashes[0].to_le_bytes()) + .push_slice(self.hashes[0].to_le_bytes()) .push_opcode(OP_EQUALVERIFY) .push_opcode(OP_SHA256) - .push_slice(&self.hashes[1].to_le_bytes()) + .push_slice(self.hashes[1].to_le_bytes()) .push_opcode(OP_EQUAL) .into_script() } @@ -65,4 +71,3 @@ mod tests { // TODO:Test if script returns 1 given input witness with [preimages[0], preimages[1] } } -