diff --git a/beacon-light-client/plonky2/crates/circuits/src/deposits_accumulator_balance_aggregator/inner_level.rs b/beacon-light-client/plonky2/crates/circuits/src/deposits_accumulator_balance_aggregator/inner_level.rs index e8f341442..2e301f217 100644 --- a/beacon-light-client/plonky2/crates/circuits/src/deposits_accumulator_balance_aggregator/inner_level.rs +++ b/beacon-light-client/plonky2/crates/circuits/src/deposits_accumulator_balance_aggregator/inner_level.rs @@ -22,7 +22,8 @@ use crate::{ common_targets::{BasicRecursiveInnerCircuitTarget, PubkeyTarget}, deposits_accumulator_balance_aggregator::first_level::DepositAccumulatorBalanceAggregatorFirstLevel, utils::circuit::{ - assert_bool_arrays_are_equal, bits_to_biguint_target, bool_arrays_are_equal, verify_proof, + assert_bool_arrays_are_equal, biguint_target_from_le_bits, bool_arrays_are_equal, + verify_proof, }, }; @@ -134,8 +135,8 @@ fn cmp_pubkey, const D: usize>( pk1: PubkeyTarget, pk2: PubkeyTarget, ) -> BoolTarget { - let pk1_bu = bits_to_biguint_target(builder, pk1.to_vec()); - let pk2_bu = bits_to_biguint_target(builder, pk2.to_vec()); + let pk1_bu = biguint_target_from_le_bits(builder, &pk1); + let pk2_bu = biguint_target_from_le_bits(builder, &pk2); builder.cmp_biguint(&pk1_bu, &pk2_bu) } diff --git a/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/hashing/merkle/mod.rs b/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/hashing/merkle/mod.rs index dd7c58de3..822d65400 100644 --- a/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/hashing/merkle/mod.rs +++ b/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/hashing/merkle/mod.rs @@ -8,7 +8,6 @@ use crate::common_targets::Sha256Target; pub mod poseidon; pub mod sha256; -pub mod ssz; pub fn pick_left_and_right_hash, const D: usize>( builder: &mut CircuitBuilder, diff --git a/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/hashing/merkle/ssz.rs b/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/hashing/merkle/ssz.rs deleted file mode 100644 index 18227d548..000000000 --- a/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/hashing/merkle/ssz.rs +++ /dev/null @@ -1,30 +0,0 @@ -use plonky2::{ - field::extension::Extendable, hash::hash_types::RichField, iop::target::BoolTarget, - plonk::circuit_builder::CircuitBuilder, -}; -use plonky2_crypto::biguint::BigUintTarget; - -use crate::{ - common_targets::SSZTarget, - utils::circuit::{biguint_to_bits_target, bits_to_biguint_target, reverse_endianness}, -}; - -pub fn ssz_num_to_bits, const D: usize>( - builder: &mut CircuitBuilder, - num: &BigUintTarget, - bit_len: usize, -) -> SSZTarget { - assert!(bit_len <= 256); - - let mut bits = reverse_endianness(&biguint_to_bits_target(builder, num)); - bits.extend((bit_len..256).map(|_| builder._false())); - - bits.try_into().unwrap() -} - -pub fn ssz_num_from_bits, const D: usize>( - builder: &mut CircuitBuilder, - bits: &[BoolTarget], -) -> BigUintTarget { - bits_to_biguint_target(builder, reverse_endianness(bits)) -} diff --git a/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/mod.rs b/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/mod.rs index 7a490966e..af658666d 100644 --- a/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/mod.rs +++ b/beacon-light-client/plonky2/crates/circuits/src/utils/circuit/mod.rs @@ -17,10 +17,7 @@ use plonky2::{ proof::ProofWithPublicInputsTarget, }, }; -use plonky2_crypto::{ - biguint::{BigUintTarget, CircuitBuilderBiguint}, - u32::arithmetic_u32::U32Target, -}; +use plonky2_crypto::{biguint::BigUintTarget, u32::arithmetic_u32::U32Target}; use crate::common_targets::SSZTarget; @@ -118,48 +115,23 @@ pub fn arrays_are_equal, const D: usize>( result } -pub fn bits_to_biguint_target, const D: usize>( +pub fn biguint_target_from_le_bits, const D: usize>( builder: &mut CircuitBuilder, - bits_target: Vec, + bits: &[BoolTarget], ) -> BigUintTarget { - let bit_len = bits_target.len(); + let bit_len = bits.len(); assert_eq!(bit_len % 32, 0); let mut u32_targets = Vec::new(); for i in 0..bit_len / 32 { u32_targets.push(U32Target( - builder.le_sum(bits_target[i * 32..(i + 1) * 32].iter().rev()), + builder.le_sum(bits[i * 32..(i + 1) * 32].iter().rev()), )); } u32_targets.reverse(); BigUintTarget { limbs: u32_targets } } -pub fn biguint_to_bits_target, const D: usize>( - builder: &mut CircuitBuilder, - a: &BigUintTarget, -) -> Vec { - let mut res = Vec::new(); - for i in (0..a.num_limbs()).rev() { - let bit_targets = builder.split_le_base::<2>(a.get_limb(i).0, 32); - for j in (0..32).rev() { - res.push(BoolTarget::new_unsafe(bit_targets[j])); - } - } - - res -} - -pub fn biguint_to_le_bits_target, const D: usize>( - builder: &mut CircuitBuilder, - a: &BigUintTarget, -) -> Vec { - biguint_to_bits_target(builder, a) - .into_iter() - .rev() - .collect_vec() -} - pub fn bits_to_bytes_target, const D: usize>( builder: &mut CircuitBuilder, bits: &[BoolTarget], @@ -170,30 +142,6 @@ pub fn bits_to_bytes_target, const D: usize>( .collect_vec() } -pub fn reverse_endianness(bits: &[BoolTarget]) -> Vec { - bits.chunks(8).rev().flatten().cloned().collect() -} - -pub fn select_biguint, const D: usize>( - builder: &mut CircuitBuilder, - b: BoolTarget, - x: &BigUintTarget, - y: &BigUintTarget, -) -> BigUintTarget { - let not_b = builder.not(b); - - let maybe_x = builder.mul_biguint_by_bool(x, b); - - let maybe_y = builder.mul_biguint_by_bool(y, not_b); - - let mut result = builder.add_biguint(&maybe_y, &maybe_x); - - // trim the carry - result.limbs.pop(); - - result -} - pub fn target_to_be_bits, const D: usize>( builder: &mut CircuitBuilder, number: Target, @@ -206,13 +154,6 @@ pub fn target_to_be_bits, const D: usize>( .try_into() .unwrap() } -pub fn is_equal_u32, const D: usize>( - builder: &mut CircuitBuilder, - x: U32Target, - y: U32Target, -) -> BoolTarget { - builder.is_equal(x.0, y.0) -} fn split_into_chunks(leaf: &[BoolTarget; 256]) -> [[BoolTarget; 64]; 4] { let mut chunks = Vec::new(); @@ -245,35 +186,15 @@ pub fn get_balance_from_leaf, const D: usize>( accumulator } -pub fn biguint_target_from_limbs(limbs: &[Target]) -> BigUintTarget { - BigUintTarget { - limbs: limbs.iter().cloned().map(|x| U32Target(x)).collect_vec(), - } -} - -pub fn biguint_is_equal, const D: usize>( - builder: &mut CircuitBuilder, - a: &BigUintTarget, - b: &BigUintTarget, -) -> BoolTarget { - assert!(a.limbs.len() == b.limbs.len()); - - let mut all_equal = builder._true(); - - for i in 0..a.limbs.len() { - let equal = builder.is_equal(a.limbs[i].0, b.limbs[i].0); - all_equal = builder.and(all_equal, equal); - } - - all_equal -} - #[cfg(test)] mod test_ssz_num_from_bits { use anyhow::Result; use circuit::{ circuit_builder_extensions::CircuitBuilderExtensions, - targets::uint::{ops::arithmetic::Zero, Uint64Target}, + targets::uint::{ + ops::{arithmetic::Zero, comparison::EqualTo}, + Uint64Target, + }, }; use itertools::Itertools; use num::{BigUint, Num}; @@ -288,12 +209,9 @@ mod test_ssz_num_from_bits { config::PoseidonGoldilocksConfig, }, }; - use plonky2_crypto::biguint::CircuitBuilderBiguint; use serde::Deserialize; use std::{fs, iter::repeat, println}; - use crate::utils::circuit::hashing::merkle::ssz::ssz_num_from_bits; - use super::get_balance_from_leaf; #[derive(Debug, Default, Deserialize)] @@ -359,8 +277,8 @@ mod test_ssz_num_from_bits { .parse::() .unwrap(); - if num_bits % 32 != 0 { - // For now lets test only multiples of 32 + if num_bits != 64 { + // For now lets test only test 64 bits continue; } @@ -379,9 +297,9 @@ mod test_ssz_num_from_bits { .map(|_| builder.add_virtual_bool_target_safe()) .collect::>(); - let target = ssz_num_from_bits(&mut builder, &bits); + let target = Uint64Target::from_le_bytes(&bits, &mut builder); - let value = test_case.value.parse::().expect( + let value = test_case.value.parse::().expect( format!( "Unable to parse value: {}_{}", test_case.r#type, test_case.tags[2] @@ -389,9 +307,9 @@ mod test_ssz_num_from_bits { .as_str(), ); - let expected_target = builder.constant_biguint(&value); - - builder.connect_biguint(&target, &expected_target); + let expected_target = Uint64Target::constant(value, &mut builder); + let values_are_equal = target.equal_to(expected_target, &mut builder); + builder.assert_true(values_are_equal); let data = builder.build::();