Skip to content

Commit

Permalink
Poseidon with logup
Browse files Browse the repository at this point in the history
  • Loading branch information
spapinistarkware committed Jul 8, 2024
1 parent 56e7a66 commit cac4ec9
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 77 deletions.
88 changes: 85 additions & 3 deletions crates/prover/src/builder/logup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use itertools::Itertools;
use num_traits::Zero;
use num_traits::{One, Zero};
use tracing::{span, Level};

use super::EvalAtRow;
use crate::core::backend::simd::column::SecureFieldVec;
use crate::core::backend::simd::m31::LOG_N_LANES;
use crate::core::backend::simd::qm31::PackedSecureField;
Expand All @@ -14,10 +15,91 @@ use crate::core::fields::secure_column::SecureColumn;
use crate::core::fields::FieldExpOps;
use crate::core::poly::circle::{CanonicCoset, CircleEvaluation};
use crate::core::poly::BitReversedOrder;
use crate::core::utils::bit_reverse_index;
use crate::core::utils::{bit_reverse_index, shifted_secure_combination};
use crate::core::ColumnVec;

#[derive(Copy, Clone, Debug)]
pub struct LogupAtRow<const BATCH_SIZE: usize, E: EvalAtRow> {
pub interaction: usize,
pub queue: [(E::EF, E::EF); BATCH_SIZE],
pub queue_size: usize,
pub claimed_sum: SecureField,
pub prev_mask: E::EF,
pub is_first: E::F,
}
impl<const BATCH_SIZE: usize, E: EvalAtRow> LogupAtRow<BATCH_SIZE, E> {
pub fn new(interaction: usize, claimed_sum: SecureField, is_first: E::F) -> Self {
Self {
interaction,
queue: [(E::EF::zero(), E::EF::zero()); BATCH_SIZE],
queue_size: 0,
claimed_sum,
prev_mask: E::EF::zero(),
is_first,
}
}
pub fn push_lookup(
&mut self,
eval: &mut E,
numerator: E::EF,
values: &[E::F],
lookup_elements: LookupElements,
) {
let shifted_value = shifted_secure_combination(
values,
E::EF::zero() + lookup_elements.alpha,
E::EF::zero() + lookup_elements.z,
);
self.push_frac(eval, numerator, shifted_value);
}

pub fn push_frac(&mut self, eval: &mut E, p: E::EF, q: E::EF) {
if self.queue_size < BATCH_SIZE {
self.queue[self.queue_size] = (p, q);
self.queue_size += 1;
return;
}

// Compute sum_i pi/qi over batch, as a fraction, p/q.
let (num, denom) = self
.queue
.iter()
.copied()
.fold((E::EF::zero(), E::EF::one()), |(p0, q0), (pi, qi)| {
(p0 * qi + pi * q0, qi * q0)
});

self.queue[0] = (p, q);
self.queue_size = 1;

// Add a constraint that p / q = diff.
let cur = E::combine_ef(std::array::from_fn(|_| {
eval.next_interaction_mask(1, [0])[0]
}));
let diff = cur - self.prev_mask;
self.prev_mask = cur;
eval.add_constraint(diff * denom - num);
}

pub fn finalize(self, eval: &mut E) {
let (p, q) = self.queue[0..self.queue_size]
.iter()
.copied()
.fold((E::EF::zero(), E::EF::one()), |(p0, q0), (pi, qi)| {
(p0 * qi + pi * q0, qi * q0)
});

let cumulative_mask_values =
std::array::from_fn(|_| eval.next_interaction_mask(self.interaction, [0, -1]));
let cur = E::combine_ef(cumulative_mask_values.map(|[cur, _prev]| cur));
let up = E::combine_ef(cumulative_mask_values.map(|[_cur, prev]| prev));
let up = up - self.is_first * self.claimed_sum;
let diff = cur - up - self.prev_mask;

eval.add_constraint(diff * q - p);
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct LookupElements {
pub z: SecureField,
pub alpha: SecureField,
Expand Down
3 changes: 2 additions & 1 deletion crates/prover/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod logup;
mod point;

use std::fmt::Debug;
use std::ops::{Add, AddAssign, Mul, Sub};
use std::ops::{Add, AddAssign, Mul, Neg, Sub};

pub use assert::AssertEvaluator;
pub use domain::DomainEvaluator;
Expand Down Expand Up @@ -34,6 +34,7 @@ pub trait EvalAtRow {
+ Copy
+ Debug
+ Zero
+ Neg<Output = Self::EF>
+ Add<SecureField, Output = Self::EF>
+ Sub<SecureField, Output = Self::EF>
+ Mul<SecureField, Output = Self::EF>
Expand Down
Loading

0 comments on commit cac4ec9

Please sign in to comment.