Skip to content

Commit

Permalink
Utreexo integration tests (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus authored Oct 21, 2024
1 parent 44831a2 commit c830ced
Show file tree
Hide file tree
Showing 41 changed files with 1,853 additions and 3 deletions.
Empty file.
1 change: 1 addition & 0 deletions packages/utreexo/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ cairo_test.workspace = true
[scripts]
# TODO: cairo lint
lint = "scarb fmt"
test = "scarb cairo-test && scarb build && ../../scripts/data/integration_tests.sh"
1 change: 1 addition & 0 deletions packages/utreexo/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod stump {
pub mod proof;
pub mod accumulator;
}
pub mod test;

use core::poseidon::PoseidonTrait;
use core::hash::{HashStateTrait, HashStateExTrait};
Expand Down
2 changes: 1 addition & 1 deletion packages/utreexo/src/stump/proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use utils::{numeric::u64_next_power_of_two, sort::bubble_sort};

/// Utreexo inclusion proof for multiple outputs.
/// Compatible with https://github.com/utreexo/utreexo
#[derive(Drop, Copy)]
#[derive(Drop, Copy, Serde, Debug)]
pub struct UtreexoBatchProof {
/// List of sibling nodes required to calculate the roots.
pub proof: Span<felt252>,
Expand Down
43 changes: 43 additions & 0 deletions packages/utreexo/src/test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::stump::state::UtreexoStumpState;
use crate::stump::proof::UtreexoBatchProof;
use crate::stump::accumulator::StumpUtreexoAccumulator;
use core::testing::get_available_gas;

#[derive(Drop, Serde, Debug)]
struct Args {
state: UtreexoStumpState,
proof: UtreexoBatchProof,
leaves_to_del: Array<felt252>,
leaves_to_add: Array<felt252>,
expected_state: UtreexoStumpState,
}

fn main(mut arguments: Span<felt252>, _flags: felt252) {
let mut gas_before = get_available_gas();

let Args { mut state, proof, leaves_to_del, leaves_to_add: _, expected_state } =
Serde::deserialize(
ref arguments
)
.expect('Failed to deserialize');

match state.verify_and_delete(@proof, leaves_to_del.span()) {
Result::Ok(new_state) => { state = new_state; },
Result::Err(err) => {
println!("FAIL: gas_spent={} error='{:?}'", gas_before - get_available_gas(), err);
panic!();
}
}

if state != expected_state {
println!(
"FAIL: gas_spent={} error='expected state {:?}, actual {:?}'",
gas_before - get_available_gas(),
expected_state,
state
);
panic!();
}

println!("OK: gas_spent={}", gas_before - get_available_gas());
}
44 changes: 44 additions & 0 deletions packages/utreexo/tests/data/cached_proof_test_case_0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [
359114454570462701179676018441683730149326686283278794303413350979946254235,
2920447154653459698578961030005574439730780339384884329678783637696763668074
],
"targets": [
0,
1
]
},
"leaves_to_del": [],
"leaves_to_add": [
20,
21,
22
],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 1566871981950648467436488177931532718969053285649065514773309084960104998335
},
{
"variant_id": 0,
"value": 2027894189441199778894566861688137166317361494271272989413883987271219468103
},
{
"variant_id": 0,
"value": 22
}
],
"leaves": 11
}
}
40 changes: 40 additions & 0 deletions packages/utreexo/tests/data/cached_proof_test_case_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [
5,
359114454570462701179676018441683730149326686283278794303413350979946254235,
2476911194812244264213538976037850550079366744233323933541290896048104351430
],
"targets": [
0,
1,
4
]
},
"leaves_to_del": [],
"leaves_to_add": [
8
],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 1684605927563332242289960912912659092722359194724270059585709599845081574916
},
{
"variant_id": 0,
"value": 8
}
],
"leaves": 9
}
}
38 changes: 38 additions & 0 deletions packages/utreexo/tests/data/cached_proof_test_case_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [
5,
2476911194812244264213538976037850550079366744233323933541290896048104351430,
1229371501456391789924831928153470943555736434402105893904574254763197682709
],
"targets": [
4
]
},
"leaves_to_del": [],
"leaves_to_add": [
8
],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 2543242248923883501889459312371029730658374096283188732149186252132178387348
},
{
"variant_id": 0,
"value": 8
}
],
"leaves": 9
}
}
49 changes: 49 additions & 0 deletions packages/utreexo/tests/data/cached_proof_test_case_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
},
{
"variant_id": 0,
"value": 2224391216833402212724735773212940252356536072547264452695587483243907176367
},
{
"variant_id": 0,
"value": 3009277733733429566191908933097273596911357184294896938062356457901603258437
}
],
"leaves": 14
},
"proof": {
"nodes": [
1229371501456391789924831928153470943555736434402105893904574254763197682709
],
"targets": [
4,
5,
6,
7
]
},
"leaves_to_del": [],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 1229371501456391789924831928153470943555736434402105893904574254763197682709
},
{
"variant_id": 0,
"value": 2224391216833402212724735773212940252356536072547264452695587483243907176367
},
{
"variant_id": 0,
"value": 3009277733733429566191908933097273596911357184294896938062356457901603258437
}
],
"leaves": 14
}
}
32 changes: 32 additions & 0 deletions packages/utreexo/tests/data/cached_proof_test_case_4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [
1,
359114454570462701179676018441683730149326686283278794303413350979946254235,
2920447154653459698578961030005574439730780339384884329678783637696763668074
],
"targets": [
0
]
},
"leaves_to_del": [],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 1390204248042318895134432832714975405388923780014300346567243638667265469832
}
],
"leaves": 8
}
}
36 changes: 36 additions & 0 deletions packages/utreexo/tests/data/cached_proof_test_case_5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 313455620397951742217570265587996884547193273693570485296736643582499576122
}
],
"leaves": 32
},
"proof": {
"nodes": [
2,
7,
0,
5,
2761512008972594569397269935073111909677583342716463654277011780899949492239,
226102115184302870269476346253132176384258397936010785698544099242811230814
],
"targets": [
3,
6
]
},
"leaves_to_del": [],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 3131467188211843618406071253624707105300726788230916916663054629597474049049
}
],
"leaves": 32
}
}
42 changes: 42 additions & 0 deletions packages/utreexo/tests/data/cached_proof_test_case_6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1347517334382637346943874592983838377757642136153945464698821274069571697599
},
{
"variant_id": 0,
"value": 11
}
],
"leaves": 12
},
"proof": {
"nodes": [
2,
7,
0,
5
],
"targets": [
3,
6
]
},
"leaves_to_del": [],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 2876516617194610423170642440651301959278853848182275138875020734413818300640
},
{
"variant_id": 0,
"value": 11
}
],
"leaves": 12
}
}
Loading

0 comments on commit c830ced

Please sign in to comment.