Skip to main content

logicaffeine_kernel/
word_ring.rs

1//! Kernel ring proofs for the word ring ℤ/2ⁿ (`Word8`/`Word16`/`Word32`/`Word64`).
2//!
3//! The [`crate::ring`] procedure proves a polynomial identity by reducing both sides to a
4//! canonical multivariate polynomial over ℤ and comparing. An identity that canonicalizes equal
5//! holds in **every** commutative ring — so it holds in the word ring ℤ/2ⁿ, whose `+`/`*` are
6//! `wrapping_add`/`wrapping_mul` (the quotient of ℤ by 2ⁿℤ; the ring axioms survive the quotient).
7//!
8//! This is the soundness certificate behind every reassociation and product reshaping the
9//! optimizer performs on wrapping arithmetic: associativity, commutativity, distributivity, and
10//! the Karatsuba/gauss product form are all kernel-certified here, at FULL word width with no
11//! bound on the operands. The bit-logic side of the crypto (`xor`, `rotl`) is not polynomial and
12//! is certified separately by `bitblast→CDCL`.
13//!
14//! Each lemma returns `true` iff the kernel certifies it; the tests pin both the positive proofs
15//! and their non-vacuity (a WRONG identity must NOT canonicalize equal).
16
17use crate::ring::Polynomial;
18
19#[inline]
20fn a() -> Polynomial {
21    Polynomial::var(0)
22}
23#[inline]
24fn b() -> Polynomial {
25    Polynomial::var(1)
26}
27#[inline]
28fn c() -> Polynomial {
29    Polynomial::var(2)
30}
31#[inline]
32fn d() -> Polynomial {
33    Polynomial::var(3)
34}
35
36/// `(a + b) + c = a + (b + c)` — additive associativity in ℤ/2ⁿ.
37pub fn add_associative() -> bool {
38    a().add(&b())
39        .add(&c())
40        .canonical_eq(&a().add(&b().add(&c())))
41}
42
43/// `a + b = b + a` — additive commutativity in ℤ/2ⁿ.
44pub fn add_commutative() -> bool {
45    a().add(&b()).canonical_eq(&b().add(&a()))
46}
47
48/// `(a · b) · c = a · (b · c)` — multiplicative associativity in ℤ/2ⁿ.
49pub fn mul_associative() -> bool {
50    a().mul(&b())
51        .mul(&c())
52        .canonical_eq(&a().mul(&b().mul(&c())))
53}
54
55/// `a · b = b · a` — multiplicative commutativity in ℤ/2ⁿ.
56pub fn mul_commutative() -> bool {
57    a().mul(&b()).canonical_eq(&b().mul(&a()))
58}
59
60/// `a · (b + c) = a · b + a · c` — left distributivity in ℤ/2ⁿ.
61pub fn left_distributive() -> bool {
62    a().mul(&b().add(&c()))
63        .canonical_eq(&a().mul(&b()).add(&a().mul(&c())))
64}
65
66/// `a + 0 = a` — additive identity in ℤ/2ⁿ.
67pub fn additive_identity() -> bool {
68    a().add(&Polynomial::constant(0)).canonical_eq(&a())
69}
70
71/// `a · 1 = a` — multiplicative identity in ℤ/2ⁿ.
72pub fn multiplicative_identity() -> bool {
73    a().mul(&Polynomial::constant(1)).canonical_eq(&a())
74}
75
76/// `(a + b)(c + d) = ac + ad + bc + bd` — the Karatsuba/gauss product expansion, the identity the
77/// 3-multiply NTT butterfly and complex-multiply reshaping rely on, certified at full word width.
78pub fn karatsuba_expand() -> bool {
79    let lhs = a().add(&b()).mul(&c().add(&d()));
80    let rhs = a()
81        .mul(&c())
82        .add(&a().mul(&d()))
83        .add(&b().mul(&c()))
84        .add(&b().mul(&d()));
85    lhs.canonical_eq(&rhs)
86}
87
88/// Every word-ring law the optimizer leans on, kernel-certified — a single gate.
89pub fn all_word_ring_laws_certified() -> bool {
90    add_associative()
91        && add_commutative()
92        && mul_associative()
93        && mul_commutative()
94        && left_distributive()
95        && additive_identity()
96        && multiplicative_identity()
97        && karatsuba_expand()
98}
99
100#[cfg(test)]
101#[path = "../../logicaffeine_tests/unit/logicaffeine_kernel/word_ring/tests.rs"]
102mod tests;