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

word stage3: try remove rlc related #1112

Open
wants to merge 12 commits into
base: word_hi_lo
Choose a base branch
from
1 change: 1 addition & 0 deletions aggregator/src/tests/rlc/dynamic_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl Circuit<Fr> for DynamicHashCircuit {

impl CircuitExt<Fr> for DynamicHashCircuit {}

// TODO: enable it later after other test pass.
#[test]
fn test_hash_circuit() {
const LEN: usize = 100;
Expand Down
11 changes: 5 additions & 6 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,26 +443,25 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {

// 1. Handle contract creation transaction.
let (init_code_rlc, keccak_code_hash) = cb.condition(tx_is_create.expr(), |cb| {
let caller_nonce_hash_bytes_rlc =
cb.word_rlc(caller_nonce_hash_bytes.limbs.clone().map(|l| l.expr()));
// let caller_nonce_hash_bytes_rlc =
// cb.word_rlc(caller_nonce_hash_bytes.limbs.clone().map(|l| l.expr()));
cb.keccak_table_lookup(
create.input_rlc(cb),
create.input_length(),
caller_nonce_hash_bytes_rlc,
//caller_nonce_hash_bytes_rlc,
caller_nonce_hash_bytes.to_word(),
);

//let keccak_code_hash = cb.query_cell_phase2();
let keccak_code_hash = cb.query_word32();

let init_code_rlc = cb.query_cell_phase2();
let keccak_code_hash_rlc =
cb.word_rlc(keccak_code_hash.limbs.clone().map(|l| l.expr()));

// keccak table lookup for init code.
cb.keccak_table_lookup(
init_code_rlc.expr(),
tx_call_data_length.expr(),
keccak_code_hash_rlc,
// keccak_code_hash_rlc,
keccak_code_hash.to_word(),
);
// copy table lookup for init code.
Expand Down
3 changes: 2 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/blockhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl<F: Field> ExecutionGadget<F> for BlockHashGadget<F> {
let is_valid = and::expr([block_number.lt_cap(), diff_lt.expr()]);
//let block_hash = cb.query_word_unchecked(); // use in stage2 after block table --> word
let block_hash = cb.query_word32();
#[cfg(not(feature = "scroll"))]
let block_hash_rlc = cb.word_rlc(block_hash.limbs.clone().map(|ref l| l.expr()));

cb.condition(is_valid.expr(), |cb| {
Expand Down Expand Up @@ -115,7 +116,7 @@ impl<F: Field> ExecutionGadget<F> for BlockHashGadget<F> {
.unwrap(),
),
(2 * N_BYTES_U64).expr(),
block_hash_rlc,
//block_hash_rlc,
block_hash.to_word(),
);
});
Expand Down
13 changes: 6 additions & 7 deletions zkevm-circuits/src/evm_circuit/execution/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,26 +398,25 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
and::expr([is_precheck_ok.clone(), not_address_collision.expr()]),
|cb| {
cb.condition(init_code.has_length(), |cb| {
let keccak_code_hash_rlc =
cb.word_rlc(keccak_code_hash.limbs.clone().map(|l| l.expr()));
// let keccak_code_hash_rlc =
// cb.word_rlc(keccak_code_hash.limbs.clone().map(|l| l.expr()));
cb.keccak_table_lookup(
//create.input_rlc(cb),
init_code_rlc.expr(),
//create.input_length(),
init_code.length(),
keccak_code_hash_rlc,
// keccak_code_hash_rlc,
keccak_code_hash.to_word(),
);
});

// keccak table lookup to verify contract address.
let kecck_output_exprs = keccak_output.limbs.clone().map(|l| l.expr());
let keccak_output_rlc = cb.word_rlc(kecck_output_exprs);
// let kecck_output_exprs = keccak_output.limbs.clone().map(|l| l.expr());
// let keccak_output_rlc = cb.word_rlc(kecck_output_exprs);
cb.keccak_table_lookup(
create.input_rlc(cb),
create.input_length(),
keccak_output_rlc,
//keccak_output.expr(),
// keccak_output_rlc,
keccak_output.to_word(),
);

Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ impl<F: Field> ExecutionGadget<F> for Sha3Gadget<F> {
cb.require_zero("rlc_acc == 0 for size = 0", rlc_acc.expr());
});

let output_rlc = cb.word_rlc(sha3_digest.limbs.clone().map(|l| l.expr()));
// let output_rlc = cb.word_rlc(sha3_digest.limbs.clone().map(|l| l.expr()));
cb.keccak_table_lookup(
rlc_acc.expr(),
memory_address.length(),
output_rlc,
//output_rlc,
sha3_digest.to_word(),
);

Expand Down
4 changes: 1 addition & 3 deletions zkevm-circuits/src/evm_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub(crate) enum Lookup<F> {
input_len: Expression<F>,
/// Output (hash) until this state. This is the RLC representation of
/// the final output keccak256 hash of the input.
output_rlc: Expression<F>,
// output_rlc: Expression<F>,
output: Word<Expression<F>>,
},
/// Lookup to sha256 table.
Expand Down Expand Up @@ -494,14 +494,12 @@ impl<F: Field> Lookup<F> {
Self::KeccakTable {
input_rlc,
input_len,
output_rlc,
output,
} => vec![
1.expr(), // q_enable
1.expr(), // is_final
input_rlc.clone(),
input_len.clone(),
output_rlc.clone(),
output.lo(),
output.hi(),
],
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,15 +1678,15 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
&mut self,
input_rlc: Expression<F>,
input_len: Expression<F>,
output_rlc: Expression<F>,
//output_rlc: Expression<F>,
output: Word<Expression<F>>,
) {
self.add_lookup(
"keccak lookup",
Lookup::KeccakTable {
input_rlc,
input_len,
output_rlc,
//output_rlc,
output,
},
);
Expand Down
32 changes: 15 additions & 17 deletions zkevm-circuits/src/keccak_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ use crate::{
},
table::{KeccakTable, LookupTable},
util::{
// word::{self, WordExpr},
Challenges,
SubCircuit,
SubCircuitConfig,
word::{self, WordExpr},
Challenges, SubCircuit, SubCircuitConfig,
},
witness,
};
Expand Down Expand Up @@ -112,8 +110,8 @@ impl<F: Field> SubCircuitConfig<F> for KeccakCircuitConfig<F> {
let is_final = keccak_table.is_final;
let length = keccak_table.input_len;
let data_rlc = keccak_table.input_rlc;
let hash_rlc = keccak_table.output_rlc;
//let hash_word = keccak_table.output;
// let hash_rlc = keccak_table.output_rlc;
let hash_word = keccak_table.output;

let normalize_3 = array_init::array_init(|_| meta.lookup_table_column());
let normalize_4 = array_init::array_init(|_| meta.lookup_table_column());
Expand Down Expand Up @@ -580,18 +578,18 @@ impl<F: Field> SubCircuitConfig<F> for KeccakCircuitConfig<F> {
});
}
let hash_bytes_le = hash_bytes.into_iter().rev().collect::<Vec<_>>();
let rlc = compose_rlc::expr(&hash_bytes_le, challenges.evm_word());
// let rlc = compose_rlc::expr(&hash_bytes_le, challenges.evm_word());
cb.condition(start_new_hash, |cb| {
// cb.require_equal_word(
// "hash output check",
// word::Word32::new(hash_bytes_le.try_into().expect("32 limbs")).to_word(),
// hash_word.map(|col| meta.query_advice(col, Rotation::cur())),
// );
cb.require_equal(
"hash rlc check",
rlc,
meta.query_advice(hash_rlc, Rotation::cur()),
cb.require_equal_word(
"hash output check",
word::Word32::new(hash_bytes_le.try_into().expect("32 limbs")).to_word(),
hash_word.map(|col| meta.query_advice(col, Rotation::cur())),
);
// cb.require_equal(
// "hash rlc check",
// rlc,
// meta.query_advice(hash_rlc, Rotation::cur()),
// );
});
cb.gate(meta.query_fixed(q_round_last, Rotation::cur()))
});
Expand Down Expand Up @@ -949,7 +947,7 @@ impl<F: Field> KeccakCircuitConfig<F> {
Value::known(F::from(row.is_final as u64)),
row.data_rlc,
Value::known(F::from(row.length as u64)),
row.hash_rlc,
//row.hash_rlc,
row.hash.lo(),
row.hash.hi(),
],
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,15 +646,15 @@ impl<F: Field> SubCircuitConfig<F> for PiCircuitConfig<F> {

let rpi_rlc = meta.query_advice(rpi, Rotation::cur());
let rpi_length = meta.query_advice(rpi_length_acc, Rotation::cur());
let output = meta.query_advice(rpi_rlc_acc, Rotation::cur());
// let output = meta.query_advice(rpi_rlc_acc, Rotation::cur());
let output_word = rpi_word.query_advice(meta, Rotation::cur());

let input_exprs = vec![
1.expr(), // q_enable = true
1.expr(), // is_final = true
rpi_rlc,
rpi_length,
output,
//output,
output_word.lo(),
output_word.hi(),
];
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/sig_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<F: Field> SubCircuitConfig<F> for SigCircuitConfig<F> {
is_enable.clone(),
is_enable.clone() * meta.query_advice(rlc_column, Rotation(1)),
is_enable.clone() * 64usize.expr(),
is_enable.clone() * meta.query_advice(rlc_column, Rotation(2)),
//is_enable.clone() * meta.query_advice(rlc_column, Rotation(2)),
is_enable.clone() * meta.query_advice(rlc_column_word.lo(), Rotation::cur()),
is_enable * meta.query_advice(rlc_column_word.hi(), Rotation::cur()),
];
Expand All @@ -191,7 +191,7 @@ impl<F: Field> SubCircuitConfig<F> for SigCircuitConfig<F> {
meta.query_advice(keccak_table.is_final, Rotation::cur()),
meta.query_advice(keccak_table.input_rlc, Rotation::cur()),
meta.query_advice(keccak_table.input_len, Rotation::cur()),
meta.query_advice(keccak_table.output_rlc, Rotation::cur()),
//meta.query_advice(keccak_table.output_rlc, Rotation::cur()),
meta.query_advice(keccak_table.output.lo(), Rotation::cur()),
meta.query_advice(keccak_table.output.hi(), Rotation::cur()),
];
Expand Down
20 changes: 10 additions & 10 deletions zkevm-circuits/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ pub struct KeccakTable {
/// Byte array input length
pub input_len: Column<Advice>,
/// RLC of the hash result
pub output_rlc: Column<Advice>, // RLC of hash of input bytes
// pub output_rlc: Column<Advice>, // RLC of hash of input bytes
/// TODO: finally remove output_rlc and use Word hi lo
pub output: word::Word<Column<Advice>>,
}
Expand All @@ -1470,7 +1470,7 @@ impl<F: Field> LookupTable<F> for KeccakTable {
self.is_final.into(),
self.input_rlc.into(),
self.input_len.into(),
self.output_rlc.into(),
// self.output_rlc.into(),
self.output.lo().into(),
self.output.hi().into(),
]
Expand All @@ -1497,7 +1497,7 @@ impl KeccakTable {
is_final: meta.advice_column(),
input_rlc: meta.advice_column_in(SecondPhase),
input_len: meta.advice_column(),
output_rlc: meta.advice_column_in(SecondPhase),
//output_rlc: meta.advice_column_in(SecondPhase),
output: word::Word::new([meta.advice_column(), meta.advice_column()]),
}
}
Expand All @@ -1507,7 +1507,7 @@ impl KeccakTable {
pub fn assignments<F: Field>(
input: &[u8],
challenges: &Challenges<Value<F>>,
) -> Vec<[Value<F>; 6]> {
) -> Vec<[Value<F>; 5]> {
let input_rlc = challenges
.keccak_input()
.map(|challenge| rlc::value(input.iter().rev(), challenge));
Expand All @@ -1516,17 +1516,17 @@ impl KeccakTable {
keccak.update(input);
let output = keccak.digest();
let output_word = Word::from_big_endian(output.as_slice());
let output_bytes = output_word.to_le_bytes();
let output_rlc = challenges
.evm_word()
.map(|challenge| rlc::value(&output_bytes, challenge));
// let output_bytes = output_word.to_le_bytes();
// let output_rlc = challenges
// .evm_word()
// .map(|challenge| rlc::value(&output_bytes, challenge));
let output_lo_hi = word::Word::from(output_word);

vec![[
Value::known(F::one()),
input_rlc,
Value::known(input_len),
output_rlc,
// output_rlc,
Value::known(output_lo_hi.lo()),
Value::known(output_lo_hi.hi()),
]]
Expand All @@ -1539,7 +1539,7 @@ impl KeccakTable {
&self,
region: &mut Region<F>,
offset: usize,
values: [Value<F>; 6],
values: [Value<F>; 5],
) -> Result<Vec<AssignedCell<F, F>>, Error> {
let mut res = vec![];
for (&column, value) in <KeccakTable as LookupTable<F>>::advice_columns(self)
Expand Down
1 change: 0 additions & 1 deletion zkevm-circuits/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,6 @@ impl<F: Field> TxCircuitConfig<F> {
1.expr(), // is_final
meta.query_advice(tx_table.value.lo(), Rotation::next()), // input_rlc
meta.query_advice(tx_table.value.lo(), Rotation::cur()), // input_len
meta.query_advice(tx_table.value.lo(), Rotation(2)), // output_rlc
meta.query_advice(tx_hash_word.lo(), Rotation::cur()), // output_word lo
meta.query_advice(tx_hash_word.hi(), Rotation::cur()), // output_word hi
]
Expand Down
Loading