plonky2/src/fri/prover.rs

171 lines
5.5 KiB
Rust
Raw Normal View History

2021-07-15 07:40:41 -07:00
use rayon::prelude::*;
2021-05-18 16:09:22 +02:00
use crate::field::extension_field::{flatten, unflatten, Extendable};
use crate::field::field_types::Field;
use crate::fri::proof::{FriInitialTreeProof, FriProof, FriQueryRound, FriQueryStep};
2021-05-05 18:23:59 +02:00
use crate::fri::FriConfig;
use crate::hash::hash_types::HashOut;
use crate::hash::hashing::hash_n_to_1;
use crate::hash::merkle_tree::MerkleTree;
use crate::iop::challenger::Challenger;
use crate::plonk::plonk_common::reduce_with_powers;
2021-05-05 18:23:59 +02:00
use crate::polynomial::polynomial::{PolynomialCoeffs, PolynomialValues};
2021-07-15 07:40:41 -07:00
use crate::timed;
2021-05-05 18:23:59 +02:00
use crate::util::reverse_index_bits_in_place;
use crate::util::timing::TimingTree;
2021-05-05 18:23:59 +02:00
/// Builds a FRI proof.
2021-05-18 15:44:50 +02:00
pub fn fri_proof<F: Field + Extendable<D>, const D: usize>(
2021-05-06 17:09:55 +02:00
initial_merkle_trees: &[&MerkleTree<F>],
2021-05-05 18:23:59 +02:00
// Coefficients of the polynomial on which the LDT is performed. Only the first `1/rate` coefficients are non-zero.
2021-07-21 08:26:56 -07:00
lde_polynomial_coeffs: PolynomialCoeffs<F::Extension>,
2021-05-05 18:23:59 +02:00
// Evaluation of the polynomial on the large domain.
2021-07-21 08:26:56 -07:00
lde_polynomial_values: PolynomialValues<F::Extension>,
2021-05-05 18:23:59 +02:00
challenger: &mut Challenger<F>,
config: &FriConfig,
timing: &mut TimingTree,
2021-05-18 15:44:50 +02:00
) -> FriProof<F, D> {
2021-05-05 18:23:59 +02:00
let n = lde_polynomial_values.values.len();
assert_eq!(lde_polynomial_coeffs.coeffs.len(), n);
// Commit phase
let (trees, final_coeffs) = fri_committed_trees(
lde_polynomial_coeffs,
lde_polynomial_values,
challenger,
config,
);
// PoW phase
let current_hash = challenger.get_hash();
2021-07-15 07:40:41 -07:00
let pow_witness = timed!(
timing,
"find for proof-of-work witness",
fri_proof_of_work(current_hash, config)
2021-07-15 07:40:41 -07:00
);
2021-05-05 18:23:59 +02:00
// Query phase
let query_round_proofs =
fri_prover_query_rounds(initial_merkle_trees, &trees, challenger, n, config);
FriProof {
commit_phase_merkle_roots: trees.iter().map(|t| t.root).collect(),
query_round_proofs,
final_poly: final_coeffs,
pow_witness,
}
}
2021-05-18 15:44:50 +02:00
fn fri_committed_trees<F: Field + Extendable<D>, const D: usize>(
2021-07-21 08:26:56 -07:00
mut coeffs: PolynomialCoeffs<F::Extension>,
mut values: PolynomialValues<F::Extension>,
2021-05-05 18:23:59 +02:00
challenger: &mut Challenger<F>,
config: &FriConfig,
2021-05-18 15:44:50 +02:00
) -> (Vec<MerkleTree<F>>, PolynomialCoeffs<F::Extension>) {
2021-05-05 18:23:59 +02:00
let mut trees = Vec::new();
let mut shift = F::MULTIPLICATIVE_GROUP_GENERATOR;
let num_reductions = config.reduction_arity_bits.len();
for i in 0..num_reductions {
let arity = 1 << config.reduction_arity_bits[i];
reverse_index_bits_in_place(&mut values.values);
let tree = MerkleTree::new(
values
.values
.chunks(arity)
2021-05-18 15:22:06 +02:00
.map(|chunk: &[F::Extension]| flatten(chunk))
2021-05-05 18:23:59 +02:00
.collect(),
false,
);
challenger.observe_hash(&tree.root);
trees.push(tree);
2021-05-18 15:22:06 +02:00
let beta = challenger.get_extension_challenge();
2021-05-05 18:23:59 +02:00
// P(x) = sum_{i<r} x^i * P_i(x^r) becomes sum_{i<r} beta^i * P_i(x).
coeffs = PolynomialCoeffs::new(
coeffs
.coeffs
.chunks_exact(arity)
.map(|chunk| reduce_with_powers(chunk, beta))
.collect::<Vec<_>>(),
);
shift = shift.exp_u32(arity as u32);
values = coeffs.coset_fft(shift.into())
2021-05-05 18:23:59 +02:00
}
2021-07-19 16:24:21 +02:00
coeffs.trim();
2021-05-18 15:22:06 +02:00
challenger.observe_extension_elements(&coeffs.coeffs);
2021-05-05 18:23:59 +02:00
(trees, coeffs)
}
fn fri_proof_of_work<F: Field>(current_hash: HashOut<F>, config: &FriConfig) -> F {
2021-07-15 07:40:41 -07:00
(0..u64::MAX)
.into_par_iter()
.find_any(|&i| {
2021-05-05 18:23:59 +02:00
hash_n_to_1(
current_hash
.elements
.iter()
.copied()
.chain(Some(F::from_canonical_u64(i)))
.collect(),
false,
)
.to_canonical_u64()
2021-06-17 09:49:41 +02:00
.leading_zeros()
2021-07-21 13:05:32 -07:00
>= config.proof_of_work_bits + (64 - F::order().bits()) as u32
2021-05-05 18:23:59 +02:00
})
.map(F::from_canonical_u64)
2021-07-15 07:40:41 -07:00
.expect("Proof of work failed. This is highly unlikely!")
2021-05-05 18:23:59 +02:00
}
2021-05-18 15:44:50 +02:00
fn fri_prover_query_rounds<F: Field + Extendable<D>, const D: usize>(
2021-05-06 17:09:55 +02:00
initial_merkle_trees: &[&MerkleTree<F>],
2021-05-05 18:23:59 +02:00
trees: &[MerkleTree<F>],
challenger: &mut Challenger<F>,
n: usize,
config: &FriConfig,
2021-05-18 15:44:50 +02:00
) -> Vec<FriQueryRound<F, D>> {
2021-05-05 18:23:59 +02:00
(0..config.num_query_rounds)
.map(|_| fri_prover_query_round(initial_merkle_trees, trees, challenger, n, config))
.collect()
}
2021-05-18 15:44:50 +02:00
fn fri_prover_query_round<F: Field + Extendable<D>, const D: usize>(
2021-05-06 17:09:55 +02:00
initial_merkle_trees: &[&MerkleTree<F>],
2021-05-05 18:23:59 +02:00
trees: &[MerkleTree<F>],
challenger: &mut Challenger<F>,
n: usize,
config: &FriConfig,
2021-05-18 15:44:50 +02:00
) -> FriQueryRound<F, D> {
2021-05-05 18:23:59 +02:00
let mut query_steps = Vec::new();
let x = challenger.get_challenge();
let mut x_index = x.to_canonical_u64() as usize % n;
let initial_proof = initial_merkle_trees
.iter()
.map(|t| (t.get(x_index).to_vec(), t.prove(x_index)))
.collect::<Vec<_>>();
for (i, tree) in trees.iter().enumerate() {
let arity_bits = config.reduction_arity_bits[i];
let arity = 1 << arity_bits;
2021-05-18 15:22:06 +02:00
let mut evals = unflatten(tree.get(x_index >> arity_bits));
2021-05-05 18:23:59 +02:00
evals.remove(x_index & (arity - 1));
let merkle_proof = tree.prove(x_index >> arity_bits);
query_steps.push(FriQueryStep {
evals,
merkle_proof,
});
x_index >>= arity_bits;
}
FriQueryRound {
initial_trees_proof: FriInitialTreeProof {
evals_proofs: initial_proof,
},
steps: query_steps,
}
}