Skip to content

Commit

Permalink
wip: experimenting with a packed transaction model
Browse files Browse the repository at this point in the history
  • Loading branch information
cfcosta committed Aug 4, 2024
1 parent 4d30db4 commit b539449
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
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.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde_bytes = "0.11.15"

proptest = { version = "1.5.0", optional = true }
test-strategy = { version = "0.4.0", optional = true }
smallvec = { version = "1.13.2", features = ["serde"] }

[features]
default = ["proptest"]
Expand Down
6 changes: 6 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ risc0-zkvm = { version = "1.0.5", features = ["std"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tracing-timing = "0.6.0"
smallvec = { version = "1.13.2", features = ["serde"] }
iai = "0.1.1"

[features]
default = []
metal = ["risc0-zkvm/metal"]
cuda = ["risc0-zkvm/cuda"]

[[bench]]
name = "foo"
harness = false
73 changes: 73 additions & 0 deletions examples/benches/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#[no_std]
use mugraph_core::{types::Hash, util::BitSet8};
use smallvec::SmallVec;

#[derive(Default)]
pub struct Transaction {
pub mask: BitSet8,
pub inputs: Inputs<4>,
pub outputs: Outputs<4>,
pub data: SmallVec<[[u8; 256]; 8]>,
}

#[derive(Default)]
pub struct Inputs<const N: usize> {
pub parents: SmallVec<[Hash; N]>,
pub indexes: SmallVec<[u8; N]>,
pub asset_ids: SmallVec<[Hash; N]>,
pub amounts: SmallVec<[u64; N]>,
pub program_id: SmallVec<[Option<Hash>; N]>,
pub data: SmallVec<[u32; N]>,
}

#[derive(Default)]
pub struct Outputs<const N: usize> {
pub asset_ids: SmallVec<[Hash; N]>,
pub amounts: SmallVec<[u64; N]>,
pub program_id: SmallVec<[Option<Hash>; N]>,
pub data: SmallVec<[u32; N]>,
}

pub struct Seal {
pub nullifiers: SmallVec<[Hash; 4]>,
}

fn test() {
let transaction = Transaction::default();

let mut input_amounts = [0u64; 256];
let mut output_amounts = [0u64; 256];

for (asset_id, amount) in transaction
.inputs
.asset_ids
.iter()
.zip(transaction.inputs.amounts.iter())
{
let index = asset_id.as_bytes()[0] as usize;
input_amounts[index] = input_amounts[index].wrapping_add(*amount);
}

for (asset_id, amount) in transaction
.outputs
.asset_ids
.iter()
.zip(transaction.outputs.amounts.iter())
{
let index = asset_id.as_bytes()[0] as usize;
output_amounts[index] = output_amounts[index].wrapping_add(*amount);
}

let mut is_valid = true;

for i in 0..256 {
is_valid &= input_amounts[i] == output_amounts[i];
}

assert!(
is_valid,
"Input and output amounts do not match for all asset IDs"
);
}

iai::main!(test);

0 comments on commit b539449

Please sign in to comment.