Skip to content

Commit

Permalink
fix undeterministic hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltea committed Aug 28, 2023
1 parent 6bb4a52 commit 324e4c4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 77 deletions.
4 changes: 2 additions & 2 deletions ligthclient-circuits/src/committee_update_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::{

use crate::{
gadget::crypto::{
CachedHashChip, Fp2Point, FpPoint, G1Chip, G1Point, G2Chip, G2Point, HashChip,
HashToCurveCache, HashToCurveChip, Sha256Chip, SpreadConfig,
Fp2Point, FpPoint, G1Chip, G1Point, G2Chip, G2Point, HashChip, HashToCurveCache,
HashToCurveChip, Sha256Chip, SpreadConfig,
},
poseidon::{g1_array_poseidon, poseidon_sponge},
sha256_circuit::{util::NUM_ROUNDS, Sha256CircuitConfig},
Expand Down
55 changes: 0 additions & 55 deletions ligthclient-circuits/src/gadget/crypto/cached_hash.rs

This file was deleted.

2 changes: 0 additions & 2 deletions ligthclient-circuits/src/gadget/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod cached_hash;
mod hash2curve;
mod sha256;
mod sha256_wide;
mod util;

pub use cached_hash::CachedHashChip;
use eth_types::{AppCurveExt, HashCurveExt};
use halo2_ecc::{
bigint::ProperCrtUint,
Expand Down
2 changes: 0 additions & 2 deletions ligthclient-circuits/src/gadget/crypto/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::collections::HashMap;
use std::{cell::RefCell, char::MAX};

use crate::gadget::crypto::sha256::compression::{sha256_compression, INIT_STATE};
use crate::gadget::rlc;
use crate::util::AssignedValueCell;
use crate::{
sha256_circuit::{util::Sha256AssignedRows, Sha256CircuitConfig},
Expand Down Expand Up @@ -75,7 +74,6 @@ impl<'a, F: Field> HashChip<F> for Sha256Chip<'a, F> {
ctx: &mut Context<F>,
region: &mut Region<'_, F>,
) -> Result<AssignedHashResult<F>, Error> {
let binary_input: HashInput<u8> = input.clone().into();
let assigned_input = input.into_assigned(ctx);

// let mut extra_assignment = self.extra_assignments.borrow_mut();
Expand Down
8 changes: 3 additions & 5 deletions ligthclient-circuits/src/ssz_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use itertools::Itertools;

use crate::{
gadget::crypto::HashChip,
util::{IntoConstant, IntoWitness},
witness::{HashInput, HashInputChunk},
};

Expand Down Expand Up @@ -34,16 +35,13 @@ where
let len_even = chunks.len() + chunks.len() % 2;
let padded_chunks = chunks
.into_iter()
.pad_using(len_even, |_| {
HashInputChunk::from(
ZERO_HASHES[depth].map(|b| ctx.load_constant(F::from(b as u64))),
)
})
.pad_using(len_even, |_| ZERO_HASHES[depth].as_slice().into_constant())
.collect_vec();

chunks = padded_chunks
.into_iter()
.tuples()
.take(3)
.map(|(left, right)| {
hasher
.digest::<128>(HashInput::TwoToOne(left, right), ctx, region)
Expand Down
83 changes: 72 additions & 11 deletions ligthclient-circuits/src/sync_step_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::{

use crate::{
gadget::crypto::{
CachedHashChip, Fp2Point, FpPoint, G1Chip, G1Point, G2Chip, G2Point, HashChip,
HashToCurveCache, HashToCurveChip, Sha256Chip, SpreadConfig,
Fp2Point, FpPoint, G1Chip, G1Point, G2Chip, G2Point, HashChip, HashToCurveCache,
HashToCurveChip, Sha256Chip, SpreadConfig,
},
poseidon::{g1_array_poseidon, poseidon_sponge},
sha256_circuit::{util::NUM_ROUNDS, Sha256CircuitConfig},
Expand Down Expand Up @@ -248,7 +248,6 @@ impl<S: Spec, F: Field> Circuit<F> for SyncStepCircuit<S, F> {
use ff::Field;
fp12_chip.load_constant(ctx, Fq12::one())
};
let hasher = CachedHashChip::new(&sha256_chip);
let mut h2c_cache = HashToCurveCache::<F>::default();

// Verify attestted header
Expand All @@ -260,9 +259,9 @@ impl<S: Spec, F: Field> Circuit<F> for SyncStepCircuit<S, F> {
self.attested_block.body_root.as_ref().into_witness(),
];

let attested_header = ssz_merkleize_chunks(ctx, &mut region, &hasher, chunks)?;
let attested_header = ssz_merkleize_chunks(ctx, &mut region, &sha256_chip, chunks)?;

let signing_root = hasher
let signing_root = sha256_chip
.digest::<128>(
HashInput::TwoToOne(
attested_header.into(),
Expand Down Expand Up @@ -377,7 +376,7 @@ impl<S: Spec> AppCircuitExt<bn256::Fr> for SyncStepCircuit<S, bn256::Fr> {
std::env::set_var("LOOKUP_BITS", (k - 1).to_string());
let params: FlexGateConfigParams =
serde_json::from_str(&var("FLEX_GATE_CONFIG_PARAMS").unwrap()).unwrap();
println!("params: {:?}", params);
println!("config used: {:?}", params);
params
}

Expand Down Expand Up @@ -425,6 +424,36 @@ impl<S: Spec, F: Field> Default for SyncStepCircuit<S, F> {
dry_run: false,
_spec: PhantomData,
}
// TODO: hard code default data
// let state_input: SyncStateInput =
// serde_json::from_slice(&fs::read("../test_data/sync_state.json").unwrap()).unwrap();
// let state: SyncState<F> = state_input.into();

// Self {
// builder,
// signature: state.sync_signature.clone(),
// domain: state.domain,
// attested_block: state.attested_block.clone(),
// pubkeys: state
// .sync_committee
// .iter()
// .cloned()
// .map(|v| {
// G1Affine::from_uncompressed_unchecked(
// &v.pubkey_uncompressed.as_slice().try_into().unwrap(),
// )
// .unwrap()
// })
// .collect_vec(),
// pariticipation_bits: state
// .sync_committee
// .iter()
// .cloned()
// .map(|v| v.is_attested)
// .collect_vec(),
// dry_run: false,
// _spec: PhantomData,
// }
}
}

Expand Down Expand Up @@ -466,26 +495,38 @@ mod tests {
use rayon::iter::ParallelIterator;
use rayon::prelude::{IndexedParallelIterator, IntoParallelIterator};
use snark_verifier_sdk::{
evm::{evm_verify, gen_evm_proof_shplonk},
halo2::{aggregation::AggregationCircuit, gen_proof_shplonk, gen_snark_shplonk},
CircuitExt, SHPLONK,
};

fn get_circuit_with_data(k: usize) -> SyncStepCircuit<Test, Fr> {
fn get_circuit_with_data(k: usize) -> (SyncStepCircuit<Test, Fr>, FlexGateConfigParams) {
let builder = GateThreadBuilder::new(false);
let state_input: SyncStateInput =
serde_json::from_slice(&fs::read("../test_data/sync_state.json").unwrap()).unwrap();
let state = state_input.into();

let _ = SyncStepCircuit::<Test, Fr>::parametrize(k);
let config = if let Ok(f) = fs::read("./config/sync_step.json") {
serde_json::from_slice(&f).expect("read config file")
} else {
SyncStepCircuit::<Test, Fr>::parametrize(k)
};

set_var(
"FLEX_GATE_CONFIG_PARAMS",
serde_json::to_string(&config).unwrap(),
);
set_var("LOOKUP_BITS", (config.k - 1).to_string());
println!("config used: {:?}", config);

let builder = RefCell::from(builder);
SyncStepCircuit::new_from_state(builder, &state)
(SyncStepCircuit::new_from_state(builder, &state), config)
}

#[test]
fn test_sync_circuit() {
let k = 17;
let circuit = get_circuit_with_data(k);
let (circuit, _) = get_circuit_with_data(k);

let timer = start_timer!(|| "sync circuit mock prover");
let prover = MockProver::<Fr>::run(k as u32, &circuit, vec![]).unwrap();
Expand All @@ -496,7 +537,7 @@ mod tests {
#[test]
fn test_sync_proofgen() {
let k = 20;
let circuit = get_circuit_with_data(k);
let (circuit, _) = get_circuit_with_data(k);

let params = gen_srs(k as u32);

Expand All @@ -507,4 +548,24 @@ mod tests {

assert!(full_verifier(&params, pkey.get_vk(), proof, public_inputs))
}

#[test]
fn test_sync_evm_verify() {
let k = 20;
let (circuit, config) = get_circuit_with_data(k);

let (params, pk) = SyncStepCircuit::<Test, Fr>::setup(&config, None);

let instances = circuit.instances();
let num_instance = circuit.num_instance();
let deployment_code = gen_evm_verifier_shplonk::<SyncStepCircuit<Test, Fr>>(
&params,
pk.get_vk(),
num_instance,
None,
);
let proof = gen_evm_proof_shplonk(&params, &pk, circuit, instances.clone());

evm_verify(deployment_code, instances, proof);
}
}

0 comments on commit 324e4c4

Please sign in to comment.