Source code for primitives.pol_map
"""
Polynomial mapping data structures.
This module provides faithful Python translations of the C++ mapping structures
from pil2-stark/src/starkpil/stark_info.hpp.
These structures describe how polynomials are organized in memory and how they
map to different stages of the STARK proof system.
"""
from dataclasses import dataclass, field
from enum import Enum
from typing import NamedTuple
# --- Polynomial Identification ---
[docs]
class PolynomialId(NamedTuple):
"""Identifies a polynomial in verification context.
Used as dict key for buffer-free polynomial access in the verifier.
The verifier parses proof data into dict[PolynomialId, FF3] where
values are vectorized across all query points.
Attributes:
type: 'cm' (committed), 'const' (constant), 'custom' (custom commit)
name: Polynomial name from starkinfo (e.g., 'a', 'gsum')
index: Array index for multi-instance polynomials (0 for scalars)
stage: Stage number (1+ for committed, 0 for constants)
"""
# --- Field Type Enum ---
[docs]
class FieldType(Enum):
"""Field element type for type-safe field discrimination."""
[docs]
FF = 1 # Base field (Goldilocks)
[docs]
FF3 = 3 # Cubic extension (Goldilocks3)
# C++: pil2-stark/src/starkpil/stark_info.hpp::PolMap (lines 93-106)
@dataclass
[docs]
class PolMap:
"""Maps a polynomial to its location in the proof system."""
[docs]
lengths: list[int] = field(default_factory=list)
@property
[docs]
def dim(self) -> int:
"""Backwards compatibility: returns 1 for FF, 3 for FF3."""
return self.field_type.value
# C++: pil2-stark/src/starkpil/stark_info.hpp::EvMap (lines 108-135)
@dataclass
[docs]
class EvMap:
"""Maps an evaluation point to its polynomial source.
Corresponds to C++ class EvMap in stark_info.hpp (lines 108-135).
Attributes:
type: Source type (cm=committed, const_=constant, custom=custom commit)
id: Polynomial ID within source type
row_offset: Row offset for evaluation (-1, 0, or 1). C++ name: "prime"
commit_id: Commitment ID (only for custom type)
opening_pos: Position in opening points array
"""
# C++: stark_info.hpp::EvMap::eType (lines 109-114)
[docs]
class Type(Enum):
"""Evaluation source type.
Corresponds to C++ enum eType in EvMap (lines 111-116).
"""
[docs]
cm = 0 # Committed polynomial
[docs]
const_ = 1 # Constant polynomial
[docs]
custom = 2 # Custom commit
[docs]
row_offset: int # C++: "prime" - row offset for evaluation point
# C++: stark_info.hpp::EvMap::setType (lines 124-134)
@staticmethod
[docs]
def type_from_string(s: str) -> 'EvMap.Type':
"""Convert string to Type enum.
Corresponds to C++ EvMap::setType() (lines 124-134).
Args:
s: Type string ("cm", "const", or "custom")
Returns:
Corresponding Type enum value
Raises:
ValueError: If string is not a valid type
"""
if s == "cm":
return EvMap.Type.cm
elif s == "const":
return EvMap.Type.const_
elif s == "custom":
return EvMap.Type.custom
else:
raise ValueError(f"EvMap: invalid type string: {s}")
# C++: No direct equivalent (challenge info embedded in StarkInfo)
@dataclass
[docs]
class ChallengeMap:
"""Maps a challenge to its derivation stage."""
@property
[docs]
def dim(self) -> int:
"""Backwards compatibility: returns 1 for FF, 3 for FF3."""
return self.field_type.value
# C++: pil2-stark/src/starkpil/stark_info.hpp::CustomCommits (lines 52-58)
@dataclass
[docs]
class CustomCommits:
"""Custom commitment configuration.
Corresponds to C++ class CustomCommits in stark_info.hpp (lines 52-58).
Attributes:
name: Custom commit name
stage_widths: Number of columns at each stage
public_values: Indices of public values used
"""
[docs]
stage_widths: list[int] = field(default_factory=list)
[docs]
public_values: list[int] = field(default_factory=list)
# C++: pil2-stark/src/starkpil/stark_info.hpp::Boundary (lines 60-66)
@dataclass
[docs]
class Boundary:
"""Constraint boundary specification.
Corresponds to C++ class Boundary in stark_info.hpp (lines 60-66).
Attributes:
name: Boundary name (e.g., "everyRow", "everyFrame")
offset_min: Minimum row offset (only for "everyFrame")
offset_max: Maximum row offset (only for "everyFrame")
"""