From 98b89641ab83c744492f2d9a81d050111b9217af Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Sun, 3 Mar 2024 15:24:18 -0800 Subject: [PATCH] explicitly pass in trusted_block, trusted_header_hash, target_block --- circuits/builder/verify.rs | 24 ++++++++++++++++++------ circuits/skip.rs | 12 ++++-------- circuits/step.rs | 11 +++-------- circuits/variables.rs | 7 +------ 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/circuits/builder/verify.rs b/circuits/builder/verify.rs index bec8ac7..aa81b73 100644 --- a/circuits/builder/verify.rs +++ b/circuits/builder/verify.rs @@ -102,6 +102,8 @@ pub trait TendermintVerify, const D: usize> { fn verify_step( &mut self, expected_chain_id_bytes: &[u8], + prev_block_number: U64Variable, + prev_header_hash: Bytes32Variable, step: &VerifyStepVariable, ); @@ -125,6 +127,9 @@ pub trait TendermintVerify, const D: usize> { &mut self, expected_chain_id_bytes: &[u8], skip_max: usize, + trusted_block: U64Variable, + trusted_header_hash: Bytes32Variable, + target_block: U64Variable, skip: &VerifySkipVariable, ); } @@ -463,15 +468,19 @@ impl, const D: usize> TendermintVerify for CircuitBu fn verify_step( &mut self, expected_chain_id_bytes: &[u8], + prev_block_number: U64Variable, + prev_header_hash: Bytes32Variable, step: &VerifyStepVariable, ) { + let one = self.one(); + let next_block = self.add(prev_block_number, one); // Verify the new Tendermint consensus block. self.verify_header::( expected_chain_id_bytes, &step.next_block_validators, step.next_block_nb_validators, &step.next_header, - &step.next_block, + &next_block, &step.next_header_chain_id_proof, &step.next_header_height_proof, &step.next_header_validators_hash_proof, @@ -481,7 +490,7 @@ impl, const D: usize> TendermintVerify for CircuitBu // Verify the previous header hash in the new header matches the previous header. self.verify_prev_header_in_header( &step.next_header, - step.prev_header, + prev_header_hash, &step.next_header_last_block_id_proof, ); @@ -490,7 +499,7 @@ impl, const D: usize> TendermintVerify for CircuitBu step.next_header_validators_hash_proof.leaf[2..2 + HASH_SIZE].into(); self.verify_prev_header_next_validators_hash( new_validators_hash, - &step.prev_header, + &prev_header_hash, &step.prev_header_next_validators_hash_proof, ); } @@ -516,11 +525,14 @@ impl, const D: usize> TendermintVerify for CircuitBu &mut self, expected_chain_id_bytes: &[u8], skip_max: usize, + trusted_block: U64Variable, + trusted_header_hash: Bytes32Variable, + target_block: U64Variable, skip: &VerifySkipVariable, ) { // Verify the target block is non-sequential with the trusted block and within maximum // skip distance. - self.verify_skip_distance(skip_max, &skip.trusted_block, &skip.target_block); + self.verify_skip_distance(skip_max, &trusted_block, &target_block); // Verify the validators from the target block marked present_on_trusted_header // are present on the trusted header, and comprise at least 1/3 of the total voting power @@ -528,7 +540,7 @@ impl, const D: usize> TendermintVerify for CircuitBu self.verify_trusted_validators( &skip.target_block_validators, skip.target_block_nb_validators, - skip.trusted_header, + trusted_header_hash, &skip.trusted_header_validator_hash_proof, &skip.trusted_header_validator_hash_fields, skip.trusted_block_nb_validators, @@ -540,7 +552,7 @@ impl, const D: usize> TendermintVerify for CircuitBu &skip.target_block_validators, skip.target_block_nb_validators, &skip.target_header, - &skip.target_block, + &target_block, &skip.target_header_chain_id_proof, &skip.target_header_height_proof, &skip.target_header_validator_hash_proof, diff --git a/circuits/skip.rs b/circuits/skip.rs index fd9d5dc..88ea7a4 100644 --- a/circuits/skip.rs +++ b/circuits/skip.rs @@ -42,17 +42,16 @@ impl, const D: usize> TendermintSkipCircuit for Circ input_stream, SkipOffchainInputs:: {}, ); - let mut skip_variable = - output_stream.read::>(self); - skip_variable.trusted_block = trusted_block; - skip_variable.trusted_header = trusted_header_hash; - skip_variable.target_block = target_block; + let skip_variable = output_stream.read::>(self); let target_header = skip_variable.target_header; self.verify_skip::( chain_id_bytes, skip_max, + trusted_block, + trusted_header_hash, + target_block, &skip_variable, ); target_header @@ -85,15 +84,12 @@ impl, const D: usize> let verify_skip_struct = VerifySkipStruct:: { target_header: result.target_header.into(), - target_block, target_block_validators: result.target_block_validators, target_block_nb_validators: L::Field::from_canonical_usize(result.nb_target_validators), target_block_round: result.round as u64, target_header_chain_id_proof: result.target_block_chain_id_proof, target_header_height_proof: result.target_block_height_proof, target_header_validator_hash_proof: result.target_block_validators_hash_proof, - trusted_header: result.trusted_header.into(), - trusted_block, trusted_block_nb_validators: L::Field::from_canonical_usize( result.nb_trusted_validators, ), diff --git a/circuits/step.rs b/circuits/step.rs index 317adfe..b9c315e 100644 --- a/circuits/step.rs +++ b/circuits/step.rs @@ -36,17 +36,14 @@ impl, const D: usize> TendermintStepCircuit for Circ input_stream, StepOffchainInputs:: {}, ); - let mut step_variable = - output_stream.read::>(self); - step_variable.prev_header = prev_header_hash; - let one = self.one(); - let next_block = self.add(prev_block_number, one); - step_variable.next_block = next_block; + let step_variable = output_stream.read::>(self); let next_header = step_variable.next_header; self.verify_step::( chain_id_bytes, + prev_block_number, + prev_header_hash, &step_variable, ); next_header @@ -77,7 +74,6 @@ impl, const D: usize> let verify_step_struct = VerifyStepStruct:: { next_header: result.next_header.into(), - next_block: prev_block_number + 1, next_block_validators: result.next_block_validators, next_block_nb_validators: L::Field::from_canonical_usize(result.nb_validators), next_block_round: result.round as u64, @@ -85,7 +81,6 @@ impl, const D: usize> next_header_height_proof: result.next_block_height_proof, next_header_validators_hash_proof: result.next_block_validators_hash_proof, next_header_last_block_id_proof: result.next_block_last_block_id_proof, - prev_header: prev_header_hash, prev_header_next_validators_hash_proof: result.prev_block_next_validators_hash_proof, }; diff --git a/circuits/variables.rs b/circuits/variables.rs index 8c021f8..a1e44fa 100644 --- a/circuits/variables.rs +++ b/circuits/variables.rs @@ -96,16 +96,13 @@ pub struct ValidatorHashFieldVariable { #[derive(Debug, Clone, CircuitVariable)] #[value_name(VerifySkipStruct)] pub struct VerifySkipVariable { - pub target_header: TendermintHashVariable, - pub target_block: U64Variable, + pub target_header: Bytes32Variable, pub target_block_validators: ArrayVariable, pub target_block_nb_validators: Variable, pub target_block_round: U64Variable, pub target_header_chain_id_proof: ChainIdProofVariable, pub target_header_height_proof: HeightProofVariable, pub target_header_validator_hash_proof: HashInclusionProofVariable, - pub trusted_header: TendermintHashVariable, - pub trusted_block: U64Variable, pub trusted_block_nb_validators: Variable, pub trusted_header_validator_hash_proof: HashInclusionProofVariable, pub trusted_header_validator_hash_fields: @@ -117,7 +114,6 @@ pub struct VerifySkipVariable { #[value_name(VerifyStepStruct)] pub struct VerifyStepVariable { pub next_header: Bytes32Variable, - pub next_block: U64Variable, pub next_block_validators: ArrayVariable, pub next_block_nb_validators: Variable, pub next_block_round: U64Variable, @@ -125,6 +121,5 @@ pub struct VerifyStepVariable { pub next_header_height_proof: HeightProofVariable, pub next_header_validators_hash_proof: HashInclusionProofVariable, pub next_header_last_block_id_proof: BlockIDInclusionProofVariable, - pub prev_header: Bytes32Variable, pub prev_header_next_validators_hash_proof: HashInclusionProofVariable, }