Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnark-plonky2): Add gnark verifier for plonky2 circuits #272

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,7 @@ redis-server/
*.plonky2_targets
*.plonky2_circuit

beacon-light-client/plonky2/circuits_executables/verifier/circuit/
beacon-light-client/plonky2/circuits_executables/verifier/data/

.DS_Store
4 changes: 2 additions & 2 deletions beacon-light-client/plonky2/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Beacon Light Client Task Execution and Data Flow
# The Balance Verification Task Execution and Data Flow

The Beacon Light Client relies heavily on a Redis instance to handle task management. This document provides an overview of the technical structure of the system, including how tasks are produced, consumed, and executed.
The Balance Verification relies heavily on a Redis instance to handle task management. This document provides an overview of the technical structure of the system, including how tasks are produced, consumed, and executed.

## Commitment Mapper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ pub fn build_commitment_mapper_first_level_circuit() -> (
#[cfg(test)]
mod test {
use anyhow::Result;
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
use plonky2::iop::witness::{PartialWitness};

use crate::{
build_commitment_mapper_first_level_circuit::build_commitment_mapper_first_level_circuit,
utils::ETH_SHA256_BIT_SIZE,
utils::{SetBytesArray, ETH_SHA256_BIT_SIZE},
};

#[test]
Expand All @@ -121,126 +121,34 @@ mod test {

let mut pw = PartialWitness::new();

let validator_pubkey = [
"1", "0", "0", "1", "0", "0", "1", "1", "0", "0", "1", "1", "1", "0", "1", "0", "1",
"1", "0", "1", "1", "0", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1", "0", "0",
"0", "1", "1", "0", "1", "1", "0", "1", "1", "0", "0", "0", "1", "0", "0", "0", "0",
"0", "0", "1", "0", "1", "1", "0", "0", "1", "1", "1", "0", "1", "1", "1", "0", "1",
"0", "0", "0", "0", "0", "1", "1", "0", "0", "1", "0", "1", "1", "0", "1", "1", "0",
"1", "0", "1", "0", "1", "1", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", "0",
"1", "0", "0", "1", "0", "1", "0", "1", "1", "0", "1", "1", "0", "1", "1", "0", "0",
"0", "1", "0", "0", "1", "0", "1", "0", "1", "0", "1", "1", "1", "1", "0", "1", "0",
"1", "0", "0", "0", "1", "1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0",
"0", "1", "0", "1", "1", "0", "0", "1", "1", "0", "0", "0", "1", "1", "0", "1", "1",
"1", "0", "1", "0", "0", "0", "1", "1", "0", "1", "1", "0", "0", "0", "1", "1", "1",
"0", "1", "1", "1", "0", "0", "1", "1", "1", "0", "0", "1", "0", "1", "0", "0", "1",
"0", "0", "0", "0", "1", "0", "1", "0", "1", "1", "1", "0", "0", "0", "0", "1", "0",
"0", "0", "1", "1", "1", "1", "0", "1", "0", "0", "0", "1", "1", "1", "1", "0", "1",
"1", "1", "0", "0", "1", "1", "0", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1",
"0", "0", "1", "1", "0", "0", "1", "1", "1", "1", "0", "1", "0", "1", "0", "0", "0",
"1", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "1", "1", "1", "0", "0", "0",
"0", "1", "1", "1", "0", "0", "1", "0", "1", "1", "1", "0", "1", "0", "1", "0", "0",
"1", "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "0", "1", "0", "1", "1", "0",
"0", "0", "1", "0", "1", "0", "0", "1", "0", "1", "0", "1", "0", "1", "1", "1", "0",
"0", "0", "0", "1", "0", "0", "1", "1", "0", "1", "0", "0", "0", "0", "1", "0", "1",
"0", "1", "1", "1", "0", "1", "0", "0", "1", "0", "1", "1", "0", "0", "0", "1", "1",
"0", "0", "1", "0", "0", "1", "0", "1", "0", "1",
];
let validator_pubkey =hex::decode("933ad9491b62059dd065b560d256d8957a8c402cc6e8d8ee7290ae11e8f7329267a8811c397529dac52ae1342ba58c95").unwrap();
let withdrawal_credentials =
hex::decode("0100000000000000000000000d369bb49efa5100fd3b86a9f828c55da04d2d50")
.unwrap();

let withdraw_credentials = [
"0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "1",
"0", "1", "0", "0", "1", "1", "0", "1", "1", "0", "1", "0", "0", "1", "1", "0", "1",
"1", "1", "0", "1", "1", "0", "1", "0", "0", "1", "0", "0", "1", "1", "1", "1", "0",
"1", "1", "1", "1", "1", "0", "1", "0", "0", "1", "0", "1", "0", "0", "0", "1", "0",
"0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "0", "1", "0", "0",
"1", "1", "1", "0", "1", "1", "1", "0", "0", "0", "0", "1", "1", "0", "1", "0", "1",
"0", "1", "0", "0", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0", "1", "0",
"1", "0", "0", "0", "1", "1", "0", "0", "0", "1", "0", "1", "0", "1", "0", "1", "1",
"1", "0", "1", "1", "0", "1", "0", "0", "0", "0", "0", "0", "1", "0", "0", "1", "1",
"0", "1", "0", "0", "1", "0", "1", "1", "0", "1", "0", "1", "0", "1", "0", "0", "0",
"0",
];
let effective_balance =
hex::decode("0040597307000000000000000000000000000000000000000000000000000000")
.unwrap();

let effective_balance = [
"0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0",
"1", "0", "1", "1", "0", "0", "1", "0", "1", "1", "1", "0", "0", "1", "1", "0", "0",
"0", "0", "0", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0",
];
let slashed =
hex::decode("0000000000000000000000000000000000000000000000000000000000000000")
.unwrap();

let slashed = [
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0",
];
let activation_eligibility_epoch =
hex::decode("0000000000000000000000000000000000000000000000000000000000000000")
.unwrap();

let activation_eligibility_epoch = [
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0",
];
let activation_epoch =
hex::decode("0000000000000000000000000000000000000000000000000000000000000000")
.unwrap();

let withdrawable_epoch = [
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0",
];
let exit_epoch =
hex::decode("ffffffffffffffff000000000000000000000000000000000000000000000000")
.unwrap();

let withdrawable_epoch =
hex::decode("ffffffffffffffff000000000000000000000000000000000000000000000000")
.unwrap();

let validator_hash_tree_root = [
"0", "0", "1", "0", "1", "0", "1", "1", "1", "0", "1", "0", "1", "1", "1", "1", "0",
Expand All @@ -261,43 +169,36 @@ mod test {
"1",
];

for i in 0..384 {
pw.set_bool_target(
validator_commitment.validator.pubkey[i],
validator_pubkey[i] == "1",
);
}
pw.set_bytes_array(&validator_commitment.validator.pubkey, &validator_pubkey);

for i in 0..ETH_SHA256_BIT_SIZE {
pw.set_bool_target(
validator_commitment.validator.withdrawal_credentials[i],
withdraw_credentials[i] == "1",
);

pw.set_bool_target(
validator_commitment.validator.effective_balance[i],
effective_balance[i] == "1",
);

pw.set_bool_target(validator_commitment.validator.slashed[i], slashed[i] == "1");

pw.set_bool_target(
validator_commitment.validator.activation_eligibility_epoch[i],
activation_eligibility_epoch[i] == "1",
);

pw.set_bool_target(validator_commitment.validator.activation_epoch[i], false);

pw.set_bool_target(
validator_commitment.validator.exit_epoch[i],
if i < 64 { true } else { false },
);

pw.set_bool_target(
validator_commitment.validator.withdrawable_epoch[i],
withdrawable_epoch[i] == "1",
);
}
pw.set_bytes_array(
&validator_commitment.validator.withdrawal_credentials,
&withdrawal_credentials,
);

pw.set_bytes_array(
&validator_commitment.validator.effective_balance,
&effective_balance,
);

pw.set_bytes_array(&validator_commitment.validator.slashed, &slashed);

pw.set_bytes_array(
&validator_commitment.validator.activation_eligibility_epoch,
&activation_eligibility_epoch,
);

pw.set_bytes_array(
&validator_commitment.validator.activation_epoch,
&activation_epoch,
);

pw.set_bytes_array(&validator_commitment.validator.exit_epoch, &exit_epoch);

pw.set_bytes_array(
&validator_commitment.validator.withdrawable_epoch,
&withdrawable_epoch,
);

let proof = data.prove(pw).unwrap();

Expand Down
46 changes: 35 additions & 11 deletions beacon-light-client/plonky2/circuits/src/build_final_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use plonky2::{
},
fri::{reduction_strategies::FriReductionStrategy, FriConfig},
hash::hash_types::HashOutTarget,
iop::target::{BoolTarget, Target},
iop::target::BoolTarget,
plonk::{
circuit_builder::CircuitBuilder,
circuit_data::{CircuitConfig, CircuitData, VerifierCircuitTarget},
Expand All @@ -22,7 +22,9 @@ use crate::{
build_validator_balance_circuit::ValidatorBalanceProofTargetsExt,
is_valid_merkle_branch::{is_valid_merkle_branch, IsValidMerkleBranchTargets},
sha256::make_circuits,
utils::{create_bool_target_array, ssz_num_to_bits, ETH_SHA256_BIT_SIZE},
utils::{
biguint_to_bits_target, create_bool_target_array, ssz_num_to_bits, ETH_SHA256_BIT_SIZE,
},
};

pub struct BalanceFinalLayerTargets {
Expand Down Expand Up @@ -181,17 +183,38 @@ pub fn build_final_circuit(
let slot_merkle_branch =
create_and_connect_merkle_branch(&mut builder, 34, &slot_bits, &state_root);

builder.register_public_inputs(&state_root.iter().map(|x| x.target).collect::<Vec<Target>>());
let public_inputs_hasher = make_circuits(&mut builder, 3 * ETH_SHA256_BIT_SIZE as u64);

builder.register_public_inputs(
&withdrawal_credentials
.limbs
.iter()
.map(|x| x.0)
.collect_vec(),
);
let withdrawal_credentials_bits =
biguint_to_bits_target::<GoldilocksField, 2, 2>(&mut builder, &withdrawal_credentials);

let final_sum_bits = ssz_num_to_bits(&mut builder, &balance_sum, 64);

for i in 0..ETH_SHA256_BIT_SIZE {
builder.connect(public_inputs_hasher.message[i].target, state_root[i].target);
builder.connect(
public_inputs_hasher.message[ETH_SHA256_BIT_SIZE + i].target,
withdrawal_credentials_bits[i].target,
);
builder.connect(
public_inputs_hasher.message[2 * ETH_SHA256_BIT_SIZE + i].target,
final_sum_bits[i].target,
);
}

let mut sha256_hash = public_inputs_hasher.digest;

// Mask the last 3 bits in big endian as zero
sha256_hash[0] = builder._false();
sha256_hash[1] = builder._false();
sha256_hash[2] = builder._false();

let tokens = sha256_hash[0..256]
.chunks(8)
.map(|x| builder.le_sum(x.iter().rev()))
.collect_vec();

builder.register_public_inputs(&balance_sum.limbs.iter().map(|x| x.0).collect_vec());
builder.register_public_inputs(&tokens);

let data = builder.build::<C>();

Expand Down Expand Up @@ -331,6 +354,7 @@ fn create_and_connect_merkle_branch(
merkle_branch
}

#[allow(dead_code)]
fn create_final_config() -> CircuitConfig {
let standard_recursion_config = CircuitConfig::standard_recursion_config();

Expand Down
Loading
Loading