Skip to content

Commit

Permalink
Add LogUp stark memory example (#946)
Browse files Browse the repository at this point in the history
* create file

* continuity and single value constraint

* imp air

* permutation constraint

* evaluate function for SingleValueConstraint

* add last element constraint

* add public inputs

* add sort function for the trace

* add integration test

* fix clippy

* fix constraints

* add documentation

* handle possible panic

* rename variables

* fix doc

* FRI verification fail

* different way of doing logup rap

* add m0 public input boundary constraint

* change end expemtions to 1

* change cp degree bound

* fix clippy

* fix comment

* fix read_mem and tests for small fields. And add byteConversion for degree4BabyBear

* Add AsBytes for FE<Degree4BabyBear>

* fix cargo check with no-std

* fix cargo fmt

* fix cargo fmt

* fix cargo fmt

* fix clippy no-std

* disable log up read only memory test for metal

* fix clippy metal

* revert commit

* fix cargo test metal

* Change extend for extend_to_slice. Add blog post reference. Fix crate import.

* revert extend_from_slice to extend

---------

Co-authored-by: Nicole <nicole@Nicoles-MacBook-Air.local>
Co-authored-by: Joaquin Carletti <joaquin.carletti@lambdaclass.com>
Co-authored-by: jotabulacios <jbulacios@fi.uba.ar>
Co-authored-by: jotabulacios <45471455+jotabulacios@users.noreply.github.com>
Co-authored-by: Diego K <43053772+diegokingston@users.noreply.github.com>
  • Loading branch information
6 people authored Jan 27, 2025
1 parent 6fb54a2 commit 3b16015
Show file tree
Hide file tree
Showing 4 changed files with 824 additions and 0 deletions.
56 changes: 56 additions & 0 deletions math/src/field/fields/fft_friendly/quartic_babybear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use crate::field::{
#[cfg(feature = "lambdaworks-serde-binary")]
use crate::traits::ByteConversion;

#[cfg(all(feature = "lambdaworks-serde-binary", feature = "alloc"))]
use crate::traits::AsBytes;

/// We are implementig the extension of Baby Bear of degree 4 using the irreducible polynomial x^4 + 11.
/// BETA = 11 and -BETA = -11 is the non-residue.
pub const BETA: FieldElement<Babybear31PrimeField> =
Expand Down Expand Up @@ -262,6 +265,59 @@ impl ByteConversion for [FieldElement<Babybear31PrimeField>; 4] {
}
}

#[cfg(feature = "lambdaworks-serde-binary")]
impl ByteConversion for FieldElement<Degree4BabyBearExtensionField> {
fn to_bytes_be(&self) -> alloc::vec::Vec<u8> {
let mut byte_slice = ByteConversion::to_bytes_be(&self.value()[0]);
byte_slice.extend(ByteConversion::to_bytes_be(&self.value()[1]));
byte_slice.extend(ByteConversion::to_bytes_be(&self.value()[2]));
byte_slice.extend(ByteConversion::to_bytes_be(&self.value()[3]));
byte_slice
}

fn to_bytes_le(&self) -> alloc::vec::Vec<u8> {
let mut byte_slice = ByteConversion::to_bytes_le(&self.value()[0]);
byte_slice.extend(ByteConversion::to_bytes_le(&self.value()[1]));
byte_slice.extend(ByteConversion::to_bytes_le(&self.value()[2]));
byte_slice.extend(ByteConversion::to_bytes_le(&self.value()[3]));
byte_slice
}

fn from_bytes_be(bytes: &[u8]) -> Result<Self, crate::errors::ByteConversionError>
where
Self: Sized,
{
const BYTES_PER_FIELD: usize = 8;
let x0 = FieldElement::from_bytes_be(&bytes[0..BYTES_PER_FIELD])?;
let x1 = FieldElement::from_bytes_be(&bytes[BYTES_PER_FIELD..BYTES_PER_FIELD * 2])?;
let x2 = FieldElement::from_bytes_be(&bytes[BYTES_PER_FIELD * 2..BYTES_PER_FIELD * 3])?;
let x3 = FieldElement::from_bytes_be(&bytes[BYTES_PER_FIELD * 3..BYTES_PER_FIELD * 4])?;

Ok(Self::new([x0, x1, x2, x3]))
}

fn from_bytes_le(bytes: &[u8]) -> Result<Self, crate::errors::ByteConversionError>
where
Self: Sized,
{
const BYTES_PER_FIELD: usize = 8;
let x0 = FieldElement::from_bytes_le(&bytes[0..BYTES_PER_FIELD])?;
let x1 = FieldElement::from_bytes_le(&bytes[BYTES_PER_FIELD..BYTES_PER_FIELD * 2])?;
let x2 = FieldElement::from_bytes_le(&bytes[BYTES_PER_FIELD * 2..BYTES_PER_FIELD * 3])?;
let x3 = FieldElement::from_bytes_le(&bytes[BYTES_PER_FIELD * 3..BYTES_PER_FIELD * 4])?;

Ok(Self::new([x0, x1, x2, x3]))
}
}

#[cfg(feature = "lambdaworks-serde-binary")]
#[cfg(feature = "alloc")]
impl AsBytes for FieldElement<Degree4BabyBearExtensionField> {
fn as_bytes(&self) -> alloc::vec::Vec<u8> {
self.to_bytes_be()
}
}

impl IsFFTField for Degree4BabyBearExtensionField {
const TWO_ADICITY: u64 = 29;
const TWO_ADIC_PRIMITVE_ROOT_OF_UNITY: Self::BaseType = [
Expand Down
1 change: 1 addition & 0 deletions provers/stark/src/examples/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ pub mod fibonacci_2_columns;
pub mod fibonacci_rap;
pub mod quadratic_air;
pub mod read_only_memory;
pub mod read_only_memory_logup;
pub mod simple_fibonacci;
pub mod simple_periodic_cols;
Loading

0 comments on commit 3b16015

Please sign in to comment.