use crate::forms::integral::codes::extended_golay_generator_rows;
use crate::forms::IntegralForm;
use crate::linalg::integer::normalize_relation_rows;
use crate::scalar::Rational;
const fn const_pow(mut base: u128, mut exp: u32) -> u128 {
let mut acc = 1u128;
while exp > 0 {
if exp % 2 == 1 {
acc *= base;
}
exp /= 2;
if exp > 0 {
base *= base;
}
}
acc
}
pub const LEECH_AUT_ORDER: u128 =
const_pow(2, 22) * const_pow(3, 9) * const_pow(5, 4) * const_pow(7, 2) * 11 * 13 * 23;
fn checked_rat_mul((a, b): (i128, i128), (c, d): (i128, i128)) -> Option<(i128, i128)> {
let r = Rational::try_new(a, b)?.checked_mul(&Rational::try_new(c, d)?)?;
Some((r.numer(), r.denom()))
}
fn checked_rat_add((a, b): (i128, i128), (c, d): (i128, i128)) -> Option<(i128, i128)> {
let r = Rational::try_new(a, b)?.checked_add(&Rational::try_new(c, d)?)?;
Some((r.numer(), r.denom()))
}
fn checked_rat_sub(a: (i128, i128), (c, d): (i128, i128)) -> Option<(i128, i128)> {
checked_rat_add(a, (c.checked_neg()?, d))
}
fn checked_rat_mul_int((a, b): (i128, i128), c: i128) -> Option<(i128, i128)> {
checked_rat_mul((a, b), (c, 1))
}
pub(crate) fn bernoulli(n: usize) -> Option<(i128, i128)> {
let mut a = vec![(0i128, 1i128); n + 1];
for m in 0..=n {
let den = i128::try_from(m + 1).ok()?;
a[m] = (1, den);
for j in (1..=m).rev() {
let diff = checked_rat_sub(a[j - 1], a[j])?;
a[j - 1] = checked_rat_mul_int(diff, i128::try_from(j).ok()?)?;
}
}
Some(a[0])
}
fn bernoulli_abs(k: u128) -> Option<(i128, i128)> {
let n = usize::try_from(k).ok()?;
let (num, den) = bernoulli(n)?;
Some((num.abs(), den))
}
pub fn mass_even_unimodular(n: u128) -> Option<(i128, i128)> {
if n == 0 || !n.is_multiple_of(8) || n > 24 {
return None;
}
let m = n / 2;
let mut acc = (1i128, 1i128);
for j in 1..m {
acc = checked_rat_mul(acc, bernoulli_abs(2 * j)?)?;
acc = checked_rat_mul(acc, (1, 4 * j as i128))?;
}
acc = checked_rat_mul(acc, bernoulli_abs(m)?)?;
acc = checked_rat_mul(acc, (1, n as i128))?;
Some(acc)
}
fn leech_spanning_set() -> Vec<Vec<i128>> {
let mut s: Vec<Vec<i128>> = Vec::new();
for g in extended_golay_generator_rows() {
s.push(g.iter().map(|&x| 2 * x as i128).collect());
}
for i in 1..24 {
let mut v = vec![0i128; 24];
v[0] = 4;
v[i] = 4;
s.push(v);
}
let mut odd = vec![1i128; 24];
odd[0] = -3;
s.push(odd);
s
}
pub fn leech() -> IntegralForm {
let basis = normalize_relation_rows(leech_spanning_set());
assert_eq!(basis.len(), 24, "Leech spanning set must have rank 24");
let n = 24;
let mut gram = vec![vec![0i128; n]; n];
for i in 0..n {
for j in 0..n {
let dot: i128 = basis[i].iter().zip(&basis[j]).map(|(&x, &y)| x * y).sum();
assert!(
dot % 8 == 0,
"√8·Λ inner products must be divisible by 8 (got {dot})"
);
gram[i][j] = dot / 8;
}
}
IntegralForm::new(gram).expect("Leech Gram is symmetric")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::forms::e_8;
#[test]
fn mass_recovers_e8_weyl_group_order() {
assert_eq!(mass_even_unimodular(8), Some((1, 696_729_600)));
}
#[test]
fn mass_matches_known_values_through_24() {
assert_eq!(
mass_even_unimodular(16),
Some((691, 277_667_181_515_243_520_000))
);
assert_eq!(
mass_even_unimodular(24),
Some((
1_027_637_932_586_061_520_960_267,
129_477_933_340_026_851_560_636_148_613_120_000_000
))
);
}
#[test]
fn mass_rejects_non_multiples_of_eight() {
assert_eq!(mass_even_unimodular(0), None);
assert_eq!(mass_even_unimodular(4), None);
assert_eq!(mass_even_unimodular(12), None);
assert_eq!(mass_even_unimodular(7), None);
assert_eq!(mass_even_unimodular(32), None);
}
#[test]
fn bernoulli_recurrence_matches_standard_even_values() {
assert_eq!(bernoulli_abs(2), Some((1, 6)));
assert_eq!(bernoulli_abs(12), Some((691, 2730)));
assert_eq!(bernoulli_abs(24), Some((236_364_091, 2730)));
}
#[test]
fn bernoulli_one_is_the_plus_convention() {
assert_eq!(bernoulli(1), Some((1, 2)));
}
#[test]
fn golay_is_a_self_dual_doubly_even_code() {
let g = extended_golay_generator_rows();
for row in &g {
let wt: u8 = row.iter().sum();
assert_eq!(wt % 4, 0, "doubly even: weight {wt}");
}
for i in 0..12 {
for j in 0..12 {
let ip: u8 = g[i].iter().zip(&g[j]).map(|(&a, &b)| a * b).sum();
assert_eq!(ip % 2, 0, "self-orthogonal rows {i},{j}");
}
}
}
#[test]
fn leech_is_the_rootless_even_unimodular_rank24() {
let l = leech();
assert_eq!(l.dim(), 24);
assert_eq!(l.determinant(), 1, "unimodular");
assert!(l.is_even(), "even");
let roots = l.short_vectors(2).expect("positive definite");
assert!(
roots.is_empty(),
"Leech has no roots (found {})",
roots.len()
);
}
#[test]
fn leech_aut_order_has_the_co0_factorisation() {
let expected: u128 =
2u128.pow(22) * 3u128.pow(9) * 5u128.pow(4) * 7u128.pow(2) * 11 * 13 * 23;
assert_eq!(LEECH_AUT_ORDER, expected);
assert!(e_8().is_even() && e_8().is_unimodular());
}
}