1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
use std::fmt;
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use num::Zero;
use num::rational::Ratio;
use crate::arithmetic_utils::kronecker_symbol;
/// Error returned by [`Lattice::short_vectors`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShortVectorError {
/// The form is not positive definite, so there is no finite set of
/// minimal-norm nonzero vectors (e.g. an indefinite lattice can scale
/// an isotropic vector by any nonzero integer and stay at norm `0`).
NotPositiveDefinite,
/// The lattice is positive definite, but this implementation does not
/// (yet) know how to enumerate its minimal-norm vectors.
Unknown,
}
impl fmt::Display for ShortVectorError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotPositiveDefinite => write!(
f,
"short vectors are only well-defined for a positive-definite lattice"
),
Self::Unknown => write!(
f,
"short vectors are not known for this lattice implementation"
),
}
}
}
impl std::error::Error for ShortVectorError {}
/// A rank-`N` lattice: a free `Z`-module isomorphic to `Z^N` as an abelian
/// group, equipped with a rational-valued symmetric bilinear form. This
/// covers not just integral lattices but also rational ones such as the
/// dual of an integral lattice, whose Gram matrix need not have integer
/// entries (e.g. `A_n*` has pairings in `(1/(n+1))Z`).
///
/// Scalar multiplication is restricted to `i128` — a lattice is not a vector
/// space, so there is no division and no action of arbitrary rationals or
/// reals.
pub trait Lattice<const N: usize>:
Add<Self, Output = Self>
+ Sub<Self, Output = Self>
+ AddAssign<Self>
+ SubAssign<Self>
+ Mul<i128, Output = Self>
+ MulAssign<i128>
+ PartialEq
+ Zero
+ Sized
{
/// The dual lattice `L* = Hom(L, Z)`, the lattice of `Z`-linear
/// functionals on `L`, with its own induced lattice structure.
type DualLattice: Lattice<N>;
/// The symmetric bilinear form `<self, other>`.
fn inner_product(&self, other: &Self) -> Ratio<i128>;
/// The associated quadratic form, `<self, self>`.
fn lattice_norm_sq(&self) -> Ratio<i128> {
self.inner_product(self)
}
/// Whether `<v, w>` is an integer for every `v, w` in the lattice, i.e.
/// whether this is an integral lattice rather than a properly rational
/// one (such as the dual of an integral lattice).
fn is_integral() -> bool;
/// Whether `<v, v>` is an even integer for every `v` in the lattice
/// (e.g. root lattices such as `E8`), as opposed to merely an integer
/// for every `v` (in which case the lattice is odd). Implies
/// [`is_integral`](Self::is_integral).
fn is_even() -> bool;
/// Whether the lattice is unimodular, i.e. equal to its own
/// [`DualLattice`](Self::DualLattice) under the inner product
/// (equivalently, [`discriminant`](Self::discriminant) is `1`).
fn is_self_dual() -> bool;
/// The signature `(p, q, r)`: the number of positive, negative, and zero
/// eigenvalues of the Gram matrix of `inner_product`, counted with
/// multiplicity, so `p + q + r = N` always. `r` is the dimension of the
/// radical; the form is non-degenerate iff `r == 0`.
fn signature() -> (usize, usize, usize);
/// The absolute value of the determinant of the Gram matrix of
/// `inner_product`, `|det(G)|`. This is independent of the choice of
/// basis, since a `GL_N(Z)` change of basis scales `det(G)` by
/// `det(A)^2 = 1`. It is `0` exactly when the form is degenerate
/// (`r > 0` in [`signature`](Self::signature)), and need not be an
/// integer for a non-integral rational lattice.
fn discriminant() -> Ratio<u128>;
/// The nonzero vectors of minimal norm, e.g. the roots of a root
/// lattice.
///
/// # Errors
///
/// Returns [`ShortVectorError::NotPositiveDefinite`] if
/// `signature() != (N, 0, 0)`, since an indefinite or degenerate form
/// need not have a finite set of minimal-norm vectors. May return
/// [`ShortVectorError::Unknown`] for a positive-definite lattice whose
/// minimal vectors this implementation does not compute.
fn short_vectors() -> Result<Vec<Self>, ShortVectorError>;
/// The discriminant-group quadratic form, evaluated at a representative
/// `x` of a class in the discriminant group `L*/L`.
///
/// Rather than model the quotient `L*/L` itself, this takes a
/// representative `x ∈ L*` directly: `<x, x>` for two representatives
/// of the same class in `L*/L` differ by an integer (or by an even
/// integer, if `L` is even). The result is normalized into that range —
/// `[0, 1)` in general, or `[0, 2)` if [`is_even`](Self::is_even) — so
/// equal classes always compare as equal `Ratio<i128>` values.
fn discriminant_group_quadratic(in_disc_group: Self::DualLattice) -> Ratio<i128> {
let raw = in_disc_group.lattice_norm_sq();
let modulus = Ratio::from_integer(if Self::is_even() { 2 } else { 1 });
raw - modulus * (raw / modulus).floor()
}
/// Sanity check from Milgram's formula: an even unimodular lattice must
/// have signature `p - q ≡ 0 (mod 8)`. Vacuously `true` when the
/// lattice isn't both even and self-dual, since that's the hypothesis
/// this checks. O(1): unlike a full discriminant-form Gauss sum (which
/// would apply to any even lattice, not just unimodular ones, but needs
/// to enumerate all of `L*/L`), this only ever inspects `signature()`.
#[must_use = "If this fails the implementation is broken"]
fn satisfies_milgram() -> bool {
if !(Self::is_even() && Self::is_self_dual()) {
return true;
}
let (p, q, _) = Self::signature();
(p as i128 - q as i128).rem_euclid(8) == 0
}
/// The weight of the theta series `Θ(τ) = Σ_{x ∈ L} q^{<x,x>/2}`: the
/// exponent `N/2` in its transformation law (see
/// [`theta_series_character`](Self::theta_series_character)), an
/// integer iff `N` is even.
///
/// `Θ` is a genuine power series in `q` only when `L` is even (every
/// exponent `<x,x>/2` is then an integer). Otherwise it's still a
/// perfectly good holomorphic function of `τ` — just one expanded in a
/// smaller nome: in `q^{1/2}` if `L` is integral but odd (some
/// exponents are half-integers), or in `q^{1/(2s)}` for a properly
/// rational `L` whose pairings have denominator `s` (worse still). None
/// of that is an error condition here, and doesn't require enumerating
/// any vectors, unlike the theta series itself; it only affects which
/// multiplier system `Θ` transforms under, which this method doesn't
/// otherwise track.
///
/// # Errors
///
/// Returns [`ShortVectorError::NotPositiveDefinite`] if
/// `signature() != (N, 0, 0)`: the sum then diverges (or is a
/// non-holomorphic Siegel theta function rather than a modular form of
/// weight `N/2`), since e.g. an isotropic vector contributes `q^0`
/// infinitely often along an indefinite direction.
fn theta_series_weight() -> Result<Ratio<i128>, ShortVectorError> {
if Self::signature() != (N, 0, 0) {
return Err(ShortVectorError::NotPositiveDefinite);
}
Ok(Ratio::new(N as i128, 2))
}
/// `χ(d)` in the theta series' transformation law: for a
/// positive-definite, integral lattice of even rank `N`, and any
/// `g = (a b; c d) ∈ Gamma_0(M)` (`M` the lattice's level — not
/// computed here),
///
/// `Θ(gτ) = Θ((aτ + b)/(cτ + d)) = χ(d) · (cτ + d)^{N/2} · Θ(τ)`,
///
/// where `Θ(τ) = Σ_{x∈L} q^{<x,x>/2}` and `χ(d) = (D|d)` is the
/// Kronecker symbol of `D = (-1)^{N/2} * disc(L)`. Returns `Ok(None)`
/// if this doesn't apply: `N` is odd (half-integral weight needs a
/// different multiplier theory), or `L` isn't integral (a properly
/// rational `L` needs an even finer multiplier system, per
/// [`theta_series_weight`](Self::theta_series_weight)). `L` being
/// integral but odd is *not* excluded here — the formula only needs
/// integrality, not evenness, of `L` itself.
///
/// # Errors
///
/// Propagates [`ShortVectorError::NotPositiveDefinite`] from
/// [`theta_series_weight`](Self::theta_series_weight).
fn theta_series_character(d: i128) -> Result<Option<i128>, ShortVectorError> {
Self::theta_series_weight()?;
if !N.is_multiple_of(2) || !Self::is_integral() {
return Ok(None);
}
let sign: i128 = if (N / 2).is_multiple_of(2) { 1 } else { -1 };
#[allow(clippy::cast_possible_wrap)]
let discriminant = sign * Self::discriminant().to_integer() as i128;
Ok(Some(kronecker_symbol(discriminant, d)))
}
}
#[cfg(test)]
mod tests {
use num::rational::Ratio;
use super::{Lattice, ShortVectorError};
use crate::lattice::{
HyperbolicPlane, NegatedLattice, RootLatticeA, RootLatticeE8, StandardLattice,
};
#[test]
fn e8_theta_series_is_weight_4_trivial_character() {
// Matches the classical fact that E8's theta series is the weight-4
// level-1 Eisenstein series E4 (trivial character, since disc = 1).
assert_eq!(
RootLatticeE8::theta_series_weight(),
Ok(Ratio::from_integer(4))
);
// Trivial character: kronecker_symbol(1, d) = 1 for any d.
assert_eq!(RootLatticeE8::theta_series_character(7), Ok(Some(1)));
assert_eq!(RootLatticeE8::theta_series_character(2), Ok(Some(1)));
}
#[test]
fn a2_theta_series_character_matches_q_sqrt_minus_3_splitting() {
// A_2's theta series is twisted by the discriminant -3 character,
// matching the discriminant of Q(sqrt(-3)).
assert_eq!(
RootLatticeA::<2>::theta_series_weight(),
Ok(Ratio::from_integer(1))
);
// 7 = 1 (mod 3) splits in Q(sqrt(-3)); 5 = 2 (mod 3) is inert.
assert_eq!(RootLatticeA::<2>::theta_series_character(7), Ok(Some(1)));
assert_eq!(RootLatticeA::<2>::theta_series_character(5), Ok(Some(-1)));
}
#[test]
fn integral_but_odd_lattice_still_has_a_character() {
assert!(StandardLattice::<4>::is_integral());
assert!(!StandardLattice::<4>::is_even());
assert_eq!(
StandardLattice::<4>::theta_series_weight(),
Ok(Ratio::from_integer(2))
);
// sign = (-1)^(4/2) = 1, disc = 1, so the discriminant is 1 and the
// character is trivial — Some(1), not None.
assert_eq!(StandardLattice::<4>::theta_series_character(7), Ok(Some(1)));
}
#[test]
fn odd_rank_has_no_integral_character() {
// A_3 (rank 3, odd) is still positive definite, so the weight 3/2
// is well-defined, but the integer-weight character doesn't apply.
assert_eq!(
RootLatticeA::<3>::theta_series_weight(),
Ok(Ratio::new(3, 2))
);
assert_eq!(RootLatticeA::<3>::theta_series_character(7), Ok(None));
}
#[test]
fn indefinite_lattice_has_divergent_theta_series() {
assert_eq!(
HyperbolicPlane::theta_series_weight(),
Err(ShortVectorError::NotPositiveDefinite)
);
assert_eq!(
HyperbolicPlane::theta_series_character(7),
Err(ShortVectorError::NotPositiveDefinite)
);
}
#[test]
fn even_unimodular_lattices_satisfy_milgram() {
assert!(HyperbolicPlane::satisfies_milgram()); // p - q = 0
assert!(RootLatticeE8::satisfies_milgram()); // p - q = 8
assert!(NegatedLattice::<RootLatticeE8>::satisfies_milgram()); // p - q = -8
}
#[test]
fn non_unimodular_even_lattice_is_vacuously_true() {
// A_3 is even but not self-dual, so the check doesn't apply.
assert!(!RootLatticeA::<3>::is_self_dual());
assert!(RootLatticeA::<3>::satisfies_milgram());
}
/// A deliberately wrong rank-1 lattice (claims even + self-dual with
/// signature `(1, 0, 0)`, i.e. `p - q = 1`) to confirm
/// `satisfies_milgram` actually detects a violation rather than always
/// passing vacuously.
#[derive(Clone, Copy, Debug, PartialEq)]
struct BrokenLattice(i128);
impl std::ops::Add for BrokenLattice {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}
}
impl std::ops::Sub for BrokenLattice {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self(self.0 - rhs.0)
}
}
impl std::ops::AddAssign for BrokenLattice {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
}
}
impl std::ops::SubAssign for BrokenLattice {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}
impl std::ops::Mul<i128> for BrokenLattice {
type Output = Self;
fn mul(self, rhs: i128) -> Self {
Self(self.0 * rhs)
}
}
impl std::ops::MulAssign<i128> for BrokenLattice {
fn mul_assign(&mut self, rhs: i128) {
self.0 *= rhs;
}
}
impl num::Zero for BrokenLattice {
fn zero() -> Self {
Self(0)
}
fn is_zero(&self) -> bool {
self.0 == 0
}
}
impl Lattice<1> for BrokenLattice {
type DualLattice = Self;
fn inner_product(&self, other: &Self) -> num::rational::Ratio<i128> {
num::rational::Ratio::from_integer(self.0 * other.0)
}
fn is_integral() -> bool {
true
}
fn is_even() -> bool {
true
}
fn is_self_dual() -> bool {
true
}
fn signature() -> (usize, usize, usize) {
(1, 0, 0)
}
fn discriminant() -> num::rational::Ratio<u128> {
num::rational::Ratio::from_integer(1)
}
fn short_vectors() -> Result<Vec<Self>, ShortVectorError> {
Err(ShortVectorError::Unknown)
}
}
#[test]
fn detects_a_genuine_milgram_violation() {
assert!(!BrokenLattice::satisfies_milgram());
}
}