diff --git a/src/inputs.rs b/src/inputs.rs index d5ca3163..c75ba047 100644 --- a/src/inputs.rs +++ b/src/inputs.rs @@ -45,23 +45,28 @@ pub struct InclusionProof { } #[derive(Debug, Clone)] -pub struct CelestiaStepBlockProof { +pub struct CelestiaBaseBlockProof { pub validators: Vec, pub header: Vec, - pub prev_header: Vec, pub data_hash_proof: InclusionProof, pub validator_hash_proof: InclusionProof, pub next_validators_hash_proof: InclusionProof, - pub last_block_id_proof: InclusionProof, pub round_present: bool, } +#[derive(Debug, Clone)] +pub struct CelestiaSequentialBlockProof { + pub prev_header: Vec, + pub last_block_id_proof: InclusionProof, + pub base: CelestiaBaseBlockProof, +} + #[derive(Debug, Clone)] pub struct CelestiaSkipBlockProof { pub trusted_header: Vec, pub trusted_validator_hash_proof: InclusionProof, pub trusted_validator_fields: Vec, - pub step: CelestiaStepBlockProof, + pub base: CelestiaBaseBlockProof, } // If hash_so_far is on the left, False, else True @@ -104,10 +109,7 @@ fn get_signed_block(block: usize) -> Box { block } -pub fn generate_step_inputs(block: usize) -> CelestiaStepBlockProof { - // Generate test cases from Celestia block: - let block = get_signed_block(block); - +fn generate_base_inputs(block: Box) -> CelestiaBaseBlockProof { let mut validators = Vec::new(); // Signatures or dummy @@ -227,6 +229,29 @@ pub fn generate_step_inputs(block: usize) -> CelestiaStepBlockProof { proof: enc_next_validators_hash_proof.aunts, }; + + println!("num validators: {}", validators.len()); + + let celestia_block_proof = CelestiaBaseBlockProof { + validators, + header: header_hash.as_bytes().to_vec(), + data_hash_proof, + validator_hash_proof: validators_hash_proof, + next_validators_hash_proof, + round_present: block.commit.round.value() > 0, + }; + + celestia_block_proof + +} + +pub fn generate_step_inputs(block: usize) -> CelestiaSequentialBlockProof { + // Generate test cases from Celestia block: + let block = get_signed_block(block); + + let (_root, proofs) = generate_proofs_from_header(&block.header); + let total = proofs[0].total; + let enc_last_block_id_proof = proofs[4].clone(); let enc_last_block_id_proof_indices = get_path_indices(4, total); println!("last block proof indices: {:?}", enc_last_block_id_proof_indices); @@ -246,27 +271,23 @@ pub fn generate_step_inputs(block: usize) -> CelestiaStepBlockProof { println!("last block id (len): {}", last_block_id.len()); assert_eq!(prev_header_hash.as_bytes(), &last_block_id[2..34], "computed hash does not match"); - - println!("num validators: {}", validators.len()); - - let celestia_block_proof = CelestiaStepBlockProof { - validators, - header: header_hash.as_bytes().to_vec(), + let base = generate_base_inputs(block); + + CelestiaSequentialBlockProof { prev_header: prev_header_hash.as_bytes().to_vec(), - data_hash_proof, - validator_hash_proof: validators_hash_proof, - next_validators_hash_proof, last_block_id_proof, - round_present: block.commit.round.value() > 0, - }; - - celestia_block_proof + base + } } -pub fn generate_skip_inputs(block: usize, trusted_block: usize) -> CelestiaSkipBlockProof { - let step_inputs = generate_step_inputs(block); - +// Where block is the block we want to generate inputs for, and trusted_block is the block we're skipping from +pub fn generate_skip_inputs(trusted_block: usize, block: usize) -> CelestiaSkipBlockProof { // Generate test cases from Celestia block: + let block = get_signed_block(block); + + let base = generate_base_inputs(block); + + // Get the trusted_block let trusted_block = get_signed_block(trusted_block); let mut validators = Vec::new(); @@ -332,7 +353,7 @@ pub fn generate_skip_inputs(block: usize, trusted_block: usize) -> CelestiaSkipB trusted_header: trusted_block.header.hash().into(), trusted_validator_hash_proof: validators_hash_proof, trusted_validator_fields: validators, - step: step_inputs + base } } diff --git a/src/skip.rs b/src/skip.rs index 17be0870..8d3ae9ea 100644 --- a/src/skip.rs +++ b/src/skip.rs @@ -31,18 +31,6 @@ use crate::utils::{ use crate::validator::TendermintMarshaller; use crate::voting::TendermintVoting; -#[derive(Debug, Clone)] -pub struct ValidatorTarget { - pubkey: EDDSAPublicKeyTarget, - signature: EDDSASignatureTarget, - message: ValidatorMessageTarget, - message_bit_length: Target, - voting_power: I64Target, - validator_byte_length: Target, - enabled: BoolTarget, - signed: BoolTarget, -} - /// The protobuf-encoded leaf (a hash), and it's corresponding proof and path indices against the header. #[derive(Debug, Clone)] pub struct HashInclusionProofTarget { diff --git a/src/step.rs b/src/step.rs index a7a0ebe3..cbad05a1 100644 --- a/src/step.rs +++ b/src/step.rs @@ -62,14 +62,19 @@ pub struct BlockIDInclusionProofTarget { } #[derive(Debug, Clone)] -pub struct CelestiaBlockProofTarget { +pub struct SequentialProofTarget { + prev_header: TendermintHashTarget, + last_block_id_proof: BlockIDInclusionProofTarget, + base: BaseBlockProofTarget, +} + +#[derive(Debug, Clone)] +pub struct BaseBlockProofTarget { validators: Vec>, header: TendermintHashTarget, - prev_header: TendermintHashTarget, data_hash_proof: HashInclusionProofTarget, validator_hash_proof: HashInclusionProofTarget, next_validators_hash_proof: HashInclusionProofTarget, - last_block_id_proof: BlockIDInclusionProofTarget, round_present: BoolTarget, } @@ -325,7 +330,7 @@ pub fn make_step_circuit< const VALIDATOR_SET_SIZE_MAX: usize >( builder: &mut CircuitBuilder, -) -> CelestiaBlockProofTarget +) -> SequentialProofTarget where Config::Hasher: AlgebraicHasher, { @@ -387,15 +392,12 @@ where &round_present, ); - CelestiaBlockProofTarget:: { - validators, - header, + let base: BaseBlockProofTarget:: = BaseBlockProofTarget { validators, header, data_hash_proof, validator_hash_proof, next_validators_hash_proof, round_present }; + + SequentialProofTarget:: { prev_header, - data_hash_proof, - validator_hash_proof, - next_validators_hash_proof, last_block_id_proof, - round_present, + base } } @@ -420,7 +422,7 @@ pub(crate) mod tests { use plonky2x::ecc::ed25519::curve::curve_types::AffinePoint; use plonky2x::ecc::ed25519::field::ed25519_scalar::Ed25519Scalar; - use crate::inputs::{generate_step_inputs, CelestiaStepBlockProof}; + use crate::inputs::{generate_step_inputs, CelestiaSequentialBlockProof}; use crate::utils::{to_be_bits}; use log; @@ -495,52 +497,59 @@ pub(crate) mod tests { println!("Making step circuit"); - let celestia_proof_target = + let celestia_sequential_proof_target = make_step_circuit::(&mut builder); // Note: Length of output is the closest power of 2 gte the number of validators for this block. - let celestia_block_proof: CelestiaStepBlockProof = generate_step_inputs(block); + let celestia_block_proof: CelestiaSequentialBlockProof = generate_step_inputs(block); println!("Generated inputs"); - println!("Number of validators: {}", celestia_block_proof.validators.len()); + println!("Number of validators: {}", celestia_block_proof.base.validators.len()); timed!(timing, "assigning inputs", { // Set target for header - let header_bits = to_be_bits(celestia_block_proof.header); + let header_bits = to_be_bits(celestia_block_proof.base.header); + for i in 0..HASH_SIZE_BITS { + pw.set_bool_target(celestia_sequential_proof_target.base.header.0[i], header_bits[i]); + } + + // Set target for prev header + let prev_header_bits = to_be_bits(celestia_block_proof.prev_header); for i in 0..HASH_SIZE_BITS { - pw.set_bool_target(celestia_proof_target.header.0[i], header_bits[i]); + pw.set_bool_target(celestia_sequential_proof_target.prev_header.0[i], prev_header_bits[i]); } // Set target for round present pw.set_bool_target( - celestia_proof_target.round_present, - celestia_block_proof.round_present, + celestia_sequential_proof_target.base.round_present, + celestia_block_proof.base.round_present, ); // Set the encoded leaf for each of the proofs - let data_hash_enc_leaf = to_be_bits(celestia_block_proof.data_hash_proof.enc_leaf); - let val_hash_enc_leaf = to_be_bits(celestia_block_proof.validator_hash_proof.enc_leaf); + let data_hash_enc_leaf = to_be_bits(celestia_block_proof.base.data_hash_proof.enc_leaf); + let val_hash_enc_leaf = to_be_bits(celestia_block_proof.base.validator_hash_proof.enc_leaf); let next_val_hash_enc_leaf = - to_be_bits(celestia_block_proof.next_validators_hash_proof.enc_leaf); + to_be_bits(celestia_block_proof.base.next_validators_hash_proof.enc_leaf); let last_block_id_enc_leaf = to_be_bits(celestia_block_proof.last_block_id_proof.enc_leaf); for i in 0..PROTOBUF_HASH_SIZE_BITS { pw.set_bool_target( - celestia_proof_target.data_hash_proof.enc_leaf.0[i], + celestia_sequential_proof_target.base.data_hash_proof.enc_leaf.0[i], data_hash_enc_leaf[i], ); pw.set_bool_target( - celestia_proof_target.validator_hash_proof.enc_leaf.0[i], + celestia_sequential_proof_target.base.validator_hash_proof.enc_leaf.0[i], val_hash_enc_leaf[i], ); pw.set_bool_target( - celestia_proof_target.next_validators_hash_proof.enc_leaf.0[i], + celestia_sequential_proof_target.base.next_validators_hash_proof.enc_leaf.0[i], next_val_hash_enc_leaf[i], ); } + // Set targets for last block id leaf for i in 0..PROTOBUF_BLOCK_ID_SIZE_BITS { pw.set_bool_target( - celestia_proof_target.last_block_id_proof.enc_leaf.0[i], + celestia_sequential_proof_target.last_block_id_proof.enc_leaf.0[i], last_block_id_enc_leaf[i], ); } @@ -548,49 +557,50 @@ pub(crate) mod tests { for i in 0..HEADER_PROOF_DEPTH { // Set path indices for each of the proof indices pw.set_bool_target( - celestia_proof_target.data_hash_proof.path[i], - celestia_block_proof.data_hash_proof.path[i], + celestia_sequential_proof_target.base.data_hash_proof.path[i], + celestia_block_proof.base.data_hash_proof.path[i], ); pw.set_bool_target( - celestia_proof_target.validator_hash_proof.path[i], - celestia_block_proof.validator_hash_proof.path[i], + celestia_sequential_proof_target.base.validator_hash_proof.path[i], + celestia_block_proof.base.validator_hash_proof.path[i], ); pw.set_bool_target( - celestia_proof_target.next_validators_hash_proof.path[i], - celestia_block_proof.next_validators_hash_proof.path[i], + celestia_sequential_proof_target.base.next_validators_hash_proof.path[i], + celestia_block_proof.base.next_validators_hash_proof.path[i], ); pw.set_bool_target( - celestia_proof_target.last_block_id_proof.path[i], + celestia_sequential_proof_target.last_block_id_proof.path[i], celestia_block_proof.last_block_id_proof.path[i], ); let data_hash_aunt = - to_be_bits(celestia_block_proof.data_hash_proof.proof[i].to_vec()); + to_be_bits(celestia_block_proof.base.data_hash_proof.proof[i].to_vec()); let val_hash_aunt = - to_be_bits(celestia_block_proof.validator_hash_proof.proof[i].to_vec()); + to_be_bits(celestia_block_proof.base.validator_hash_proof.proof[i].to_vec()); let next_val_aunt = - to_be_bits(celestia_block_proof.next_validators_hash_proof.proof[i].to_vec()); + to_be_bits(celestia_block_proof.base.next_validators_hash_proof.proof[i].to_vec()); + let last_block_id_aunt = to_be_bits(celestia_block_proof.last_block_id_proof.proof[i].to_vec()); // Set aunts for each of the proofs for j in 0..HASH_SIZE_BITS { pw.set_bool_target( - celestia_proof_target.data_hash_proof.proof[i].0[j], + celestia_sequential_proof_target.base.data_hash_proof.proof[i].0[j], data_hash_aunt[j], ); pw.set_bool_target( - celestia_proof_target.validator_hash_proof.proof[i].0[j], + celestia_sequential_proof_target.base.validator_hash_proof.proof[i].0[j], val_hash_aunt[j], ); pw.set_bool_target( - celestia_proof_target.next_validators_hash_proof.proof[i].0[j], + celestia_sequential_proof_target.base.next_validators_hash_proof.proof[i].0[j], next_val_aunt[j], ); pw.set_bool_target( - celestia_proof_target.last_block_id_proof.proof[i].0[j], + celestia_sequential_proof_target.last_block_id_proof.proof[i].0[j], last_block_id_aunt[j], ); } @@ -598,7 +608,7 @@ pub(crate) mod tests { // Set the targets for each of the validators for i in 0..VALIDATOR_SET_SIZE_MAX { - let validator = &celestia_block_proof.validators[i]; + let validator = &celestia_block_proof.base.validators[i]; let signature_bytes = validator.signature.clone().into_bytes(); let voting_power_lower = (validator.voting_power & ((1 << 32) - 1)) as u32; @@ -616,17 +626,17 @@ pub(crate) mod tests { // Set the targets for the public key pw.set_affine_point_target( - &celestia_proof_target.validators[i].pubkey.0, + &celestia_sequential_proof_target.base.validators[i].pubkey.0, &pub_key_uncompressed, ); // Set signature targets pw.set_affine_point_target( - &celestia_proof_target.validators[i].signature.r, + &celestia_sequential_proof_target.base.validators[i].signature.r, &sig_r, ); pw.set_biguint_target( - &celestia_proof_target.validators[i].signature.s.value, + &celestia_sequential_proof_target.base.validators[i].signature.s.value, &sig_s_biguint, ); @@ -634,43 +644,43 @@ pub(crate) mod tests { // Set messages for each of the proofs for j in 0..message_bits.len() { pw.set_bool_target( - celestia_proof_target.validators[i].message.0[j], + celestia_sequential_proof_target.base.validators[i].message.0[j], message_bits[j], ); } for j in message_bits.len()..VALIDATOR_MESSAGE_BYTES_LENGTH_MAX * 8 { - pw.set_bool_target(celestia_proof_target.validators[i].message.0[j], false); + pw.set_bool_target(celestia_sequential_proof_target.base.validators[i].message.0[j], false); } // Set voting power targets pw.set_u32_target( - celestia_proof_target.validators[i].voting_power.0[0], + celestia_sequential_proof_target.base.validators[i].voting_power.0[0], voting_power_lower, ); pw.set_u32_target( - celestia_proof_target.validators[i].voting_power.0[1], + celestia_sequential_proof_target.base.validators[i].voting_power.0[1], voting_power_upper, ); // Set length targets pw.set_target( - celestia_proof_target.validators[i].validator_byte_length, + celestia_sequential_proof_target.base.validators[i].validator_byte_length, F::from_canonical_usize(validator.validator_byte_length), ); let message_bit_length = validator.message_bit_length; pw.set_target( - celestia_proof_target.validators[i].message_bit_length, + celestia_sequential_proof_target.base.validators[i].message_bit_length, F::from_canonical_usize(message_bit_length), ); // Set enabled and signed pw.set_bool_target( - celestia_proof_target.validators[i].enabled, + celestia_sequential_proof_target.base.validators[i].enabled, validator.enabled, ); println!("validator {} signed: {}", i, validator.signed); - pw.set_bool_target(celestia_proof_target.validators[i].signed, validator.signed); + pw.set_bool_target(celestia_sequential_proof_target.base.validators[i].signed, validator.signed); } }); let inner_data = builder.build::();