zorch.utils.binary_field¶
Binary-field ⟷ F_2 bit-vector representation for any binary tower dtype
(binary_field_ghash, binary_field_t*).
An element of GF(2^W) is a W-dimensional vector over F_2; [unpack] exposes that
coefficient vector and [pack] rebuilds the element — the GF(2^W) ≅ F_2^W
isomorphism the ring-switch / tensor-algebra kernels ride on.
It is a shift/mask, NOT a bitcast. A hardware bitcast is byte-granular (the finest
binary-field bitcast is GF(2^128) ↔ GF(2^8); GF(2^128) → F_2 fails the
compatible-width check), so reaching the individual F_2 coefficients needs the
explicit unpack. The uint32 storage limbs the shift/mask rides on stay private.
uint32 is the finest limb, so it keeps the bit kernels valid for the widest set
of tower dtypes — [field_bit_width] needs the width to be a whole number of
limbs, so a uint64 limb would reject the 32-bit tower level (binary_field_t5)
— and keeps the ring-switch {0, 1} × limb products narrow.
field_bit_width ¶
field_bit_width(dtype: Any) -> int
W: the GF(2)-dimension of dtype (= its storage bits).
Source code in zorch/utils/binary_field.py
55 56 57 58 59 60 61 62 63 | |
byte_select_xor_reduce ¶
byte_select_xor_reduce(
selectors: Array, values: Array
) -> Array
XOR weights selected by row-major packed bytes.
selectors is a uint8 matrix (n, B/8), where byte j stores selector
bits 8*j .. 8*j+7 least-significant-bit first. values is a binary-field
vector (B,); the result (n,) XORs values[b] wherever row bit b is set.
Where a kernel exists, a 256-entry XOR table per byte position feeds the
gather, matching flock-core's UniSkipFoldTable: the kernel reads B/8
selector bytes per row and never expands the (n, B) bit matrix. Backends
without one keep a compact source-level expression as the portable oracle.
Source code in zorch/utils/binary_field.py
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
bit_select_xor_reduce ¶
bit_select_xor_reduce(
selectors: Array,
values: Array,
*,
reduce: BitSelectReduction
) -> Array
Select binary-field values with packed bits and XOR-reduce one axis.
selectors may be a one-dimensional vector of GF(2^W) elements.
With reduce="elements", values has the same shape and the result has
shape (W,): result bit-slice b XORs values[i] wherever bit b of
selector i is set. With reduce="bits", values has shape (W,) and
the result has the selectors' shape: each result i XORs values[b]
wherever bit b of selector i is set.
reduce="elements" also batches: a selectors-major values of shape
(n, N) reduces all N columns against the shared selectors in one pass —
the result is (N, W), row k the reduction against values[:, k]. A
batched ring-switch open uses this so the packed witness (the selectors) is
read once for all N claims instead of once per claim.
For reduce="bits", selectors may instead be an explicit Boolean/integer
0/1 matrix of shape (n, B) with values.shape == (B,). This is the form
used when a consumer already holds unpacked witness rows.
Where a kernel exists, the lowering streams directly into the limb output.
It never materializes the broadcast (n, W, L) selection; its working set
is one fixed-size register block. Backends without one use the compact
source-level expression so the primitive remains portable and easy to
validate.
Source code in zorch/utils/binary_field.py
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 541 542 543 544 545 546 547 548 549 550 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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
unpack ¶
unpack(x: Array) -> Array
(...,) GF(2^W) -> (..., W) F_2: the element's F_2-coefficient vector,
coefficient r at index r. The GF(2^W) ≅ F_2^W iso, realized as a shift/mask
over the storage limbs (a bitcast cannot reach sub-byte coefficients) — the
same bits [_bits] returns, retyped from uint32 to binary_field_t0.
Source code in zorch/utils/binary_field.py
608 609 610 611 612 613 | |
pack ¶
pack(coeffs: Array, dtype: Any) -> Array
(..., W) F_2 -> (...,) GF(2^W). Inverse of [unpack]: repack each 32
coefficients into a uint32 limb (Σ_r coeff_r · 2^r), then reinterpret.
Source code in zorch/utils/binary_field.py
616 617 618 619 620 621 | |