Skip to content

Commit

Permalink
Merge pull request #507 from chainwayxyz/ozan/old-circuits/cleanup
Browse files Browse the repository at this point in the history
Old circuits cleanup
  • Loading branch information
mmtftr authored Feb 6, 2025
2 parents c268007 + 4016175 commit db889f1
Show file tree
Hide file tree
Showing 40 changed files with 2,732 additions and 1,000 deletions.
21 changes: 21 additions & 0 deletions .github/actions/install-risc0/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: 'Install risc0'
description: 'Installs risc0 toolchain'

inputs:
github_token:
description: 'GitHub token for authentication'
required: true

runs:
using: "composite"
steps:
- name: Install cargo-risczero
uses: taiki-e/install-action@v2
with:
tool: [email protected]

- name: Install risc0-zkvm toolchain
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
run: cargo risczero install --version r0.1.81.0
4 changes: 4 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-risc0
- uses: ./.github/actions/build-prerequisites

- name: Save build artifacts
Expand All @@ -32,6 +33,7 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-risc0
- uses: ./.github/actions/build-prerequisites

- name: Save build artifacts
Expand Down Expand Up @@ -65,6 +67,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-risc0
- uses: ./.github/actions/build-prerequisites
- uses: ./.github/actions/test-prerequisites

Expand Down Expand Up @@ -98,6 +101,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-risc0
- uses: ./.github/actions/build-prerequisites
- uses: ./.github/actions/test-prerequisites

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/code_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-risc0
- uses: ./.github/actions/build-prerequisites
- uses: ./.github/actions/test-prerequisites

Expand Down
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["core"] # TODO: Add "circuits" back later
members = ["core", "circuits-lib"] # Add "risc0-circuits/operator", "risc0-circuits/watchtower" later

[workspace.dependencies]
hex = "0.4.3"
Expand Down
8 changes: 4 additions & 4 deletions circuits/Cargo.toml → circuits-lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "clementine-circuits"
version = "0.0.0"
name = "circuits-lib"
version = "0.1.0"
edition = "2021"

[dependencies]
crypto-bigint = { workspace = true }
k256 = { workspace = true, features = ["arithmetic", "serde", "expose-field", "ecdsa"] }
sha2 = { workspace = true }
serde = { workspace = true, features = ["derive"] }
lazy_static = { workspace = true, features = ["spin_no_std"] }
tracing = { workspace = true, default-features = false }
risc0-zkvm = {version = "1.2.0", default-features = false, features = ["std"]}
borsh = {version = "1.5.3", features = ["derive"] }
2 changes: 2 additions & 0 deletions circuits-lib/src/common/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Constant bridge amount in sats
pub const BRIDGE_AMOUNT_SATS: u64 = 1_000_000_000;
23 changes: 23 additions & 0 deletions circuits-lib/src/common/hashes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use sha2::{Digest, Sha256};

pub fn calculate_double_sha256(input: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::default();
hasher.update(input);
let result = hasher.finalize_reset();
hasher.update(result);
hasher.finalize().into()
}

pub fn calculate_sha256(input: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::default();
hasher.update(input);
hasher.finalize().into()
}

/// Utility function to hash two nodes together
pub fn hash_pair(left: [u8; 32], right: [u8; 32]) -> [u8; 32] {
let mut hasher = Sha256::default();
hasher.update(left);
hasher.update(right);
hasher.finalize().into()
}
3 changes: 3 additions & 0 deletions circuits-lib/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod constants;
pub mod hashes;
pub mod zkvm;
60 changes: 60 additions & 0 deletions circuits-lib/src/common/zkvm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::io::Write;

use borsh::BorshDeserialize;
use risc0_zkvm::guest::env::{self};

pub trait ZkvmGuest {
fn read_from_host<T: borsh::BorshDeserialize>(&self) -> T;
fn commit<T: borsh::BorshSerialize>(&self, item: &T);
fn verify<T: borsh::BorshSerialize>(&self, method_id: [u32; 8], journal: &T);
}

#[derive(Debug, Clone)]
pub struct Proof {
pub method_id: [u32; 8],
pub journal: Vec<u8>,
}

pub trait ZkvmHost {
// Adding data to the host
fn write<T: borsh::BorshSerialize>(&self, value: &T);

fn add_assumption(&self, proof: Proof);

// Proves with the given data
fn prove(&self, elf: &[u32]) -> Proof;
}

#[derive(Debug, Clone)]
pub struct Risc0Guest;

impl Risc0Guest {
pub fn new() -> Self {
Self {}
}
}

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

impl ZkvmGuest for Risc0Guest {
fn read_from_host<T: borsh::BorshDeserialize>(&self) -> T {
let mut reader = env::stdin();
BorshDeserialize::deserialize_reader(&mut reader)
.expect("Failed to deserialize input from host")
}

fn commit<T: borsh::BorshSerialize>(&self, item: &T) {
// use risc0_zkvm::guest::env::Write as _;
let buf = borsh::to_vec(item).expect("Serialization to vec is infallible");
let mut journal = env::journal();
journal.write_all(&buf).unwrap();
}

fn verify<T: borsh::BorshSerialize>(&self, method_id: [u32; 8], output: &T) {
env::verify(method_id, &borsh::to_vec(output).unwrap()).unwrap();
}
}
26 changes: 26 additions & 0 deletions circuits-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub mod common;
pub mod operator;
pub mod watchtower;

use common::zkvm::ZkvmGuest;
// use operator::{OperatorCircuitInput, OperatorCircuitOutput};
// use watchtower::{WatchtowerCircuitInput, WatchtowerCircuitOutput};
pub use risc0_zkvm;

pub fn operator_circuit(_guest: &impl ZkvmGuest) {
// let start = risc0_zkvm::guest::env::cycle_count();
// let input: OperatorCircuitInput = guest.read_from_host();
// TODO: Implement operator circuit
// guest.commit(&OperatorCircuitOutput {});
// let end = risc0_zkvm::guest::env::cycle_count();
// println!("Operator circuit took {:?} cycles", end - start);
}

pub fn watchtower_circuit(_guest: &impl ZkvmGuest) {
// let start = risc0_zkvm::guest::env::cycle_count();
// let input: WatchtowerCircuitInput = guest.read_from_host();
// TODO: Implement watchtower circuit
// guest.commit(&WatchtowerCircuitOutput {});
// let end = risc0_zkvm::guest::env::cycle_count();
// println!("Operator circuit took {:?} cycles", end - start);
}
8 changes: 8 additions & 0 deletions circuits-lib/src/operator/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct OperatorCircuitInput {}

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct OperatorCircuitOutput {}
8 changes: 8 additions & 0 deletions circuits-lib/src/watchtower/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct WatchtowerCircuitInput {}

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct WatchtowerCircuitOutput {}
Loading

0 comments on commit db889f1

Please sign in to comment.