Skip to content

Commit

Permalink
Sync upstream (#109)
Browse files Browse the repository at this point in the history
Co-authored-by: Linda Guiga <[email protected]>
Co-authored-by: Hamy Ratoanina <[email protected]>
Co-authored-by: Daniel-Aaron-Bloom <[email protected]>
Co-authored-by: nuno <[email protected]>
Co-authored-by: Matthias Görgens <[email protected]>
Co-authored-by: Robin Salen <[email protected]>
Co-authored-by: Gio <[email protected]>
Co-authored-by: Robin Salen <[email protected]>
  • Loading branch information
9 people authored Jul 17, 2024
1 parent 75e686f commit 54b17c1
Show file tree
Hide file tree
Showing 32 changed files with 314 additions and 206 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/pr_checking.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: PR check

on:
pull_request:
types: [opened, reopened, synchronize]

permissions:
pull-requests: write

jobs:
pr_check:
name: Validate PR
runs-on: ubuntu-latest
steps:
- name: Set up keywords
id: setup_keywords
run: echo "RESTRICTED_KEYWORDS=$(echo '${{ secrets.RESTRICTED_KEYWORDS }}' | jq -r '.[]' | tr '\n' ' ')" >> $GITHUB_ENV

- name: Check for spam PR
id: check
run: |
# Initialize variables to track spam presence
title_is_spam=false
description_is_spam=false
# Check title for spam
for keyword in $RESTRICTED_KEYWORDS; do
if echo "${{ github.event.pull_request.title }}" | grep -i -q "$keyword"; then
title_is_spam=true
break
fi
done
# Check description for spam
for keyword in $RESTRICTED_KEYWORDS; do
if echo "${{ github.event.pull_request.body }}" | grep -i -q "$keyword"; then
description_is_spam=true
break
fi
done
# Set the output based on the presence of spam
if [ "$title_is_spam" = true ] || [ "$description_is_spam" = true ]; then
echo "is_spam=true" >> $GITHUB_ENV
else
echo "is_spam=false" >> $GITHUB_ENV
fi
- name: Checkout repository
uses: actions/checkout@v4

- name: Close PR if spam are found and author is not a contributor or member
if: ${{ env.is_spam == 'true' && github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR' }}
run: gh pr close ${{ github.event.pull_request.number }} --comment "Spam detected"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion field/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#![feature(specialization)]
#![cfg_attr(target_arch = "x86_64", feature(stdarch_x86_avx512))]
#![cfg_attr(not(test), no_std)]
#![cfg(not(test))]

extern crate alloc;

pub(crate) mod arch;
Expand Down
48 changes: 47 additions & 1 deletion field/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,13 @@ pub trait Field:
}

fn powers(&self) -> Powers<Self> {
self.shifted_powers(Self::ONE)
}

fn shifted_powers(&self, start: Self) -> Powers<Self> {
Powers {
base: *self,
current: Self::ONE,
current: start,
}
}

Expand Down Expand Up @@ -571,6 +575,7 @@ pub trait PrimeField64: PrimeField + Field64 {
}

/// An iterator over the powers of a certain base element `b`: `b^0, b^1, b^2, ...`.
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone, Debug)]
pub struct Powers<F: Field> {
base: F,
Expand All @@ -585,6 +590,24 @@ impl<F: Field> Iterator for Powers<F> {
self.current *= self.base;
Some(result)
}

fn size_hint(&self) -> (usize, Option<usize>) {
(usize::MAX, None)
}

fn nth(&mut self, n: usize) -> Option<F> {
let result = self.current * self.base.exp_u64(n.try_into().unwrap());
self.current = result * self.base;
Some(result)
}

fn last(self) -> Option<F> {
panic!("called `Iterator::last()` on an infinite sequence")
}

fn count(self) -> usize {
panic!("called `Iterator::count()` on an infinite sequence")
}
}

impl<F: Field> Powers<F> {
Expand All @@ -600,3 +623,26 @@ impl<F: Field> Powers<F> {
}
}
}

#[cfg(test)]
mod tests {
use super::Field;
use crate::goldilocks_field::GoldilocksField;

#[test]
fn test_powers_nth() {
type F = GoldilocksField;

const N: usize = 10;
let powers_of_two: Vec<F> = F::TWO.powers().take(N).collect();

for (n, &expect) in powers_of_two.iter().enumerate() {
let mut iter = F::TWO.powers();
assert_eq!(iter.nth(n), Some(expect));

for &expect_next in &powers_of_two[n + 1..] {
assert_eq!(iter.next(), Some(expect_next));
}
}
}
}
2 changes: 1 addition & 1 deletion plonky2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ name = "field_arithmetic"
harness = false

[[bench]]
name = "field_merkle_tree"
name = "batch_merkle_tree"
harness = false

[[bench]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod allocator;

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::hash::field_merkle_tree::FieldMerkleTree;
use plonky2::hash::batch_merkle_tree::BatchMerkleTree;
use plonky2::hash::hash_types::RichField;
use plonky2::hash::keccak::KeccakHash;
use plonky2::hash::poseidon::PoseidonHash;
Expand All @@ -13,9 +13,9 @@ const ELEMS_PER_LEAF_1: usize = 70;
const ELEMS_PER_LEAF_2: usize = 5;
const ELEMS_PER_LEAF_3: usize = 100;

pub(crate) fn bench_field_merkle_tree<F: RichField, H: Hasher<F>>(c: &mut Criterion) {
let mut group = c.benchmark_group(&format!(
"field-merkle-tree<{}, {}>",
pub(crate) fn bench_batch_merkle_tree<F: RichField, H: Hasher<F>>(c: &mut Criterion) {
let mut group = c.benchmark_group(format!(
"batch-merkle-tree<{}, {}>",
type_name::<F>(),
type_name::<H>()
));
Expand All @@ -29,14 +29,14 @@ pub(crate) fn bench_field_merkle_tree<F: RichField, H: Hasher<F>>(c: &mut Criter
vec![F::rand_vec(ELEMS_PER_LEAF_2); size >> 1],
vec![F::rand_vec(ELEMS_PER_LEAF_3); size >> 2],
];
b.iter(|| FieldMerkleTree::<F, H>::new(black_box(leaves.clone()), black_box(5)));
b.iter(|| BatchMerkleTree::<F, H>::new(black_box(leaves.clone()), black_box(5)));
});
}
}

fn criterion_benchmark(c: &mut Criterion) {
bench_field_merkle_tree::<GoldilocksField, PoseidonHash>(c);
bench_field_merkle_tree::<GoldilocksField, KeccakHash<25>>(c);
bench_batch_merkle_tree::<GoldilocksField, PoseidonHash>(c);
bench_batch_merkle_tree::<GoldilocksField, KeccakHash<25>>(c);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
4 changes: 2 additions & 2 deletions plonky2/benches/ffts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use plonky2::field::types::Field;
use tynm::type_name;

pub(crate) fn bench_ffts<F: Field>(c: &mut Criterion) {
let mut group = c.benchmark_group(&format!("fft<{}>", type_name::<F>()));
let mut group = c.benchmark_group(format!("fft<{}>", type_name::<F>()));

for size_log in [13, 14, 15, 16] {
let size = 1 << size_log;
Expand All @@ -21,7 +21,7 @@ pub(crate) fn bench_ffts<F: Field>(c: &mut Criterion) {
pub(crate) fn bench_ldes<F: Field>(c: &mut Criterion) {
const RATE_BITS: usize = 3;

let mut group = c.benchmark_group(&format!("lde<{}>", type_name::<F>()));
let mut group = c.benchmark_group(format!("lde<{}>", type_name::<F>()));

for size_log in [13, 14, 15, 16] {
let orig_size = 1 << (size_log - RATE_BITS);
Expand Down
2 changes: 1 addition & 1 deletion plonky2/benches/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tynm::type_name;
const ELEMS_PER_LEAF: usize = 135;

pub(crate) fn bench_merkle_tree<F: RichField, H: Hasher<F>>(c: &mut Criterion) {
let mut group = c.benchmark_group(&format!(
let mut group = c.benchmark_group(format!(
"merkle-tree<{}, {}>",
type_name::<F>(),
type_name::<H>()
Expand Down
4 changes: 4 additions & 0 deletions plonky2/src/batch_fri/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod oracle;
pub mod prover;
pub mod recursive_verifier;
pub mod verifier;
28 changes: 14 additions & 14 deletions plonky2/src/fri/batch_oracle.rs → plonky2/src/batch_fri/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use plonky2_field::types::Field;
use plonky2_maybe_rayon::*;
use plonky2_util::{log2_strict, reverse_index_bits_in_place};

use crate::fri::batch_prover::batch_fri_proof;
use crate::batch_fri::prover::batch_fri_proof;
use crate::fri::oracle::PolynomialBatch;
use crate::fri::proof::FriProof;
use crate::fri::structure::{FriBatchInfo, FriInstanceInfo};
use crate::fri::FriParams;
use crate::hash::field_merkle_tree::FieldMerkleTree;
use crate::hash::batch_merkle_tree::BatchMerkleTree;
use crate::hash::hash_types::RichField;
use crate::iop::challenger::Challenger;
use crate::plonk::config::GenericConfig;
Expand All @@ -25,12 +25,12 @@ use crate::util::timing::TimingTree;
use crate::util::{reverse_bits, transpose};

/// Represents a batch FRI oracle, i.e. a batch of polynomials with different degrees which have
/// been Merkle-ized in a Field Merkle Tree.
/// been Merkle-ized in a [`BatchMerkleTree`].
#[derive(Eq, PartialEq, Debug)]
pub struct BatchFriOracle<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
{
pub polynomials: Vec<PolynomialCoeffs<F>>,
pub field_merkle_tree: FieldMerkleTree<F, C::Hasher>,
pub batch_merkle_tree: BatchMerkleTree<F, C::Hasher>,
// The degree bits of each polynomial group.
pub degree_bits: Vec<usize>,
pub rate_bits: usize,
Expand Down Expand Up @@ -105,19 +105,19 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
}
}

let field_merkle_tree = timed!(
let batch_merkle_tree = timed!(
timing,
"build Field Merkle tree",
FieldMerkleTree::new(leaves, cap_height)
BatchMerkleTree::new(leaves, cap_height)
);

degree_bits.sort_unstable();
degree_bits.dedup();
degree_bits.reverse();
assert_eq!(field_merkle_tree.leaves.len(), degree_bits.len());
assert_eq!(batch_merkle_tree.leaves.len(), degree_bits.len());
Self {
polynomials,
field_merkle_tree,
batch_merkle_tree,
degree_bits,
rate_bits,
blinding,
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
batch_fri_proof::<F, C, D>(
&oracles
.iter()
.map(|o| &o.field_merkle_tree)
.map(|o| &o.batch_merkle_tree)
.collect::<Vec<_>>(),
final_lde_polynomial_coeff[0].clone(),
&final_lde_polynomial_values,
Expand All @@ -202,7 +202,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
) -> &[F] {
let index = index * step;
let index = reverse_bits(index, self.degree_bits[degree_bits_index] + self.rate_bits);
let slice = &self.field_merkle_tree.leaves[degree_bits_index][index];
let slice = &self.batch_merkle_tree.leaves[degree_bits_index][index];
&slice[slice_start..slice_start + slice_len]
}

Expand Down Expand Up @@ -257,8 +257,8 @@ mod test {
use plonky2_field::types::Sample;

use super::*;
use crate::fri::batch_oracle::BatchFriOracle;
use crate::fri::batch_verifier::verify_batch_fri_proof;
use crate::batch_fri::oracle::BatchFriOracle;
use crate::batch_fri::verifier::verify_batch_fri_proof;
use crate::fri::reduction_strategies::FriReductionStrategy;
use crate::fri::structure::{
FriBatchInfo, FriBatchInfoTarget, FriInstanceInfo, FriInstanceInfoTarget, FriOpeningBatch,
Expand Down Expand Up @@ -323,7 +323,7 @@ mod test {
);

let mut challenger = Challenger::<F, H>::new();
challenger.observe_cap(&trace_oracle.field_merkle_tree.cap);
challenger.observe_cap(&trace_oracle.batch_merkle_tree.cap);
let zeta = challenger.get_extension_challenge::<D>();
let eta = challenger.get_extension_challenge::<D>();
let poly0 = &trace_oracle.polynomials[0];
Expand Down Expand Up @@ -452,7 +452,7 @@ mod test {
&fri_params.config,
);
let degree_bits = [k0, k1, k2];
let merkle_cap = trace_oracle.field_merkle_tree.cap;
let merkle_cap = trace_oracle.batch_merkle_tree.cap;
verify_batch_fri_proof::<GoldilocksField, C, D>(
&degree_bits,
&fri_instances,
Expand Down
Loading

0 comments on commit 54b17c1

Please sign in to comment.