Skip to content

Commit

Permalink
Rename Bitwise to Xor (#545)
Browse files Browse the repository at this point in the history
We have already had a PR to rename the Bitwise STARK table into Xor
Stark table to be easier to understand #513

Unfortunatelly, that PR did not take care of all Bitwise occurances that
are relevant for the rename. Hence this PR tries to resolve it.

Note that we have still kept the `cpu/bitwise.rs` with the same name.
This is due to the fact that it handles multiple bit operations, rather
than only XOR. For the same reason we have kept several other
occurances.

Co-authored-by: Jaynti Kanani <jdkanani@gmail.com>
  • Loading branch information
ElusAegis and jdkanani authored Aug 20, 2023
1 parent 62d56cd commit f15bbf0
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 45 deletions.
4 changes: 2 additions & 2 deletions circuits/src/generation/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn generate_cpu_trace<F: RichField>(
// TODO(Matthias): find a way to make either compiler or runtime complain
// if we have two (conflicting) users in the same row.
bitshift: Bitshift::from(0).map(F::from_canonical_u64),
xor: generate_bitwise_row(&inst, state),
xor: generate_xor_row(&inst, state),

..CpuState::default()
};
Expand Down Expand Up @@ -167,7 +167,7 @@ fn generate_sign_handling<F: RichField>(row: &mut CpuState<F>, aux: &Aux) {
row.abs_diff = F::from_noncanonical_u64(abs_diff);
}

fn generate_bitwise_row<F: RichField>(inst: &Instruction, state: &State) -> XorView<F> {
fn generate_xor_row<F: RichField>(inst: &Instruction, state: &State) -> XorView<F> {
let a = match inst.op {
Op::AND | Op::OR | Op::XOR => state.get_register_value(inst.args.rs1),
Op::SRL | Op::SLL => 0b1_1111,
Expand Down
22 changes: 9 additions & 13 deletions circuits/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
//! appropriate values based on the [`Program`] and [`ExecutionRecord`].

pub mod bitshift;
pub mod bitwise;
pub mod cpu;
pub mod instruction;
pub mod memory;
pub mod program;
pub mod rangecheck;
pub mod xor;

use itertools::Itertools;
use mozak_vm::elf::Program;
Expand All @@ -22,9 +22,9 @@ use starky::stark::Stark;
use starky::vars::StarkEvaluationVars;

use self::bitshift::generate_shift_amount_trace;
use self::bitwise::generate_bitwise_trace;
use self::cpu::{generate_cpu_trace, generate_cpu_trace_extended};
use self::rangecheck::generate_rangecheck_trace;
use self::xor::generate_xor_trace;
use crate::bitshift::stark::BitshiftStark;
use crate::cpu::stark::CpuStark;
use crate::generation::program::generate_program_rom_trace;
Expand All @@ -41,19 +41,19 @@ pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
) -> [Vec<PolynomialValues<F>>; NUM_TABLES] {
let cpu_rows = generate_cpu_trace::<F>(program, record);
let rangecheck_rows = generate_rangecheck_trace::<F>(&cpu_rows);
let bitwise_rows = generate_bitwise_trace(&cpu_rows);
let xor_rows = generate_xor_trace(&cpu_rows);
let shift_amount_rows = generate_shift_amount_trace(&cpu_rows);
let program_rows = generate_program_rom_trace(program);

let cpu_trace = trace_to_poly_values(generate_cpu_trace_extended(cpu_rows, &program_rows));
let rangecheck_trace = trace_to_poly_values(rangecheck_rows);
let bitwise_trace = trace_rows_to_poly_values(bitwise_rows);
let xor_trace = trace_rows_to_poly_values(xor_rows);
let shift_amount_trace = trace_rows_to_poly_values(shift_amount_rows);
let program_trace = trace_rows_to_poly_values(program_rows);
[
cpu_trace,
rangecheck_trace,
bitwise_trace,
xor_trace,
shift_amount_trace,
program_trace,
]
Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn debug_traces<F: RichField + Extendable<D>, const D: usize>(
[(); XorStark::<F, D>::COLUMNS]:,
[(); BitshiftStark::<F, D>::COLUMNS]:,
[(); ProgramStark::<F, D>::COLUMNS]:, {
let [cpu_trace, rangecheck_trace, bitwise_trace, shift_amount_trace, program_trace]: [Vec<
let [cpu_trace, rangecheck_trace, xor_trace, shift_amount_trace, program_trace]: [Vec<
PolynomialValues<F>,
>;
NUM_TABLES] = generate_traces(program, record);
Expand All @@ -111,17 +111,13 @@ pub fn debug_traces<F: RichField + Extendable<D>, const D: usize>(
rangecheck_trace,
"RANGE_CHECK_STARK",
),
// Bitwise
debug_single_trace::<F, D, XorStark<F, D>>(
&mozak_stark.xor_stark,
bitwise_trace,
"BITWISE_STARK",
),
// Xor
debug_single_trace::<F, D, XorStark<F, D>>(&mozak_stark.xor_stark, xor_trace, "XOR_STARK",),
// Bitshift
debug_single_trace::<F, D, BitshiftStark<F, D>>(
&mozak_stark.shift_amount_stark,
shift_amount_trace,
"BITWISE_STARK",
"XOR_STARK",
),
]
.into_iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::cpu::columns::CpuState;
use crate::utils::pad_trace_with_default;
use crate::xor::columns::{XorColumnsView, XorView};

fn filter_bitwise_trace<F: RichField>(
fn filter_xor_trace<F: RichField>(
step_rows: &[CpuState<F>],
) -> impl Iterator<Item = XorView<F>> + '_ {
step_rows.iter().filter_map(|row| {
Expand All @@ -25,9 +25,9 @@ fn to_bits<F: RichField>(val: F) -> [F; u32::BITS as usize] {
#[must_use]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::cast_possible_truncation)]
pub fn generate_bitwise_trace<F: RichField>(cpu_trace: &[CpuState<F>]) -> Vec<XorColumnsView<F>> {
pub fn generate_xor_trace<F: RichField>(cpu_trace: &[CpuState<F>]) -> Vec<XorColumnsView<F>> {
pad_trace_with_default(
filter_bitwise_trace(cpu_trace)
filter_xor_trace(cpu_trace)
.map(|execution| XorColumnsView {
is_execution_row: F::ONE,
execution,
Expand Down
12 changes: 6 additions & 6 deletions circuits/src/stark/mozak_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Default for MozakStark<F, D>
program_stark: ProgramStark::default(),
cross_table_lookups: [
RangecheckCpuTable::lookups(),
BitwiseCpuTable::lookups(),
XorCpuTable::lookups(),
BitshiftCpuTable::lookups(),
InnerCpuTable::lookups(),
ProgramCpuTable::lookups(),
Expand Down Expand Up @@ -79,7 +79,7 @@ pub(crate) const NUM_TABLES: usize = 5;
pub enum TableKind {
Cpu = 0,
RangeCheck = 1,
Bitwise = 2,
Xor = 2,
Bitshift = 3,
Program = 4,
}
Expand All @@ -90,7 +90,7 @@ impl TableKind {
[
TableKind::Cpu,
TableKind::RangeCheck,
TableKind::Bitwise,
TableKind::Xor,
TableKind::Bitshift,
TableKind::Program,
]
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<F: Field> CpuTable<F> {
impl<F: Field> XorTable<F> {
#[allow(clippy::new_ret_no_self)]
pub fn new(columns: Vec<Column<F>>, filter_column: Column<F>) -> Table<F> {
Table::new(TableKind::Bitwise, columns, filter_column)
Table::new(TableKind::Xor, columns, filter_column)
}
}

Expand Down Expand Up @@ -182,9 +182,9 @@ impl<F: Field> Lookups<F> for RangecheckCpuTable<F> {
}
}

pub struct BitwiseCpuTable<F: Field>(CrossTableLookup<F>);
pub struct XorCpuTable<F: Field>(CrossTableLookup<F>);

impl<F: Field> Lookups<F> for BitwiseCpuTable<F> {
impl<F: Field> Lookups<F> for XorCpuTable<F> {
fn lookups() -> CrossTableLookup<F> {
CrossTableLookup::new(
vec![CpuTable::new(
Expand Down
6 changes: 3 additions & 3 deletions circuits/src/stark/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ where
let xor_proof = prove_single_table::<F, C, XorStark<F, D>, D>(
&mozak_stark.xor_stark,
config,
&traces_poly_values[TableKind::Bitwise as usize],
&trace_commitments[TableKind::Bitwise as usize],
&ctl_data_per_table[TableKind::Bitwise as usize],
&traces_poly_values[TableKind::Xor as usize],
&trace_commitments[TableKind::Xor as usize],
&ctl_data_per_table[TableKind::Xor as usize],
challenger,
timing,
)?;
Expand Down
6 changes: 3 additions & 3 deletions circuits/src/stark/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ where

verify_stark_proof_with_challenges::<F, C, XorStark<F, D>, D>(
&xor_stark,
&all_proof.stark_proofs[TableKind::Bitwise as usize],
&stark_challenges[TableKind::Bitwise as usize],
&ctl_vars_per_table[TableKind::Bitwise as usize],
&all_proof.stark_proofs[TableKind::Xor as usize],
&stark_challenges[TableKind::Xor as usize],
&ctl_vars_per_table[TableKind::Xor as usize],
config,
)?;

Expand Down
4 changes: 2 additions & 2 deletions circuits/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use starky::verifier::verify_stark_proof;
use crate::bitshift::stark::BitshiftStark;
use crate::cpu::stark::CpuStark;
use crate::generation::bitshift::generate_shift_amount_trace;
use crate::generation::bitwise::generate_bitwise_trace;
use crate::generation::cpu::{generate_cpu_trace, generate_cpu_trace_extended};
use crate::generation::memory::generate_memory_trace;
use crate::generation::program::generate_program_rom_trace;
use crate::generation::rangecheck::generate_rangecheck_trace;
use crate::generation::xor::generate_xor_trace;
use crate::memory::stark::MemoryStark;
use crate::rangecheck::stark::RangeCheckStark;
use crate::stark::mozak_stark::MozakStark;
Expand Down Expand Up @@ -118,7 +118,7 @@ impl ProveAndVerify for XorStark<F, D> {

let stark = S::default();
let cpu_trace = generate_cpu_trace(program, record);
let trace_poly_values = trace_rows_to_poly_values(generate_bitwise_trace(&cpu_trace));
let trace_poly_values = trace_rows_to_poly_values(generate_xor_trace(&cpu_trace));
let proof = prove_table::<F, C, S, D>(
stark,
&config,
Expand Down
10 changes: 4 additions & 6 deletions circuits/src/xor/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ pub struct XorView<T> {
}
columns_view_impl!(XorView);

/// Columns containing the data which are looked from cpu table into Bitwise
/// stark. [`CpuTable`](crate::cross_table_lookup::CpuTable)
/// [`BitwiseTable`](crate::cross_table_lookup::BitwiseTable).
/// Columns containing the data which are looked from the CPU table into Xor
/// stark table.
#[must_use]
pub fn data_for_cpu<F: Field>() -> Vec<Column<F>> { Column::singles(MAP.execution) }

/// Column for a binary filter to indicate a lookup from the
/// [`CpuTable`](crate::cross_table_lookup::CpuTable) in the Mozak
/// [`BitwiseTable`](crate::cross_table_lookup::BitwiseTable).
/// Column for a binary filter to indicate a lookup from the CPU table into Xor
/// stark table.
#[must_use]
pub fn filter_for_cpu<F: Field>() -> Column<F> { Column::single(MAP.is_execution_row) }
2 changes: 1 addition & 1 deletion circuits/src/xor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This module contains the **`Bitwise` STARK Table**.
//! This module contains the **`Xor` STARK Table**.
//! This Stark is used to contain the Xor evaluation of the execution.
//! Using the Xor table, we can then construct the other
//! bitwise operations, such as `And` and `Or`.
Expand Down
8 changes: 2 additions & 6 deletions circuits/src/xor/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ mod tests {
use starky::stark_testing::test_stark_low_degree;
use starky::verifier::verify_stark_proof;

use crate::generation::bitwise::generate_bitwise_trace;
use crate::generation::cpu::generate_cpu_trace;
use crate::generation::xor::generate_xor_trace;
use crate::stark::utils::trace_rows_to_poly_values;
use crate::test_utils::{standard_faster_config, C, D, F};
use crate::xor::stark::XorStark;
Expand Down Expand Up @@ -137,11 +137,7 @@ mod tests {
// assert_eq!(record.last_state.get_register_value(7), a ^ (b + imm));
let mut timing = TimingTree::new("xor", log::Level::Debug);
let cpu_trace = generate_cpu_trace(&program, &record);
let trace = timed!(
timing,
"generate_bitwise_trace",
generate_bitwise_trace(&cpu_trace)
);
let trace = timed!(timing, "generate_xor_trace", generate_xor_trace(&cpu_trace));
let trace_poly_values = timed!(timing, "trace to poly", trace_rows_to_poly_values(trace));
let stark = S::default();

Expand Down

0 comments on commit f15bbf0

Please sign in to comment.