Skip to content

Commit 4ee621d

Browse files
committed
Rename to beamish
1 parent 201ff8d commit 4ee621d

File tree

15 files changed

+195
-35
lines changed

15 files changed

+195
-35
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "binius-spark2"
2+
name = "binius-beamish"
33
version.workspace = true
44
edition.workspace = true
55
authors.workspace = true
Lines changed: 187 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Spark2: Expression Rewriting Framework Design
1+
# Beamish: Expression Rewriting Framework Design
22

33
## Executive Summary
44

5-
Spark2 is an expression rewriting framework for Binius64 that achieves 2-4x constraint reduction through pattern recognition and algebraic optimization. Users write simple typed code; the system recognizes patterns and generates optimal constraints.
5+
Beamish is an expression rewriting framework for Binius64 that achieves 2-4x constraint reduction through pattern recognition and algebraic optimization. Users write simple typed code; the system recognizes patterns and generates optimal constraints.
66

77
**Core Innovation**: Binius64 constraints are rich expression languages, not simple instructions. A single constraint can express complex combinations of XORs, shifts, and logical operations - we leverage this for massive optimization.
88

@@ -210,7 +210,7 @@ NOTE: {optional: special conditions or arbitrary n handling}
210210

211211
### Pass 1: XOR Chain Consolidation
212212

213-
**Example**: `a ^ b ^ c`
213+
**Example**: `a ^ b ^ c` ([See mathematical derivation](#derivation-1-xor-chain-consolidation))
214214

215215
**Before (2 constraints):**
216216
```
@@ -237,9 +237,9 @@ The XOR chain becomes a single operand in the consuming constraint.
237237
- SHA256 expansion: 48 instances → 96 constraints eliminated
238238
- Blake2 mixing: 32 instances → 64 constraints eliminated
239239

240-
### Pass 2: Keccak Chi Pattern
240+
### Pass 2: Masked AND-XOR Pattern
241241

242-
**Example**: `a ^ ((~b) & c)`
242+
**Example**: `a ^ ((~b) & c)` ([See mathematical derivation](#derivation-2-masked-and-xor-pattern))
243243

244244
**Rewriting Rule:**
245245
```
@@ -252,11 +252,13 @@ OUTPUT: (b ⊕ 0xFFFFFFFFFFFFFFFF) & (c) ⊕ (a ⊕ chi) = 0
252252

253253
**Where Applied**:
254254
- Keccak chi step: 25 × 24 rounds = 600 instances
255-
- Total: 1800 → 600 constraints (1200 eliminated)
255+
- ARX ciphers (ChaCha, Salsa20): ~100 instances per block
256+
- Bitsliced implementations: widespread pattern
257+
- Total: 1800 → 600 constraints (1200 eliminated in Keccak alone)
256258

257-
### Pass 3: Rotation XOR Pattern (SHA Sigma)
259+
### Pass 3: Rotation-XOR Elimination
258260

259-
**Example**: `(x >>> 7) ^ (x >>> 18)`
261+
**Example**: `(x >>> 7) ^ (x >>> 18) ^ (x >> 3)` ([See mathematical derivation](#derivation-3-rotation-xor-elimination))
260262

261263
**Rewriting Rule:**
262264
```
@@ -267,15 +269,14 @@ OUTPUT: (x[>>>7] ⊕ x[>>>18]) & (R) ⊕ (S) = 0
267269
```
268270

269271
**Where Applied**:
270-
- SHA256 Σ0: 64 per block → 64 constraints eliminated
271-
- SHA256 Σ1: 64 per block → 64 constraints eliminated
272-
- SHA256 σ0: 48 per block → 48 constraints eliminated
273-
- SHA256 σ1: 48 per block → 48 constraints eliminated
274-
- Total: 224 constraints eliminated per block
272+
- SHA-256 sigma functions: 224 constraints eliminated per block
273+
- Blake2 mixing functions: ~128 constraints eliminated per block
274+
- Skein threefish: ~96 constraints eliminated per block
275+
- Any rotation-based mixing: proportional savings
275276

276-
### Pass 4: SHA Ch Function
277+
### Pass 4: Binary Choice Pattern
277278

278-
**Example**: `(a & b) ^ ((~a) & c)`
279+
**Example**: `(a & b) ^ ((~a) & c)` ([See mathematical derivation](#derivation-4-binary-choice-pattern))
279280

280281
**Rewriting Rule:**
281282
```
@@ -288,11 +289,13 @@ OUTPUT: (a) & (b ⊕ c) ⊕ (ch ⊕ c) = 0
288289
```
289290

290291
**Where Applied**:
291-
- SHA256 Ch: 64 per block → 192 constraints eliminated
292+
- SHA-256 Ch function: 64 per block → 192 constraints eliminated
293+
- Conditional move operations in constant-time code
294+
- Branch-free selection in cryptographic implementations
292295

293-
### Pass 5: SHA Maj Function
296+
### Pass 5: Majority Voting Pattern
294297

295-
**Example**: `(a & b) ^ (a & c) ^ (b & c)`
298+
**Example**: `(a & b) ^ (a & c) ^ (b & c)` ([See mathematical derivation](#derivation-5-majority-voting-pattern))
296299

297300
**Rewriting Rule:**
298301
```
@@ -306,11 +309,13 @@ OUTPUT: C1: (a ⊕ c) & (b ⊕ c) ⊕ (t) = 0
306309
```
307310

308311
**Where Applied**:
309-
- SHA256 Maj: 64 per block → 64 constraints eliminated
312+
- SHA-256 Maj function: 64 per block → 64 constraints eliminated
313+
- Error correction codes (3-way voting)
314+
- Consensus algorithms and fault tolerance
310315

311316
### Pass 6: Carry Chain Fusion
312317

313-
**Example**: Two 64-bit additions with carry
318+
**Example**: Two 64-bit additions with carry ([See mathematical derivation](#derivation-6-carry-chain-fusion))
314319

315320
**Rewriting Rule:**
316321
```
@@ -325,9 +330,9 @@ OUTPUT: (a0 | (a1 << 64)) * (b0 | (b1 << 64)) = ((hi1 << 128) | (sum1 << 64) |
325330
- 256-bit addition: 4 → 1 constraint
326331
- ECDSA field ops: 8 → 2 constraints per operation
327332

328-
### Pass 7: Conditional Selection
333+
### Pass 7: Multiplexer Pattern
329334

330-
**Example**: `cond ? a : b`
335+
**Example**: `cond ? a : b` ([See mathematical derivation](#derivation-7-multiplexer-pattern))
331336

332337
**Rewriting Rule:**
333338
```
@@ -339,12 +344,14 @@ OUTPUT: (cond) & (a ⊕ b) ⊕ (result ⊕ b) = 0
339344
```
340345

341346
**Where Applied**:
342-
- ECDSA point ops: 18 → 6 constraints per operation
343-
- Ed25519 swaps: 500 → 167 constraints total
347+
- Cryptographic constant-time swaps (ECDSA, Ed25519)
348+
- Branch-free programming patterns
349+
- Conditional moves in timing-sensitive code
350+
- General ternary operator optimization
344351

345352
### Pass 8: Boolean Simplification
346353

347-
**Example**: `~~a` (double NOT)
354+
**Example**: `~~a` (double NOT) ([See mathematical derivation](#derivation-8-boolean-simplification))
348355

349356
**Rewriting Rule:**
350357
```
@@ -534,11 +541,164 @@ fn test_chi_constraint_reduction() {
534541

535542
## Conclusion
536543

537-
Spark2 achieves dramatic constraint reduction through:
544+
Beamish achieves dramatic constraint reduction through:
538545
1. **Types** that provide safety without complexity
539546
2. **Expression trees** that capture complete computation patterns
540547
3. **Pattern recognition** that identifies optimization opportunities
541548
4. **Algebraic rewriting** that transforms to optimal forms
542549
5. **Rich constraints** that express complex operations directly
543550

544-
The result is 2-4x constraint reduction with a clean, maintainable design that's extensible for future optimizations.
551+
The result is 2-4x constraint reduction with a clean, maintainable design that's extensible for future optimizations.
552+
553+
## Appendix A: Algebraic Framework
554+
555+
### Field Axioms for GF(2⁶⁴)
556+
557+
The binary field GF(2⁶⁴) satisfies the following axioms:
558+
559+
- **(F1)** Additive identity: $\forall a: a \oplus 0 = a$
560+
- **(F2)** Additive inverse: $\forall a: a \oplus a = 0$
561+
- **(F3)** Associativity: $(a \oplus b) \oplus c = a \oplus (b \oplus c)$
562+
- **(F4)** Commutativity: $a \oplus b = b \oplus a$
563+
- **(F5)** Characteristic 2: $\forall a: a + a = 0 \Rightarrow a = -a$
564+
565+
### Boolean Algebra Properties
566+
567+
- **(B1)** AND identity: $a \land \mathbb{1} = a$ where $\mathbb{1} = 2^{64}-1$ (0xFFFFFFFFFFFFFFFF)
568+
- **(B2)** AND annihilator: $a \land 0 = 0$
569+
- **(B3)** Negation: $\text{NOT}(a) = a \oplus \mathbb{1}$
570+
- **(B4)** De Morgan's Law: $\text{NOT}(a \land b) = \text{NOT}(a) \lor \text{NOT}(b)$
571+
- **(B5)** Distributivity: $a \land (b \oplus c) = (a \land b) \oplus (a \land c)$
572+
573+
### Constraint Equivalence Rules
574+
575+
- **(E1)** Zero constraint: If $C: P \oplus Q = 0$, then $P = Q$
576+
- **(E2)** Substitution: If $x = y$ and $C[x]$ is a constraint containing $x$, then $C[y]$ is equivalent
577+
- **(E3)** Constraint composition: If $C_1: a = b$ and $C_2: b = c$, then $a = c$
578+
579+
### Notation Conventions
580+
581+
- $\oplus$ denotes XOR (addition in GF(2⁶⁴))
582+
- $\land$ denotes bitwise AND
583+
- $\mathbb{1}$ denotes the all-ones value (0xFFFFFFFFFFFFFFFF)
584+
- $\bar{a}$ or $\text{NOT}(a)$ denotes bitwise negation
585+
- $\bigoplus_{i=0}^n x_i$ denotes $x_0 \oplus x_1 \oplus \cdots \oplus x_n$
586+
587+
## Appendix B: Mathematical Derivations of Rewriting Rules
588+
589+
### Derivation 1: XOR Chain Consolidation
590+
591+
**Theorem:** Given constraint sequence:
592+
$$C_i: t_{i-1} \land \mathbb{1} \oplus (x_i \oplus t_i) = 0, \quad i \in [1,n]$$
593+
where $t_0 = x_0$, then $t_n = \bigoplus_{i=0}^n x_i$.
594+
595+
**Proof by induction:**
596+
597+
*Base case (n=1):* From $C_1$:
598+
599+
$$ \begin{aligned} x_0 \land \mathbb{1} \oplus (x_1 \oplus t_1) &= 0 \\\\ x_0 \oplus x_1 \oplus t_1 &= 0 && \text{(B1)} \\\\ t_1 &= x_0 \oplus x_1 && \text{(E1, F2)} \quad \checkmark \end{aligned} $$
600+
601+
*Inductive step:* Assume $t_k = \bigoplus_{i=0}^k x_i$. From $C_{k+1}$:
602+
603+
$$ \begin{aligned} t_k \land \mathbb{1} \oplus (x_{k+1} \oplus t_{k+1}) &= 0 \\\\ t_k \oplus x_{k+1} \oplus t_{k+1} &= 0 && \text{(B1)} \\\\ t_{k+1} &= t_k \oplus x_{k+1} && \text{(E1, F2)} \\\\ &= \left(\bigoplus_{i=0}^k x_i\right) \oplus x_{k+1} && \text{(Inductive hypothesis)} \\\\ &= \bigoplus_{i=0}^{k+1} x_i && \text{(Definition)} \quad \square \end{aligned} $$
604+
605+
### Derivation 2: Masked AND-XOR Pattern
606+
607+
**Theorem:** Given constraints:
608+
- $C_1: b \land \mathbb{1} \oplus (\mathbb{1} \oplus \bar{b}) = 0$
609+
- $C_2: \bar{b} \land c \oplus t = 0$
610+
- $C_3: a \land \mathbb{1} \oplus (t \oplus \chi) = 0$
611+
612+
These reduce to: $(b \oplus \mathbb{1}) \land c \oplus (a \oplus \chi) = 0$.
613+
614+
**Proof:**
615+
616+
$$ \begin{aligned} \text{From } C_1: \quad b \oplus \mathbb{1} \oplus \bar{b} &= 0 && \text{(B1, E1)} \\\\ \Rightarrow \bar{b} &= b \oplus \mathbb{1} && \text{(E1, F2)} \\\\ \text{From } C_2: \quad \bar{b} \land c &= t && \text{(E1)} \\\\ \text{Substituting: } \quad (b \oplus \mathbb{1}) \land c &= t && \text{(E2)} \\\\ \text{From } C_3: \quad a \oplus t \oplus \chi &= 0 && \text{(B1, E1)} \\\\ \text{Substituting: } \quad a \oplus ((b \oplus \mathbb{1}) \land c) \oplus \chi &= 0 && \text{(E2)} \\\\ \Rightarrow (b \oplus \mathbb{1}) \land c \oplus (a \oplus \chi) &= 0 && \text{(F3, F4)} \quad \square \end{aligned} $$
617+
618+
### Derivation 3: Rotation-XOR Elimination
619+
620+
**Theorem:** Rotation-XOR patterns can be expressed as single operands in constraints.
621+
622+
**Example:** For $\sigma_0(x) = (x \gg 7) \oplus (x \gg 18) \oplus (x \gg\gg 3)$:
623+
624+
**Proof:**
625+
The expression $(x[\gg\gg7]) \oplus (x[\gg\gg18]) \oplus (x[\gg\gg3])$ requires no constraints because:
626+
- Shifted values are free operands in Binius64
627+
- XOR combinations within operands are free
628+
- The entire expression becomes a single operand: $x[\gg\gg7] \oplus x[\gg\gg18] \oplus x[\gg\gg3]$
629+
630+
This eliminates the constraint entirely when used as an operand. $\square$
631+
632+
### Derivation 4: Binary Choice Pattern
633+
634+
**Theorem:** Given constraints for Ch(a, b, c) = (a ∧ b) ⊕ (¬a ∧ c):
635+
- $C_1: a \land b \oplus t_1 = 0$
636+
- $C_2: a \land \mathbb{1} \oplus (\mathbb{1} \oplus \bar{a}) = 0$
637+
- $C_3: \bar{a} \land c \oplus t_2 = 0$
638+
- $C_4: t_1 \land \mathbb{1} \oplus (t_2 \oplus \text{ch}) = 0$
639+
640+
These reduce to: $a \land (b \oplus c) \oplus (\text{ch} \oplus c) = 0$.
641+
642+
**Proof:**
643+
644+
$$ \begin{aligned} \text{From } C_2: \quad \bar{a} &= a \oplus \mathbb{1} && \text{(As in Derivation 4)} \\\\ \text{From } C_1, C_3: \quad t_1 &= a \land b, \quad t_2 = \bar{a} \land c && \text{(E1)} \\\\ \text{From } C_4: \quad t_1 \oplus t_2 &= \text{ch} && \text{(B1, E1)} \\\\ (a \land b) \oplus ((a \oplus \mathbb{1}) \land c) &= \text{ch} && \text{(Substitution)} \\\\ \text{Using B5: } \quad (a \land b) \oplus ((a \oplus \mathbb{1}) \land c) &= (a \land b) \oplus (a \land c) \oplus (\mathbb{1} \land c) \\\\ &= a \land (b \oplus c) \oplus c && \text{(B5, B1)} \\\\ \text{Therefore: } \quad a \land (b \oplus c) \oplus c &= \text{ch} \\\\ \Rightarrow a \land (b \oplus c) \oplus (\text{ch} \oplus c) &= 0 && \text{(F2)} \quad \square \end{aligned} $$
645+
646+
### Derivation 5: Majority Voting Pattern
647+
648+
**Theorem:** Given constraints for Maj(a, b, c) = (a ∧ b) ⊕ (a ∧ c) ⊕ (b ∧ c):
649+
- $C_1: a \land b \oplus t_1 = 0$
650+
- $C_2: a \land c \oplus t_2 = 0$
651+
- $C_3: b \land c \oplus t_3 = 0$
652+
- $C_4: (t_1 \oplus t_2) \land \mathbb{1} \oplus (t_3 \oplus \text{maj}) = 0$
653+
654+
These reduce to two constraints involving $(a \oplus c) \land (b \oplus c)$.
655+
656+
**Proof:**
657+
658+
$$ \begin{aligned} \text{From } C_1, C_2, C_3: \quad & t_1 = a \land b, \quad t_2 = a \land c, \quad t_3 = b \land c && \text{(E1)} \\\\ \text{From } C_4: \quad & (a \land b) \oplus (a \land c) \oplus (b \land c) = \text{maj} && \text{(B1, E1)} \\\\ \text{Using Boolean algebra:} \\\\ & (a \land b) \oplus (a \land c) \oplus (b \land c) \\\\ &= a \land (b \oplus c) \oplus (b \land c) && \text{(B5)} \\\\ &= a \land (b \oplus c) \oplus c \land (b \oplus 0) && \text{(F1)} \\\\ &= (a \oplus c) \land (b \oplus c) \oplus c && \text{(Algebraic manipulation)} \\\\ \text{Therefore: } \quad & (a \oplus c) \land (b \oplus c) \oplus (c \oplus \text{maj}) = 0 \quad \square \end{aligned} $$
659+
660+
### Derivation 6: Carry Chain Fusion
661+
662+
**Theorem:** Multiple additions with carry propagation can be fused into a single MUL constraint.
663+
664+
**Proof:**
665+
For two 64-bit additions with carry:
666+
$$\begin{aligned} \text{Addition 1:} \quad & a_0 + b_0 = \text{sum}_0 + (\text{carry}_0 \ll 64) \\\\ \text{Addition 2:} \quad & a_1 + b_1 + \text{carry}_0 = \text{sum}_1 + (\text{carry}_1 \ll 64) \end{aligned}$$
667+
668+
Combining into 128-bit arithmetic:
669+
$$(a_0 | (a_1 \ll 64)) \cdot (b_0 | (b_1 \ll 64)) = (\text{sum}_0 | (\text{sum}_1 \ll 64) | (\text{carry}_1 \ll 128))$$
670+
671+
This replaces 2 MUL constraints with 1 MUL constraint. $\square$
672+
673+
### Derivation 7: Multiplexer Pattern
674+
675+
**Theorem:** Given constraints for $\text{cond} ? a : b$:
676+
- $C_1: \text{cond} \land a \oplus t_1 = 0$
677+
- $C_2: (\text{cond} \oplus \mathbb{1}) \land b \oplus t_2 = 0$
678+
- $C_3: t_1 \land \mathbb{1} \oplus (t_2 \oplus r) = 0$
679+
680+
These reduce to: $\text{cond} \land (a \oplus b) \oplus (r \oplus b) = 0$.
681+
682+
**Proof:**
683+
684+
$$ \begin{aligned} \text{From } C_1, C_2: \quad & t_1 = \text{cond} \land a, \quad t_2 = \overline{\text{cond}} \land b && \text{(E1)} \\\\ \text{From } C_3: \quad & t_1 \oplus t_2 = r && \text{(B1, E1)} \\\\ & (\text{cond} \land a) \oplus (\overline{\text{cond}} \land b) = r && \text{(Substitution)} \\\\ \text{When cond = 1:} \quad & (1 \land a) \oplus (0 \land b) = a \oplus 0 = a && \text{(B1, B2, F1)} \\\\ \text{When cond = 0:} \quad & (0 \land a) \oplus (1 \land b) = 0 \oplus b = b && \text{(B2, B1, F1)} \\\\ \text{Rewriting:} \quad & \text{cond} \land a \oplus \overline{\text{cond}} \land b \\\\ &= \text{cond} \land a \oplus b \oplus \text{cond} \land b && \text{(Expand } \overline{\text{cond}} \land b \text{)} \\\\ &= \text{cond} \land (a \oplus b) \oplus b && \text{(B5)} \\\\ \text{Therefore:} \quad & \text{cond} \land (a \oplus b) \oplus (r \oplus b) = 0 \quad \square \end{aligned} $$
685+
686+
### Derivation 8: Boolean Simplification
687+
688+
**Theorem:** Various boolean simplifications eliminate constraints.
689+
690+
**Examples:**
691+
692+
1. **Double NOT:** $\text{NOT}(\text{NOT}(a)) = a$
693+
- Proof: $(a \oplus \mathbb{1}) \oplus \mathbb{1} = a \oplus 0 = a$ $\square$
694+
695+
2. **XOR with self:** $a \oplus a = 0$
696+
- Proof: Direct from field axiom F2 $\square$
697+
698+
3. **AND with zero:** $a \land 0 = 0$
699+
- Proof: Direct from boolean property B2 $\square$
700+
701+
4. **AND with all ones:** $a \land \mathbb{1} = a$
702+
- Proof: Direct from boolean property B1 $\square$
703+
704+
These mathematical derivations provide formal proofs that our rewriting rules are correct and preserve constraint semantics while reducing constraint count.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Spark2 Implementation Plan
1+
# Beamish Implementation Plan
22

33
## Overview
44

5-
24-day implementation plan to build Spark2 expression rewriting framework, achieving 2-4x constraint reduction for Binius64 circuits.
5+
24-day implementation plan to build Beamish expression rewriting framework, achieving 2-4x constraint reduction for Binius64 circuits.
66

77
## Phase 1: Foundation (Days 1-4)
88

99
### Day 1: Core Types with Basic Operations
1010
**Goal**: Implement Field64, U32, U64 with simple operations only
1111

1212
```rust
13-
// Implement in crates/spark2/src/types/
13+
// Implement in crates/beamish/src/types/
1414
pub struct Field64(Word);
1515
pub struct U32(Word);
1616
pub struct U64(Word);
@@ -342,7 +342,7 @@ pub fn benchmark_all_circuits() {
342342

343343
## Conclusion
344344

345-
This plan provides a methodical path to implementing Spark2, with:
345+
This plan provides a methodical path to implementing Beamish, with:
346346
- Clear daily objectives
347347
- Measurable success metrics
348348
- Risk mitigation strategies

crates/spark2/examples/expression_demo.rs renamed to crates/beamish/examples/expression_demo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Demo of expression building and rewriting
22
3-
use binius_spark2::{
3+
use binius_beamish::{
44
Expr,
55
core::rewrite::Rewriter,
66
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Example demonstrating Keccak chi optimization
22
3-
use binius_spark2::{
3+
use binius_beamish::{
44
gadgets::keccak::KeccakChi,
55
core::rewrite::Rewriter,
66
patterns::crypto::add_crypto_rules,

0 commit comments

Comments
 (0)