Skip to content

Commit

Permalink
explicitly pass in trusted_block, trusted_header_hash, target_block
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed Mar 3, 2024
1 parent 1800e7c commit 98b8964
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
24 changes: 18 additions & 6 deletions circuits/builder/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ pub trait TendermintVerify<L: PlonkParameters<D>, const D: usize> {
fn verify_step<const VALIDATOR_SET_SIZE_MAX: usize, const CHAIN_ID_SIZE_BYTES: usize>(
&mut self,
expected_chain_id_bytes: &[u8],
prev_block_number: U64Variable,
prev_header_hash: Bytes32Variable,
step: &VerifyStepVariable<VALIDATOR_SET_SIZE_MAX>,
);

Expand All @@ -125,6 +127,9 @@ pub trait TendermintVerify<L: PlonkParameters<D>, 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<VALIDATOR_SET_SIZE_MAX>,
);
}
Expand Down Expand Up @@ -463,15 +468,19 @@ impl<L: PlonkParameters<D>, const D: usize> TendermintVerify<L, D> for CircuitBu
fn verify_step<const VALIDATOR_SET_SIZE_MAX: usize, const CHAIN_ID_SIZE_BYTES: usize>(
&mut self,
expected_chain_id_bytes: &[u8],
prev_block_number: U64Variable,
prev_header_hash: Bytes32Variable,
step: &VerifyStepVariable<VALIDATOR_SET_SIZE_MAX>,
) {
let one = self.one();
let next_block = self.add(prev_block_number, one);
// Verify the new Tendermint consensus block.
self.verify_header::<VALIDATOR_SET_SIZE_MAX, CHAIN_ID_SIZE_BYTES>(
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,
Expand All @@ -481,7 +490,7 @@ impl<L: PlonkParameters<D>, const D: usize> TendermintVerify<L, D> 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,
);

Expand All @@ -490,7 +499,7 @@ impl<L: PlonkParameters<D>, const D: usize> TendermintVerify<L, D> 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,
);
}
Expand All @@ -516,19 +525,22 @@ impl<L: PlonkParameters<D>, const D: usize> TendermintVerify<L, D> for CircuitBu
&mut self,
expected_chain_id_bytes: &[u8],
skip_max: usize,
trusted_block: U64Variable,
trusted_header_hash: Bytes32Variable,
target_block: U64Variable,
skip: &VerifySkipVariable<VALIDATOR_SET_SIZE_MAX>,
) {
// 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
// on the target block.
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,
Expand All @@ -540,7 +552,7 @@ impl<L: PlonkParameters<D>, const D: usize> TendermintVerify<L, D> 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,
Expand Down
12 changes: 4 additions & 8 deletions circuits/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@ impl<L: PlonkParameters<D>, const D: usize> TendermintSkipCircuit<L, D> for Circ
input_stream,
SkipOffchainInputs::<MAX_VALIDATOR_SET_SIZE> {},
);
let mut skip_variable =
output_stream.read::<VerifySkipVariable<MAX_VALIDATOR_SET_SIZE>>(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::<VerifySkipVariable<MAX_VALIDATOR_SET_SIZE>>(self);

let target_header = skip_variable.target_header;

self.verify_skip::<MAX_VALIDATOR_SET_SIZE, CHAIN_ID_SIZE_BYTES>(
chain_id_bytes,
skip_max,
trusted_block,
trusted_header_hash,
target_block,
&skip_variable,
);
target_header
Expand Down Expand Up @@ -85,15 +84,12 @@ impl<const MAX_VALIDATOR_SET_SIZE: usize, L: PlonkParameters<D>, const D: usize>

let verify_skip_struct = VerifySkipStruct::<MAX_VALIDATOR_SET_SIZE, L::Field> {
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,
),
Expand Down
11 changes: 3 additions & 8 deletions circuits/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@ impl<L: PlonkParameters<D>, const D: usize> TendermintStepCircuit<L, D> for Circ
input_stream,
StepOffchainInputs::<MAX_VALIDATOR_SET_SIZE> {},
);
let mut step_variable =
output_stream.read::<VerifyStepVariable<MAX_VALIDATOR_SET_SIZE>>(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::<VerifyStepVariable<MAX_VALIDATOR_SET_SIZE>>(self);

let next_header = step_variable.next_header;

self.verify_step::<MAX_VALIDATOR_SET_SIZE, CHAIN_ID_SIZE_BYTES>(
chain_id_bytes,
prev_block_number,
prev_header_hash,
&step_variable,
);
next_header
Expand Down Expand Up @@ -77,15 +74,13 @@ impl<const MAX_VALIDATOR_SET_SIZE: usize, L: PlonkParameters<D>, const D: usize>

let verify_step_struct = VerifyStepStruct::<MAX_VALIDATOR_SET_SIZE, L::Field> {
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,
next_header_chain_id_proof: result.next_block_chain_id_proof,
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,
};

Expand Down
7 changes: 1 addition & 6 deletions circuits/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,13 @@ pub struct ValidatorHashFieldVariable {
#[derive(Debug, Clone, CircuitVariable)]
#[value_name(VerifySkipStruct)]
pub struct VerifySkipVariable<const MAX_VALIDATOR_SET_SIZE: usize> {
pub target_header: TendermintHashVariable,
pub target_block: U64Variable,
pub target_header: Bytes32Variable,
pub target_block_validators: ArrayVariable<ValidatorVariable, MAX_VALIDATOR_SET_SIZE>,
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:
Expand All @@ -117,14 +114,12 @@ pub struct VerifySkipVariable<const MAX_VALIDATOR_SET_SIZE: usize> {
#[value_name(VerifyStepStruct)]
pub struct VerifyStepVariable<const MAX_VALIDATOR_SET_SIZE: usize> {
pub next_header: Bytes32Variable,
pub next_block: U64Variable,
pub next_block_validators: ArrayVariable<ValidatorVariable, MAX_VALIDATOR_SET_SIZE>,
pub next_block_nb_validators: Variable,
pub next_block_round: U64Variable,
pub next_header_chain_id_proof: ChainIdProofVariable,
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,
}

0 comments on commit 98b8964

Please sign in to comment.