zorch.coding.reed_solomon¶
Reed-Solomon as a FoldableCode: low-degree extension via the native NTT.
encode reads the message as the message_len low-order coefficients of a
polynomial, zero-pads to block_len, and evaluates it on the order-block_len
two-adic subgroup (or a coset of it). The evaluation is frx.lax.ntt — the
XLA-native NTT — which lowers to one fused kernel and auto-decomposes extension
fields into prime-field NTTs.
There is deliberately no hand-rolled butterfly: a fnp butterfly would be
log(n) unfused kernels the compiler cannot recognize as an NTT. Reed-Solomon
hands its evaluation to the native op, the way poseidon2 hands its algebra to
XLA rather than fusing it by pattern-match.
fri_fold is the codeword fold shared by every FRI-style scheme (FRI,
Basefold, WHIR, STARK); the fold half of the seam delegates to it. It lives
in this module so the fold's x-coordinates stay the same evaluation domain
the encoder used. The arbitrary-fold-factor (k-ary) generalization is
fri_fold_k plus the KFoldableCode group seam — additive to the binary
conjugate-pair fold, which stays the closed-form butterfly (see coding.md).
ReedSolomon ¶
Reed-Solomon code over dtype; implements FoldableCode.
block_len = message_len * blowup (both powers of two). With coset_shift
set to a field element outside the subgroup, the codeword is the message
polynomial evaluated on the coset coset_shift * <subgroup> rather than the
subgroup itself — FRI/STARK want an evaluation domain disjoint from the
trace domain. The shift is supplied by the caller, so the code carries no
field-generator table.
Source code in zorch/coding/reed_solomon.py
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 | |
extend ¶
extend(evals: Array) -> Array
LDE evals — the message_len evaluations on the base subgroup — to
the block_len coset codeword, transforming the last axis (any leading
batch axes are preserved). Natural order in and out.
Interpolates to coefficients (intt), then coset-evaluates (encode) —
i.e. encode(intt(evals)).
Source code in zorch/coding/reed_solomon.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
domain ¶
domain() -> Array
The points encode evaluates on, coset shift included.
Source code in zorch/coding/reed_solomon.py
210 211 212 213 214 215 216 217 | |
eval_point ¶
eval_point(positions: Array) -> Array
TensorCode seam: the multilinear point p_s that codeword coordinate
positions evaluates, so that
encode(w)[s] == eval_mle(mle_coeffs_to_evals(w), eval_point(s)) — which
turns the Ligerito proximity RHS into a point-eval of the committed w.
For the multiplicative NTT encode is monomial-basis
(encode(w)[s] = Σ_j w[j]·dₛʲ, dₛ = domain()[s]), so the generator row
(1, dₛ, dₛ², …) factors as the geometric tensor
p_s = (dₛ^{2^{k-1}}, …, dₛ², dₛ) — MSB-first to match eval_mle's
lexicographic eq order. The dₛ^{2^i} are built by repeated squaring so
no array exponent is taken (field dtypes reject fnp.power).
For a binary field encode is the additive NTT (novel basis), so the
tensor factors are the subspace polynomials Ŵ_i instead; those are the
basis-power codewords, gathered from a table built on first use.
Source code in zorch/coding/reed_solomon.py
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 | |
fold ¶
fold(codeword: Array, beta: Array) -> Array
FoldableCode fold: natural-order (x, -x) conjugate pairs. The layer
level — and with it the coset shift — is read off the codeword length.
Source code in zorch/coding/reed_solomon.py
273 274 275 276 277 278 | |
fold_values ¶
fold_values(
lo: Array,
hi: Array,
beta: Array,
positions: Array,
level: int,
) -> Array
Fold opened pairs of layer level; the x-coordinates are the first
half of the layer's (level-times-squared) evaluation domain.
Source code in zorch/coding/reed_solomon.py
292 293 294 295 296 297 298 299 300 301 | |
pair_leaves ¶
pair_leaves(codeword: Array) -> Array
Natural order: conjugates sit a half-layer apart, so leaf p is
(codeword[p], codeword[p + half]).
Source code in zorch/coding/reed_solomon.py
303 304 305 306 307 | |
check_final ¶
check_final(final: Array, claim: Array) -> Array
A message-length-1 RS codeword is the constant polynomial on any
domain, so base-code membership and message == claim collapse into one
comparison.
Source code in zorch/coding/reed_solomon.py
309 310 311 312 313 | |
pair_indices ¶
pair_indices(
positions: Array, level: int
) -> tuple[Array, Array]
Natural order: the conjugates of layer level sit a half-layer
apart, and the lo index is the landing index itself.
Source code in zorch/coding/reed_solomon.py
315 316 317 318 | |
layer_positions ¶
layer_positions(
positions: Array, num_rounds: int
) -> list[Array]
Natural order: a_i = q_i mod (n / 2^{i+1}) with q_0 = positions,
q_{i+1} = a_i, elementwise over the query axis.
Source code in zorch/coding/reed_solomon.py
320 321 322 323 324 325 326 327 328 329 | |
fold_group ¶
fold_group(codeword: Array, beta: Array) -> Array
KFoldableCode fold: regroup the layer into k-th-root cosets and
Lagrange-fold each at beta, dividing the length by fold_factor. The
level — and with it the coset shift — is read off the codeword length.
Source code in zorch/coding/reed_solomon.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
group_leaves ¶
group_leaves(codeword: Array) -> Array
Natural order: a k-th-root coset sits a sub-layer (n // k) apart, so
leaf p is (codeword[p], codeword[p + n/k], ..., codeword[p +
(k-1)n/k]) — the k-ary pair_leaves.
Source code in zorch/coding/reed_solomon.py
371 372 373 374 375 | |
group_indices ¶
group_indices(
positions: Array, level: int
) -> tuple[Array, ...]
Natural order: the k-th-root coset of layer level whose fold lands
at positions sits a sub-layer apart, and positions itself is the
landing (first) index.
Source code in zorch/coding/reed_solomon.py
377 378 379 380 381 382 | |
fold_group_values ¶
fold_group_values(
group: Array, beta: Array, positions: Array, level: int
) -> Array
Fold opened k-groups of layer level; the x-coordinates are the
group's points on the layer's (level-times-k-th-powered) domain.
Source code in zorch/coding/reed_solomon.py
384 385 386 387 388 389 390 391 392 393 394 | |
group_layer_positions ¶
group_layer_positions(
positions: Array, num_rounds: int
) -> list[Array]
Natural order: a_i = q_i mod (n / k^{i+1}) with q_0 = positions,
q_{i+1} = a_i, elementwise over the query axis — the k-ary
layer_positions.
Source code in zorch/coding/reed_solomon.py
396 397 398 399 400 401 402 403 404 405 406 | |
BitReversedReedSolomon ¶
Reed-Solomon with codewords in bit-reversed evaluation order.
Some commitment layouts store the codeword bit-reversed so a fold's point
pair sits adjacently ((2p, 2p+1)) instead of a half-layer apart — Merkle
paths of a pair then share all but their last node, and the layout is
fold-stable (folding a bit-reversed layer yields the squared domain's
codeword, again bit-reversed). The fold math is ReedSolomon's; only the
layout-dependent surfaces differ — pair geometry (pair_indices /
layer_positions), the fold's x-coordinate gather, and the
encode/domain output order.
Source code in zorch/coding/reed_solomon.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
domain ¶
domain() -> Array
The points encode evaluates on, in codeword (bit-reversed) order.
Source code in zorch/coding/reed_solomon.py
480 481 482 | |
check_final ¶
check_final(final: Array, claim: Array) -> Array
Constant-polynomial membership is order-invariant.
Source code in zorch/coding/reed_solomon.py
504 505 506 | |
pair_indices ¶
pair_indices(
positions: Array, level: int
) -> tuple[Array, Array]
Bit-reversed order: the pair landing at positions is adjacent.
Source code in zorch/coding/reed_solomon.py
508 509 510 | |
pair_leaves ¶
pair_leaves(codeword: Array) -> Array
Bit-reversed order: conjugates are adjacent, so leaf p is the pair
(codeword[2p], codeword[2p + 1]).
Source code in zorch/coding/reed_solomon.py
512 513 514 515 | |
layer_positions ¶
layer_positions(
positions: Array, num_rounds: int
) -> list[Array]
Bit-reversed order: each fold halves the index, a_i = q >> (i+1).
Source code in zorch/coding/reed_solomon.py
517 518 519 520 521 522 523 524 | |
eval_domain ¶
eval_domain(
dtype: Any,
n: int,
*,
shift: Array | None = None,
generator: int | None = None
) -> Array
The order-n two-adic subgroup points [d₀..d_{n-1}] in lax.ntt order,
or the coset points [shift·d₀..shift·d_{n-1}] when shift is given.
lax.ntt of the coefficient vector of p(X)=X (i.e. e₁) returns
[p(d₀)..p(d_{n-1})] = [d₀..d_{n-1}], so the domain is read off the same NTT
the encoder uses. generator selects the subgroup generator the NTT root is
gen^((p-1)/n) of; None uses the dtype's canonical root. It must match the
generator the codeword was encoded with — the domain order is the root's.
n must be a power of two; the order-1 subgroup is {1}.
Source code in zorch/coding/reed_solomon.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
fri_fold_values ¶
fri_fold_values(
fx: Array, fnx: Array, beta: Array, x: Array
) -> Array
g(x²) = (f(x)+f(−x))/2 + β·(f(x)−f(−x))/(2x). f-values may be EF; x carries the domain's dtype.
Source code in zorch/coding/reed_solomon.py
543 544 545 546 547 548 | |
fri_fold_k ¶
fri_fold_k(
group: Array,
beta: Array,
*,
points: Array | None = None,
coset: tuple[Array, Array] | None = None
) -> Array
k-ary FRI fold: the degree-(k-1) interpolant through a group's k
points, evaluated at beta. Pass exactly one of:
points(..., k): arbitrary coordinates in the caller's own domain order — per-group Lagrange, no domain convention. The per-query verifier path.coset = (coset_inv, generator): the points form a cosets·⟨ω⟩— batchedlax.nttINTT, evaluated atcoset_inv·beta(the unshift folds into the point).coset_invis per-groups⁻¹;generatorselects ω asgenerator^((p-1)/k), None for the canonical root. The prover path.
Byte-identical wherever both apply (the interpolant is unique); values and coordinates may be extension-field.
Source code in zorch/coding/reed_solomon.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
fri_fold ¶
fri_fold(
codeword: Array,
beta: Array,
*,
shift: Array | None = None
) -> Array
FRI-fold a natural-order RS codeword (length 2^m) by β, halving its length.
Natural order: dⱼ and d_{j+n/2} = −dⱼ are conjugates, so f(x)=codeword[:half], f(−x)=codeword[half:], x=domain[:half]. Result is the fold over the order-(n/2) squared domain, again in natural order.
shift is the coset shift of the codeword's own domain. The fold lands on
the squared domain, so the next layer's shift is shift² — iterating
callers must square it each round.
Source code in zorch/coding/reed_solomon.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |