Skip to content

Commit

Permalink
feat(cometbls-near): groth16 verifier with host functions
Browse files Browse the repository at this point in the history
Signed-off-by: aeryz <[email protected]>
  • Loading branch information
aeryz committed Jul 25, 2024
1 parent 709abc6 commit 8401044
Show file tree
Hide file tree
Showing 14 changed files with 674 additions and 11 deletions.
31 changes: 31 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
Expand Up @@ -73,7 +73,7 @@ members = [
"light-clients/near/near",
"near/dummy-ibc-app",
"near/near-ibc-tests",
"light-clients/near/ics08-near", "lib/near-verifier", "light-clients/cometbls/near", "poc-relayer",
"light-clients/near/ics08-near", "lib/near-verifier", "light-clients/cometbls/near", "poc-relayer", "near/test-circuit",
]

[workspace.package]
Expand Down
28 changes: 28 additions & 0 deletions lib/cometbls-groth16-verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ fn verify_generic_zkp_2(
.chain_update(trusted_validators_hash)
.finalize(),
);
// println!("commitment: {:?}", commitment_hash.to_be_bytes());
// drop the most significant byte to fit in bn254 F_r
inputs_hash[0] = 0;
let public_inputs: [substrate_bn::Fr; NB_PUBLIC_INPUTS] = [
Expand Down Expand Up @@ -460,4 +461,31 @@ mod tests {
Err(Error::InvalidProof)
);
}

#[test]
fn print_consts() {
let mut buffer = [0u8; 64];
GAMMA_ABC_G1[2]
.x()
.to_big_endian(&mut buffer[0..32])
.unwrap();
GAMMA_ABC_G1[2]
.y()
.to_big_endian(&mut buffer[32..64])
.unwrap();
buffer.reverse();
println!(
"{}",
format!(
r#"
G1Affine {{
x: Fq({:?}),
y: Fq({:?}),
}}
"#,
&buffer[32..64],
&buffer[0..32],
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
from
)
)]
#[derive(Default)]
pub struct ClientState {
pub chain_id: String,
pub trusting_period: u64,
Expand Down
1 change: 1 addition & 0 deletions lib/unionlabs/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use core::{
str::FromStr,
};

use borsh::BorshSerialize;
use serde::{Deserialize, Serialize};
use serde_utils::HEX_ENCODING_PREFIX;

Expand Down
2 changes: 2 additions & 0 deletions light-clients/cometbls/near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ borsh = { workspace = true, features = [ "derive"] }
thiserror = { workspace = true }
ics23 = { workspace = true }
cometbls-groth16-verifier = { workspace = true }
hex-literal = {workspace = true }
hex.workspace = true

[lints]
workspace = true
26 changes: 18 additions & 8 deletions light-clients/cometbls/near/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use unionlabs::{
id::ClientId,
};

use crate::error::Error;
use crate::{error::Error, verifier::verify_zkp};

#[near_bindgen]
#[derive(PanicOnDefault, BorshDeserialize, BorshSerialize)]
Expand Down Expand Up @@ -205,13 +205,13 @@ impl Contract {
return false;
}

// cometbls_groth16_verifier::verify_zkp(
// &self.client_state.chain_id,
// trusted_validators_hash,
// &header.signed_header,
// header.zero_knowledge_proof,
// )
// .unwrap();
verify_zkp(
&self.client_state.chain_id,
trusted_validators_hash,
&header.signed_header,
header.zero_knowledge_proof,
)
.unwrap();

true
}
Expand All @@ -221,6 +221,16 @@ impl Contract {
false
}

pub fn test_circuit(
&self,
chain_id: String,
trusted_validators_hash: unionlabs::hash::H256,
header: unionlabs::ibc::lightclients::cometbls::light_header::LightHeader,
zkp: Vec<u8>,
) {
verify_zkp(&chain_id, trusted_validators_hash, &header, zkp).unwrap()
}

pub fn update_client(&mut self, client_msg: Vec<u8>) -> (Vec<u8>, Vec<(Height, Vec<u8>)>) {
let header = Header::decode_as::<Proto>(&client_msg).unwrap();

Expand Down
9 changes: 9 additions & 0 deletions light-clients/cometbls/near/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ pub enum Error {

#[error("the chain id cannot be more than 31 bytes long to fit in the bn254 scalar field")]
InvalidChainId,

#[error("invalid zkp length")]
InvalidZKPLength,

#[error("invalid height")]
InvalidHeight,

#[error("invalid timestamp")]
InvalidTimestamp,
}
1 change: 1 addition & 0 deletions light-clients/cometbls/near/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod contract;
pub mod error;
pub mod verifier;
Loading

0 comments on commit 8401044

Please sign in to comment.