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
//! Cyclotomic-ring trait surface.
//!
//! Every `ZZn` ring (`n in {4, 6, 8, 10, 12, 16, 20, 24, 32, 60}`) is generated
//! by `define_integral_zz!` in `integral_basis.rs` and lives in `rings.rs`.
//! All trait declarations that those rings implement -- and the marker
//! traits downstream code uses to constrain "is this a ring with feature
//! X" -- collect in this file.
//!
//! The split with `numtraits.rs` is "pure-numeric foundation lives there,
//! everything specific to a complex-valued cyclotomic ring lives here".
use ;
use Hash;
use Complex64;
use Integer;
use ;
use ;
// ----------------
// Type aliases.
/// The integer scalar used throughout: every ring stores `[i64; phi(n)]`.
pub type IntT = i64;
// ----------------
// Per-element abstract operations.
/// "Counterclockwise unit": the primitive generator of the ring's CCW
/// rotation group, i.e. `e^(2*pi*i/n)` for `ZZn`.
/// Complex conjugation. Each cyclotomic ring implements its own;
/// real-only scalars (i32/i64) would be the identity but the trait is
/// only used by complex ring elements.
/// Rings that contain the imaginary unit `i` (equivalently `4 | n`).
/// Sign extraction for the real and imaginary parts of a (complex) value.
/// Each helper returns `-1`, `0`, or `1`.
///
/// Used by geometric predicates (`geometry::intersect`) to avoid
/// materializing a real-subring intermediate value; each ring routes the
/// query through its exact-sign helper from `sign.rs`.
/// Trait-dispatch hook for the unit-length segment intersection test.
///
/// `intersect_unit_segments(s1, s2)` is equivalent to
/// `geometry::intersect(s1, s2)` *given* that both segments have unit
/// length. The canonical implementation lives in
/// [`crate::cyclotomic::integral_basis::intersect_unit_segments_basis`];
/// every ring implements this trait via the
/// `impl_integral_intersect_unit_segments_via_basis!` macro, supplying
/// only a `fn(&[i64; K]) -> i8` real-subring sign function. The trait
/// exists for type dispatch (so generic code can write
/// `T::intersect_unit_segments(...)` without naming the macro
/// machinery), not for opt-in per-ring specialization.
/// Integer cell coordinates `(floor(Re(self)), floor(Im(self)))` for
/// spatial bucketing in `geom::grid::UnitSquareGrid`.
///
/// Two entry points:
///
/// * [`cell_floor`](Self::cell_floor) -- the fast path. Returns the
/// f64-only floor of `complex64()`. In debug builds it cross-checks
/// against `cell_floor_exact` via `debug_assert_eq!`. This is what
/// the grid uses in the hot path (spatial bucketing only needs
/// deterministic answers, not bit-exact ones at boundary points).
///
/// * [`cell_floor_exact`](Self::cell_floor_exact) -- the slow path,
/// ~20% of `patch_enum`/`rat_enum` runtime when called per insertion.
/// Per-ring impls use the f64 floor as a hint, then verify each axis
/// via integer ring arithmetic + `re_sign`/`im_sign` checks. f64 is
/// only a hint; output is bit-exact.
///
/// Per-ring exact implementation strategy:
///
/// * For rings containing `i` (`HasZZ4`-style: ZZ4, ZZ8, ZZ12, ZZ16,
/// ZZ20, ZZ24, ZZ32, ZZ60): cell-corner construction goes through
/// `From<(i64, i64)>` and verification through the `rect_signs`
/// primitive.
/// * For rings without `i` (today: ZZ10): per-axis sign verification
/// using ring-specific shifted-sign primitives (no `i` needed).
/// Predicate "this point is within Euclidean radius `r` of the origin",
/// i.e. `|self|^2 <= r * r` with `r >= 0`.
///
/// Used by the DFS reachability pruning in `rat_enum`. Each ring routes
/// through `impl_integral_within_radius_via_norm_sq!` (exact via
/// `(r^2 - z*conj(z)).re_sign() >= 0`, pure ring arithmetic) or a
/// hand-rolled pure-i64 fast path (ZZ12).
// ----------------
// Top-level shared trait every ring implements.
/// Bound for the scalar coefficient type of a `SymNum` ring. In the
/// integer-basis storage `Self::Scalar = i64` for all rings, but kept as
/// an associated type so the trait can absorb a future field-of-fractions
/// variant.
/// Shared cyclotomic-ring trait. The `define_integral_zz!` macro emits
/// the per-ring impl.
/// Predicates that classify a cyclotomic ring element as purely real,
/// purely imaginary, or strictly complex.
// ----------------
// Catch-all ring bound and structural marker traits.
/// Cached lookup of roots of unity for a concrete ring.
///
/// Implementations are emitted by `impl_integral_units_via_basis!` (a
/// `OnceLock`-cached unit table built from `derive_units_lookup`) or by a
/// hand-rolled per-ring `static UNIT_TABLE: [[i64; PHI]; N] = [...]` for
/// hot-path rings (ZZ12).
/// **The cyclotomic-ring bound.** Every concrete `ZZn` ring implements
/// this catch-all collection, so downstream callers only need to write
/// `T: IsRing` (plus any subring-containment marker like `HasZZ4`) rather
/// than spelling out every individual capability.
///
/// In math, a field is a ring with division. The hierarchy here mirrors
/// that: a future `IsField` would be `IsRing + Div<Self, Output = Self>`,
/// so a function written against `T: IsRing` automatically accepts a
/// field too. Functions that actually require division ask for
/// `T: IsField` (when that exists).
// Future: pub trait IsField: IsRing + Div<Self, Output = Self> {}
// ----------------
// Subring-containment markers.
/// Ring is exactly `ZZ4` (all edges are axis-aligned unit segments).
/// Unlike `HasZZ4` (which includes `ZZ8`, `ZZ12`, etc.), only `ZZ4` itself
/// implements this.
/// Rings containing `ZZ4`. Implies `IsRing` plus the lattice-point
/// constructor `From<(i64, i64)>`, since `i = unit(qturn)` is in the ring.
/// Rings containing `ZZ6`.
/// Rings containing `ZZ8`.
/// Rings containing `ZZ10`.
/// Rings containing `ZZ12`.