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

Logup for o1vm #2757

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0a07bba
Add flake.nix; flake.lock
Fizzixnerd Aug 28, 2024
1ec45d7
o1vm/pickles: first shot at logup
Fizzixnerd Nov 13, 2024
1348b91
o1vm/pickles: Format
Fizzixnerd Nov 15, 2024
047534f
WIP: remove lookup_env from proof inputs
marcbeunardeau88 Nov 20, 2024
91b3ba4
WIP WitnessColumns contains lookup related things
marcbeunardeau88 Nov 20, 2024
a8a411a
o1vm/pickles: compiles again
Fizzixnerd Nov 22, 2024
75c6c19
o1vm/pickles: do lookup constraints
Fizzixnerd Nov 26, 2024
1cb1e50
o1vm/pickles: remove dead code
Fizzixnerd Nov 26, 2024
09e9cbd
o1vm/pickles: cargo fmt
Fizzixnerd Nov 26, 2024
a24ef42
merge master
Fizzixnerd Nov 27, 2024
70392b7
o1vm/pickles: Cargo format
Fizzixnerd Nov 27, 2024
48ea3d3
o1vm/pickles: rename logup to lookup
Fizzixnerd Nov 27, 2024
d9b472b
o1vm/pickles: add joint_combiner to challenge
Fizzixnerd Nov 27, 2024
55a2441
o1vm/pickles: remove unneeded comment
Fizzixnerd Nov 27, 2024
67730a0
o1vm/pickles: format
Fizzixnerd Nov 27, 2024
4556330
o1vm/pickles: Fix compilation error
Fizzixnerd Dec 3, 2024
9f61816
WIP: ignore lookup in verifier for now
Fizzixnerd Dec 3, 2024
395e322
WIP: Format
Fizzixnerd Dec 3, 2024
21524a5
WIP: Fix index arithmetic in column_env.rs
Fizzixnerd Dec 3, 2024
87d4f9c
o1vm/main: move lookup cst into instruction loop
marcbeunardeau88 Dec 3, 2024
0b53415
question for misha
marcbeunardeau88 Dec 3, 2024
d0236c7
bug fix ?
marcbeunardeau88 Dec 3, 2024
68085b7
WIP: format
Fizzixnerd Dec 3, 2024
5cdbb94
o1vm/pickles: add initial multiplicities and fixed tables to witness
Fizzixnerd Dec 5, 2024
4fc47da
o1vm/pickles: implement add_lookup for mips witnesses
Fizzixnerd Dec 5, 2024
95563f7
o1vm/pickles: Format
Fizzixnerd Dec 5, 2024
d7b1fc9
Merge branch 'master' into fizzixnerd/logup-pickles
Fizzixnerd Jan 6, 2025
14dd237
Merge remote-tracking branch 'origin/master' into fizzixnerd/logup-pi…
Fizzixnerd Jan 6, 2025
b17b781
o1vm/pickles: get compiling again
Fizzixnerd Jan 6, 2025
2a4b4ac
o1vm/pickles/lookup: checkpoint for lookups
Fizzixnerd Jan 6, 2025
487da00
WIP
marcbeunardeau88 Jan 7, 2025
681455f
Merge Marc's work
Fizzixnerd Jan 13, 2025
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
95 changes: 95 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kimchi/src/circuits/lookup/lookups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl<F: Copy> SingleLookup<F> {
}

/// The table ID associated with a particular lookup
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LookupTableID {
/// Look up the value from the given fixed table ID
Constant(i32),
Expand Down
10 changes: 6 additions & 4 deletions msm/src/logup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ pub struct LogupWitness<F, ID: LookupTableID> {
#[derive(Debug, Clone)]
pub struct LookupProof<T, ID> {
/// The multiplicity polynomials
pub(crate) m: BTreeMap<ID, Vec<T>>,
pub m: BTreeMap<ID, Vec<T>>,
/// The polynomial keeping the sum of each row
pub(crate) h: BTreeMap<ID, Vec<T>>,
pub h: BTreeMap<ID, Vec<T>>,
/// The "running-sum" over the rows, coined `φ`
pub(crate) sum: T,
pub sum: T,
/// All fixed lookup tables values, indexed by their ID
pub(crate) fixed_tables: BTreeMap<ID, T>,
pub fixed_tables: BTreeMap<ID, T>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be reverted.

}

/// Iterator implementation to abstract the content of the structure.
Expand Down Expand Up @@ -437,6 +437,7 @@ pub fn constraint_lookups<F: PrimeField, ID: LookupTableID>(
})
.collect();

// isn't it supposed to be not table_id.runtime_create_column()
if table_id.is_fixed() || table_id.runtime_create_column() {
let table_lookup = Logup {
table_id: *table_id,
Expand Down Expand Up @@ -512,6 +513,7 @@ pub mod prover {
}

/// Represents the environment for the logup argument.
#[derive(Clone)]
Fizzixnerd marked this conversation as resolved.
Show resolved Hide resolved
pub struct Env<G: KimchiCurve, ID: LookupTableID> {
/// The polynomial of the multiplicities, indexed by the table ID.
pub lookup_counters_poly_d1: BTreeMap<ID, Vec<DensePolynomial<G::ScalarField>>>,
Expand Down
69 changes: 56 additions & 13 deletions o1vm/src/interpreters/mips/column.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::interpreters::mips::Instruction::{self, IType, JType, RType};
use kimchi::circuits::lookup::lookups::LookupTableID;
use kimchi_msm::{
columns::{Column, ColumnIndexer},
witness::Witness,
};
use std::ops::{Index, IndexMut};
use std::{
collections::HashMap,
ops::{Index, IndexMut},
};
use strum::EnumCount;

use super::{ITypeInstruction, JTypeInstruction, RTypeInstruction};
Expand Down Expand Up @@ -52,7 +56,7 @@ pub const N_MIPS_COLS: usize = N_MIPS_REL_COLS + N_MIPS_SEL_COLS;

/// Abstract columns (or variables of our multi-variate polynomials) that will
/// be used to describe our constraints.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ColumnAlias {
// Can be seen as the abstract indexed variable X_{i}
ScratchState(usize),
Expand All @@ -61,26 +65,41 @@ pub enum ColumnAlias {
ScratchStateInverse(usize),
InstructionCounter,
Selector(usize),
Lookup(LookupTableID, usize),
// TODO add
LookupPartialSum((u32, usize)),
/// Multiplicities, indexed. This corresponds to the `m_i`. First
/// indexed by table ID, then internal index.
LookupMultiplicity((u32, usize)),
/// The lookup aggregation, i.e. `phi`
LookupAggregation,
/// The fixed tables. The parameter is considered to the indexed table.
LookupFixedTable(u32),
}

/// The columns used by the MIPS circuit. The MIPS circuit is split into three
/// main opcodes: RType, JType, IType. The columns are shared between different
/// instruction types. (the total number of columns refers to the maximum of
/// columns used by each mode)
impl From<ColumnAlias> for usize {
fn from(alias: ColumnAlias) -> usize {
impl TryFrom<ColumnAlias> for usize {
type Error = String;
fn try_from(alias: ColumnAlias) -> Result<usize, String> {
// Note that SCRATCH_SIZE + 1 is for the error
match alias {
ColumnAlias::ScratchState(i) => {
assert!(i < SCRATCH_SIZE);
i
Ok(i)
}
ColumnAlias::ScratchStateInverse(i) => {
assert!(i < SCRATCH_SIZE_INVERSE);
SCRATCH_SIZE + i
Ok(SCRATCH_SIZE + i)
}
ColumnAlias::InstructionCounter => SCRATCH_SIZE + SCRATCH_SIZE_INVERSE,
ColumnAlias::Selector(s) => SCRATCH_SIZE + SCRATCH_SIZE_INVERSE + 1 + s,
ColumnAlias::InstructionCounter => Ok(SCRATCH_SIZE + SCRATCH_SIZE_INVERSE),
ColumnAlias::Selector(s) => Ok(SCRATCH_SIZE + SCRATCH_SIZE_INVERSE + 1 + s),
ColumnAlias::Lookup(table_id, idx) => Err(format!(
"Could not convert a lookup in table {:?} at index {} to a usize.",
table_id, idx
)),
}
}
}
Expand Down Expand Up @@ -124,7 +143,11 @@ impl From<Instruction> for usize {
/// - the (at most) 4 bytes of the preimage key that are currently being
/// processed
/// - 4 helpers to check if at least n bytes were read in the current row
pub type MIPSWitness<T> = Witness<N_MIPS_COLS, T>;
pub struct MIPSWitness<T> {
lookup_witness: HashMap<LookupTableID, Vec<T>>,
// TODO: Name better
column_witness: Witness<N_MIPS_COLS, T>,
}

// IMPLEMENTATIONS FOR COLUMN ALIAS

Expand All @@ -133,13 +156,33 @@ impl<T: Clone> Index<ColumnAlias> for MIPSWitness<T> {

/// Map the column alias to the actual column index.
fn index(&self, index: ColumnAlias) -> &Self::Output {
&self.cols[usize::from(index)]
match index {
ColumnAlias::ScratchState(_)
| ColumnAlias::ScratchStateInverse(_)
| ColumnAlias::InstructionCounter
| ColumnAlias::Selector(_) => {
&self.column_witness.cols[usize::try_from(index).unwrap()]
}
ColumnAlias::Lookup(table_idx, idx) => {
&self.lookup_witness.get(&table_idx).unwrap()[idx]
}
}
}
}

impl<T: Clone> IndexMut<ColumnAlias> for MIPSWitness<T> {
fn index_mut(&mut self, index: ColumnAlias) -> &mut Self::Output {
&mut self.cols[usize::from(index)]
match index {
ColumnAlias::ScratchState(_)
| ColumnAlias::ScratchStateInverse(_)
| ColumnAlias::InstructionCounter
| ColumnAlias::Selector(_) => {
&mut self.column_witness.cols[usize::try_from(index).unwrap()]
}
ColumnAlias::Lookup(table_idx, idx) => {
&mut self.lookup_witness.get_mut(&table_idx).unwrap()[idx]
}
}
}
}

Expand Down Expand Up @@ -188,13 +231,13 @@ impl<T: Clone> Index<Instruction> for MIPSWitness<T> {

/// Map the column alias to the actual column index.
fn index(&self, index: Instruction) -> &Self::Output {
&self.cols[usize::from(index)]
&self.column_witness.cols[usize::from(index)]
}
}

impl<T: Clone> IndexMut<Instruction> for MIPSWitness<T> {
fn index_mut(&mut self, index: Instruction) -> &mut Self::Output {
&mut self.cols[usize::from(index)]
&mut self.column_witness.cols[usize::from(index)]
}
}

Expand Down
16 changes: 12 additions & 4 deletions o1vm/src/interpreters/mips/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ use ark_ff::{Field, One};
use kimchi::circuits::{
expr::{ConstantTerm::Literal, Expr, ExprInner, Operations, Variable},
gate::CurrOrNext,
lookup::lookups::LookupTableID,
};
use kimchi_msm::columns::ColumnIndexer as _;
use std::array;
use std::{array, collections::HashMap};
use strum::IntoEnumIterator;

use super::column::N_MIPS_SEL_COLS;

/// The environment keeping the constraints between the different polynomials
pub struct Env<Fp> {
scratch_state_idx: usize,
scratch_state_idx_inverse: usize,
scratch_lookups: HashMap<LookupTableID, usize>,
/// A list of constraints, which are multi-variate polynomials over a field,
/// represented using the expression framework of `kimchi`.
constraints: Vec<E<Fp>>,
Expand All @@ -40,6 +40,7 @@ impl<Fp: Field> Default for Env<Fp> {
Self {
scratch_state_idx: 0,
scratch_state_idx_inverse: 0,
scratch_lookups: HashMap::new(),
constraints: Vec::new(),
lookups: Vec::new(),
selector: None,
Expand All @@ -65,6 +66,13 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
MIPSColumn::ScratchState(scratch_idx)
}

fn alloc_lookup(&mut self, table_id: LookupTableID) -> Self::Position {
let idx = self.scratch_lookups.get_mut(&table_id).unwrap();
*idx += 1;
MIPSColumn::Lookup(table_id, *idx - 1)
// TODO
}

fn alloc_scratch_inverse(&mut self) -> Self::Position {
let scratch_idx = self.scratch_state_idx_inverse;
self.scratch_state_idx_inverse += 1;
Expand All @@ -75,7 +83,7 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {

fn variable(&self, column: Self::Position) -> Self::Variable {
Expr::Atom(ExprInner::Cell(Variable {
col: column.to_column(),
col: column,
row: CurrOrNext::Curr,
}))
}
Expand Down
3 changes: 3 additions & 0 deletions o1vm/src/interpreters/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
lookups::{Lookup, LookupTableIDs},
};
use ark_ff::{One, Zero};
use kimchi::circuits::lookup::lookups::LookupTableID;
use strum::{EnumCount, IntoEnumIterator};
use strum_macros::{EnumCount, EnumIter};

Expand Down Expand Up @@ -172,6 +173,8 @@ pub trait InterpreterEnv {
/// [crate::interpreters::mips::column::SCRATCH_SIZE]
fn alloc_scratch(&mut self) -> Self::Position;

fn alloc_lookup(&mut self, table_id: LookupTableID) -> Self::Position;

fn alloc_scratch_inverse(&mut self) -> Self::Position;

type Variable: Clone
Expand Down
5 changes: 4 additions & 1 deletion o1vm/src/interpreters/mips/tests_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
preimage_oracle::PreImageOracleT,
};
use rand::{CryptoRng, Rng, RngCore};
use std::{fs, path::PathBuf};
use std::{collections::HashMap, fs, path::PathBuf};

// FIXME: we should parametrize the tests with different fields.
use ark_bn254::Fr as Fp;
Expand Down Expand Up @@ -92,6 +92,9 @@ where
scratch_state_idx_inverse: 0,
scratch_state: [Fp::from(0); SCRATCH_SIZE],
scratch_state_inverse: [Fp::from(0); SCRATCH_SIZE_INVERSE],
scratch_lookup: HashMap::new(),
lookup_fixed_tables: HashMap::new(),
lookup_multiplicities: HashMap::new(),
selector: crate::interpreters::mips::column::N_MIPS_SEL_COLS,
halt: false,
// Keccak related
Expand Down
Loading
Loading