Skip to content

Commit db889f1

Browse files
authored
Merge pull request #507 from chainwayxyz/ozan/old-circuits/cleanup
Old circuits cleanup
2 parents c268007 + 4016175 commit db889f1

File tree

40 files changed

+2732
-1000
lines changed

40 files changed

+2732
-1000
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: 'Install risc0'
2+
description: 'Installs risc0 toolchain'
3+
4+
inputs:
5+
github_token:
6+
description: 'GitHub token for authentication'
7+
required: true
8+
9+
runs:
10+
using: "composite"
11+
steps:
12+
- name: Install cargo-risczero
13+
uses: taiki-e/install-action@v2
14+
with:
15+
tool: cargo-risczero@1.2.0
16+
17+
- name: Install risc0-zkvm toolchain
18+
shell: bash
19+
env:
20+
GITHUB_TOKEN: ${{ inputs.github_token }}
21+
run: cargo risczero install --version r0.1.81.0

.github/workflows/build_and_test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v4
19+
- uses: ./.github/actions/install-risc0
1920
- uses: ./.github/actions/build-prerequisites
2021

2122
- name: Save build artifacts
@@ -32,6 +33,7 @@ jobs:
3233

3334
steps:
3435
- uses: actions/checkout@v4
36+
- uses: ./.github/actions/install-risc0
3537
- uses: ./.github/actions/build-prerequisites
3638

3739
- name: Save build artifacts
@@ -65,6 +67,7 @@ jobs:
6567
6668
steps:
6769
- uses: actions/checkout@v4
70+
- uses: ./.github/actions/install-risc0
6871
- uses: ./.github/actions/build-prerequisites
6972
- uses: ./.github/actions/test-prerequisites
7073

@@ -98,6 +101,7 @@ jobs:
98101
99102
steps:
100103
- uses: actions/checkout@v4
104+
- uses: ./.github/actions/install-risc0
101105
- uses: ./.github/actions/build-prerequisites
102106
- uses: ./.github/actions/test-prerequisites
103107

.github/workflows/code_checks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jobs:
6868
6969
steps:
7070
- uses: actions/checkout@v4
71+
- uses: ./.github/actions/install-risc0
7172
- uses: ./.github/actions/build-prerequisites
7273
- uses: ./.github/actions/test-prerequisites
7374

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "2"
3-
members = ["core"] # TODO: Add "circuits" back later
3+
members = ["core", "circuits-lib"] # Add "risc0-circuits/operator", "risc0-circuits/watchtower" later
44

55
[workspace.dependencies]
66
hex = "0.4.3"
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
2-
name = "clementine-circuits"
3-
version = "0.0.0"
2+
name = "circuits-lib"
3+
version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
crypto-bigint = { workspace = true }
8-
k256 = { workspace = true, features = ["arithmetic", "serde", "expose-field", "ecdsa"] }
97
sha2 = { workspace = true }
108
serde = { workspace = true, features = ["derive"] }
119
lazy_static = { workspace = true, features = ["spin_no_std"] }
1210
tracing = { workspace = true, default-features = false }
11+
risc0-zkvm = {version = "1.2.0", default-features = false, features = ["std"]}
12+
borsh = {version = "1.5.3", features = ["derive"] }

circuits-lib/src/common/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Constant bridge amount in sats
2+
pub const BRIDGE_AMOUNT_SATS: u64 = 1_000_000_000;

circuits-lib/src/common/hashes.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use sha2::{Digest, Sha256};
2+
3+
pub fn calculate_double_sha256(input: &[u8]) -> [u8; 32] {
4+
let mut hasher = Sha256::default();
5+
hasher.update(input);
6+
let result = hasher.finalize_reset();
7+
hasher.update(result);
8+
hasher.finalize().into()
9+
}
10+
11+
pub fn calculate_sha256(input: &[u8]) -> [u8; 32] {
12+
let mut hasher = Sha256::default();
13+
hasher.update(input);
14+
hasher.finalize().into()
15+
}
16+
17+
/// Utility function to hash two nodes together
18+
pub fn hash_pair(left: [u8; 32], right: [u8; 32]) -> [u8; 32] {
19+
let mut hasher = Sha256::default();
20+
hasher.update(left);
21+
hasher.update(right);
22+
hasher.finalize().into()
23+
}

circuits-lib/src/common/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod constants;
2+
pub mod hashes;
3+
pub mod zkvm;

circuits-lib/src/common/zkvm.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::io::Write;
2+
3+
use borsh::BorshDeserialize;
4+
use risc0_zkvm::guest::env::{self};
5+
6+
pub trait ZkvmGuest {
7+
fn read_from_host<T: borsh::BorshDeserialize>(&self) -> T;
8+
fn commit<T: borsh::BorshSerialize>(&self, item: &T);
9+
fn verify<T: borsh::BorshSerialize>(&self, method_id: [u32; 8], journal: &T);
10+
}
11+
12+
#[derive(Debug, Clone)]
13+
pub struct Proof {
14+
pub method_id: [u32; 8],
15+
pub journal: Vec<u8>,
16+
}
17+
18+
pub trait ZkvmHost {
19+
// Adding data to the host
20+
fn write<T: borsh::BorshSerialize>(&self, value: &T);
21+
22+
fn add_assumption(&self, proof: Proof);
23+
24+
// Proves with the given data
25+
fn prove(&self, elf: &[u32]) -> Proof;
26+
}
27+
28+
#[derive(Debug, Clone)]
29+
pub struct Risc0Guest;
30+
31+
impl Risc0Guest {
32+
pub fn new() -> Self {
33+
Self {}
34+
}
35+
}
36+
37+
impl Default for Risc0Guest {
38+
fn default() -> Self {
39+
Self::new()
40+
}
41+
}
42+
43+
impl ZkvmGuest for Risc0Guest {
44+
fn read_from_host<T: borsh::BorshDeserialize>(&self) -> T {
45+
let mut reader = env::stdin();
46+
BorshDeserialize::deserialize_reader(&mut reader)
47+
.expect("Failed to deserialize input from host")
48+
}
49+
50+
fn commit<T: borsh::BorshSerialize>(&self, item: &T) {
51+
// use risc0_zkvm::guest::env::Write as _;
52+
let buf = borsh::to_vec(item).expect("Serialization to vec is infallible");
53+
let mut journal = env::journal();
54+
journal.write_all(&buf).unwrap();
55+
}
56+
57+
fn verify<T: borsh::BorshSerialize>(&self, method_id: [u32; 8], output: &T) {
58+
env::verify(method_id, &borsh::to_vec(output).unwrap()).unwrap();
59+
}
60+
}

circuits-lib/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub mod common;
2+
pub mod operator;
3+
pub mod watchtower;
4+
5+
use common::zkvm::ZkvmGuest;
6+
// use operator::{OperatorCircuitInput, OperatorCircuitOutput};
7+
// use watchtower::{WatchtowerCircuitInput, WatchtowerCircuitOutput};
8+
pub use risc0_zkvm;
9+
10+
pub fn operator_circuit(_guest: &impl ZkvmGuest) {
11+
// let start = risc0_zkvm::guest::env::cycle_count();
12+
// let input: OperatorCircuitInput = guest.read_from_host();
13+
// TODO: Implement operator circuit
14+
// guest.commit(&OperatorCircuitOutput {});
15+
// let end = risc0_zkvm::guest::env::cycle_count();
16+
// println!("Operator circuit took {:?} cycles", end - start);
17+
}
18+
19+
pub fn watchtower_circuit(_guest: &impl ZkvmGuest) {
20+
// let start = risc0_zkvm::guest::env::cycle_count();
21+
// let input: WatchtowerCircuitInput = guest.read_from_host();
22+
// TODO: Implement watchtower circuit
23+
// guest.commit(&WatchtowerCircuitOutput {});
24+
// let end = risc0_zkvm::guest::env::cycle_count();
25+
// println!("Operator circuit took {:?} cycles", end - start);
26+
}

circuits-lib/src/operator/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use borsh::{BorshDeserialize, BorshSerialize};
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
5+
pub struct OperatorCircuitInput {}
6+
7+
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
8+
pub struct OperatorCircuitOutput {}

circuits-lib/src/watchtower/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use borsh::{BorshDeserialize, BorshSerialize};
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
5+
pub struct WatchtowerCircuitInput {}
6+
7+
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
8+
pub struct WatchtowerCircuitOutput {}

0 commit comments

Comments
 (0)