Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Fix no-std tests and add corresponding jobs in the CI (0xPolygonZero#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare authored Feb 6, 2024
1 parent 06444ea commit 1a08e78
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 127 deletions.
43 changes: 39 additions & 4 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ jobs:
CARGO_INCREMENTAL: 1
RUST_BACKTRACE: 1

wasm32:
name: wasm32 compatibility
wasm:
name: Check wasm32 compatibility
runs-on: ubuntu-latest
timeout-minutes: 30
if: "! contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
Expand All @@ -89,22 +89,57 @@ jobs:
with:
cache-on-failure: true

- name: Check in plonky2 subdirectory
- name: Check in plonky2 subdirectory for wasm targets
run: cargo check --manifest-path plonky2/Cargo.toml --target wasm32-unknown-unknown --no-default-features
env:
RUSTFLAGS: -Copt-level=3 -Cdebug-assertions -Coverflow-checks=y -Cdebuginfo=0
RUST_LOG: 1
CARGO_INCREMENTAL: 1
RUST_BACKTRACE: 1

- name: Check in starky subdirectory
- name: Check in starky subdirectory for wasm targets
run: cargo check --manifest-path starky/Cargo.toml --target wasm32-unknown-unknown --no-default-features
env:
RUSTFLAGS: -Copt-level=3 -Cdebug-assertions -Coverflow-checks=y -Cdebuginfo=0
RUST_LOG: 1
CARGO_INCREMENTAL: 1
RUST_BACKTRACE: 1

no_std:
name: Test Suite in no-std
runs-on: ubuntu-latest
timeout-minutes: 30
if: "! contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Install nightly toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2024-02-01

- name: Set up rust cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Run cargo test in plonky2 subdirectory (no-std)
run: cargo test --manifest-path plonky2/Cargo.toml --no-default-features --lib
env:
RUSTFLAGS: -Copt-level=3 -Cdebug-assertions -Coverflow-checks=y -Cdebuginfo=0
RUST_LOG: 1
CARGO_INCREMENTAL: 1
RUST_BACKTRACE: 1

- name: Run cargo test in starky subdirectory (no-std)
run: cargo test --manifest-path starky/Cargo.toml --no-default-features --lib
env:
RUSTFLAGS: -Copt-level=3 -Cdebug-assertions -Coverflow-checks=y -Cdebuginfo=0
RUST_LOG: 1
CARGO_INCREMENTAL: 1
RUST_BACKTRACE: 1

lints:
name: Formatting and Clippy
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions plonky2/src/gadgets/interpolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {

#[cfg(test)]
mod tests {
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use anyhow::Result;

use crate::field::extension::FieldExtension;
Expand Down
3 changes: 3 additions & 0 deletions plonky2/src/gates/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ impl<F: RichField + Extendable<D> + Poseidon, const D: usize> SimpleGenerator<F,

#[cfg(test)]
mod tests {
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

use anyhow::Result;

use crate::field::goldilocks_field::GoldilocksField;
Expand Down
17 changes: 10 additions & 7 deletions plonky2/src/hash/path_compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ mod tests {

assert_eq!(proofs, decompressed_proofs);

let compressed_proof_bytes = serde_cbor::to_vec(&compressed_proofs).unwrap();
println!(
"Compressed proof length: {} bytes",
compressed_proof_bytes.len()
);
let proof_bytes = serde_cbor::to_vec(&proofs).unwrap();
println!("Proof length: {} bytes", proof_bytes.len());
#[cfg(feature = "std")]
{
let compressed_proof_bytes = serde_cbor::to_vec(&compressed_proofs).unwrap();
println!(
"Compressed proof length: {} bytes",
compressed_proof_bytes.len()
);
let proof_bytes = serde_cbor::to_vec(&proofs).unwrap();
println!("Proof length: {} bytes", proof_bytes.len());
}
}
}
3 changes: 3 additions & 0 deletions plonky2/src/hash/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ impl<F: RichField> AlgebraicHasher<F> for PoseidonHash {

#[cfg(test)]
pub(crate) mod test_helpers {
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::field::types::Field;
use crate::hash::poseidon::{Poseidon, SPONGE_WIDTH};

Expand Down
3 changes: 3 additions & 0 deletions plonky2/src/hash/poseidon_goldilocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ mod poseidon12_mds {

#[cfg(test)]
mod tests {
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

use crate::field::goldilocks_field::GoldilocksField as F;
use crate::field::types::{Field, PrimeField64};
use crate::hash::poseidon::test_helpers::{check_consistency, check_test_vectors};
Expand Down
3 changes: 3 additions & 0 deletions plonky2/src/iop/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ impl<F: RichField + Extendable<D>, H: AlgebraicHasher<F>, const D: usize>

#[cfg(test)]
mod tests {
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::field::types::Sample;
use crate::iop::challenger::{Challenger, RecursiveChallenger};
use crate::iop::generator::generate_partial_witness;
Expand Down
161 changes: 47 additions & 114 deletions plonky2/src/lookup_test.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
static LOGGER_INITIALIZED: Once = Once::new();

use alloc::sync::Arc;
use std::sync::Once;
#[cfg(not(feature = "std"))]
use alloc::{sync::Arc, vec, vec::Vec};
#[cfg(feature = "std")]
use std::sync::{Arc, Once};

use itertools::Itertools;
use log::{Level, LevelFilter};
use log::Level;

use crate::field::types::Field;
use crate::gadgets::lookup::{OTHER_TABLE, SMALLER_TABLE, TIP5_TABLE};
use crate::gates::lookup_table::LookupTable;
use crate::gates::noop::NoopGate;
use crate::iop::witness::{PartialWitness, WitnessWrite};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use crate::plonk::prover::prove;
use crate::util::timing::TimingTree;

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

const LUT_SIZE: usize = u16::MAX as usize + 1;

#[cfg(feature = "std")]
static LOGGER_INITIALIZED: Once = Once::new();

#[test]
fn test_no_lookup() -> anyhow::Result<()> {
LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
use crate::iop::witness::PartialWitness;
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
init_logger();

let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);
Expand All @@ -41,14 +47,7 @@ fn test_no_lookup() -> anyhow::Result<()> {
#[should_panic]
#[test]
fn test_lookup_table_not_used() {
LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
init_logger();

let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);
Expand All @@ -63,14 +62,7 @@ fn test_lookup_table_not_used() {
#[should_panic]
#[test]
fn test_lookup_without_table() {
LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
init_logger();

let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);
Expand All @@ -84,17 +76,8 @@ fn test_lookup_without_table() {
// Tests two lookups in one lookup table.
#[test]
fn test_one_lookup() -> anyhow::Result<()> {
use crate::field::types::Field;
use crate::iop::witness::{PartialWitness, WitnessWrite};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
init_logger();

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
let tip5_table = TIP5_TABLE.to_vec();
let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect());
let config = CircuitConfig::standard_recursion_config();
Expand Down Expand Up @@ -145,18 +128,9 @@ fn test_one_lookup() -> anyhow::Result<()> {

// Tests one lookup in two different lookup tables.
#[test]
pub fn test_two_luts() -> anyhow::Result<()> {
use crate::field::types::Field;
use crate::iop::witness::{PartialWitness, WitnessWrite};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
fn test_two_luts() -> anyhow::Result<()> {
init_logger();

let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);

Expand Down Expand Up @@ -229,17 +203,9 @@ pub fn test_two_luts() -> anyhow::Result<()> {
}

#[test]
pub fn test_different_inputs() -> anyhow::Result<()> {
use crate::field::types::Field;
use crate::iop::witness::{PartialWitness, WitnessWrite};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
fn test_different_inputs() -> anyhow::Result<()> {
init_logger();

let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);

Expand Down Expand Up @@ -314,17 +280,9 @@ pub fn test_different_inputs() -> anyhow::Result<()> {

// This test looks up over 514 values for one LookupTableGate, which means that several LookupGates are created.
#[test]
pub fn test_many_lookups() -> anyhow::Result<()> {
use crate::field::types::Field;
use crate::iop::witness::{PartialWitness, WitnessWrite};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
fn test_many_lookups() -> anyhow::Result<()> {
init_logger();

let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);

Expand Down Expand Up @@ -404,18 +362,9 @@ pub fn test_many_lookups() -> anyhow::Result<()> {

// Tests whether, when adding the same LUT to the circuit, the circuit only adds one copy, with the same index.
#[test]
pub fn test_same_luts() -> anyhow::Result<()> {
use crate::field::types::Field;
use crate::iop::witness::{PartialWitness, WitnessWrite};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
fn test_same_luts() -> anyhow::Result<()> {
init_logger();

let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);

Expand Down Expand Up @@ -469,21 +418,11 @@ pub fn test_same_luts() -> anyhow::Result<()> {

#[test]
fn test_big_lut() -> anyhow::Result<()> {
use crate::field::types::Field;
use crate::iop::witness::{PartialWitness, WitnessWrite};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
init_logger();

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);

const LUT_SIZE: usize = u16::MAX as usize + 1;
let inputs: [u16; LUT_SIZE] = core::array::from_fn(|i| i as u16);
let lut_fn = |inp: u16| inp / 10;
let lut_index = builder.add_lookup_table_from_fn(lut_fn, &inputs);
Expand Down Expand Up @@ -522,21 +461,11 @@ fn test_big_lut() -> anyhow::Result<()> {

#[test]
fn test_many_lookups_on_big_lut() -> anyhow::Result<()> {
use crate::field::types::Field;
use crate::iop::witness::{PartialWitness, WitnessWrite};
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
init_logger();

LOGGER_INITIALIZED.call_once(|| init_logger().unwrap());
let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);

const LUT_SIZE: usize = u16::MAX as usize + 1;
let inputs: [u16; LUT_SIZE] = core::array::from_fn(|i| i as u16);
let lut_fn = |inp: u16| inp / 10;
let lut_index = builder.add_lookup_table_from_fn(lut_fn, &inputs);
Expand Down Expand Up @@ -581,11 +510,15 @@ fn test_many_lookups_on_big_lut() -> anyhow::Result<()> {
data.verify(proof)
}

fn init_logger() -> anyhow::Result<()> {
let mut builder = env_logger::Builder::from_default_env();
builder.format_timestamp(None);
builder.filter_level(LevelFilter::Debug);
fn init_logger() {
#[cfg(feature = "std")]
{
LOGGER_INITIALIZED.call_once(|| {
let mut builder = env_logger::Builder::from_default_env();
builder.format_timestamp(None);
builder.filter_level(log::LevelFilter::Debug);

builder.try_init()?;
Ok(())
builder.try_init().unwrap();
});
}
}
Loading

0 comments on commit 1a08e78

Please sign in to comment.