Added mock feature flag and test

This commit is contained in:
Uma Roy 2023-09-06 20:37:38 -07:00
parent 86fb6aa065
commit 18d314126f
7 changed files with 100 additions and 11 deletions

View File

@ -27,7 +27,7 @@ type C = KeccakGoldilocksConfig;
/// Test a simple token transfer to a new address.
#[test]
#[ignore] // Too slow to run on CI.
// #[ignore] // Too slow to run on CI.
fn test_basic_smart_contract() -> anyhow::Result<()> {
init_logger();

View File

@ -16,6 +16,7 @@ gate_testing = []
parallel = ["hashbrown/rayon", "plonky2_maybe_rayon/parallel"]
std = ["anyhow/std", "rand/std", "itertools/use_std"]
timing = ["std"]
mock = []
[dependencies]
ahash = { version = "0.8.3", default-features = false, features = ["compile-time-rng"] } # NOTE: Be sure to keep this version the same as the dependency in `hashbrown`.

View File

@ -36,9 +36,32 @@ pub struct PolynomialBatch<F: RichField + Extendable<D>, C: GenericConfig<D, F =
pub blinding: bool,
}
impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize> Default
for PolynomialBatch<F, C, D>
{
fn default() -> Self {
PolynomialBatch {
polynomials: Vec::new(),
merkle_tree: MerkleTree::default(),
degree_log: 0,
rate_bits: 0,
blinding: false,
}
}
}
impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
PolynomialBatch<F, C, D>
{
pub fn default() -> Self {
Self {
polynomials: Vec::new(),
merkle_tree: MerkleTree::default(),
degree_log: 0,
rate_bits: 0,
blinding: false,
}
}
/// Creates a list polynomial commitment for the polynomials interpolating the values in `values`.
pub fn from_values(
values: Vec<PolynomialValues<F>>,

View File

@ -17,6 +17,12 @@ use crate::util::log2_strict;
// TODO: Change H to GenericHashOut<F>, since this only cares about the hash, not the hasher.
pub struct MerkleCap<F: RichField, H: Hasher<F>>(pub Vec<H::Hash>);
impl<F: RichField, H: Hasher<F>> Default for MerkleCap<F, H> {
fn default() -> Self {
Self(Vec::new())
}
}
impl<F: RichField, H: Hasher<F>> MerkleCap<F, H> {
pub fn len(&self) -> usize {
self.0.len()
@ -54,6 +60,16 @@ pub struct MerkleTree<F: RichField, H: Hasher<F>> {
pub cap: MerkleCap<F, H>,
}
impl<F: RichField, H: Hasher<F>> Default for MerkleTree<F, H> {
fn default() -> Self {
Self {
leaves: Vec::new(),
digests: Vec::new(),
cap: MerkleCap::default(),
}
}
}
fn capacity_up_to_mut<T>(v: &mut Vec<T>, len: usize) -> &mut [MaybeUninit<T>] {
assert!(v.capacity() >= len);
let v_ptr = v.as_mut_ptr().cast::<MaybeUninit<T>>();

View File

@ -16,7 +16,7 @@ use crate::util::serialization::{Buffer, IoResult, Read, Write};
/// Given a `PartitionWitness` that has only inputs set, populates the rest of the witness using the
/// given set of generators.
pub(crate) fn generate_partial_witness<
pub fn generate_partial_witness<
'a,
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,

View File

@ -469,6 +469,51 @@ mod tests {
Ok(())
}
#[cfg(feature = "mock")]
#[test]
fn test_circuit_build_mock() {
// This code is taken from examples/fibonacci.rs
use crate::field::types::Field;
use crate::iop::generator::generate_partial_witness;
use crate::iop::witness::{PartialWitness, Witness, 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;
let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);
// The arithmetic circuit.
let initial_a = builder.add_virtual_target();
let initial_b = builder.add_virtual_target();
let mut prev_target = initial_a;
let mut cur_target = initial_b;
for _ in 0..99 {
let temp = builder.add(prev_target, cur_target);
prev_target = cur_target;
cur_target = temp;
}
// Public inputs are the two initial values (provided below) and the result (which is generated).
builder.register_public_input(initial_a);
builder.register_public_input(initial_b);
builder.register_public_input(cur_target);
// Provide initial values.
let mut pw = PartialWitness::new();
pw.set_target(initial_a, F::ZERO);
pw.set_target(initial_b, F::ONE);
let data = builder.build::<C>();
let partition_witness = generate_partial_witness(pw, &data.prover_only, &data.common);
let result = partition_witness.try_get_target(cur_target).unwrap();
assert_eq!(result, F::from_canonical_u64(3736710860384812976));
}
fn init_logger() -> anyhow::Result<()> {
let mut builder = env_logger::Builder::from_default_env();
builder.format_timestamp(None);

View File

@ -1023,15 +1023,19 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let max_fft_points = 1 << (degree_bits + max(rate_bits, log2_ceil(quotient_degree_factor)));
let fft_root_table = fft_root_table(max_fft_points);
let constants_sigmas_vecs = [constant_vecs, sigma_vecs.clone()].concat();
let constants_sigmas_commitment = PolynomialBatch::<F, C, D>::from_values(
constants_sigmas_vecs,
rate_bits,
PlonkOracle::CONSTANTS_SIGMAS.blinding,
cap_height,
&mut timing,
Some(&fft_root_table),
);
let mut constants_sigmas_commitment = PolynomialBatch::<F, C, D>::default();
#[cfg(not(feature = "mock"))]
{
let constants_sigmas_vecs = [constant_vecs, sigma_vecs.clone()].concat();
constants_sigmas_commitment = PolynomialBatch::<F, C, D>::from_values(
constants_sigmas_vecs,
rate_bits,
PlonkOracle::CONSTANTS_SIGMAS.blinding,
cap_height,
&mut timing,
Some(&fft_root_table),
);
}
// Map between gates where not all generators are used and the gate's number of used generators.
let incomplete_gates = self