Skip to content

Commit

Permalink
Merge pull request #20 from succinctlabs/tamir/update
Browse files Browse the repository at this point in the history
chore: sync fork to Plonky3
  • Loading branch information
tamirhemo authored Feb 21, 2024
2 parents cfbf958 + 1f5841c commit b6f8bb0
Show file tree
Hide file tree
Showing 31 changed files with 517 additions and 174 deletions.
Binary file added .DS_Store
Binary file not shown.
57 changes: 57 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
- name: Test
run: cargo test --verbose

- name: Test with parallel
run: cargo test --verbose --features parallel

lint:
name: Formatting and Clippy
runs-on: ubuntu-latest
Expand Down Expand Up @@ -74,3 +77,57 @@ jobs:
- name: Format
run: cargo +nightly fmt --all -- --check

check_crates:
name: Check Crates
runs-on: ubuntu-latest
if: "! contains(toJSON(github.event.commits.*.message), '[skip-ci]')"

steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable
id: rs-stable

- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: rust-${{ steps.rs-stable.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.toml', '**/Cargo.lock') }}

- name: Cargo Check Crates
run: |
cargo check --verbose --package p3-air
cargo check --verbose --package p3-baby-bear
cargo check --verbose --package p3-blake3
cargo check --verbose --package p3-brakedown
cargo check --verbose --package p3-challenger
cargo check --verbose --package p3-code
cargo check --verbose --package p3-commit
cargo check --verbose --package p3-dft
cargo check --verbose --package p3-field
cargo check --verbose --package p3-field-testing
cargo check --verbose --package p3-fri
cargo check --verbose --package p3-goldilocks
cargo check --verbose --package p3-interpolation
cargo check --verbose --package p3-keccak
cargo check --verbose --package p3-keccak-air
cargo check --verbose --package p3-lde
cargo check --verbose --package p3-matrix
cargo check --verbose --package p3-maybe-rayon
cargo check --verbose --package p3-mds
cargo check --verbose --package p3-merkle-tree
cargo check --verbose --package p3-mersenne-31
cargo check --verbose --package p3-monolith
cargo check --verbose --package p3-multi-stark
cargo check --verbose --package p3-poseidon
cargo check --verbose --package p3-poseidon2
cargo check --verbose --package p3-reed-solomon
cargo check --verbose --package p3-rescue
cargo check --verbose --package p3-symmetric
cargo check --verbose --package p3-tensor-pcs
cargo check --verbose --package p3-uni-stark
cargo check --verbose --package p3-util
10 changes: 6 additions & 4 deletions air/src/virtual_column.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use alloc::vec;
use alloc::vec::Vec;
use core::ops::{Add, Mul};
use core::ops::Mul;

use p3_field::{AbstractField, Field};

/// An affine function over columns in a PAIR.
#[derive(Clone, Debug)]
pub struct VirtualPairCol<F: Field> {
column_weights: Vec<(PairCol, F)>,
constant: F,
}

/// A column in a PAIR, i.e. either a preprocessed column or a main trace column.
#[derive(Debug, Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub enum PairCol {
Preprocessed(usize),
Main(usize),
Expand Down Expand Up @@ -111,10 +112,11 @@ impl<F: Field> VirtualPairCol<F> {

pub fn apply<Expr, Var>(&self, preprocessed: &[Var], main: &[Var]) -> Expr
where
Expr: AbstractField + Mul<F, Output = Expr> + Add<F, Output = Expr>,
F: Into<Expr>,
Expr: AbstractField + Mul<F, Output = Expr>,
Var: Into<Expr> + Copy,
{
let mut result = Expr::zero() + self.constant;
let mut result = self.constant.into();
for (column, weight) in &self.column_weights {
result += column.get(preprocessed, main).into() * *weight;
}
Expand Down
1 change: 1 addition & 0 deletions baby-bear/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ serde = { version = "1.0", default-features = false, features = ["derive"] }
p3-field-testing = { path = "../field-testing" }
criterion = "0.5.1"
rand_chacha = "0.3.1"
serde_json = "1.0.113"

[[bench]]
name = "inverse"
Expand Down
48 changes: 46 additions & 2 deletions baby-bear/src/baby_bear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use p3_field::{
};
use rand::distributions::{Distribution, Standard};
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};

/// The Baby Bear prime
const P: u32 = 0x78000001;
Expand All @@ -34,7 +34,7 @@ const MONTY_MU: u32 = if cfg!(all(target_arch = "aarch64", target_feature = "neo
const MONTY_MASK: u32 = ((1u64 << MONTY_BITS) - 1) as u32;

/// The prime field `2^31 - 2^27 + 1`, a.k.a. the Baby Bear field.
#[derive(Copy, Clone, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[derive(Copy, Clone, Default, Eq, Hash, PartialEq)]
#[repr(transparent)] // `PackedBabyBearNeon` relies on this!
pub struct BabyBear {
// This is `pub(crate)` just for tests. If you're accessing `value` outside of those, you're
Expand Down Expand Up @@ -89,6 +89,19 @@ impl Distribution<BabyBear> for Standard {
}
}

impl Serialize for BabyBear {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u32(self.as_canonical_u32())
}
}

impl<'de> Deserialize<'de> for BabyBear {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let val = u32::deserialize(d)?;
Ok(BabyBear::from_canonical_u32(val))
}
}

const MONTY_ZERO: u32 = to_monty(0);
const MONTY_ONE: u32 = to_monty(1);
const MONTY_TWO: u32 = to_monty(2);
Expand Down Expand Up @@ -493,6 +506,37 @@ mod tests {
assert_eq!(m1.exp_u64(1725656503).exp_const_u64::<7>(), m1);
assert_eq!(m2.exp_u64(1725656503).exp_const_u64::<7>(), m2);
assert_eq!(f_2.exp_u64(1725656503).exp_const_u64::<7>(), f_2);

let f_serialized = serde_json::to_string(&f).unwrap();
let f_deserialized: F = serde_json::from_str(&f_serialized).unwrap();
assert_eq!(f, f_deserialized);

let f_1_serialized = serde_json::to_string(&f_1).unwrap();
let f_1_deserialized: F = serde_json::from_str(&f_1_serialized).unwrap();
let f_1_serialized_again = serde_json::to_string(&f_1_deserialized).unwrap();
let f_1_deserialized_again: F = serde_json::from_str(&f_1_serialized_again).unwrap();
assert_eq!(f_1, f_1_deserialized);
assert_eq!(f_1, f_1_deserialized_again);

let f_2_serialized = serde_json::to_string(&f_2).unwrap();
let f_2_deserialized: F = serde_json::from_str(&f_2_serialized).unwrap();
assert_eq!(f_2, f_2_deserialized);

let f_p_minus_1_serialized = serde_json::to_string(&f_p_minus_1).unwrap();
let f_p_minus_1_deserialized: F = serde_json::from_str(&f_p_minus_1_serialized).unwrap();
assert_eq!(f_p_minus_1, f_p_minus_1_deserialized);

let f_p_minus_2_serialized = serde_json::to_string(&f_p_minus_2).unwrap();
let f_p_minus_2_deserialized: F = serde_json::from_str(&f_p_minus_2_serialized).unwrap();
assert_eq!(f_p_minus_2, f_p_minus_2_deserialized);

let m1_serialized = serde_json::to_string(&m1).unwrap();
let m1_deserialized: F = serde_json::from_str(&m1_serialized).unwrap();
assert_eq!(m1, m1_deserialized);

let m2_serialized = serde_json::to_string(&m2).unwrap();
let m2_deserialized: F = serde_json::from_str(&m2_serialized).unwrap();
assert_eq!(m2, m2_deserialized);
}

test_field!(crate::BabyBear);
Expand Down
9 changes: 9 additions & 0 deletions baby-bear/src/x86_64_avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use core::mem::transmute;
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};

use p3_field::{AbstractField, Field, PackedField};
use rand::distributions::{Distribution, Standard};
use rand::Rng;

use crate::BabyBear;

Expand Down Expand Up @@ -500,6 +502,13 @@ impl Sub<PackedBabyBearAVX2> for BabyBear {
}
}

impl Distribution<PackedBabyBearAVX2> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> PackedBabyBearAVX2 {
PackedBabyBearAVX2(rng.gen())
}
}

#[inline]
#[must_use]
fn interleave1(a: __m256i, b: __m256i) -> (__m256i, __m256i) {
Expand Down
2 changes: 1 addition & 1 deletion commit/src/pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ where
fn commit_shifted_batches(
&self,
polynomials: Vec<In>,
coset_shifts: &[Val],
coset_shift: &[Val],
) -> (Self::Commitment, Self::ProverData);

fn commit_shifted_batch(
Expand Down
1 change: 1 addition & 0 deletions field/src/extension/binomial_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl<AF: AbstractField, const D: usize> From<AF> for BinomialExtensionField<AF,
impl<F: BinomiallyExtendable<D>, const D: usize> ExtensionField<F>
for BinomialExtensionField<F, D>
{
type ExtensionPacking = BinomialExtensionField<F::Packing, D>;
}

impl<F: BinomiallyExtendable<D>, const D: usize> HasFrobenius<F> for BinomialExtensionField<F, D> {
Expand Down
10 changes: 9 additions & 1 deletion field/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ pub trait AbstractExtensionField<Base: AbstractField>:
}

pub trait ExtensionField<Base: Field>: Field + AbstractExtensionField<Base> {
type ExtensionPacking: AbstractExtensionField<Base::Packing, F = Self>
+ 'static
+ Copy
+ Send
+ Sync;

fn is_in_basefield(&self) -> bool {
self.as_base_slice()[1..].iter().all(Field::is_zero)
}
Expand All @@ -306,7 +312,9 @@ pub trait ExtensionField<Base: Field>: Field + AbstractExtensionField<Base> {
}
}

impl<F: Field> ExtensionField<F> for F {}
impl<F: Field> ExtensionField<F> for F {
type ExtensionPacking = F::Packing;
}

impl<AF: AbstractField> AbstractExtensionField<AF> for AF {
const D: usize = 1;
Expand Down
4 changes: 2 additions & 2 deletions fri/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ where
x = x.square();
}

debug_assert!(index == 0 || index == 1);
debug_assert!(x.is_one() || x == F::two_adic_generator(1));
debug_assert!(index < config.blowup(), "index was {}", index);
debug_assert_eq!(x.exp_power_of_2(config.log_blowup), F::one());

Ok(folded_eval)
}
3 changes: 3 additions & 0 deletions keccak-air/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ name = "prove_baby_bear_poseidon2"
name = "prove_goldilocks_keccak"

[features]
# TODO: Consider removing, at least when this gets split off into another repository.
# We should be able to enable p3-maybe-rayon/parallel directly; this just doesn't
# seem to work when using cargo with the -p or --package option.
parallel = ["p3-maybe-rayon/parallel"]
5 changes: 1 addition & 4 deletions keccak-air/examples/prove_baby_bear_keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use p3_challenger::DuplexChallenger;
use p3_commit::ExtensionMmcs;
use p3_dft::Radix2DitParallel;
use p3_field::extension::BinomialExtensionField;
use p3_field::Field;
use p3_fri::{FriConfig, TwoAdicFriPcs, TwoAdicFriPcsConfig};
use p3_keccak::Keccak256Hash;
use p3_keccak_air::{generate_trace_rows, KeccakAir};
Expand Down Expand Up @@ -31,9 +30,7 @@ fn main() -> Result<(), VerificationError> {
.init();

type Val = BabyBear;
type Domain = Val;
type Challenge = BinomialExtensionField<Val, 4>;
type PackedChallenge = BinomialExtensionField<<Domain as Field>::Packing, 4>;

type Perm = Poseidon2<Val, DiffusionMatrixBabybear, 16, 7>;
let perm = Perm::new_from_rng(8, 22, DiffusionMatrixBabybear, &mut thread_rng());
Expand Down Expand Up @@ -65,7 +62,7 @@ fn main() -> Result<(), VerificationError> {
TwoAdicFriPcs<TwoAdicFriPcsConfig<Val, Challenge, Challenger, Dft, ValMmcs, ChallengeMmcs>>;
let pcs = Pcs::new(fri_config, dft, val_mmcs);

type MyConfig = StarkConfig<Val, Challenge, PackedChallenge, Pcs, Challenger>;
type MyConfig = StarkConfig<Val, Challenge, Pcs, Challenger>;
let config = StarkConfig::new(pcs);

let mut challenger = Challenger::new(perm.clone());
Expand Down
4 changes: 1 addition & 3 deletions keccak-air/examples/prove_baby_bear_poseidon2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ fn main() -> Result<(), VerificationError> {
.init();

type Val = BabyBear;
type Domain = Val;
type Challenge = BinomialExtensionField<Val, 4>;
type PackedChallenge = BinomialExtensionField<<Domain as Field>::Packing, 4>;

type Perm = Poseidon2<Val, DiffusionMatrixBabybear, 16, 7>;
let perm = Perm::new_from_rng(8, 22, DiffusionMatrixBabybear, &mut thread_rng());
Expand Down Expand Up @@ -64,7 +62,7 @@ fn main() -> Result<(), VerificationError> {
TwoAdicFriPcs<TwoAdicFriPcsConfig<Val, Challenge, Challenger, Dft, ValMmcs, ChallengeMmcs>>;
let pcs = Pcs::new(fri_config, dft, val_mmcs);

type MyConfig = StarkConfig<Val, Challenge, PackedChallenge, Pcs, Challenger>;
type MyConfig = StarkConfig<Val, Challenge, Pcs, Challenger>;
let config = StarkConfig::new(pcs);

let mut challenger = Challenger::new(perm.clone());
Expand Down
5 changes: 1 addition & 4 deletions keccak-air/examples/prove_goldilocks_keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use p3_challenger::DuplexChallenger;
use p3_commit::ExtensionMmcs;
use p3_dft::Radix2DitParallel;
use p3_field::extension::BinomialExtensionField;
use p3_field::Field;
use p3_fri::{FriConfig, TwoAdicFriPcs, TwoAdicFriPcsConfig};
use p3_goldilocks::Goldilocks;
use p3_keccak::Keccak256Hash;
Expand Down Expand Up @@ -31,9 +30,7 @@ fn main() -> Result<(), VerificationError> {
.init();

type Val = Goldilocks;
type Domain = Val;
type Challenge = BinomialExtensionField<Val, 2>;
type PackedChallenge = BinomialExtensionField<<Domain as Field>::Packing, 2>;

type Perm = Poseidon2<Val, DiffusionMatrixGoldilocks, 8, 7>;
let perm = Perm::new_from_rng(8, 22, DiffusionMatrixGoldilocks, &mut thread_rng());
Expand Down Expand Up @@ -64,7 +61,7 @@ fn main() -> Result<(), VerificationError> {
TwoAdicFriPcs<TwoAdicFriPcsConfig<Val, Challenge, Challenger, Dft, ValMmcs, ChallengeMmcs>>;
let pcs = Pcs::new(fri_config, dft, val_mmcs);

type MyConfig = StarkConfig<Val, Challenge, PackedChallenge, Pcs, Challenger>;
type MyConfig = StarkConfig<Val, Challenge, Pcs, Challenger>;
let config = StarkConfig::new(pcs);

let mut challenger = Challenger::new(perm.clone());
Expand Down
2 changes: 1 addition & 1 deletion keccak-air/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::BITS_PER_LIMB;

pub(crate) const R: [[u8; 5]; 5] = [
pub const R: [[u8; 5]; 5] = [
[0, 36, 3, 41, 18],
[1, 44, 10, 45, 2],
[62, 6, 43, 15, 61],
Expand Down
2 changes: 1 addition & 1 deletion keccak-air/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod round_flags;

pub use air::*;
pub use columns::*;
pub use constants::RC;
pub use constants::*;
pub use generation::*;

pub const NUM_ROUNDS: usize = 24;
Expand Down
2 changes: 1 addition & 1 deletion merkle-tree/src/mmcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ mod tests {

type F = BabyBear;

type Perm = Poseidon2<F, DiffusionMatrixBabybear, 16, 5>;
type Perm = Poseidon2<F, DiffusionMatrixBabybear, 16, 7>;
type MyHash = PaddingFreeSponge<Perm, 16, 8, 8>;
type MyCompress = TruncatedPermutation<Perm, 2, 8, 16>;
type MyMmcs = FieldMerkleTreeMmcs<<F as Field>::Packing, MyHash, MyCompress, 8>;
Expand Down
9 changes: 9 additions & 0 deletions mersenne-31/src/aarch64_neon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use core::mem::transmute;
use core::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};

use p3_field::{AbstractField, Field, PackedField};
use rand::distributions::{Distribution, Standard};
use rand::Rng;

use crate::Mersenne31;

Expand Down Expand Up @@ -470,6 +472,13 @@ impl Sub<PackedMersenne31Neon> for Mersenne31 {
}
}

impl Distribution<PackedMersenne31Neon> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> PackedMersenne31Neon {
PackedMersenne31Neon(rng.gen())
}
}

#[inline]
#[must_use]
fn interleave1(v0: uint32x4_t, v1: uint32x4_t) -> (uint32x4_t, uint32x4_t) {
Expand Down
Loading

0 comments on commit b6f8bb0

Please sign in to comment.