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

Fix ecrecover input rlc comparison (right-padding zeroes) #585

Merged
merged 5 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions bus-mapping/src/evm/opcodes/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,6 @@ impl<const N_ARGS: usize> Opcode for CallOpcode<N_ARGS> {
callee_gas_left,
);

log::info!(
"precompile returned {:?} with len {} and gas {} and is_success {}",
result,
result.len(),
contract_gas_cost,
call.is_success(),
);

// mutate the caller memory.
let caller_ctx_mut = state.caller_ctx_mut()?;
caller_ctx_mut.return_data = result.clone();
Expand Down
1 change: 0 additions & 1 deletion bus-mapping/src/precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ impl PrecompileCalls {
match self {
Self::Ecrecover | Self::Bn128Add => Some(128),
Self::Bn128Mul => Some(96),
Self::Blake2F => Some(213),
_ => None,
}
}
Expand Down
20 changes: 17 additions & 3 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub use crate::witness;
use crate::{
evm_circuit::param::{MAX_STEP_HEIGHT, STEP_STATE_HEIGHT},
table::{
BlockTable, BytecodeTable, CopyTable, ExpTable, KeccakTable, LookupTable, RwTable,
SigTable, TxTable,
BlockTable, BytecodeTable, CopyTable, ExpTable, KeccakTable, LookupTable, PowOfRandTable,
RwTable, SigTable, TxTable,
},
util::{SubCircuit, SubCircuitConfig},
};
Expand All @@ -49,6 +49,7 @@ pub struct EvmCircuitConfig<F> {
keccak_table: KeccakTable,
exp_table: ExpTable,
sig_table: SigTable,
pow_of_rand_table: PowOfRandTable,
}

/// Circuit configuration arguments
Expand All @@ -71,6 +72,8 @@ pub struct EvmCircuitConfigArgs<F: Field> {
pub exp_table: ExpTable,
/// SigTable
pub sig_table: SigTable,
// Power of Randomness Table.
pub pow_of_rand_table: PowOfRandTable,
}

/// Circuit exported cells after synthesis, used for subcircuit
Expand All @@ -97,6 +100,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
keccak_table,
exp_table,
sig_table,
pow_of_rand_table,
}: Self::ConfigArgs,
) -> Self {
let fixed_table = [(); 4].map(|_| meta.fixed_column());
Expand All @@ -114,6 +118,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
&keccak_table,
&exp_table,
&sig_table,
&pow_of_rand_table,
));

meta.annotate_lookup_any_column(byte_table[0], || "byte_range");
Expand All @@ -128,6 +133,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
keccak_table.annotate_columns(meta);
exp_table.annotate_columns(meta);
sig_table.annotate_columns(meta);
pow_of_rand_table.annotate_columns(meta);

Self {
fixed_table,
Expand All @@ -141,6 +147,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
keccak_table,
exp_table,
sig_table,
pow_of_rand_table,
}
}
}
Expand Down Expand Up @@ -418,6 +425,7 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
let keccak_table = KeccakTable::construct(meta);
let exp_table = ExpTable::construct(meta);
let sig_table = SigTable::construct(meta);
let pow_of_rand_table = PowOfRandTable::construct(meta, &challenges_expr);
(
EvmCircuitConfig::new(
meta,
Expand All @@ -431,6 +439,7 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
keccak_table,
exp_table,
sig_table,
pow_of_rand_table,
},
),
challenges,
Expand Down Expand Up @@ -478,6 +487,9 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
config
.sig_table
.dev_load(&mut layouter, block, &challenges)?;
config
.pow_of_rand_table
.dev_load(&mut layouter, &challenges)?;

self.synthesize_sub(&config, &challenges, &mut layouter)
}
Expand Down Expand Up @@ -661,7 +673,9 @@ mod evm_circuit_stats {
exp_table,
LOOKUP_CONFIG[7].1,
sig_table,
LOOKUP_CONFIG[8].1
LOOKUP_CONFIG[8].1,
pow_of_rand_table,
LOOKUP_CONFIG[9].1
);
}

Expand Down
8 changes: 7 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::{
param::{
BLOCK_TABLE_LOOKUPS, BYTECODE_TABLE_LOOKUPS, COPY_TABLE_LOOKUPS, EXP_TABLE_LOOKUPS,
FIXED_TABLE_LOOKUPS, KECCAK_TABLE_LOOKUPS, N_BYTE_LOOKUPS, N_COPY_COLUMNS,
N_PHASE1_COLUMNS, RW_TABLE_LOOKUPS, SIG_TABLE_LOOKUPS, TX_TABLE_LOOKUPS,
N_PHASE1_COLUMNS, POW_OF_RAND_TABLE_LOOKUPS, RW_TABLE_LOOKUPS, SIG_TABLE_LOOKUPS,
TX_TABLE_LOOKUPS,
},
util::{instrumentation::Instrument, CachedRegion, CellManager, StoredExpression},
EvmCircuitExports,
Expand Down Expand Up @@ -377,6 +378,7 @@ impl<F: Field> ExecutionConfig<F> {
keccak_table: &dyn LookupTable<F>,
exp_table: &dyn LookupTable<F>,
sig_table: &dyn LookupTable<F>,
pow_of_rand_table: &dyn LookupTable<F>,
) -> Self {
let mut instrument = Instrument::default();
let q_usable = meta.complex_selector();
Expand Down Expand Up @@ -651,6 +653,7 @@ impl<F: Field> ExecutionConfig<F> {
keccak_table,
exp_table,
sig_table,
pow_of_rand_table,
&challenges,
&cell_manager,
);
Expand Down Expand Up @@ -907,6 +910,7 @@ impl<F: Field> ExecutionConfig<F> {
keccak_table: &dyn LookupTable<F>,
exp_table: &dyn LookupTable<F>,
sig_table: &dyn LookupTable<F>,
pow_of_rand_table: &dyn LookupTable<F>,
challenges: &Challenges<Expression<F>>,
cell_manager: &CellManager<F>,
) {
Expand All @@ -924,6 +928,7 @@ impl<F: Field> ExecutionConfig<F> {
Table::Keccak => keccak_table,
Table::Exp => exp_table,
Table::Sig => sig_table,
Table::PowOfRand => pow_of_rand_table,
}
.table_exprs(meta);
vec![(
Expand Down Expand Up @@ -1224,6 +1229,7 @@ impl<F: Field> ExecutionConfig<F> {
("EVM_lookup_keccak", KECCAK_TABLE_LOOKUPS),
("EVM_lookup_exp", EXP_TABLE_LOOKUPS),
("EVM_lookup_sig", SIG_TABLE_LOOKUPS),
("EVM_lookup_pow_of_rand", POW_OF_RAND_TABLE_LOOKUPS),
("EVM_adv_phase2", N_PHASE2_COLUMNS),
("EVM_copy", N_COPY_COLUMNS),
("EVM_lookup_byte", N_BYTE_LOOKUPS),
Expand Down
22 changes: 15 additions & 7 deletions zkevm-circuits/src/evm_circuit/execution/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,10 +949,6 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
.assign(region, offset, code_address)?;
self.is_precompile_lt
.assign(region, offset, code_address, 0x0Au64.into())?;
if is_precompile_call {
self.precompile_gadget
.assign(region, offset, precompile_addr.0[19].into())?;
}
let precompile_return_length = if is_precompile_call {
let value_rw = block.rws[step.rw_indices[32 + rw_offset]];
assert_eq!(
Expand Down Expand Up @@ -1028,28 +1024,40 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
.keccak_input()
.map(|randomness| rlc::value(return_bytes.iter().rev(), randomness));
(
Value::known(F::from(input_len as u64)),
input_len as u64,
input_bytes_rlc,
output_bytes_rlc,
return_bytes_rlc,
)
} else {
(
Value::known(F::zero()),
0,
Value::known(F::zero()),
Value::known(F::zero()),
Value::known(F::zero()),
)
};

self.input_len.assign(region, offset, input_len)?;
self.input_len
.assign(region, offset, Value::known(F::from(input_len)))?;
self.input_bytes_rlc
.assign(region, offset, input_bytes_rlc)?;
self.output_bytes_rlc
.assign(region, offset, output_bytes_rlc)?;
self.return_bytes_rlc
.assign(region, offset, return_bytes_rlc)?;

if is_precompile_call {
self.precompile_gadget.assign(
region,
offset,
precompile_addr.0[19].into(),
input_bytes_rlc,
cd_length.as_u64(),
region.challenges().keccak_input(),
)?;
}

Ok(())
}
}
Expand Down
9 changes: 7 additions & 2 deletions zkevm-circuits/src/evm_circuit/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub const MAX_STEP_HEIGHT: usize = 21;
pub(crate) const STEP_STATE_HEIGHT: usize = 1;

/// Number of Advice Phase2 columns in the EVM circuit
pub(crate) const N_PHASE2_COLUMNS: usize = 4;
pub(crate) const N_PHASE2_COLUMNS: usize = 5;

/// Number of Advice Phase1 columns in the EVM circuit
pub(crate) const N_PHASE1_COLUMNS: usize =
Expand All @@ -38,7 +38,8 @@ pub(crate) const EVM_LOOKUP_COLS: usize = FIXED_TABLE_LOOKUPS
+ COPY_TABLE_LOOKUPS
+ KECCAK_TABLE_LOOKUPS
+ EXP_TABLE_LOOKUPS
+ SIG_TABLE_LOOKUPS;
+ SIG_TABLE_LOOKUPS
+ POW_OF_RAND_TABLE_LOOKUPS;

/// Lookups done per row.
pub(crate) const LOOKUP_CONFIG: &[(Table, usize)] = &[
Expand All @@ -51,6 +52,7 @@ pub(crate) const LOOKUP_CONFIG: &[(Table, usize)] = &[
(Table::Keccak, KECCAK_TABLE_LOOKUPS),
(Table::Exp, EXP_TABLE_LOOKUPS),
(Table::Sig, SIG_TABLE_LOOKUPS),
(Table::PowOfRand, POW_OF_RAND_TABLE_LOOKUPS),
];

/// Fixed Table lookups done in EVMCircuit
Expand Down Expand Up @@ -80,6 +82,9 @@ pub const EXP_TABLE_LOOKUPS: usize = 1;
/// Sig Table lookups done in EVMCircuit
pub const SIG_TABLE_LOOKUPS: usize = 1;

/// Power of Randomness lookups done from EVM Circuit.
pub const POW_OF_RAND_TABLE_LOOKUPS: usize = 1;

/// Maximum number of bytes that an integer can fit in field without wrapping
/// around.
pub(crate) const MAX_N_BYTES_INTEGER: usize = 31;
Expand Down
14 changes: 14 additions & 0 deletions zkevm-circuits/src/evm_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub(crate) enum Table {
Keccak,
Exp,
Sig,
PowOfRand,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -300,6 +301,10 @@ pub(crate) enum Lookup<F> {
sig_s_rlc: Expression<F>,
recovered_addr: Expression<F>,
},
PowOfRandTable {
exponent: Expression<F>,
pow_of_rand: Expression<F>,
},
/// Conditional lookup enabled by the first element.
Conditional(Expression<F>, Box<Lookup<F>>),
}
Expand All @@ -320,6 +325,7 @@ impl<F: Field> Lookup<F> {
Self::KeccakTable { .. } => Table::Keccak,
Self::ExpTable { .. } => Table::Exp,
Self::SigTable { .. } => Table::Sig,
Self::PowOfRandTable { .. } => Table::PowOfRand,
Self::Conditional(_, lookup) => lookup.table(),
}
}
Expand Down Expand Up @@ -456,6 +462,14 @@ impl<F: Field> Lookup<F> {
sig_s_rlc.clone(),
recovered_addr.clone(),
],
Self::PowOfRandTable {
exponent,
pow_of_rand,
} => vec![
1.expr(), /* q_enable */
exponent.clone(),
pow_of_rand.clone(),
],
Self::Conditional(condition, lookup) => lookup
.input_exprs()
.into_iter()
Expand Down
17 changes: 17 additions & 0 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
}

// Sig Table

pub(crate) fn sig_table_lookup(
&mut self,
msg_hash_rlc: Expression<F>,
Expand All @@ -1373,6 +1374,22 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
);
}

// Power of Randomness Table

pub(crate) fn pow_of_rand_lookup(
&mut self,
exponent: Expression<F>,
pow_of_rand: Expression<F>,
) {
self.add_lookup(
"power of randomness",
Lookup::PowOfRandTable {
exponent,
pow_of_rand,
},
)
}

// Keccak Table

pub(crate) fn keccak_table_lookup(
Expand Down
4 changes: 4 additions & 0 deletions zkevm-circuits/src/evm_circuit/util/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ impl Instrument {
CellType::Lookup(Table::Sig) => {
report.sig_table = data_entry;
}
CellType::Lookup(Table::PowOfRand) => {
report.pow_of_rand_table = data_entry;
}
}
}
report_collection.push(report);
Expand Down Expand Up @@ -133,6 +136,7 @@ pub(crate) struct ExecStateReport {
pub(crate) keccak_table: StateReportRow,
pub(crate) exp_table: StateReportRow,
pub(crate) sig_table: StateReportRow,
pub(crate) pow_of_rand_table: StateReportRow,
}

impl From<ExecutionState> for ExecStateReport {
Expand Down
Loading