Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit 697893f

Browse files
authored
EccCircuit (related to EcPairing) multiple fixes (#756)
* fix: several fixes | wip debuging * remove unnecessary part * fix: assert equal for op output and success * fix: G2 coeffs * chore: remove info log
1 parent 9b46ddb commit 697893f

File tree

7 files changed

+237
-31
lines changed

7 files changed

+237
-31
lines changed

bus-mapping/src/circuit_input_builder/execution.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1101,10 +1101,10 @@ impl EcPairingPair {
11011101
/// Returns the big-endian representation of the G2 point in the pair.
11021102
pub fn g2_bytes_be(&self) -> Vec<u8> {
11031103
std::iter::empty()
1104-
.chain(self.g2_point.x.c0.to_bytes().iter().rev())
11051104
.chain(self.g2_point.x.c1.to_bytes().iter().rev())
1106-
.chain(self.g2_point.y.c0.to_bytes().iter().rev())
1105+
.chain(self.g2_point.x.c0.to_bytes().iter().rev())
11071106
.chain(self.g2_point.y.c1.to_bytes().iter().rev())
1107+
.chain(self.g2_point.y.c0.to_bytes().iter().rev())
11081108
.cloned()
11091109
.collect()
11101110
}
@@ -1114,10 +1114,10 @@ impl EcPairingPair {
11141114
std::iter::empty()
11151115
.chain(self.g1_point.x.to_bytes().iter().rev())
11161116
.chain(self.g1_point.y.to_bytes().iter().rev())
1117-
.chain(self.g2_point.x.c0.to_bytes().iter().rev())
11181117
.chain(self.g2_point.x.c1.to_bytes().iter().rev())
1119-
.chain(self.g2_point.y.c0.to_bytes().iter().rev())
1118+
.chain(self.g2_point.x.c0.to_bytes().iter().rev())
11201119
.chain(self.g2_point.y.c1.to_bytes().iter().rev())
1120+
.chain(self.g2_point.y.c0.to_bytes().iter().rev())
11211121
.cloned()
11221122
.collect()
11231123
}

bus-mapping/src/evm/opcodes/precompiles/ec_pairing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ pub(crate) fn opt_data(
6464
.unwrap();
6565
G2Affine {
6666
x: Fq2 {
67-
c0: g2_x1,
68-
c1: g2_x2,
67+
c0: g2_x2,
68+
c1: g2_x1,
6969
},
7070
y: Fq2 {
71-
c0: g2_y1,
72-
c1: g2_y2,
71+
c0: g2_y2,
72+
c1: g2_y1,
7373
},
7474
}
7575
};

zkevm-circuits/src/ecc_circuit.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::marker::PhantomData;
55

66
use bus_mapping::{
7-
circuit_input_builder::{EcAddOp, EcMulOp, EcPairingOp},
7+
circuit_input_builder::{EcAddOp, EcMulOp, EcPairingOp, N_BYTES_PER_PAIR, N_PAIRING_PER_OP},
88
precompile::PrecompileCalls,
99
};
1010
use eth_types::{Field, ToScalar};
@@ -176,7 +176,7 @@ impl<F: Field, const XI_0: i64> EccCircuit<F, XI_0> {
176176
let keccak_powers = std::iter::successors(Some(Value::known(F::one())), |coeff| {
177177
Some(challenges.keccak_input() * coeff)
178178
})
179-
.take(4 * 192)
179+
.take(N_PAIRING_PER_OP * N_BYTES_PER_PAIR)
180180
.map(|x| QuantumCell::Witness(x))
181181
.collect_vec();
182182

@@ -561,12 +561,12 @@ impl<F: Field, const XI_0: i64> EccCircuit<F, XI_0> {
561561
};
562562
G1Assigned {
563563
decomposed,
564-
x_rlc: pairing_chip.fp_chip.range.gate.inner_product(
564+
x_rlc: ecc_chip.field_chip().range().gate().inner_product(
565565
ctx,
566566
x_cells,
567567
powers_of_rand.iter().cloned(),
568568
),
569-
y_rlc: pairing_chip.fp_chip.range.gate.inner_product(
569+
y_rlc: ecc_chip.field_chip().range().gate().inner_product(
570570
ctx,
571571
y_cells,
572572
powers_of_rand.iter().cloned(),
@@ -593,22 +593,22 @@ impl<F: Field, const XI_0: i64> EccCircuit<F, XI_0> {
593593
};
594594
G2Assigned {
595595
decomposed,
596-
x_c0_rlc: pairing_chip.fp_chip.range.gate.inner_product(
596+
x_c0_rlc: ecc_chip.field_chip().range().gate().inner_product(
597597
ctx,
598598
x_c0_cells,
599599
powers_of_rand.iter().cloned(),
600600
),
601-
x_c1_rlc: pairing_chip.fp_chip.range.gate.inner_product(
601+
x_c1_rlc: ecc_chip.field_chip().range().gate().inner_product(
602602
ctx,
603603
x_c1_cells,
604604
powers_of_rand.iter().cloned(),
605605
),
606-
y_c0_rlc: pairing_chip.fp_chip.range.gate.inner_product(
606+
y_c0_rlc: ecc_chip.field_chip().range().gate().inner_product(
607607
ctx,
608608
y_c0_cells,
609609
powers_of_rand.iter().cloned(),
610610
),
611-
y_c1_rlc: pairing_chip.fp_chip.range.gate.inner_product(
611+
y_c1_rlc: ecc_chip.field_chip().range().gate().inner_product(
612612
ctx,
613613
y_c1_cells,
614614
powers_of_rand.iter().cloned(),
@@ -628,18 +628,17 @@ impl<F: Field, const XI_0: i64> EccCircuit<F, XI_0> {
628628
std::iter::empty()
629629
.chain(g1.decomposed.x_cells.iter().rev())
630630
.chain(g1.decomposed.y_cells.iter().rev())
631-
.chain(g2.decomposed.x_c0_cells.iter().rev())
632631
.chain(g2.decomposed.x_c1_cells.iter().rev())
633-
.chain(g2.decomposed.y_c0_cells.iter().rev())
632+
.chain(g2.decomposed.x_c0_cells.iter().rev())
634633
.chain(g2.decomposed.y_c1_cells.iter().rev())
634+
.chain(g2.decomposed.y_c0_cells.iter().rev())
635635
.cloned()
636-
.rev()
637636
.collect::<Vec<QuantumCell<F>>>()
638637
})
639638
.collect::<Vec<QuantumCell<F>>>();
640-
let input_rlc = pairing_chip.fp_chip.range.gate.inner_product(
639+
let input_rlc = ecc_chip.field_chip().range().gate().inner_product(
641640
ctx,
642-
input_cells,
641+
input_cells.into_iter().rev(),
643642
powers_of_rand.iter().cloned(),
644643
);
645644

@@ -662,12 +661,14 @@ impl<F: Field, const XI_0: i64> EccCircuit<F, XI_0> {
662661
fp12_chip.is_equal(ctx, &gt, &one)
663662
};
664663

664+
let op_output = ecc_chip.field_chip().range().gate().load_witness(
665+
ctx,
666+
Value::known(op.output.to_scalar().expect("EcPairing output = {0, 1}")),
667+
);
665668
ecc_chip.field_chip().range().gate().assert_equal(
666669
ctx,
667670
QuantumCell::Existing(success),
668-
QuantumCell::Witness(Value::known(
669-
op.output.to_scalar().expect("EcPairing output = {0, 1}"),
670-
)),
671+
QuantumCell::Existing(op_output),
671672
);
672673

673674
log::trace!("[ECC] EcPairingAssignment END:");

zkevm-circuits/src/evm_circuit.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
326326

327327
config.load_fixed_table(layouter, self.fixed_table_tags.clone())?;
328328
config.load_byte_table(layouter)?;
329+
config.pow_of_rand_table.assign(layouter, challenges)?;
329330
let export = config.execution.assign_block(layouter, block, challenges)?;
330331
self.exports.borrow_mut().replace(export);
331332
Ok(())
@@ -516,9 +517,6 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
516517
&block.get_ec_pairing_ops(),
517518
&challenges,
518519
)?;
519-
config
520-
.pow_of_rand_table
521-
.dev_load(&mut layouter, &challenges)?;
522520

523521
self.synthesize_sub(&config, &challenges, &mut layouter)
524522
}

zkevm-circuits/src/super_circuit.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,8 @@ impl<
643643
.synthesize_sub(&config.tx_circuit, challenges, layouter)?;
644644
self.sig_circuit
645645
.synthesize_sub(&config.sig_circuit, challenges, layouter)?;
646+
self.ecc_circuit
647+
.synthesize_sub(&config.ecc_circuit, challenges, layouter)?;
646648
self.modexp_circuit
647649
.synthesize_sub(&config.modexp_circuit, challenges, layouter)?;
648650
self.state_circuit
@@ -804,7 +806,7 @@ impl<
804806
log::debug!("super circuit needs k = {}", k);
805807

806808
let circuit =
807-
SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA,MAX_INNER_BLOCKS, MOCK_RANDOMNESS>::new_from_block(&block);
809+
SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA,MAX_INNER_BLOCKS, MOCK_RANDOMNESS>::new_from_block(&block);
808810

809811
let instance = circuit.instance();
810812
Ok((k, circuit, instance))

0 commit comments

Comments
 (0)