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
// 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.
//! Contains STARK proof struct and associated components.
use crate::{ProofOptions, TraceInfo, TraceLayout};
use core::cmp;
use crypto::Hasher;
use fri::FriProof;
use math::FieldElement;
use utils::{
collections::Vec, ByteReader, Deserializable, DeserializationError, Serializable, SliceReader,
};
mod context;
pub use context::Context;
mod commitments;
pub use commitments::Commitments;
mod queries;
pub use queries::Queries;
mod ood_frame;
pub use ood_frame::OodFrame;
mod table;
pub use table::Table;
#[cfg(test)]
mod tests;
// CONSTANTS
// ================================================================================================
const GRINDING_CONTRIBUTION_FLOOR: u32 = 80;
const MAX_PROXIMITY_PARAMETER: u64 = 1000;
// STARK PROOF
// ================================================================================================
/// A proof generated by Winterfell prover.
///
/// A STARK proof contains information proving that a computation was executed correctly. A proof
/// also contains basic metadata for the computation, but neither the definition of the computation
/// itself, nor public inputs consumed by the computation are contained in a proof.
///
/// A proof can be serialized into a sequence of bytes using [to_bytes()](StarkProof::to_bytes)
/// function, and deserialized from a sequence of bytes using [from_bytes()](StarkProof::from_bytes)
/// function.
///
/// To estimate soundness of a proof (in bits), [security_level()](StarkProof::security_level)
/// function can be used.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct StarkProof {
/// Basic metadata about the execution of the computation described by this proof.
pub context: Context,
/// Number of unique queries made by the verifier. This will be different from the
/// context.options.num_queries if the same position in the domain was queried more than once.
pub num_unique_queries: u8,
/// Commitments made by the prover during the commit phase of the protocol.
pub commitments: Commitments,
/// Decommitments of extended execution trace values (for all trace segments) at position
/// queried by the verifier.
pub trace_queries: Vec<Queries>,
/// Decommitments of constraint composition polynomial evaluations at positions queried by
/// the verifier.
pub constraint_queries: Queries,
/// Trace and constraint polynomial evaluations at an out-of-domain point.
pub ood_frame: OodFrame,
/// Low-degree proof for a DEEP composition polynomial.
pub fri_proof: FriProof,
/// Proof-of-work nonce for query seed grinding.
pub pow_nonce: u64,
}
impl StarkProof {
/// Returns STARK protocol parameters used to generate this proof.
pub fn options(&self) -> &ProofOptions {
self.context.options()
}
/// Returns a layout describing how columns of the execution trace described by this context
/// are arranged into segments.
pub fn trace_layout(&self) -> &TraceLayout {
self.context.trace_layout()
}
/// Returns trace length for the computation described by this proof.
pub fn trace_length(&self) -> usize {
self.context.trace_length()
}
/// Returns trace info for the computation described by this proof.
pub fn get_trace_info(&self) -> TraceInfo {
self.context.get_trace_info()
}
/// Returns the size of the LDE domain for the computation described by this proof.
pub fn lde_domain_size(&self) -> usize {
self.context.lde_domain_size()
}
// SECURITY LEVEL
// --------------------------------------------------------------------------------------------
/// Returns security level of this proof (in bits).
///
/// When `conjectured` is true, conjectured security level is returned; otherwise, provable
/// security level is returned. Usually, the number of queries needed for provable security is
/// 2x - 3x higher than the number of queries needed for conjectured security at the same
/// security level.
pub fn security_level<H: Hasher>(&self, conjectured: bool) -> u32 {
if conjectured {
get_conjectured_security(
self.context.options(),
self.context.num_modulus_bits(),
self.trace_length(),
H::COLLISION_RESISTANCE,
)
} else {
get_proven_security(
self.context.options(),
self.context.num_modulus_bits(),
self.trace_length(),
H::COLLISION_RESISTANCE,
)
}
}
// SERIALIZATION / DESERIALIZATION
// --------------------------------------------------------------------------------------------
/// Serializes this proof into a vector of bytes.
pub fn to_bytes(&self) -> Vec<u8> {
Serializable::to_bytes(self)
}
/// Returns a STARK proof read from the specified `source`.
///
/// # Errors
/// Returns an error of a valid STARK proof could not be read from the specified `source`.
pub fn from_bytes(source: &[u8]) -> Result<Self, DeserializationError> {
Deserializable::read_from_bytes(source)
}
/// Creates a dummy `StarkProof` for use in tests.
pub fn new_dummy() -> Self {
use crate::FieldExtension;
use crypto::hashers::Blake3_192 as DummyHasher;
use crypto::BatchMerkleProof;
use math::fields::f64::BaseElement as DummyField;
Self {
context: Context::new::<DummyField>(
&TraceInfo::new(1, 8),
ProofOptions::new(1, 2, 2, FieldExtension::None, 8, 1),
),
num_unique_queries: 0,
commitments: Commitments::default(),
trace_queries: Vec::new(),
constraint_queries: Queries::new::<_, DummyField>(
BatchMerkleProof::<DummyHasher<DummyField>> {
leaves: Vec::new(),
nodes: Vec::new(),
depth: 0,
},
vec![vec![DummyField::ONE]],
),
ood_frame: OodFrame::default(),
fri_proof: FriProof::new_dummy(),
pow_nonce: 0,
}
}
}
// SERIALIZATION
// ================================================================================================
impl Serializable for StarkProof {
fn write_into<W: utils::ByteWriter>(&self, target: &mut W) {
self.context.write_into(target);
target.write_u8(self.num_unique_queries);
self.commitments.write_into(target);
self.trace_queries.write_into(target);
self.constraint_queries.write_into(target);
self.ood_frame.write_into(target);
self.fri_proof.write_into(target);
self.pow_nonce.write_into(target)
}
}
impl Deserializable for StarkProof {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let context = Context::read_from(source)?;
let num_unique_queries = source.read_u8()?;
let commitments = Commitments::read_from(source)?;
let num_trace_segments = context.trace_layout().num_segments();
let mut trace_queries = Vec::with_capacity(num_trace_segments);
for _ in 0..num_trace_segments {
trace_queries.push(Queries::read_from(source)?);
}
let proof = StarkProof {
context,
num_unique_queries,
commitments,
trace_queries,
constraint_queries: Queries::read_from(source)?,
ood_frame: OodFrame::read_from(source)?,
fri_proof: FriProof::read_from(source)?,
pow_nonce: source.read_u64()?,
};
if source.has_more_bytes() {
return Err(DeserializationError::UnconsumedBytes);
}
Ok(proof)
}
}
// HELPER FUNCTIONS
// ================================================================================================
/// Computes conjectured security level for the specified proof parameters.
fn get_conjectured_security(
options: &ProofOptions,
base_field_bits: u32,
trace_domain_size: usize,
collision_resistance: u32,
) -> u32 {
// compute max security we can get for a given field size
let field_size = base_field_bits * options.field_extension().degree();
let field_security = field_size - (trace_domain_size * options.blowup_factor()).ilog2();
// compute security we get by executing multiple query rounds
let security_per_query = options.blowup_factor().ilog2();
let mut query_security = security_per_query * options.num_queries() as u32;
// include grinding factor contributions only for proofs adequate security
if query_security >= GRINDING_CONTRIBUTION_FLOOR {
query_security += options.grinding_factor();
}
cmp::min(cmp::min(field_security, query_security) - 1, collision_resistance)
}
/// Estimates proven security level for the specified proof parameters.
fn get_proven_security(
options: &ProofOptions,
base_field_bits: u32,
trace_domain_size: usize,
collision_resistance: u32,
) -> u32 {
let m_min: usize = 3;
let m_max = compute_upper_m(trace_domain_size);
let m_optimal = (m_min as u32..m_max as u32)
.max_by_key(|&a| {
proven_security_protocol_for_m(
options,
base_field_bits,
trace_domain_size,
a as usize,
)
})
.expect(
"Should not fail since m_max is larger than m_min for all trace sizes of length greater than 4",
);
cmp::min(
proven_security_protocol_for_m(
options,
base_field_bits,
trace_domain_size,
m_optimal as usize,
),
collision_resistance as u64,
) as u32
}
/// Computes proven security level for the specified proof parameters for a fixed
/// value of the proximity parameter m in the list-decoding regime.
fn proven_security_protocol_for_m(
options: &ProofOptions,
base_field_bits: u32,
trace_domain_size: usize,
m: usize,
) -> u64 {
let extension_field_bits = (base_field_bits * options.field_extension().degree()) as f64;
let num_fri_queries = options.num_queries() as f64;
let m = m as f64;
let rho = 1.0 / options.blowup_factor() as f64;
let alpha = (1.0 + 0.5 / m) * sqrt(rho);
let theta = 1.0 - alpha;
let max_deg = options.blowup_factor() as f64;
let lde_domain_size = (trace_domain_size * options.blowup_factor()) as f64;
let trace_domain_size = trace_domain_size as f64;
// Computes FRI commit-phase (i.e., pre-query) soundness error.
// This considers only the first term given in eq. 7 in https://eprint.iacr.org/2022/1216.pdf,
// i.e. 0.5 * (m + 0.5)^7 * n^2 / (rho^1.5.q) as all other terms are negligible in comparison.
let fri_commit_err_bits = extension_field_bits
- log2((0.5 * powf(m + 0.5, 7.0) / powf(rho, 1.5)) * powf(lde_domain_size, 2.0));
// Compute FRI query-phase soundness error
let fri_queries_err_bits =
options.grinding_factor() as f64 - log2(powf(1.0 - theta, num_fri_queries));
// Combined error for FRI
let fri_err_bits = cmp::min(fri_commit_err_bits as u64, fri_queries_err_bits as u64);
if fri_err_bits < 1 {
return 0;
}
let fri_err_bits = fri_err_bits - 1;
// To apply Theorem 8 in https://eprint.iacr.org/2022/1216.pdf, we need to apply FRI with
// a slightly larger agreement parameter alpha.
// More concretely, we need alpha > rho_plus.sqrt() where rho_plus is the rate in function field
// F(Z) and defined as (trace_domain_size + 2.0) / lde_domain_size .
// This means that the range of m needs to be restricted in order to ensure that
// alpha := 1 - theta := rho.sqrt() * (1 + 1/2m) is greater than rho_plus.sqrt().
// Determining the range of m is the responsibility of the calling function.
// Now, once m is fixed, we need to make sure that we choose an m_plus such that
// alpha <= rho_plus.sqrt() * (1 + 1/2m_plus). This m_plus will be used to define
// the list-decoding list size in F(Z).
// Modified rate in function field F(Z)
let rho_plus = (trace_domain_size + 2.0) / lde_domain_size;
// New proximity parameter m_plus, corresponding to rho_plus, needed to make sure that
// alpha < rho_plus.sqrt() * (1 + 1 / (2 * m_plus))
let m_plus = ceil(1.0 / (2.0 * (alpha / sqrt(rho_plus) - 1.0)));
// List size
let l_plus = (2.0 * m_plus + 1.0) / (2.0 * sqrt(rho_plus));
// ALI related soundness error. Note that C here is equal to 1 because of our use of
// linear batching.
let ali_err_bits = -log2(l_plus) + extension_field_bits;
// DEEP related soundness error. Note that this uses that the denominator |F| - |D ∪ H|
// can be approximated by |F| for all practical domain sizes. We also use the blow-up factor
// as an upper bound for the maximal constraint degree.
let deep_err_bits =
-log2(l_plus * (max_deg * (trace_domain_size + 1.0) + (trace_domain_size - 1.0)))
+ extension_field_bits;
let min = cmp::min(cmp::min(fri_err_bits, ali_err_bits as u64), deep_err_bits as u64);
if min < 1 {
return 0;
}
min - 1
}
// HELPER FUNCTIONS
// ================================================================================================
/// Computes the largest proximity parameter m needed for Theorem 8
/// in <https://eprint.iacr.org/2022/1216.pdf> to work.
fn compute_upper_m(h: usize) -> f64 {
let h = h as f64;
let m_max = ceil(0.25 * h * (1.0 + sqrt(1.0 + 2.0 / h)));
// We cap the range to 1000 as the optimal m value will be in the lower range of [m_min, m_max]
// since increasing m too much will lead to a deterioration in the FRI commit soundness making
// any benefit gained in the FRI query soundess mute.
cmp::min(m_max as u64, MAX_PROXIMITY_PARAMETER) as f64
}
#[cfg(feature = "std")]
pub fn log2(value: f64) -> f64 {
value.log2()
}
#[cfg(not(feature = "std"))]
pub fn log2(value: f64) -> f64 {
libm::log2(value)
}
#[cfg(feature = "std")]
pub fn sqrt(value: f64) -> f64 {
value.sqrt()
}
#[cfg(not(feature = "std"))]
pub fn sqrt(value: f64) -> f64 {
libm::sqrt(value)
}
#[cfg(feature = "std")]
pub fn powf(value: f64, exp: f64) -> f64 {
value.powf(exp)
}
#[cfg(not(feature = "std"))]
pub fn powf(value: f64, exp: f64) -> f64 {
libm::pow(value, exp)
}
#[cfg(feature = "std")]
pub fn ceil(value: f64) -> f64 {
value.ceil()
}
#[cfg(not(feature = "std"))]
pub fn ceil(value: f64) -> f64 {
libm::ceil(value)
}