Skip to content

Commit 08a16b4

Browse files
committed
fix
1 parent 4cc22ed commit 08a16b4

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

lightclient-circuits/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ strum_macros = "0.25"
4545
rand = "0.8"
4646
lazy_static = "1.4"
4747
getset = "0.1.2"
48+
rand_chacha = "0.3.0"
4849

4950
[dev-dependencies]
5051
rstest = "0.18.2"

lightclient-circuits/config/sync_step_testnet.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"params": {
33
"k": 21,
44
"num_advice_per_phase": [
5-
7
5+
8
66
],
77
"num_fixed": 1,
88
"num_lookup_advice_per_phase": [
@@ -15,12 +15,13 @@
1515
},
1616
"break_points": [
1717
[
18-
2097141,
19-
2097141,
2018
2097142,
2119
2097140,
20+
2097140,
2221
2097142,
23-
2097141
22+
2097141,
23+
2097140,
24+
2097142
2425
]
2526
]
2627
}

lightclient-circuits/src/sync_step_circuit.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -274,32 +274,7 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {
274274
poseidon_commitment,
275275
]]
276276
}
277-
}
278-
279-
// Truncate the SHA256 digest to 253 bits and convert to one field element.
280-
pub fn truncate_sha256_into_single_elem<F: Field>(
281-
ctx: &mut Context<F>,
282-
gate: &impl GateInstructions<F>,
283-
hash_bytes: [AssignedValue<F>; 32],
284-
) -> AssignedValue<F> {
285-
let public_input_commitment_bytes = {
286-
let mut truncated_hash = hash_bytes;
287-
let cleared_byte = {
288-
let bits = gate.num_to_bits(ctx, truncated_hash[31], 8);
289-
gate.bits_to_num(ctx, &bits[..5])
290-
};
291-
truncated_hash[31] = cleared_byte;
292-
truncated_hash
293-
};
294-
295-
let byte_bases = (0..32)
296-
.map(|i| QuantumCell::Constant(gate.pow_of_two()[i * 8]))
297-
.collect_vec();
298-
299-
gate.inner_product(ctx, public_input_commitment_bytes, byte_bases)
300-
}
301277

302-
impl<S: Spec, F: Field> StepCircuit<S, F> {
303278
/// Decompresses siganure from bytes and assigns it to the circuit.
304279
fn assign_signature(
305280
ctx: &mut Context<F>,
@@ -389,6 +364,29 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {
389364
}
390365
}
391366

367+
// Truncate the SHA256 digest to 253 bits and convert to one field element.
368+
pub fn truncate_sha256_into_single_elem<F: Field>(
369+
ctx: &mut Context<F>,
370+
gate: &impl GateInstructions<F>,
371+
hash_bytes: [AssignedValue<F>; 32],
372+
) -> AssignedValue<F> {
373+
let public_input_commitment_bytes = {
374+
let mut truncated_hash = hash_bytes;
375+
let cleared_byte = {
376+
let bits = gate.num_to_bits(ctx, truncated_hash[31], 8);
377+
gate.bits_to_num(ctx, &bits[..5])
378+
};
379+
truncated_hash[31] = cleared_byte;
380+
truncated_hash
381+
};
382+
383+
let byte_bases = (0..32)
384+
.map(|i| QuantumCell::Constant(gate.pow_of_two()[i * 8]))
385+
.collect_vec();
386+
387+
gate.inner_product(ctx, public_input_commitment_bytes, byte_bases)
388+
}
389+
392390
impl<S: Spec> AppCircuit for StepCircuit<S, bn256::Fr> {
393391
type Pinning = Eth2ConfigPinning;
394392
type Witness = witness::SyncStepArgs<S>;

lightclient-circuits/src/witness/step.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use eth_types::Spec;
66
use ethereum_consensus_types::signing::compute_signing_root;
77
use ethereum_consensus_types::BeaconBlockHeader;
8+
use ff::Field;
89
use halo2curves::bls12_381::hash_to_curve::ExpandMsgXmd;
910
use halo2curves::bls12_381::{hash_to_curve, Fr, G1, G2};
1011
use halo2curves::group::Curve;
1112
use itertools::Itertools;
13+
use rand::SeedableRng;
1214
use serde::{Deserialize, Serialize};
1315
use ssz_rs::{Merkleized, Node};
14-
use std::iter;
1516
use std::marker::PhantomData;
1617
use std::ops::Deref;
1718

@@ -86,30 +87,38 @@ impl<S: Spec> Default for SyncStepArgs<S> {
8687
let signing_root =
8788
compute_signing_root(attested_header.hash_tree_root().unwrap(), DOMAIN).unwrap();
8889

89-
let sk = Fr::from_bytes(&[1; 32]).unwrap();
90+
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0);
91+
92+
let sks = (0..S::SYNC_COMMITTEE_SIZE)
93+
.map(|_| Fr::random(&mut rng))
94+
.collect_vec();
9095
let msg = <G2 as hash_to_curve::HashToCurve<ExpandMsgXmd<sha2::Sha256>>>::hash_to_curve(
9196
signing_root.deref(),
9297
S::DST,
9398
)
9499
.to_affine();
95100

96-
let aggregated_signature = vec![msg * sk; S::SYNC_COMMITTEE_SIZE]
97-
.into_iter()
101+
let aggregated_signature = sks
102+
.iter()
103+
.map(|sk| msg * sk)
98104
.fold(G2::identity(), |acc, x| acc + x)
99105
.to_affine();
100106

101107
let signature_compressed = aggregated_signature.to_compressed_be().to_vec();
102108

103-
let pubkey_uncompressed = (G1::generator() * sk)
104-
.to_affine()
105-
.to_uncompressed_be()
106-
.to_vec();
109+
let pubkeys_uncompressed = sks
110+
.iter()
111+
.map(|sk| {
112+
(G1::generator() * sk)
113+
.to_affine()
114+
.to_uncompressed_be()
115+
.to_vec()
116+
})
117+
.collect_vec();
107118

108119
Self {
109120
signature_compressed,
110-
pubkeys_uncompressed: iter::repeat(pubkey_uncompressed)
111-
.take(S::SYNC_COMMITTEE_SIZE)
112-
.collect_vec(),
121+
pubkeys_uncompressed,
113122
pariticipation_bits: vec![true; S::SYNC_COMMITTEE_SIZE],
114123
domain: DOMAIN,
115124
attested_header,

0 commit comments

Comments
 (0)