Skip to content

Commit

Permalink
feat(axon-tools): add precompile payload codec (#1607)
Browse files Browse the repository at this point in the history
* feat(axon-tools): add precompile payload codec

* make sort

* add verify proof function
  • Loading branch information
Eason Gao authored Dec 1, 2023
1 parent c3bec49 commit bd7f55c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions devtools/axon-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ hash = ["tiny-keccak"]
hex = ["faster-hex"]
impl-rlp = ["rlp", "rlp-derive", "ethereum-types/rlp"]
impl-serde = ["serde", "ethereum-types/serialize"]
precompile = ["ethers-contract", "ethers-core"]
std = ["cita_trie", "hex", "log", "derive_more", "serde_json"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -49,6 +50,10 @@ features = ["serde"]
version = "4.0"
optional = true

[dependencies.ckb-types]
version = "0.111"
optional = true

[dependencies.derive_more]
version = "0.99"
optional = true
Expand All @@ -58,6 +63,14 @@ version = "0.14"
default-features = false
features = ["serialize"]

[dependencies.ethers-contract]
version = "2.0"
optional = true

[dependencies.ethers-core]
version = "2.0"
optional = true

[dependencies.faster-hex]
version = "0.8"
optional = true
Expand Down
2 changes: 2 additions & 0 deletions devtools/axon-tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod hash;
#[cfg(feature = "hex")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "hex")))]
pub mod hex;
#[cfg(feature = "precompile")]
pub mod precompile;
#[cfg(feature = "proof")]
mod proof;
mod rlp_codec;
Expand Down
62 changes: 62 additions & 0 deletions devtools/axon-tools/src/precompile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use ckb_types::utilities::{merkle_root, MerkleProof};
use ckb_types::{packed, prelude::*};
use ethers_contract::{EthAbiCodec, EthAbiType};

#[derive(EthAbiCodec, EthAbiType, Clone, Debug, PartialEq, Eq)]
pub struct VerifyProofPayload {
/// If the verify_type is 0, the leaves should be in the
/// raw_transactions_root, otherwise in the witnesses_root.
pub verify_type: u8,
pub transactions_root: [u8; 32],
pub witnesses_root: [u8; 32],
pub raw_transactions_root: [u8; 32],
pub proof: Proof,
}

#[derive(EthAbiCodec, EthAbiType, Clone, Debug, PartialEq, Eq)]
pub struct Proof {
pub indices: Vec<u32>,
pub lemmas: Vec<[u8; 32]>,
pub leaves: Vec<[u8; 32]>,
}

/// A standalone function to verify the ckb-merkle-binary-tree proof.
pub fn verify_proof(payload: VerifyProofPayload) -> Result<(), String> {
// Firstly, verify the transactions_root is consist of the raw_transactions_root
// and witnesses_root
let transactions_root: packed::Byte32 = payload.transactions_root.pack();
let raw_transactions_root: packed::Byte32 = payload.raw_transactions_root.pack();
let witnesses_root: packed::Byte32 = payload.witnesses_root.pack();

if merkle_root(&[raw_transactions_root.clone(), witnesses_root.clone()]) != transactions_root {
return Err(String::from("verify transactions_root fail"));
}

// Then, verify the given indices and lemmas can prove the leaves contains in
// the raw_transactions_root or the witnesses_root.
// If the verify_type is 0, the leaves should be in the raw_transactions_root,
// otherwise in the witnesses_root.
let lemmas = payload
.proof
.lemmas
.iter()
.map(|l| l.pack())
.collect::<Vec<_>>();
let leaves = payload
.proof
.leaves
.iter()
.map(|l| l.pack())
.collect::<Vec<_>>();
let verifying_root = if payload.verify_type == 0 {
raw_transactions_root
} else {
witnesses_root
};

if MerkleProof::new(payload.proof.indices, lemmas).verify(&verifying_root, &leaves) {
return Ok(());
}

Err(String::from("verify raw transactions root failed"))
}

0 comments on commit bd7f55c

Please sign in to comment.