Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ekrem/ci #8

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type-complexity-threshold = 9999
too-many-arguments-threshold = 20
16 changes: 16 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
- 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/[email protected]
- name: Run Tests
run: |
if ! cargo test ; then
echo "Tests failed."
exit 1
fi
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
12 changes: 9 additions & 3 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ pub struct Circuit {
pub wires: Vec<Wire>,
}

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![],
};
}
}
}

Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ pub struct NotGate {

impl NotGate {
pub fn new(input_wires: Vec<Wire>, output_wires: Vec<Wire>) -> 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()
}
}

Expand All @@ -29,16 +29,16 @@ pub struct AndGate {

impl AndGate {
pub fn new(input_wires: Vec<Wire>, output_wires: Vec<Wire>) -> 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()
}
}

Expand All @@ -49,15 +49,15 @@ pub struct XorGate {

impl XorGate {
pub fn new(input_wires: Vec<Wire>, output_wires: Vec<Wire>) -> 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()
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
3 changes: 1 addition & 2 deletions src/traits/bit_commitment.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub trait BitCommitmentTrait {
}
pub trait BitCommitmentTrait {}
3 changes: 1 addition & 2 deletions src/traits/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

// This trait defines the behavior of a circuit.
pub trait CircuitTrait {
fn evaluate(&self);

fn from_bristol(file: &str) -> Self;

fn generate_commitment_tree(&self);
}
}
2 changes: 1 addition & 1 deletion src/traits/gate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub trait GateTrait {
fn create_challenge_script(&self) -> String;
}
}
4 changes: 2 additions & 2 deletions src/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod bit_commitment;
pub mod gate;
pub mod circuit;
pub mod wire;
pub mod gate;
pub mod wire;
4 changes: 3 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::io::{self, BufRead};
use std::path::Path;

pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
15 changes: 10 additions & 5 deletions src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ pub struct Wire {
pub selector: Option<bool>,
}

impl Default for Wire {
fn default() -> Self {
Self::new()
}
}

impl Wire {
pub fn new() -> Self {
let mut rng = rand::thread_rng();
Expand All @@ -26,22 +32,22 @@ 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,
};
}
}
}

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()
}
Expand All @@ -65,4 +71,3 @@ mod tests {
// TODO:Test if script returns 1 given input witness with [preimages[0], preimages[1]
}
}