2022-05-18 09:22:58 +02:00
|
|
|
use plonky2::field::extension_field::Extendable;
|
2022-05-06 17:22:30 +02:00
|
|
|
use plonky2::hash::hash_types::RichField;
|
|
|
|
|
|
2022-05-13 10:48:56 +02:00
|
|
|
use crate::config::StarkConfig;
|
2022-05-18 09:22:58 +02:00
|
|
|
use crate::cpu::cpu_stark::CpuStark;
|
2022-05-11 14:35:33 +02:00
|
|
|
use crate::cross_table_lookup::CrossTableLookup;
|
2022-05-18 09:22:58 +02:00
|
|
|
use crate::keccak::keccak_stark::KeccakStark;
|
2022-05-06 17:22:30 +02:00
|
|
|
use crate::stark::Stark;
|
|
|
|
|
|
2022-05-12 20:38:11 +02:00
|
|
|
#[derive(Clone)]
|
2022-05-06 17:35:25 +02:00
|
|
|
pub struct AllStark<F: RichField + Extendable<D>, const D: usize> {
|
2022-05-11 14:35:33 +02:00
|
|
|
pub cpu_stark: CpuStark<F, D>,
|
|
|
|
|
pub keccak_stark: KeccakStark<F, D>,
|
2022-05-17 09:24:22 +02:00
|
|
|
pub cross_table_lookups: Vec<CrossTableLookup<F>>,
|
2022-05-06 17:22:30 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-13 10:48:56 +02:00
|
|
|
impl<F: RichField + Extendable<D>, const D: usize> AllStark<F, D> {
|
|
|
|
|
pub(crate) fn nums_permutation_zs(&self, config: &StarkConfig) -> Vec<usize> {
|
2022-05-19 09:41:15 +02:00
|
|
|
let ans = vec![
|
2022-05-13 10:48:56 +02:00
|
|
|
self.cpu_stark.num_permutation_batches(config),
|
|
|
|
|
self.keccak_stark.num_permutation_batches(config),
|
2022-05-19 09:41:15 +02:00
|
|
|
];
|
|
|
|
|
debug_assert_eq!(ans.len(), Table::num_tables());
|
|
|
|
|
ans
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn permutation_batch_sizes(&self) -> Vec<usize> {
|
|
|
|
|
let ans = vec![
|
|
|
|
|
self.cpu_stark.permutation_batch_size(),
|
|
|
|
|
self.keccak_stark.permutation_batch_size(),
|
|
|
|
|
];
|
|
|
|
|
debug_assert_eq!(ans.len(), Table::num_tables());
|
|
|
|
|
ans
|
2022-05-13 10:48:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 17:22:30 +02:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
|
pub enum Table {
|
|
|
|
|
Cpu = 0,
|
|
|
|
|
Keccak = 1,
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 09:41:15 +02:00
|
|
|
impl Table {
|
|
|
|
|
pub(crate) fn num_tables() -> usize {
|
|
|
|
|
Table::Keccak as usize + 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 20:38:11 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use anyhow::Result;
|
2022-06-13 18:54:12 +02:00
|
|
|
use itertools::{izip, Itertools};
|
2022-05-12 20:38:11 +02:00
|
|
|
use plonky2::field::field_types::Field;
|
2022-05-24 17:50:28 +02:00
|
|
|
use plonky2::iop::witness::PartialWitness;
|
|
|
|
|
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
|
|
|
|
use plonky2::plonk::circuit_data::CircuitConfig;
|
2022-05-12 20:38:11 +02:00
|
|
|
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
|
|
|
|
|
use plonky2::util::timing::TimingTree;
|
2022-06-10 21:02:56 +02:00
|
|
|
use rand::{thread_rng, Rng};
|
2022-05-12 20:38:11 +02:00
|
|
|
|
2022-05-18 09:22:58 +02:00
|
|
|
use crate::all_stark::{AllStark, Table};
|
2022-05-12 20:38:11 +02:00
|
|
|
use crate::config::StarkConfig;
|
2022-06-13 18:54:12 +02:00
|
|
|
use crate::cpu::columns::{KECCAK_INPUT_LIMBS, KECCAK_OUTPUT_LIMBS};
|
2022-05-18 09:22:58 +02:00
|
|
|
use crate::cpu::cpu_stark::CpuStark;
|
2022-06-14 00:53:31 +02:00
|
|
|
use crate::cross_table_lookup::{Column, CrossTableLookup, TableWithColumns};
|
2022-06-10 19:30:51 +02:00
|
|
|
use crate::keccak::keccak_stark::{KeccakStark, NUM_INPUTS, NUM_ROUNDS};
|
2022-05-24 17:50:28 +02:00
|
|
|
use crate::proof::AllProof;
|
2022-05-12 20:38:11 +02:00
|
|
|
use crate::prover::prove;
|
2022-05-24 17:50:28 +02:00
|
|
|
use crate::recursive_verifier::{
|
|
|
|
|
add_virtual_all_proof, set_all_proof_target, verify_proof_circuit,
|
|
|
|
|
};
|
2022-06-03 19:24:47 -07:00
|
|
|
use crate::stark::Stark;
|
|
|
|
|
use crate::util::trace_rows_to_poly_values;
|
2022-05-12 20:38:11 +02:00
|
|
|
use crate::verifier::verify_proof;
|
2022-06-10 21:02:56 +02:00
|
|
|
use crate::{cpu, keccak};
|
2022-05-12 20:38:11 +02:00
|
|
|
|
2022-05-24 17:50:28 +02:00
|
|
|
const D: usize = 2;
|
|
|
|
|
type C = PoseidonGoldilocksConfig;
|
|
|
|
|
type F = <C as GenericConfig<D>>::F;
|
2022-05-12 20:38:11 +02:00
|
|
|
|
2022-05-24 17:50:28 +02:00
|
|
|
fn get_proof(config: &StarkConfig) -> Result<(AllStark<F, D>, AllProof<F, C, D>)> {
|
2022-05-12 20:38:11 +02:00
|
|
|
let cpu_stark = CpuStark::<F, D> {
|
|
|
|
|
f: Default::default(),
|
|
|
|
|
};
|
2022-06-06 12:50:56 -07:00
|
|
|
let cpu_rows = 256;
|
2022-05-18 09:22:58 +02:00
|
|
|
|
2022-05-12 20:38:11 +02:00
|
|
|
let keccak_stark = KeccakStark::<F, D> {
|
|
|
|
|
f: Default::default(),
|
|
|
|
|
};
|
2022-06-01 09:20:39 -07:00
|
|
|
|
2022-06-10 21:02:56 +02:00
|
|
|
let mut rng = thread_rng();
|
2022-06-13 18:54:12 +02:00
|
|
|
let num_keccak_perms = 2;
|
|
|
|
|
let keccak_inputs = (0..num_keccak_perms)
|
2022-06-10 19:30:51 +02:00
|
|
|
.map(|_| [0u64; NUM_INPUTS].map(|_| rng.gen()))
|
2022-06-06 10:31:42 -07:00
|
|
|
.collect_vec();
|
|
|
|
|
let keccak_trace = keccak_stark.generate_trace(keccak_inputs);
|
2022-06-13 18:54:12 +02:00
|
|
|
let keccak_input_limbs: Vec<[F; 2 * NUM_INPUTS]> = (0..num_keccak_perms)
|
2022-06-10 21:02:56 +02:00
|
|
|
.map(|i| {
|
|
|
|
|
(0..2 * NUM_INPUTS)
|
|
|
|
|
.map(|j| {
|
2022-06-14 01:21:17 +02:00
|
|
|
keccak::registers::reg_input_limb(j)
|
|
|
|
|
.eval_table(&keccak_trace, (i + 1) * NUM_ROUNDS - 1)
|
2022-06-10 21:02:56 +02:00
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.try_into()
|
|
|
|
|
.unwrap()
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2022-06-13 18:54:12 +02:00
|
|
|
let keccak_output_limbs: Vec<[F; 2 * NUM_INPUTS]> = (0..num_keccak_perms)
|
2022-06-10 21:02:56 +02:00
|
|
|
.map(|i| {
|
|
|
|
|
(0..2 * NUM_INPUTS)
|
|
|
|
|
.map(|j| {
|
|
|
|
|
keccak_trace[keccak::registers::reg_output_limb(j)].values
|
|
|
|
|
[(i + 1) * NUM_ROUNDS - 1]
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.try_into()
|
|
|
|
|
.unwrap()
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2022-06-06 10:31:42 -07:00
|
|
|
|
2022-06-06 12:50:56 -07:00
|
|
|
let mut cpu_trace_rows = vec![];
|
|
|
|
|
for i in 0..cpu_rows {
|
|
|
|
|
let mut cpu_trace_row = [F::ZERO; CpuStark::<F, D>::COLUMNS];
|
|
|
|
|
cpu_trace_row[cpu::columns::IS_CPU_CYCLE] = F::ONE;
|
2022-06-10 21:02:56 +02:00
|
|
|
cpu_trace_row[cpu::columns::OPCODE] = F::from_canonical_usize(i);
|
2022-06-06 12:50:56 -07:00
|
|
|
cpu_stark.generate(&mut cpu_trace_row);
|
|
|
|
|
cpu_trace_rows.push(cpu_trace_row);
|
|
|
|
|
}
|
2022-06-13 18:54:12 +02:00
|
|
|
for i in 0..num_keccak_perms {
|
2022-06-10 21:02:56 +02:00
|
|
|
cpu_trace_rows[i][cpu::columns::IS_KECCAK] = F::ONE;
|
2022-06-13 18:54:12 +02:00
|
|
|
for (j, input, output) in
|
|
|
|
|
izip!(0..2 * NUM_INPUTS, KECCAK_INPUT_LIMBS, KECCAK_OUTPUT_LIMBS)
|
|
|
|
|
{
|
|
|
|
|
cpu_trace_rows[i][input] = keccak_input_limbs[i][j];
|
|
|
|
|
cpu_trace_rows[i][output] = keccak_output_limbs[i][j];
|
2022-06-10 21:02:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-06 12:50:56 -07:00
|
|
|
let cpu_trace = trace_rows_to_poly_values(cpu_trace_rows);
|
2022-06-06 10:31:42 -07:00
|
|
|
|
2022-06-13 18:54:12 +02:00
|
|
|
let mut cpu_keccak_input_output = cpu::columns::KECCAK_INPUT_LIMBS.collect::<Vec<_>>();
|
2022-06-10 21:02:56 +02:00
|
|
|
cpu_keccak_input_output.extend(cpu::columns::KECCAK_OUTPUT_LIMBS);
|
|
|
|
|
let mut keccak_keccak_input_output = (0..2 * NUM_INPUTS)
|
|
|
|
|
.map(keccak::registers::reg_input_limb)
|
|
|
|
|
.collect::<Vec<_>>();
|
2022-06-14 01:21:17 +02:00
|
|
|
keccak_keccak_input_output.extend(Column::singles(
|
2022-06-15 17:01:30 +02:00
|
|
|
(0..2 * NUM_INPUTS).map(keccak::registers::reg_output_limb),
|
2022-06-14 01:21:17 +02:00
|
|
|
));
|
2022-06-06 14:37:21 -07:00
|
|
|
let cross_table_lookups = vec![CrossTableLookup::new(
|
2022-06-09 18:13:41 +02:00
|
|
|
vec![TableWithColumns::new(
|
|
|
|
|
Table::Cpu,
|
2022-06-15 17:01:30 +02:00
|
|
|
Column::singles(cpu_keccak_input_output).collect(),
|
2022-06-14 19:09:03 +02:00
|
|
|
Some(Column::single(cpu::columns::IS_KECCAK)),
|
2022-06-09 18:13:41 +02:00
|
|
|
)],
|
2022-06-10 21:02:56 +02:00
|
|
|
TableWithColumns::new(
|
|
|
|
|
Table::Keccak,
|
2022-06-14 01:21:17 +02:00
|
|
|
keccak_keccak_input_output,
|
2022-06-14 19:09:03 +02:00
|
|
|
Some(Column::single(keccak::registers::reg_step(NUM_ROUNDS - 1))),
|
2022-06-10 21:02:56 +02:00
|
|
|
),
|
|
|
|
|
None,
|
2022-06-06 14:37:21 -07:00
|
|
|
)];
|
2022-05-12 20:38:11 +02:00
|
|
|
|
|
|
|
|
let all_stark = AllStark {
|
|
|
|
|
cpu_stark,
|
|
|
|
|
keccak_stark,
|
|
|
|
|
cross_table_lookups,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let proof = prove::<F, C, D>(
|
|
|
|
|
&all_stark,
|
2022-05-26 20:44:59 +02:00
|
|
|
config,
|
2022-05-12 20:38:11 +02:00
|
|
|
vec![cpu_trace, keccak_trace],
|
|
|
|
|
vec![vec![]; 2],
|
|
|
|
|
&mut TimingTree::default(),
|
|
|
|
|
)?;
|
|
|
|
|
|
2022-05-24 17:50:28 +02:00
|
|
|
Ok((all_stark, proof))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_all_stark() -> Result<()> {
|
|
|
|
|
let config = StarkConfig::standard_fast_config();
|
|
|
|
|
let (all_stark, proof) = get_proof(&config)?;
|
2022-05-12 20:38:11 +02:00
|
|
|
verify_proof(all_stark, proof, &config)
|
2022-05-06 17:22:30 +02:00
|
|
|
}
|
2022-05-24 17:50:28 +02:00
|
|
|
|
|
|
|
|
#[test]
|
2022-05-26 16:27:15 +02:00
|
|
|
fn test_all_stark_recursive_verifier() -> Result<()> {
|
2022-05-24 17:50:28 +02:00
|
|
|
init_logger();
|
|
|
|
|
|
|
|
|
|
let config = StarkConfig::standard_fast_config();
|
|
|
|
|
let (all_stark, proof) = get_proof(&config)?;
|
|
|
|
|
verify_proof(all_stark.clone(), proof.clone(), &config)?;
|
|
|
|
|
|
|
|
|
|
recursive_proof(all_stark, proof, &config, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn recursive_proof(
|
|
|
|
|
inner_all_stark: AllStark<F, D>,
|
|
|
|
|
inner_proof: AllProof<F, C, D>,
|
|
|
|
|
inner_config: &StarkConfig,
|
|
|
|
|
print_gate_counts: bool,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
let circuit_config = CircuitConfig::standard_recursion_config();
|
|
|
|
|
let mut builder = CircuitBuilder::<F, D>::new(circuit_config);
|
|
|
|
|
let mut pw = PartialWitness::new();
|
2022-05-26 16:27:15 +02:00
|
|
|
let degree_bits = inner_proof.degree_bits(inner_config);
|
|
|
|
|
let nums_ctl_zs = inner_proof.nums_ctl_zs();
|
|
|
|
|
let pt = add_virtual_all_proof(
|
|
|
|
|
&mut builder,
|
|
|
|
|
&inner_all_stark,
|
|
|
|
|
inner_config,
|
|
|
|
|
°ree_bits,
|
|
|
|
|
&nums_ctl_zs,
|
|
|
|
|
);
|
2022-05-24 17:50:28 +02:00
|
|
|
set_all_proof_target(&mut pw, &pt, &inner_proof, builder.zero());
|
|
|
|
|
|
|
|
|
|
verify_proof_circuit::<F, C, D>(&mut builder, inner_all_stark, pt, inner_config);
|
|
|
|
|
|
|
|
|
|
if print_gate_counts {
|
|
|
|
|
builder.print_gate_counts(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let data = builder.build::<C>();
|
|
|
|
|
let proof = data.prove(pw)?;
|
|
|
|
|
data.verify(proof)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init_logger() {
|
|
|
|
|
let _ = env_logger::builder().format_timestamp(None).try_init();
|
|
|
|
|
}
|
2022-05-06 17:22:30 +02:00
|
|
|
}
|