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
//! A FRI-based STARK implementation over the Goldilocks field, with support
//! for recursive proof verification through the plonky2 SNARK backend.
//!
//! This library is intended to provide all the necessary tools to prove,
//! verify, and recursively verify STARK statements. While the library
//! is tailored for a system with a single STARK, it also is flexible
//! enough to support a multi-STARK system, i.e. a system of independent
//! STARK statements possibly sharing common values. See section below for
//! more information on how to define such a system.
//!
//!
//! # Defining a STARK statement
//!
//! A STARK system is configured by a [`StarkConfig`][crate::config::StarkConfig]
//! defining all the parameters to be used when generating proofs associated
//! to the statement. How constraints should be defined over the STARK trace is
//! defined through the [`Stark`][crate::stark::Stark] trait, that takes a
//! [`StarkEvaluationFrame`][crate::evaluation_frame::StarkEvaluationFrame] of
//! two consecutive rows and a list of public inputs.
//!
//! ### Example: Fibonacci sequence
//!
//! To build a STARK for the modified Fibonacci sequence starting with two
//! user-provided values `x0` and `x1`, one can do the following:
//!
//! ```rust
//! # use core::marker::PhantomData;
//! // Imports all basic types.
//! use plonky2::field::extension::{Extendable, FieldExtension};
//! use plonky2::field::packed::PackedField;
//! use plonky2::field::polynomial::PolynomialValues;
//! use plonky2::hash::hash_types::RichField;
//! # use starky::util::trace_rows_to_poly_values;
//!
//! // Imports to define the constraints of our STARK.
//! use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
//! use starky::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
//! use starky::stark::Stark;
//!
//! // Imports to define the recursive constraints of our STARK.
//! use plonky2::iop::ext_target::ExtensionTarget;
//! use plonky2::plonk::circuit_builder::CircuitBuilder;
//!
//! pub struct FibonacciStark<F: RichField + Extendable<D>, const D: usize> {
//! num_rows: usize,
//! _phantom: PhantomData<F>,
//! }
//!
//! // Define witness generation.
//! impl<F: RichField + Extendable<D>, const D: usize> FibonacciStark<F, D> {
//! // The first public input is `x0`.
//! const PI_INDEX_X0: usize = 0;
//! // The second public input is `x1`.
//! const PI_INDEX_X1: usize = 1;
//! // The third public input is the second element of the last row,
//! // which should be equal to the `num_rows`-th Fibonacci number.
//! const PI_INDEX_RES: usize = 2;
//!
//! /// Generate the trace using `x0, x1, 0` as initial state values.
//! fn generate_trace(&self, x0: F, x1: F) -> Vec<PolynomialValues<F>> {
//! let mut trace_rows = (0..self.num_rows)
//! .scan([x0, x1, F::ZERO], |acc, _| {
//! let tmp = *acc;
//! acc[0] = tmp[1];
//! acc[1] = tmp[0] + tmp[1];
//! acc[2] = tmp[2] + F::ONE;
//! Some(tmp)
//! })
//! .collect::<Vec<_>>();
//!
//! // Transpose the row-wise trace for the prover.
//! trace_rows_to_poly_values(trace_rows)
//! }
//! }
//!
//! // Define constraints.
//! const COLUMNS: usize = 3;
//! const PUBLIC_INPUTS: usize = 3;
//!
//! impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for FibonacciStark<F, D> {
//! type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, P::Scalar, COLUMNS, PUBLIC_INPUTS>
//! where
//! FE: FieldExtension<D2, BaseField = F>,
//! P: PackedField<Scalar = FE>;
//!
//! type EvaluationFrameTarget =
//! StarkFrame<ExtensionTarget<D>, ExtensionTarget<D>, COLUMNS, PUBLIC_INPUTS>;
//!
//! // Define this STARK's constraints.
//! fn eval_packed_generic<FE, P, const D2: usize>(
//! &self,
//! vars: &Self::EvaluationFrame<FE, P, D2>,
//! yield_constr: &mut ConstraintConsumer<P>,
//! ) where
//! FE: FieldExtension<D2, BaseField = F>,
//! P: PackedField<Scalar = FE>,
//! {
//! let local_values = vars.get_local_values();
//! let next_values = vars.get_next_values();
//! let public_inputs = vars.get_public_inputs();
//!
//! // Check public inputs.
//! yield_constr.constraint_first_row(local_values[0] - public_inputs[Self::PI_INDEX_X0]);
//! yield_constr.constraint_first_row(local_values[1] - public_inputs[Self::PI_INDEX_X1]);
//! yield_constr.constraint_last_row(local_values[1] - public_inputs[Self::PI_INDEX_RES]);
//!
//! // Enforce the Fibonacci transition constraints.
//! // x0' <- x1
//! yield_constr.constraint_transition(next_values[0] - local_values[1]);
//! // x1' <- x0 + x1
//! yield_constr.constraint_transition(next_values[1] - local_values[0] - local_values[1]);
//! }
//!
//! // Define the constraints to recursively verify this STARK.
//! fn eval_ext_circuit(
//! &self,
//! builder: &mut CircuitBuilder<F, D>,
//! vars: &Self::EvaluationFrameTarget,
//! yield_constr: &mut RecursiveConstraintConsumer<F, D>,
//! ) {
//! let local_values = vars.get_local_values();
//! let next_values = vars.get_next_values();
//! let public_inputs = vars.get_public_inputs();
//!
//! // Check public inputs.
//! let pis_constraints = [
//! builder.sub_extension(local_values[0], public_inputs[Self::PI_INDEX_X0]),
//! builder.sub_extension(local_values[1], public_inputs[Self::PI_INDEX_X1]),
//! builder.sub_extension(local_values[1], public_inputs[Self::PI_INDEX_RES]),
//! ];
//!
//! yield_constr.constraint_first_row(builder, pis_constraints[0]);
//! yield_constr.constraint_first_row(builder, pis_constraints[1]);
//! yield_constr.constraint_last_row(builder, pis_constraints[2]);
//!
//! // Enforce the Fibonacci transition constraints.
//! // x0' <- x1
//! let first_col_constraint = builder.sub_extension(next_values[0], local_values[1]);
//! yield_constr.constraint_transition(builder, first_col_constraint);
//! // x1' <- x0 + x1
//! let second_col_constraint = {
//! let tmp = builder.sub_extension(next_values[1], local_values[0]);
//! builder.sub_extension(tmp, local_values[1])
//! };
//! yield_constr.constraint_transition(builder, second_col_constraint);
//! }
//!
//! fn constraint_degree(&self) -> usize {
//! 2
//! }
//! }
//! ```
//!
//! One can then instantiate a new `FibonacciStark` instance, generate an associated
//! STARK trace, and generate a proof for it.
//!
//! ```rust
//! # use anyhow::Result;
//! # use core::marker::PhantomData;
//! # // Imports all basic types.
//! # use plonky2::field::extension::{Extendable, FieldExtension};
//! # use plonky2::field::types::Field;
//! # use plonky2::field::packed::PackedField;
//! # use plonky2::field::polynomial::PolynomialValues;
//! # use plonky2::hash::hash_types::RichField;
//! # use starky::util::trace_rows_to_poly_values;
//! # // Imports to define the constraints of our STARK.
//! # use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
//! # use starky::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
//! # use starky::stark::Stark;
//! # // Imports to define the recursive constraints of our STARK.
//! # use plonky2::iop::ext_target::ExtensionTarget;
//! # use plonky2::plonk::circuit_builder::CircuitBuilder;
//! # use plonky2::util::timing::TimingTree;
//! # use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
//! # use starky::prover::prove;
//! # use starky::verifier::verify_stark_proof;
//! # use starky::config::StarkConfig;
//! #
//! # #[derive(Copy, Clone)]
//! # pub struct FibonacciStark<F: RichField + Extendable<D>, const D: usize> {
//! # num_rows: usize,
//! # _phantom: PhantomData<F>,
//! # }
//! # // Define witness generation.
//! # impl<F: RichField + Extendable<D>, const D: usize> FibonacciStark<F, D> {
//! # // The first public input is `x0`.
//! # const PI_INDEX_X0: usize = 0;
//! # // The second public input is `x1`.
//! # const PI_INDEX_X1: usize = 1;
//! # // The third public input is the second element of the last row,
//! # // which should be equal to the `num_rows`-th Fibonacci number.
//! # const PI_INDEX_RES: usize = 2;
//! # /// Generate the trace using `x0, x1, 0` as initial state values.
//! # fn generate_trace(&self, x0: F, x1: F) -> Vec<PolynomialValues<F>> {
//! # let mut trace_rows = (0..self.num_rows)
//! # .scan([x0, x1, F::ZERO], |acc, _| {
//! # let tmp = *acc;
//! # acc[0] = tmp[1];
//! # acc[1] = tmp[0] + tmp[1];
//! # acc[2] = tmp[2] + F::ONE;
//! # Some(tmp)
//! # })
//! # .collect::<Vec<_>>();
//! # // Transpose the row-wise trace for the prover.
//! # trace_rows_to_poly_values(trace_rows)
//! # }
//! # const fn new(num_rows: usize) -> Self {
//! # Self {
//! # num_rows,
//! # _phantom: PhantomData,
//! # }
//! # }
//! # }
//! # // Define constraints.
//! # const COLUMNS: usize = 3;
//! # const PUBLIC_INPUTS: usize = 3;
//! # impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for FibonacciStark<F, D> {
//! # type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, P::Scalar, COLUMNS, PUBLIC_INPUTS>
//! # where
//! # FE: FieldExtension<D2, BaseField = F>,
//! # P: PackedField<Scalar = FE>;
//! # type EvaluationFrameTarget =
//! # StarkFrame<ExtensionTarget<D>, ExtensionTarget<D>, COLUMNS, PUBLIC_INPUTS>;
//! # // Define this STARK's constraints.
//! # fn eval_packed_generic<FE, P, const D2: usize>(
//! # &self,
//! # vars: &Self::EvaluationFrame<FE, P, D2>,
//! # yield_constr: &mut ConstraintConsumer<P>,
//! # ) where
//! # FE: FieldExtension<D2, BaseField = F>,
//! # P: PackedField<Scalar = FE>,
//! # {
//! # let local_values = vars.get_local_values();
//! # let next_values = vars.get_next_values();
//! # let public_inputs = vars.get_public_inputs();
//! # // Check public inputs.
//! # yield_constr.constraint_first_row(local_values[0] - public_inputs[Self::PI_INDEX_X0]);
//! # yield_constr.constraint_first_row(local_values[1] - public_inputs[Self::PI_INDEX_X1]);
//! # yield_constr.constraint_last_row(local_values[1] - public_inputs[Self::PI_INDEX_RES]);
//! # // Enforce the Fibonacci transition constraints.
//! # // x0' <- x1
//! # yield_constr.constraint_transition(next_values[0] - local_values[1]);
//! # // x1' <- x0 + x1
//! # yield_constr.constraint_transition(next_values[1] - local_values[0] - local_values[1]);
//! # }
//! # // Define the constraints to recursively verify this STARK.
//! # fn eval_ext_circuit(
//! # &self,
//! # builder: &mut CircuitBuilder<F, D>,
//! # vars: &Self::EvaluationFrameTarget,
//! # yield_constr: &mut RecursiveConstraintConsumer<F, D>,
//! # ) {
//! # let local_values = vars.get_local_values();
//! # let next_values = vars.get_next_values();
//! # let public_inputs = vars.get_public_inputs();
//! # // Check public inputs.
//! # let pis_constraints = [
//! # builder.sub_extension(local_values[0], public_inputs[Self::PI_INDEX_X0]),
//! # builder.sub_extension(local_values[1], public_inputs[Self::PI_INDEX_X1]),
//! # builder.sub_extension(local_values[1], public_inputs[Self::PI_INDEX_RES]),
//! # ];
//! # yield_constr.constraint_first_row(builder, pis_constraints[0]);
//! # yield_constr.constraint_first_row(builder, pis_constraints[1]);
//! # yield_constr.constraint_last_row(builder, pis_constraints[2]);
//! # // Enforce the Fibonacci transition constraints.
//! # // x0' <- x1
//! # let first_col_constraint = builder.sub_extension(next_values[0], local_values[1]);
//! # yield_constr.constraint_transition(builder, first_col_constraint);
//! # // x1' <- x0 + x1
//! # let second_col_constraint = {
//! # let tmp = builder.sub_extension(next_values[1], local_values[0]);
//! # builder.sub_extension(tmp, local_values[1])
//! # };
//! # yield_constr.constraint_transition(builder, second_col_constraint);
//! # }
//! # fn constraint_degree(&self) -> usize {
//! # 2
//! # }
//! # }
//! # fn fibonacci<F: Field>(n: usize, x0: F, x1: F) -> F {
//! # (0..n).fold((x0, x1), |x, _| (x.1, x.0 + x.1)).1
//! # }
//! #
//! const D: usize = 2;
//! const CONFIG: StarkConfig = StarkConfig::standard_fast_config();
//! type C = PoseidonGoldilocksConfig;
//! type F = <C as GenericConfig<D>>::F;
//! type S = FibonacciStark<F, D>;
//!
//! fn main() {
//! let num_rows = 1 << 10;
//! let x0 = F::from_canonical_u32(2);
//! let x1 = F::from_canonical_u32(7);
//!
//! let public_inputs = [x0, x1, fibonacci(num_rows - 1, x0, x1)];
//! let stark = FibonacciStark::<F, D>::new(num_rows);
//! let trace = stark.generate_trace(public_inputs[0], public_inputs[1]);
//!
//! let proof = prove::<F, C, S, D>(
//! stark,
//! &CONFIG,
//! trace,
//! &public_inputs,
//! &mut TimingTree::default(),
//! ).expect("We should have a valid proof!");
//!
//! verify_stark_proof(stark, proof, &CONFIG)
//! .expect("We should be able to verify this proof!")
//! }
//! ```
//!
#![allow(clippy::too_many_arguments)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::type_complexity)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
extern crate alloc;
mod get_challenges;
pub mod config;
pub mod constraint_consumer;
pub mod cross_table_lookup;
pub mod evaluation_frame;
pub mod lookup;
pub mod proof;
pub mod prover;
pub mod recursive_verifier;
pub mod stark;
pub mod stark_testing;
pub mod util;
mod vanishing_poly;
pub mod verifier;
#[cfg(test)]
pub mod fibonacci_stark;
#[cfg(test)]
pub mod permutation_stark;
#[cfg(test)]
pub mod unconstrained_stark;