1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//! This crate contains Winterfell STARK prover.
//!
//! This prover can be used to generate proofs of computational integrity using the
//! [STARK](https://eprint.iacr.org/2018/046) (Scalable Transparent ARguments of Knowledge)
//! protocol.
//!
//! When the crate is compiled with `concurrent` feature enabled, proof generation will be
//! performed in multiple threads (usually, as many threads as there are logical cores on the
//! machine). The number of threads can be configured via `RAYON_NUM_THREADS` environment
//! variable.
//!
//! # Usage
//! To generate a proof that a computation was executed correctly, you'll need to do the
//! following:
//!
//! 1. Define an *algebraic intermediate representation* (AIR) for your computation. This can
//! be done by implementing [Air] trait.
//! 2. Define an execution trace for your computation. This can be done by implementing [Trace]
//! trait. Alternatively, you can use [TraceTable] struct which already implements [Trace]
//! trait in cases when this generic implementation works for your use case.
//! 3. Execute your computation and record its execution trace.
//! 4. Define your prover by implementing [Prover] trait. Then execute [Prover::prove()] function
//! passing the trace generated in the previous step into it as a parameter. The function will
//! return a instance of [StarkProof].
//!
//! This [StarkProof] can be serialized and sent to a STARK verifier for verification. The size
//! of proof depends on the specifics of a given computation, but for most computations it should
//! be in the range between 15 KB (for very small computations) and 300 KB (for very large
//! computations).
//!
//! Proof generation time is also highly dependent on the specifics of a given computation, but
//! also depends on the capabilities of the machine used to generate the proofs (i.e. on number
//! of CPU cores and memory bandwidth).
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;
pub use air::{
proof::StarkProof, Air, AirContext, Assertion, BoundaryConstraint, BoundaryConstraintGroup,
ConstraintCompositionCoefficients, ConstraintDivisor, DeepCompositionCoefficients,
EvaluationFrame, FieldExtension, HashFunction, ProofOptions, TraceInfo,
TransitionConstraintDegree, TransitionConstraintGroup,
};
pub use utils::{
iterators, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
};
use fri::FriProver;
use utils::collections::Vec;
pub use math;
use math::{
fft::infer_degree,
fields::{CubeExtension, QuadExtension},
ExtensibleField, FieldElement, StarkField,
};
pub use crypto;
use crypto::{
hashers::{Blake3_192, Blake3_256, Sha3_256},
ElementHasher,
};
#[cfg(feature = "std")]
use log::debug;
#[cfg(feature = "std")]
use math::log2;
#[cfg(feature = "std")]
use std::time::Instant;
mod domain;
use domain::StarkDomain;
mod constraints;
use constraints::{ConstraintCommitment, ConstraintEvaluator};
mod composer;
use composer::DeepCompositionPoly;
mod trace;
use trace::TracePolyTable;
pub use trace::{Trace, TraceTable, TraceTableFragment};
mod channel;
use channel::ProverChannel;
mod errors;
pub use errors::ProverError;
#[cfg(test)]
pub mod tests;
// PROVER
// ================================================================================================
/// Defines a STARK prover for a computation.
///
/// A STARK prover can be used to generate STARK proofs. The prover contains definitions of a
/// computation's AIR (specified via [Air](Prover::Air) associated type) and execution trace
/// (specified via [Trace](Prover::Trace) associated type), and exposes [prove()](Prover::prove)
/// method which can be used to build STARK proofs for provided execution traces.
///
/// Thus, once a prover is defined and instantiated, generating a STARK proof consists of two
/// steps:
/// 1. Build an execution trace for a specific instance of the computation.
/// 2. Invoke [Prover::prove()] method generate a proof using the trace from the previous step
/// as a witness.
///
/// The generated proof is built using protocol parameters defined by the [ProofOptions] struct
/// return from [Prover::options] method.
pub trait Prover {
/// Base field for the computation described by this prover.
type BaseField: StarkField + ExtensibleField<2> + ExtensibleField<3>;
/// Algebraic intermediate representation (AIR) for the computation described by this prover.
type Air: Air<BaseField = Self::BaseField>;
/// Execution trace of the computation described by this prover.
type Trace: Trace<BaseField = Self::BaseField>;
// REQUIRED METHODS
// --------------------------------------------------------------------------------------------
/// Returns a set of public inputs for an instance of the computation defined by the provided
/// trace.
///
/// Public inputs need to be shared with the verifier in order for them to verify a proof.
fn get_pub_inputs(&self, trace: &Self::Trace) -> <<Self as Prover>::Air as Air>::PublicInputs;
/// Returns [ProofOptions] which this prover uses to generate STARK proofs.
///
/// Proof options defines basic protocol parameters such as: number of queries, blowup factor,
/// grinding factor, hash function to be used in the protocol etc. These properties directly
/// inform such metrics as proof generation time, proof size, and proof security level.
fn options(&self) -> &ProofOptions;
// PROVIDED METHODS
// --------------------------------------------------------------------------------------------
/// Returns a STARK proof attesting to a correct execution of a computation defined by the
/// provided trace.
///
/// The returned [StarkProof] attests that the specified `trace` is a valid execution trace of
/// the computation described by [Self::Air](Prover::Air) and generated using some set of
/// secret and public inputs. Public inputs must match the value returned from
/// [Self::get_pub_inputs()](Prover::get_pub_inputs) for the provided trace.
#[rustfmt::skip]
fn prove(&self, trace: Self::Trace) -> Result<StarkProof, ProverError> {
// figure out which version of the generic proof generation procedure to run. this is a sort
// of static dispatch for selecting two generic parameter: extension field and hash function.
match self.options().field_extension() {
FieldExtension::None => match self.options().hash_fn() {
HashFunction::Blake3_256 => self.generate_proof::<Self::BaseField, Blake3_256<Self::BaseField>>(trace),
HashFunction::Blake3_192 => self.generate_proof::<Self::BaseField, Blake3_192<Self::BaseField>>(trace),
HashFunction::Sha3_256 => self.generate_proof::<Self::BaseField, Sha3_256<Self::BaseField>>(trace),
},
FieldExtension::Quadratic => {
if !<QuadExtension<Self::BaseField>>::is_supported() {
return Err(ProverError::UnsupportedFieldExtension(2));
}
match self.options().hash_fn() {
HashFunction::Blake3_256 => self.generate_proof::<QuadExtension<Self::BaseField>, Blake3_256<Self::BaseField>>(trace),
HashFunction::Blake3_192 => self.generate_proof::<QuadExtension<Self::BaseField>, Blake3_192<Self::BaseField>>(trace),
HashFunction::Sha3_256 => self.generate_proof::<QuadExtension<Self::BaseField>, Sha3_256<Self::BaseField>>(trace),
}
}
FieldExtension::Cubic => {
if !<CubeExtension<Self::BaseField>>::is_supported() {
return Err(ProverError::UnsupportedFieldExtension(3));
}
match self.options().hash_fn() {
HashFunction::Blake3_256 => self.generate_proof::<CubeExtension<Self::BaseField>, Blake3_256<Self::BaseField>>(trace),
HashFunction::Blake3_192 => self.generate_proof::<CubeExtension<Self::BaseField>, Blake3_192<Self::BaseField>>(trace),
HashFunction::Sha3_256 => self.generate_proof::<CubeExtension<Self::BaseField>, Sha3_256<Self::BaseField>>(trace),
}
}
}
}
// HELPER METHODS
// --------------------------------------------------------------------------------------------
/// Performs the actual proof generation procedure, generating the proof that the provided
/// execution `trace` is valid against this prover's AIR.
/// TODO: make this function un-callable externally?
#[doc(hidden)]
fn generate_proof<E, H>(&self, trace: Self::Trace) -> Result<StarkProof, ProverError>
where
E: FieldElement<BaseField = Self::BaseField>,
H: ElementHasher<BaseField = Self::BaseField>,
{
// 0 ----- instantiate AIR and prover channel ---------------------------------------------
// serialize public inputs; these will be included in the seed for the public coin
let pub_inputs = self.get_pub_inputs(&trace);
let mut pub_inputs_bytes = Vec::new();
pub_inputs.write_into(&mut pub_inputs_bytes);
// create an instance of AIR for the provided parameters. this takes a generic description
// of the computation (provided via AIR type), and creates a description of a specific
// execution of the computation for the provided public inputs.
let air = Self::Air::new(trace.get_info(), pub_inputs, self.options().clone());
// make sure the specified trace is valid against the AIR. This checks validity of both,
// assertions and state transitions. we do this in debug mode only because this is a very
// expensive operation.
#[cfg(debug_assertions)]
trace.validate(&air);
// create a channel which is used to simulate interaction between the prover and the
// verifier; the channel will be used to commit to values and to draw randomness that
// should come from the verifier.
let mut channel = ProverChannel::<Self::Air, E, H>::new(&air, pub_inputs_bytes);
// 1 ----- extend execution trace ---------------------------------------------------------
// build computation domain; this is used later for polynomial evaluations
#[cfg(feature = "std")]
let now = Instant::now();
let domain = StarkDomain::new(&air);
#[cfg(feature = "std")]
debug!(
"Built domain of 2^{} elements in {} ms",
log2(domain.lde_domain_size()),
now.elapsed().as_millis()
);
// extend the execution trace; this interpolates each register of the trace into a
// polynomial, and then evaluates the polynomial over the LDE domain; each of the trace
// polynomials has degree = trace_length - 1
let (extended_trace, trace_polys) = trace.extend(&domain);
#[cfg(feature = "std")]
debug!(
"Extended execution trace of {} registers from 2^{} to 2^{} steps ({}x blowup) in {} ms",
extended_trace.width(),
log2(trace_polys.poly_size()),
log2(extended_trace.len()),
extended_trace.blowup(),
now.elapsed().as_millis()
);
// 2 ----- commit to the extended execution trace -----------------------------------------
#[cfg(feature = "std")]
let now = Instant::now();
let trace_tree = extended_trace.build_commitment::<H>();
channel.commit_trace(*trace_tree.root());
#[cfg(feature = "std")]
debug!(
"Committed to extended execution trace by building a Merkle tree of depth {} in {} ms",
trace_tree.depth(),
now.elapsed().as_millis()
);
// 3 ----- evaluate constraints -----------------------------------------------------------
// evaluate constraints specified by the AIR over the constraint evaluation domain, and
// compute random linear combinations of these evaluations using coefficients drawn from
// the channel; this step evaluates only constraint numerators, thus, only constraints with
// identical denominators are merged together. the results are saved into a constraint
// evaluation table where each column contains merged evaluations of constraints with
// identical denominators.
#[cfg(feature = "std")]
let now = Instant::now();
let constraint_coeffs = channel.get_constraint_composition_coeffs();
let evaluator = ConstraintEvaluator::new(&air, constraint_coeffs);
let constraint_evaluations = evaluator.evaluate(&extended_trace, &domain);
#[cfg(feature = "std")]
debug!(
"Evaluated constraints over domain of 2^{} elements in {} ms",
log2(constraint_evaluations.num_rows()),
now.elapsed().as_millis()
);
// 4 ----- commit to constraint evaluations -----------------------------------------------
// first, build constraint composition polynomial from the constraint evaluation table:
// - divide all constraint evaluation columns by their respective divisors
// - combine them into a single column of evaluations,
// - interpolate the column into a polynomial in coefficient form
// - "break" the polynomial into a set of column polynomials each of degree equal to
// trace_length - 1
#[cfg(feature = "std")]
let now = Instant::now();
let composition_poly = constraint_evaluations.into_poly()?;
#[cfg(feature = "std")]
debug!(
"Converted constraint evaluations into {} composition polynomial columns of degree {} in {} ms",
composition_poly.num_columns(),
composition_poly.column_degree(),
now.elapsed().as_millis()
);
// then, evaluate composition polynomial columns over the LDE domain
#[cfg(feature = "std")]
let now = Instant::now();
let composed_evaluations = composition_poly.evaluate(&domain);
#[cfg(feature = "std")]
debug!(
"Evaluated composition polynomial columns over LDE domain (2^{} elements) in {} ms",
log2(composed_evaluations[0].len()),
now.elapsed().as_millis()
);
// finally, commit to the composition polynomial evaluations
#[cfg(feature = "std")]
let now = Instant::now();
let constraint_commitment = ConstraintCommitment::<E, H>::new(composed_evaluations);
channel.commit_constraints(constraint_commitment.root());
#[cfg(feature = "std")]
debug!(
"Committed to composed evaluations by building a Merkle tree of depth {} in {} ms",
constraint_commitment.tree_depth(),
now.elapsed().as_millis()
);
// 5 ----- build DEEP composition polynomial ----------------------------------------------
#[cfg(feature = "std")]
let now = Instant::now();
// draw an out-of-domain point z. Depending on the type of E, the point is drawn either
// from the base field or from an extension field defined by E.
//
// The purpose of sampling from the extension field here (instead of the base field) is to
// increase security. Soundness is limited by the size of the field that the random point
// is drawn from, and we can potentially save on performance by only drawing this point
// from an extension field, rather than increasing the size of the field overall.
let z = channel.get_ood_point();
// evaluate trace and constraint polynomials at the OOD point z, and send the results to
// the verifier. the trace polynomials are actually evaluated over two points: z and z * g,
// where g is the generator of the trace domain.
let ood_frame = trace_polys.get_ood_frame(z);
channel.send_ood_evaluation_frame(&ood_frame);
let ood_evaluations = composition_poly.evaluate_at(z);
channel.send_ood_constraint_evaluations(&ood_evaluations);
// draw random coefficients to use during DEEP polynomial composition, and use them to
// initialize the DEEP composition polynomial
let deep_coefficients = channel.get_deep_composition_coeffs();
let mut deep_composition_poly = DeepCompositionPoly::new(&air, z, deep_coefficients);
// combine all trace polynomials together and merge them into the DEEP composition
// polynomial
deep_composition_poly.add_trace_polys(trace_polys, ood_frame);
// merge columns of constraint composition polynomial into the DEEP composition polynomial;
deep_composition_poly.add_composition_poly(composition_poly, ood_evaluations);
// raise the degree of the DEEP composition polynomial by one to make sure it is equal to
// trace_length - 1
deep_composition_poly.adjust_degree();
#[cfg(feature = "std")]
debug!(
"Built DEEP composition polynomial of degree {} in {} ms",
deep_composition_poly.degree(),
now.elapsed().as_millis()
);
// make sure the degree of the DEEP composition polynomial is equal to trace polynomial
// degree
assert_eq!(domain.trace_length() - 1, deep_composition_poly.degree());
// 6 ----- evaluate DEEP composition polynomial over LDE domain ---------------------------
#[cfg(feature = "std")]
let now = Instant::now();
let deep_evaluations = deep_composition_poly.evaluate(&domain);
// we check the following condition in debug mode only because infer_degree is an expensive
// operation
debug_assert_eq!(
domain.trace_length() - 1,
infer_degree(&deep_evaluations, domain.offset())
);
#[cfg(feature = "std")]
debug!(
"Evaluated DEEP composition polynomial over LDE domain (2^{} elements) in {} ms",
log2(domain.lde_domain_size()),
now.elapsed().as_millis()
);
// 7 ----- compute FRI layers for the composition polynomial ------------------------------
#[cfg(feature = "std")]
let now = Instant::now();
let mut fri_prover = FriProver::new(air.options().to_fri_options());
fri_prover.build_layers(&mut channel, deep_evaluations);
#[cfg(feature = "std")]
debug!(
"Computed {} FRI layers from composition polynomial evaluations in {} ms",
fri_prover.num_layers(),
now.elapsed().as_millis()
);
// 8 ----- determine query positions ------------------------------------------------------
#[cfg(feature = "std")]
let now = Instant::now();
// apply proof-of-work to the query seed
channel.grind_query_seed();
// generate pseudo-random query positions
let query_positions = channel.get_query_positions();
#[cfg(feature = "std")]
debug!(
"Determined {} query positions in {} ms",
query_positions.len(),
now.elapsed().as_millis()
);
// 9 ----- build proof object -------------------------------------------------------------
#[cfg(feature = "std")]
let now = Instant::now();
// generate FRI proof
let fri_proof = fri_prover.build_proof(&query_positions);
// query the execution trace at the selected position; for each query, we need the
// state of the trace at that position + Merkle authentication path
let trace_queries = extended_trace.query(trace_tree, &query_positions);
// query the constraint commitment at the selected positions; for each query, we need just
// a Merkle authentication path. this is because constraint evaluations for each step are
// merged into a single value and Merkle authentication paths contain these values already
let constraint_queries = constraint_commitment.query(&query_positions);
// build the proof object
let proof = channel.build_proof(trace_queries, constraint_queries, fri_proof);
#[cfg(feature = "std")]
debug!("Built proof object in {} ms", now.elapsed().as_millis());
Ok(proof)
}
}
