Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed Aug 25, 2023
1 parent 89dbaa9 commit 85d4bd4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 96 deletions.
34 changes: 3 additions & 31 deletions src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs;

/// Source (tendermint-rs): https://github.com/informalsystems/tendermint-rs/blob/e930691a5639ef805c399743ac0ddbba0e9f53da/tendermint/src/merkle.rs#L32
use crate::utils::{
compute_hash_from_aunts, compute_hash_from_proof, generate_proofs_from_block_id,
compute_hash_from_aunts,
generate_proofs_from_header, leaf_hash, non_absent_vote, SignedBlock, TempSignedBlock,
};
use ed25519_consensus::SigningKey;
Expand Down Expand Up @@ -165,7 +165,7 @@ fn generate_base_inputs<const VALIDATOR_SET_SIZE_MAX: usize>(
}

// These are empty signatures (not included in val hash)
for i in block.commit.signatures.len()..VALIDATOR_SET_SIZE_MAX {
for _ in block.commit.signatures.len()..VALIDATOR_SET_SIZE_MAX {
let priv_key_bytes = vec![0u8; 32];
let signing_key =
private_key::Ed25519::try_from(&priv_key_bytes[..]).expect("failed to create key");
Expand Down Expand Up @@ -344,7 +344,6 @@ pub fn generate_skip_inputs<const VALIDATOR_SET_SIZE_MAX: usize>(
let block_validators = trusted_block.validator_set.validators();

for i in 0..trusted_block.commit.signatures.len() {
let val_idx = ValidatorIndex::try_from(i).unwrap();
let validator = Box::new(
match trusted_block
.validator_set
Expand All @@ -364,7 +363,7 @@ pub fn generate_skip_inputs<const VALIDATOR_SET_SIZE_MAX: usize>(
}

// These are empty signatures (not included in val hash)
for i in trusted_block.commit.signatures.len()..VALIDATOR_SET_SIZE_MAX {
for _ in trusted_block.commit.signatures.len()..VALIDATOR_SET_SIZE_MAX {
let priv_key_bytes = vec![0u8; 32];
let signing_key =
private_key::Ed25519::try_from(&priv_key_bytes[..]).expect("failed to create key");
Expand Down Expand Up @@ -407,35 +406,8 @@ pub fn generate_skip_inputs<const VALIDATOR_SET_SIZE_MAX: usize>(

#[cfg(test)]
pub(crate) mod tests {
use crate::utils::generate_proofs_from_block_id;

use super::*;

#[test]
fn test_prev_header_check() {
let block_1 = get_signed_block(11000);
let block_2 = get_signed_block(11001);

assert_eq!(
block_1.header.hash(),
block_2.header.last_block_id.unwrap().hash
);

let (_root, proofs) = generate_proofs_from_header(&block_2.header);
let total = proofs[0].total;
let last_block_id_proof = proofs[4].clone();
let last_block_id_proof_indices = get_path_indices(4, total);
println!("last_block_id_proof: {:?}", last_block_id_proof.aunts);

let (_root, proofs) = generate_proofs_from_block_id(&block_2.header.last_block_id.unwrap());
let last_block_id = block_2.header.last_block_id.unwrap();

let total = proofs[0].total;
let prev_header_hash_proof = proofs[0].clone();
let prev_header_hash_proof_indices = get_path_indices(0, total);
println!("prev_header_hash_proof: {:?}", prev_header_hash_proof.aunts);
}

#[test]
fn get_shared_voting_power() {
let block_1 = get_signed_block(11000);
Expand Down
14 changes: 5 additions & 9 deletions src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ use plonky2x::ecc::ed25519::gadgets::eddsa::verify_variable_signatures_circuit;
use plonky2x::ecc::ed25519::gadgets::eddsa::{
verify_signatures_circuit, EDDSAPublicKeyTarget, EDDSASignatureTarget,
};
use plonky2x::hash::sha::sha512::calculate_num_chunks;
use plonky2x::num::nonnative::nonnative::CircuitBuilderNonNative;

use crate::utils::to_be_bits;
use crate::utils::{
EncTendermintHashTarget, TendermintHashTarget, ValidatorMessageTarget, HASH_SIZE_BITS,
TendermintHashTarget, ValidatorMessageTarget, HASH_SIZE_BITS,
VALIDATOR_MESSAGE_BYTES_LENGTH_MAX,
};

Expand Down Expand Up @@ -135,9 +134,8 @@ impl<F: RichField + Extendable<D>, const D: usize> TendermintSignature<F, D>
for i in 0..DUMMY_MSG_LENGTH_BITS {
message.push(self.constant_bool(dummy_msg_bits[i]));
}
for i in DUMMY_MSG_LENGTH_BITS..VALIDATOR_MESSAGE_BYTES_LENGTH_MAX * 8 {
message.push(self._false());
}
// Fill out the rest of the message with zeros
message.resize(VALIDATOR_MESSAGE_BYTES_LENGTH_MAX * 8, self._false());

let dummy_msg_length = self.constant(F::from_canonical_usize(DUMMY_MSG_LENGTH_BITS));

Expand Down Expand Up @@ -393,7 +391,7 @@ pub(crate) mod tests {
type C = PoseidonGoldilocksConfig;
const D: usize = 2;

let mut pw = PartialWitness::new();
let pw = PartialWitness::new();
let mut builder = CircuitBuilder::<F, D>::new(CircuitConfig::standard_ecc_config());

let msg_bits = to_be_bits(msg_bytes.to_vec());
Expand All @@ -403,9 +401,7 @@ pub(crate) mod tests {
for i in 0..msg_bits.len() {
msg_bits_target.push(builder.constant_bool(msg_bits[i]));
}
for i in msg_bits.len()..VALIDATOR_MESSAGE_BYTES_LENGTH_MAX * 8 {
msg_bits_target.push(builder._false());
}
msg_bits_target.resize(VALIDATOR_MESSAGE_BYTES_LENGTH_MAX * 8, builder._false());

let msg_bits_target = ValidatorMessageTarget(msg_bits_target.try_into().unwrap());

Expand Down
2 changes: 0 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use plonky2::hash::hash_types::RichField;

use plonky2::iop::target::BoolTarget;
use plonky2x::ecc::ed25519::curve::curve_types::Curve;
use plonky2x::ecc::ed25519::gadgets::eddsa::EDDSAPublicKeyTarget;
use plonky2x::num::u32::gadgets::arithmetic_u32::U32Target;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
Expand Down
19 changes: 8 additions & 11 deletions src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ use plonky2x::ecc::ed25519::gadgets::curve::{AffinePointTarget, CircuitBuilderCu
use plonky2x::hash::sha::sha256::{sha256, sha256_variable_length_single_chunk};
use plonky2x::num::u32::gadgets::arithmetic_u32::CircuitBuilderU32;

use crate::utils::EncBlockIDTarget;
use crate::utils::PROTOBUF_BLOCK_ID_SIZE_BITS;
use crate::utils::{
EncTendermintHashTarget, I64Target, MarshalledValidatorTarget, TendermintHashTarget,
HASH_SIZE_BITS, PROTOBUF_HASH_SIZE_BITS, VALIDATOR_BIT_LENGTH_MAX, VALIDATOR_BYTE_LENGTH_MAX,
I64Target, MarshalledValidatorTarget, TendermintHashTarget,
HASH_SIZE_BITS, VALIDATOR_BIT_LENGTH_MAX, VALIDATOR_BYTE_LENGTH_MAX,
VOTING_POWER_BITS_LENGTH_MAX, VOTING_POWER_BYTES_LENGTH_MAX,
};

Expand Down Expand Up @@ -501,16 +499,15 @@ pub(crate) mod tests {
use tendermint_proto::types::BlockId as RawBlockId;
use tendermint_proto::Protobuf;

use crate::inputs::get_path_indices;
use crate::utils::{HEADER_PROOF_DEPTH, VALIDATOR_BIT_LENGTH_MAX};

use crate::utils::{generate_proofs_from_header, hash_all_leaves, leaf_hash};

use plonky2x::num::u32::gadgets::arithmetic_u32::U32Target;

use crate::{
utils::{f_bits_to_bytes, to_be_bits},
validator::{I64Target, TendermintMarshaller},
utils::{f_bits_to_bytes, to_be_bits, generate_proofs_from_header, hash_all_leaves, leaf_hash,
I64Target, MarshalledValidatorTarget, TendermintHashTarget,
HASH_SIZE_BITS, PROTOBUF_HASH_SIZE_BITS, PROTOBUF_BLOCK_ID_SIZE_BITS, VALIDATOR_BIT_LENGTH_MAX, HEADER_PROOF_DEPTH
},
validator::TendermintMarshaller,
inputs::get_path_indices
};

type C = PoseidonGoldilocksConfig;
Expand Down
88 changes: 45 additions & 43 deletions src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,50 @@
//! read more about them here: https://protobuf.dev/programming-guides/encoding/#varints.

use curta::math::extension::CubicParameters;
use plonky2::field::extension::Extendable;
use plonky2::iop::target::BoolTarget;
use plonky2::iop::target::Target;
use plonky2::iop::witness::WitnessWrite;
use plonky2::plonk::config::AlgebraicHasher;
use plonky2::plonk::config::GenericConfig;
use plonky2::{hash::hash_types::RichField, plonk::circuit_builder::CircuitBuilder};
use plonky2x::ecc::ed25519::curve::curve_types::Curve;
use plonky2x::ecc::ed25519::curve::ed25519::Ed25519;
use plonky2x::ecc::ed25519::gadgets::curve::CircuitBuilderCurve;
use plonky2x::ecc::ed25519::gadgets::eddsa::{EDDSAPublicKeyTarget, EDDSASignatureTarget};
use plonky2x::num::nonnative::nonnative::CircuitBuilderNonNative;
use plonky2x::num::u32::gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target};
use plonky2x::prelude::PartialWitness;

use curta::math::goldilocks::cubic::GoldilocksCubicParameters;
use plonky2::{
field::{extension::Extendable, types::Field},
iop::{
target::{Target, BoolTarget},
witness::WitnessWrite,
},
plonk::{
config::{AlgebraicHasher, GenericConfig},
circuit_builder::CircuitBuilder,
},
hash::hash_types::RichField,
};

use plonky2x::{
ecc::ed25519::{
curve::{curve_types::{AffinePoint, Curve}, ed25519::Ed25519},
gadgets::{
curve::{CircuitBuilderCurve, WitnessAffinePoint},
eddsa::{EDDSAPublicKeyTarget, EDDSASignatureTarget},
},
field::ed25519_scalar::Ed25519Scalar,
},
num::{
nonnative::nonnative::CircuitBuilderNonNative,
u32::{gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target}, witness::WitnessU32},
biguint::WitnessBigUint,
},
prelude::PartialWitness,
};

use num::BigUint;
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::field::types::Field;
use plonky2::plonk::{circuit_data::CircuitConfig, config::PoseidonGoldilocksConfig};
use plonky2x::ecc::ed25519::gadgets::curve::WitnessAffinePoint;
use plonky2x::num::biguint::WitnessBigUint;
use plonky2x::num::u32::witness::WitnessU32;

use plonky2x::ecc::ed25519::curve::curve_types::AffinePoint;
use plonky2x::ecc::ed25519::field::ed25519_scalar::Ed25519Scalar;

use crate::inputs::CelestiaBaseBlockProof;
use crate::inputs::CelestiaSkipBlockProof;
use crate::inputs::{generate_step_inputs, CelestiaStepBlockProof};

use crate::signature::TendermintSignature;
use crate::utils::to_be_bits;
use crate::utils::EncBlockIDTarget;
use crate::utils::PROTOBUF_BLOCK_ID_SIZE_BITS;
use crate::utils::{
EncTendermintHashTarget, I64Target, MarshalledValidatorTarget, TendermintHashTarget,
ValidatorMessageTarget, HASH_SIZE_BITS, HEADER_PROOF_DEPTH, PROTOBUF_HASH_SIZE_BITS,
VALIDATOR_MESSAGE_BYTES_LENGTH_MAX,

use crate::{
inputs::{CelestiaBaseBlockProof, CelestiaSkipBlockProof, CelestiaStepBlockProof},
signature::TendermintSignature,
utils::{
EncTendermintHashTarget, I64Target, MarshalledValidatorTarget, TendermintHashTarget,
ValidatorMessageTarget, HASH_SIZE_BITS, HEADER_PROOF_DEPTH, PROTOBUF_HASH_SIZE_BITS,
VALIDATOR_MESSAGE_BYTES_LENGTH_MAX, PROTOBUF_BLOCK_ID_SIZE_BITS, EncBlockIDTarget, to_be_bits
},
validator::TendermintMarshaller,
voting::TendermintVoting,
};
use crate::validator::TendermintMarshaller;
use crate::voting::TendermintVoting;


#[derive(Debug, Clone)]
pub struct ValidatorTarget<C: Curve> {
Expand Down Expand Up @@ -1204,7 +1206,7 @@ pub(crate) mod tests {

fn test_step_template<const VALIDATOR_SET_SIZE_MAX: usize>(block: usize) {
let _ = env_logger::builder().is_test(true).try_init();
let mut timing = TimingTree::new("Celestia Header Verify", log::Level::Debug);
let mut timing = TimingTree::new("Verify Celestia Step", log::Level::Debug);

let mut pw = PartialWitness::new();
let config = CircuitConfig::standard_ecc_config();
Expand Down Expand Up @@ -1260,7 +1262,7 @@ pub(crate) mod tests {

fn test_skip_template<const VALIDATOR_SET_SIZE_MAX: usize>(trusted_block: usize, block: usize) {
let _ = env_logger::builder().is_test(true).try_init();
let mut timing = TimingTree::new("Celestia Header Verify", log::Level::Debug);
let mut timing = TimingTree::new("Verify Celestia Skip", log::Level::Debug);

let mut pw = PartialWitness::new();
let config = CircuitConfig::standard_ecc_config();
Expand All @@ -1272,7 +1274,7 @@ pub(crate) mod tests {
type C = PoseidonGoldilocksConfig;
const D: usize = 2;

println!("Making step circuit");
println!("Making skip circuit");

let celestia_skip_proof_target =
make_skip_circuit::<GoldilocksField, D, Curve, C, E, VALIDATOR_SET_SIZE_MAX>(
Expand Down

0 comments on commit 85d4bd4

Please sign in to comment.