Skip to content

Commit

Permalink
Merge pull request #1244 from o1-labs/zkvm/keccak/gate
Browse files Browse the repository at this point in the history
`Keccak` gates for zkVM
  • Loading branch information
dannywillems authored Nov 14, 2023
2 parents 9df9bbb + 6f819ba commit 9355d19
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 63 deletions.
27 changes: 27 additions & 0 deletions kimchi/src/circuits/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,38 @@ impl<F: Field, T: ExprOps<F>> ArgumentEnv<F, T> {
T::witness(Next, col, self.data.as_ref())
}

/// Witness cells in current row in an interval [from, to)
pub fn witness_curr_chunk(&self, from: usize, to: usize) -> Vec<T> {
let mut chunk = Vec::with_capacity(to - from);
for i in from..to {
chunk.push(self.witness_curr(i));
}
chunk
}

/// Witness cells in next row in an interval [from, to)
pub fn witness_next_chunk(&self, from: usize, to: usize) -> Vec<T> {
let mut chunk = Vec::with_capacity(to - from);
for i in from..to {
chunk.push(self.witness_next(i));
}
chunk
}

/// Coefficient value at index idx
pub fn coeff(&self, idx: usize) -> T {
T::coeff(idx, self.data.as_ref())
}

/// Chunk of consecutive coefficients in an interval [from, to)
pub fn coeff_chunk(&self, from: usize, to: usize) -> Vec<T> {
let mut chunk = Vec::with_capacity(to - from);
for i in from..to {
chunk.push(self.coeff(i));
}
chunk
}

/// Constant value (see [ConstantExpr] for supported constants)
pub fn constant(&self, expr: ConstantExpr<F>) -> T {
T::constant(expr, self.data.as_ref())
Expand Down
12 changes: 11 additions & 1 deletion kimchi/src/circuits/gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use thiserror::Error;
use super::{
argument::ArgumentWitness,
expr,
polynomials::{rot, xor},
polynomials::{keccak, rot, xor},
};

/// A row accessible from a given row, corresponds to the fact that we open all polynomials
Expand Down Expand Up @@ -113,6 +113,8 @@ pub enum GateType {
// Gates for Keccak
Xor16,
Rot64,
KeccakRound,
KeccakSponge,
}

/// Gate error
Expand Down Expand Up @@ -230,6 +232,12 @@ impl<F: PrimeField + SquareRootField> CircuitGate<F> {
Rot64 => self
.verify_witness::<G>(row, witness, &index.cs, public)
.map_err(|e| e.to_string()),
KeccakRound => self
.verify_witness::<G>(row, witness, &index.cs, public)
.map_err(|e| e.to_string()),
KeccakSponge => self
.verify_witness::<G>(row, witness, &index.cs, public)
.map_err(|e| e.to_string()),
}
}

Expand Down Expand Up @@ -323,6 +331,8 @@ impl<F: PrimeField + SquareRootField> CircuitGate<F> {
}
GateType::Xor16 => xor::Xor16::constraint_checks(&env, &mut cache),
GateType::Rot64 => rot::Rot64::constraint_checks(&env, &mut cache),
GateType::KeccakRound => keccak::KeccakRound::constraint_checks(&env, &mut cache),
GateType::KeccakSponge => keccak::KeccakSponge::constraint_checks(&env, &mut cache),
};

// Check for failed constraints
Expand Down
Loading

0 comments on commit 9355d19

Please sign in to comment.