mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-07 00:03:10 +00:00
Fix tests
This commit is contained in:
parent
a90ea6ec79
commit
1d368782f2
@ -317,89 +317,94 @@ impl<F: Extendable<D>, const D: usize, const R: usize> SimpleGenerator<F>
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use std::convert::TryInto;
|
||||
// use std::sync::Arc;
|
||||
//
|
||||
// use anyhow::Result;
|
||||
//
|
||||
// use crate::field::crandall_field::CrandallField;
|
||||
// use crate::field::field_types::Field;
|
||||
// use crate::gates::gate::Gate;
|
||||
// use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
|
||||
// use crate::gates::gmimc::{GMiMCGate, W};
|
||||
// use crate::hash::gmimc::gmimc_permute_naive;
|
||||
// use crate::iop::generator::generate_partial_witness;
|
||||
// use crate::iop::wire::Wire;
|
||||
// use crate::iop::witness::PartialWitness;
|
||||
// use crate::util::timing::TimingTree;
|
||||
//
|
||||
// #[test]
|
||||
// fn generated_output() {
|
||||
// type F = CrandallField;
|
||||
// const R: usize = 101;
|
||||
// let constants = Arc::new([F::TWO; R]);
|
||||
// type Gate = GMiMCGate<F, 4, R>;
|
||||
// let gate = Gate::new(constants.clone());
|
||||
//
|
||||
// let permutation_inputs = (0..W).map(F::from_canonical_usize).collect::<Vec<_>>();
|
||||
//
|
||||
// let mut witness = PartialWitness::new(gate.num_wires());
|
||||
// witness.set_wire(
|
||||
// Wire {
|
||||
// gate: 0,
|
||||
// input: Gate::WIRE_SWAP,
|
||||
// },
|
||||
// F::ZERO,
|
||||
// );
|
||||
// for i in 0..W {
|
||||
// witness.set_wire(
|
||||
// Wire {
|
||||
// gate: 0,
|
||||
// input: Gate::wire_input(i),
|
||||
// },
|
||||
// permutation_inputs[i],
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// let generators = gate.generators(0, &[]);
|
||||
// generate_partial_witness(
|
||||
// &mut witness,
|
||||
// &generators,
|
||||
// gate.num_wires(),
|
||||
// 1,
|
||||
// 1,
|
||||
// &mut TimingTree::default(),
|
||||
// );
|
||||
//
|
||||
// let expected_outputs: [F; W] =
|
||||
// gmimc_permute_naive(permutation_inputs.try_into().unwrap(), constants);
|
||||
//
|
||||
// for i in 0..W {
|
||||
// let out = witness.get_wire(Wire {
|
||||
// gate: 0,
|
||||
// input: Gate::wire_output(i),
|
||||
// });
|
||||
// assert_eq!(out, expected_outputs[i]);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn low_degree() {
|
||||
// type F = CrandallField;
|
||||
// const R: usize = 101;
|
||||
// let constants = Arc::new([F::TWO; R]);
|
||||
// let gate = GMiMCGate::<F, 4, R>::new(constants);
|
||||
// test_low_degree(gate)
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn eval_fns() -> Result<()> {
|
||||
// type F = CrandallField;
|
||||
// const R: usize = 101;
|
||||
// let constants = Arc::new([F::TWO; R]);
|
||||
// let gate = GMiMCGate::<F, 4, R>::new(constants);
|
||||
// test_eval_fns(gate)
|
||||
// }
|
||||
// }
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::convert::TryInto;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::field::crandall_field::CrandallField;
|
||||
use crate::field::field_types::Field;
|
||||
use crate::gates::gate::Gate;
|
||||
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
|
||||
use crate::gates::gmimc::{GMiMCGate, W};
|
||||
use crate::hash::gmimc::gmimc_permute_naive;
|
||||
use crate::iop::generator::generate_partial_witness;
|
||||
use crate::iop::target::Target;
|
||||
use crate::iop::wire::Wire;
|
||||
use crate::iop::witness::{PartialWitness, PartitionWitness, Witness};
|
||||
use crate::util::timing::TimingTree;
|
||||
|
||||
#[test]
|
||||
fn generated_output() {
|
||||
type F = CrandallField;
|
||||
const R: usize = 101;
|
||||
let constants = Arc::new([F::TWO; R]);
|
||||
type Gate = GMiMCGate<F, 4, R>;
|
||||
let gate = Gate::new(constants.clone());
|
||||
|
||||
let permutation_inputs = (0..W).map(F::from_canonical_usize).collect::<Vec<_>>();
|
||||
|
||||
let mut witness = PartialWitness::new();
|
||||
witness.set_wire(
|
||||
Wire {
|
||||
gate: 0,
|
||||
input: Gate::WIRE_SWAP,
|
||||
},
|
||||
F::ZERO,
|
||||
);
|
||||
for i in 0..W {
|
||||
witness.set_wire(
|
||||
Wire {
|
||||
gate: 0,
|
||||
input: Gate::wire_input(i),
|
||||
},
|
||||
permutation_inputs[i],
|
||||
);
|
||||
}
|
||||
|
||||
let mut partition_witness = PartitionWitness::new(gate.num_wires(), gate.num_wires(), 1);
|
||||
for input in 0..gate.num_wires() {
|
||||
partition_witness.add(Target::Wire(Wire { gate: 0, input }));
|
||||
}
|
||||
for (&t, &v) in witness.target_values.iter() {
|
||||
partition_witness.set_target(t, v);
|
||||
}
|
||||
let generators = gate.generators(0, &[]);
|
||||
generate_partial_witness(
|
||||
&mut partition_witness,
|
||||
&generators,
|
||||
&mut TimingTree::default(),
|
||||
);
|
||||
|
||||
let expected_outputs: [F; W] =
|
||||
gmimc_permute_naive(permutation_inputs.try_into().unwrap(), constants);
|
||||
|
||||
for i in 0..W {
|
||||
let out = partition_witness.get_wire(Wire {
|
||||
gate: 0,
|
||||
input: Gate::wire_output(i),
|
||||
});
|
||||
assert_eq!(out, expected_outputs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn low_degree() {
|
||||
type F = CrandallField;
|
||||
const R: usize = 101;
|
||||
let constants = Arc::new([F::TWO; R]);
|
||||
let gate = GMiMCGate::<F, 4, R>::new(constants);
|
||||
test_low_degree(gate)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_fns() -> Result<()> {
|
||||
type F = CrandallField;
|
||||
const R: usize = 101;
|
||||
let constants = Arc::new([F::TWO; R]);
|
||||
let gate = GMiMCGate::<F, 4, R>::new(constants);
|
||||
test_eval_fns(gate)
|
||||
}
|
||||
}
|
||||
|
||||
@ -353,89 +353,86 @@ impl RecursiveChallenger {
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use crate::field::crandall_field::CrandallField;
|
||||
// use crate::field::field_types::Field;
|
||||
// use crate::iop::challenger::{Challenger, RecursiveChallenger};
|
||||
// use crate::iop::generator::generate_partial_witness;
|
||||
// use crate::iop::target::Target;
|
||||
// use crate::iop::witness::PartialWitness;
|
||||
// use crate::plonk::circuit_builder::CircuitBuilder;
|
||||
// use crate::plonk::circuit_data::CircuitConfig;
|
||||
// use crate::util::timing::TimingTree;
|
||||
//
|
||||
// #[test]
|
||||
// fn no_duplicate_challenges() {
|
||||
// type F = CrandallField;
|
||||
// let mut challenger = Challenger::new();
|
||||
// let mut challenges = Vec::new();
|
||||
//
|
||||
// for i in 1..10 {
|
||||
// challenges.extend(challenger.get_n_challenges(i));
|
||||
// challenger.observe_element(F::rand());
|
||||
// }
|
||||
//
|
||||
// let dedup_challenges = {
|
||||
// let mut dedup = challenges.clone();
|
||||
// dedup.dedup();
|
||||
// dedup
|
||||
// };
|
||||
// assert_eq!(dedup_challenges, challenges);
|
||||
// }
|
||||
//
|
||||
// /// Tests for consistency between `Challenger` and `RecursiveChallenger`.
|
||||
// #[test]
|
||||
// fn test_consistency() {
|
||||
// type F = CrandallField;
|
||||
//
|
||||
// // These are mostly arbitrary, but we want to test some rounds with enough inputs/outputs to
|
||||
// // trigger multiple absorptions/squeezes.
|
||||
// let num_inputs_per_round = vec![2, 5, 3];
|
||||
// let num_outputs_per_round = vec![1, 2, 4];
|
||||
//
|
||||
// // Generate random input messages.
|
||||
// let inputs_per_round: Vec<Vec<F>> = num_inputs_per_round
|
||||
// .iter()
|
||||
// .map(|&n| F::rand_vec(n))
|
||||
// .collect();
|
||||
//
|
||||
// let mut challenger = Challenger::new();
|
||||
// let mut outputs_per_round: Vec<Vec<F>> = Vec::new();
|
||||
// for (r, inputs) in inputs_per_round.iter().enumerate() {
|
||||
// challenger.observe_elements(inputs);
|
||||
// outputs_per_round.push(challenger.get_n_challenges(num_outputs_per_round[r]));
|
||||
// }
|
||||
//
|
||||
// let config = CircuitConfig {
|
||||
// num_wires: 12 + 12 + 1 + 101,
|
||||
// num_routed_wires: 27,
|
||||
// ..CircuitConfig::default()
|
||||
// };
|
||||
// let mut witness = PartialWitness::new(config.num_wires);
|
||||
// let mut builder = CircuitBuilder::<F, 4>::new(config.clone());
|
||||
// let mut recursive_challenger = RecursiveChallenger::new(&mut builder);
|
||||
// let mut recursive_outputs_per_round: Vec<Vec<Target>> = Vec::new();
|
||||
// for (r, inputs) in inputs_per_round.iter().enumerate() {
|
||||
// recursive_challenger.observe_elements(&builder.constants(inputs));
|
||||
// recursive_outputs_per_round.push(
|
||||
// recursive_challenger.get_n_challenges(&mut builder, num_outputs_per_round[r]),
|
||||
// );
|
||||
// }
|
||||
// let circuit = builder.build();
|
||||
// generate_partial_witness(
|
||||
// &mut witness,
|
||||
// &circuit.prover_only.generators,
|
||||
// config.num_wires,
|
||||
// circuit.common.degree(),
|
||||
// circuit.prover_only.num_virtual_targets,
|
||||
// &mut TimingTree::default(),
|
||||
// );
|
||||
// let recursive_output_values_per_round: Vec<Vec<F>> = recursive_outputs_per_round
|
||||
// .iter()
|
||||
// .map(|outputs| witness.get_targets(outputs))
|
||||
// .collect();
|
||||
//
|
||||
// assert_eq!(outputs_per_round, recursive_output_values_per_round);
|
||||
// }
|
||||
// }
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::field::crandall_field::CrandallField;
|
||||
use crate::field::field_types::Field;
|
||||
use crate::iop::challenger::{Challenger, RecursiveChallenger};
|
||||
use crate::iop::generator::generate_partial_witness;
|
||||
use crate::iop::target::Target;
|
||||
use crate::iop::witness::{PartialWitness, Witness};
|
||||
use crate::plonk::circuit_builder::CircuitBuilder;
|
||||
use crate::plonk::circuit_data::CircuitConfig;
|
||||
use crate::util::timing::TimingTree;
|
||||
|
||||
#[test]
|
||||
fn no_duplicate_challenges() {
|
||||
type F = CrandallField;
|
||||
let mut challenger = Challenger::new();
|
||||
let mut challenges = Vec::new();
|
||||
|
||||
for i in 1..10 {
|
||||
challenges.extend(challenger.get_n_challenges(i));
|
||||
challenger.observe_element(F::rand());
|
||||
}
|
||||
|
||||
let dedup_challenges = {
|
||||
let mut dedup = challenges.clone();
|
||||
dedup.dedup();
|
||||
dedup
|
||||
};
|
||||
assert_eq!(dedup_challenges, challenges);
|
||||
}
|
||||
|
||||
/// Tests for consistency between `Challenger` and `RecursiveChallenger`.
|
||||
#[test]
|
||||
fn test_consistency() {
|
||||
type F = CrandallField;
|
||||
|
||||
// These are mostly arbitrary, but we want to test some rounds with enough inputs/outputs to
|
||||
// trigger multiple absorptions/squeezes.
|
||||
let num_inputs_per_round = vec![2, 5, 3];
|
||||
let num_outputs_per_round = vec![1, 2, 4];
|
||||
|
||||
// Generate random input messages.
|
||||
let inputs_per_round: Vec<Vec<F>> = num_inputs_per_round
|
||||
.iter()
|
||||
.map(|&n| F::rand_vec(n))
|
||||
.collect();
|
||||
|
||||
let mut challenger = Challenger::new();
|
||||
let mut outputs_per_round: Vec<Vec<F>> = Vec::new();
|
||||
for (r, inputs) in inputs_per_round.iter().enumerate() {
|
||||
challenger.observe_elements(inputs);
|
||||
outputs_per_round.push(challenger.get_n_challenges(num_outputs_per_round[r]));
|
||||
}
|
||||
|
||||
let config = CircuitConfig {
|
||||
num_wires: 12 + 12 + 1 + 101,
|
||||
num_routed_wires: 27,
|
||||
..CircuitConfig::default()
|
||||
};
|
||||
let mut builder = CircuitBuilder::<F, 4>::new(config.clone());
|
||||
let mut recursive_challenger = RecursiveChallenger::new(&mut builder);
|
||||
let mut recursive_outputs_per_round: Vec<Vec<Target>> = Vec::new();
|
||||
for (r, inputs) in inputs_per_round.iter().enumerate() {
|
||||
recursive_challenger.observe_elements(&builder.constants(inputs));
|
||||
recursive_outputs_per_round.push(
|
||||
recursive_challenger.get_n_challenges(&mut builder, num_outputs_per_round[r]),
|
||||
);
|
||||
}
|
||||
let circuit = builder.build();
|
||||
let mut partition_witness = circuit.prover_only.partition_witness.clone();
|
||||
generate_partial_witness(
|
||||
&mut partition_witness,
|
||||
&circuit.prover_only.generators,
|
||||
&mut TimingTree::default(),
|
||||
);
|
||||
let recursive_output_values_per_round: Vec<Vec<F>> = recursive_outputs_per_round
|
||||
.iter()
|
||||
.map(|outputs| partition_witness.get_targets(outputs))
|
||||
.collect();
|
||||
|
||||
assert_eq!(outputs_per_round, recursive_output_values_per_round);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user