main.nr 2.2 KB
use dep::std;
use dep::helpers;
use dep::helpers::hash;
use dep::helpers::structs;

fn main(
    pi_contract_hash: pub Field,

    accumulator: Field,
    batch: structs::Batch,
    tx_verifier: structs::VerifierTx,
    recursion_verifier: structs::Verifier
) {

    let tx_root_calc: Field = hash::hash_tree_four(batch.utxo_roots); 

    assert(batch.batch_oracle == helpers::ZERO_VALUE); 
    
    let batch_root_calc: Field = hash::hash([tx_root_calc, batch.batch_oracle]);
    let new_root_calc: Field = hash::hash([batch_root_calc, batch.old_state_root]);
    let new_path_calc: [Field; 20] = hash::compute_sibling_path(
        batch.hist_tree.old_path, 
        batch.hist_tree.leaf,
        batch.hist_tree.index
    );
    assert(batch.new_state_root == new_root_calc); 

    let calc_hist_root = hash::compute_merkle_root(
            batch.hist_tree.leaf,
            batch.hist_tree.index,
            batch.hist_tree.old_path
    );

    assert(calc_hist_root == batch.hist_tree.root);

    let hist_root_calc = hash::compute_merkle_root(
            new_root_calc,
            batch.hist_tree.index + 1,
            new_path_calc
    );

    assert(hist_root_calc == batch.hist_tree.new_root);

    let mut hash_validation: [u8; 832] = [0; 832];
    for i in 0..32 {
        hash_validation[i] = hash::field_to_u8(batch.new_state_root)[i];
        hash_validation[i + 32] = hash::field_to_u8(batch.hist_tree.root)[i];
        hash_validation[i + 64] = hash::field_to_u8(batch.hist_tree.new_root)[i];
        hash_validation[i + 96] = hash::field_to_u8(accumulator)[i];
        hash_validation[i + 128] = hash::field_to_u8(tx_verifier.key_hash)[i];
        hash_validation[i + 160] = hash::field_to_u8(recursion_verifier.key_hash)[i];
        for j in 0..20 {
            hash_validation[i + 192 + (32 * j)] = hash::field_to_u8(new_path_calc[j])[i];
        }
    }

    let hash_generated: Field = hash::hash_to_field(std::hash::keccak256(hash_validation, hash_validation.len() as u32));
    assert(pi_contract_hash == hash_generated);

    std::verify_proof(
        recursion_verifier.verification_key.as_slice(), 
        recursion_verifier.proof.as_slice(), 
        [accumulator].as_slice(), 
        recursion_verifier.key_hash
    )

}