1use std::alloc::Global;
2use std::collections::HashMap;
3use std::marker::PhantomData;
4use std::mem::replace;
5use std::rc::Rc;
6
7use feanor_serde::dependent_tuple::DeserializeSeedDependentTuple;
8use feanor_serde::impl_deserialize_seed_for_dependent_struct;
9use feanor_serde::map::DeserializeSeedMapped;
10use feanor_serde::newtype_struct::{DeserializeSeedNewtypeStruct, SerializableNewtypeStruct};
11use feanor_serde::seq::{DeserializeSeedSeq, SerializableSeq};
12use serde::{Deserialize, Serialize};
13
14use crate::algorithms::eea::{inv_crt, signed_gcd};
15use crate::algorithms::int_bisect::root_floor;
16use crate::algorithms::int_factor::factor;
17use crate::algorithms::linsolve::LinSolveRingStore;
18use crate::algorithms::linsolve::smith::{determinant_using_pre_smith, pre_smith};
19use crate::algorithms::lll::exact::lll;
20use crate::algorithms::matmul::{MatmulAlgorithm, STANDARD_MATMUL};
21use crate::divisibility::{DivisibilityRing, DivisibilityRingStore};
22use crate::field::FieldStore;
23use crate::group::{HashableGroupEl, MultGroup, *};
24use crate::homomorphism::Homomorphism;
25use crate::integer::{BigIntRing, int_cast};
26use crate::iters::multi_cartesian_product;
27use crate::matrix::transform::TransformTarget;
28use crate::matrix::*;
29use crate::ordered::OrderedRingStore;
30use crate::pid::PrincipalIdealRingStore;
31use crate::primitive_int::StaticRing;
32use crate::ring::*;
33use crate::rings::finite::FiniteRingStore;
34use crate::rings::rational::RationalField;
35use crate::rings::zn::{ZnRing, ZnRingStore, zn_big};
36use crate::serialization::{DeserializeWithRing, SerializeWithRing};
37
38const ZZ: StaticRing<i64> = StaticRing::<i64>::RING;
39const ZZbig: BigIntRing = BigIntRing::RING;
40
41#[stability::unstable(feature = "enable")]
50pub struct SubgroupBase<G: AbelianGroupStore> {
51 parent: G,
52 generators: Vec<GroupEl<G>>,
53 order_multiple: El<BigIntRing>,
54 order_factorization: Vec<(i64, usize)>,
55 scaled_relation_lattices: Vec<Vec<OwnedMatrix<i64>>>,
59 scaled_generating_sets: Vec<Vec<Vec<GroupEl<G>>>>,
64}
65
66#[stability::unstable(feature = "enable")]
68#[allow(type_alias_bounds)]
69pub type Subgroup<G: AbelianGroupStore> = GroupValue<SubgroupBase<G>>;
70
71impl<G: AbelianGroupStore> Subgroup<G> {
72 #[stability::unstable(feature = "enable")]
79 pub fn new(group: G, order_multiple: El<BigIntRing>, generators: Vec<GroupEl<G>>) -> Self {
80 let n = generators.len();
81 if n == 0 {
82 return GroupValue::from(SubgroupBase {
83 parent: group,
84 generators: Vec::new(),
85 order_multiple: ZZbig.clone_el(&order_multiple),
86 order_factorization: factor(ZZbig, order_multiple)
87 .into_iter()
88 .map(|(p, e)| (int_cast(p, ZZ, ZZbig), e))
89 .collect(),
90 scaled_generating_sets: Vec::new(),
91 scaled_relation_lattices: Vec::new(),
92 });
93 } else {
94 let mut result = Self::new(group, order_multiple, Vec::new());
95 for g in generators {
96 result = result.add_generator(g);
97 }
98 return result;
99 }
100 }
101
102 #[stability::unstable(feature = "enable")]
104 pub fn parent(&self) -> &G { self.get_group().parent() }
105
106 #[stability::unstable(feature = "enable")]
108 pub fn subgroup_order(&self) -> El<BigIntRing> { self.get_group().subgroup_order() }
109
110 #[stability::unstable(feature = "enable")]
112 pub fn generators(&self) -> &[GroupEl<G>] { self.get_group().generators() }
113
114 #[stability::unstable(feature = "enable")]
116 pub fn add_generator(self, new_gen_base: GroupEl<G>) -> Self { Self::from(self.into().add_generator(new_gen_base)) }
117
118 #[stability::unstable(feature = "enable")]
121 pub fn contains(&self, element: &GroupEl<G>) -> bool { self.get_group().contains(element) }
122
123 #[stability::unstable(feature = "enable")]
126 pub fn dlog(&self, target: &GroupEl<G>) -> Option<Vec<i64>> { self.get_group().dlog(target) }
127
128 #[stability::unstable(feature = "enable")]
130 pub fn enumerate_elements<'a>(&'a self) -> impl use<'a, G> + Clone + Iterator<Item = GroupEl<G>> {
131 self.get_group().enumerate_elements()
132 }
133}
134
135impl<G: AbelianGroupStore> SubgroupBase<G> {
136 #[stability::unstable(feature = "enable")]
137 pub fn parent(&self) -> &G { &self.parent }
138
139 #[stability::unstable(feature = "enable")]
141 pub fn subgroup_order(&self) -> El<BigIntRing> {
142 let mut result = ZZbig.one();
143 let n = self.generators.len();
144 if n == 0 {
145 return result;
146 }
147 for i in 0..self.order_factorization.len() {
148 let (p, e) = self.order_factorization[i];
149 let relation_lattice = self.scaled_relation_lattices[i][e].data();
150 let Zpne = zn_big::Zn::new(ZZbig, ZZbig.pow(int_cast(p, ZZbig, StaticRing::<i64>::RING), e * n));
151 let mod_pne = Zpne.can_hom(&StaticRing::<i64>::RING).unwrap();
152 let relation_lattice_det = determinant_using_pre_smith(
153 &Zpne,
154 OwnedMatrix::from_fn(relation_lattice.row_count(), relation_lattice.col_count(), |k, l| {
155 mod_pne.map(*relation_lattice.at(k, l))
156 })
157 .data_mut(),
158 Global,
159 );
160 ZZbig.mul_assign(
161 &mut result,
162 signed_gcd(
163 ZZbig.clone_el(Zpne.modulus()),
164 Zpne.smallest_positive_lift(relation_lattice_det),
165 ZZbig,
166 ),
167 );
168 }
169 return result;
170 }
171
172 #[stability::unstable(feature = "enable")]
175 pub fn order_multiple(&self) -> &El<BigIntRing> { &self.order_multiple }
176
177 #[stability::unstable(feature = "enable")]
179 pub fn generators(&self) -> &[GroupEl<G>] { &self.generators }
180
181 #[stability::unstable(feature = "enable")]
186 pub fn add_generator(self, new_gen_base: GroupEl<G>) -> Self {
187 let group = &self.parent;
188 assert!(group.is_identity(&group.pow(&new_gen_base, &self.order_multiple)));
189 let ZZ_to_ZZbig = ZZbig.can_hom(&ZZ).unwrap();
190
191 let mut scaled_relation_lattices = Vec::new();
192 let mut scaled_generating_sets = Vec::new();
193 for p_idx in 0..self.order_factorization.len() {
194 let (p, e) = self.order_factorization[p_idx];
195 let p_bigint = ZZ_to_ZZbig.map(p);
196 let power = ZZbig
197 .checked_div(&self.order_multiple, &ZZbig.pow(ZZbig.clone_el(&p_bigint), e))
198 .unwrap();
199 let gens = self.generators.iter().map(|g| group.pow(g, &power)).collect::<Vec<_>>();
200 let new_gen = group.pow(&new_gen_base, &power);
201
202 let n = self.generators.len();
203
204 let mut main_relation_matrix = OwnedMatrix::zero(n + 1, n + 1, ZZ);
205 for i in 0..n {
206 for j in 0..n {
207 *main_relation_matrix.at_mut(i, j) = *self.scaled_relation_lattices[p_idx][e].at(i, j);
208 }
209 }
210 *main_relation_matrix.at_mut(n, n) = -ZZ.pow(p, e);
211 for k in 0..e {
212 if let Some(dlog) =
213 self.padic_dlog(p_idx, e, &group.pow(&new_gen, &ZZbig.pow(ZZbig.clone_el(&p_bigint), k)))
214 {
215 *main_relation_matrix.at_mut(n, n) = -ZZ.pow(p, k);
216 for j in 0..n {
217 *main_relation_matrix.at_mut(n, j) = dlog[j];
218 }
219 break;
220 }
221 }
222 debug_assert!(main_relation_matrix.data().row_iter().all(|row| group.is_identity(
223 &(0..n).fold(group.pow(&new_gen, &ZZ_to_ZZbig.map(row[n])), |current, i| {
224 group.op(current, group.pow(&gens[i], &ZZ_to_ZZbig.map(row[i])))
225 })
226 )));
227
228 let mut result = Vec::with_capacity(e + 1);
229 result.push(main_relation_matrix);
230 for _ in 0..e {
231 result.push(Self::relation_lattice_basis_downscale_p(
232 result.last().unwrap().data(),
233 p,
234 ));
235 }
236 result.reverse();
237 scaled_relation_lattices.push(result);
238
239 let mut generating_sets = Vec::new();
240 for i in 0..e {
241 let generating_set = scaled_relation_lattices.last().unwrap()[i]
242 .data()
243 .row_iter()
244 .map(|row| {
245 let scale = ZZbig.pow(ZZbig.clone_el(&p_bigint), e - i - 1);
246 let result = (0..n).fold(
247 group.pow(&new_gen, &ZZ_to_ZZbig.mul_ref_map(&scale, &row[n])),
248 |current, j| {
249 group.op(current, group.pow(&gens[j], &ZZ_to_ZZbig.mul_ref_map(&scale, &row[j])))
250 },
251 );
252 debug_assert!(group.is_identity(&group.pow(&result, &p_bigint)));
253 result
254 })
255 .collect::<Vec<_>>();
256 generating_sets.push(generating_set);
257 }
258 scaled_generating_sets.push(generating_sets);
259 }
260
261 return Self {
262 generators: self
263 .generators
264 .iter()
265 .map(|g| group.clone_el(g))
266 .chain([new_gen_base])
267 .collect(),
268 order_multiple: ZZbig.clone_el(&self.order_multiple),
269 order_factorization: self.order_factorization.clone(),
270 scaled_generating_sets,
271 scaled_relation_lattices,
272 parent: self.parent,
273 };
274 }
275
276 fn padic_dlog(&self, p_idx: usize, e: usize, target: &GroupEl<G>) -> Option<Vec<i64>> {
292 let group = &self.parent;
293 let ZZ_to_ZZbig = ZZbig.can_hom(&ZZ).unwrap();
294
295 let n = self.generators.len();
296 if n == 0 {
297 return if group.is_identity(target) {
298 Some(Vec::new())
299 } else {
300 None
301 };
302 } else if e == 0 {
303 debug_assert!(group.is_identity(target));
304 return Some((0..n).map(|_| 0).collect());
305 }
306
307 let p = self.order_factorization[p_idx].0;
308 debug_assert!(group.is_identity(&group.pow(target, &ZZbig.pow(ZZ_to_ZZbig.map(p), e))));
309
310 let power = ZZbig
311 .checked_div(&self.order_multiple, &ZZbig.pow(int_cast(p, ZZbig, ZZ), e))
312 .unwrap();
313 let gens = self.generators.iter().map(|g| group.pow(g, &power)).collect::<Vec<_>>();
314
315 let G_mod_H_dlog = self.padic_dlog(p_idx, e - 1, &group.pow(target, &ZZ_to_ZZbig.map(p)))?;
318 debug_assert!(group.eq_el(
319 &group.pow(target, &ZZ_to_ZZbig.map(p)),
320 &(0..n).fold(group.identity(), |current, i| {
321 group.op(current, group.pow(&gens[i], &ZZ_to_ZZbig.map(p * G_mod_H_dlog[i])))
322 })
323 ));
324
325 let delta = (0..n).fold(group.clone_el(target), |current, i| {
327 group.op(current, group.pow(&gens[i], &ZZ_to_ZZbig.map(-G_mod_H_dlog[i])))
328 });
329 debug_assert!(group.is_identity(&group.pow(&delta, &ZZ_to_ZZbig.map(p))));
330
331 let H_generators = &self.scaled_generating_sets[p_idx][e - 1];
332
333 let H_dlog_wrt_H_gens = baby_giant_step(
334 group,
335 delta,
336 H_generators,
337 &(0..n).map(|_| int_cast(p, ZZbig, ZZ)).collect::<Vec<_>>(),
338 )?;
339 let H_dlog = {
340 let mut result = (0..n).map(|_| 0).collect::<Vec<_>>();
341 STANDARD_MATMUL.matmul(
342 TransposableSubmatrix::from(Submatrix::from_1d(&H_dlog_wrt_H_gens, 1, n)),
343 TransposableSubmatrix::from(self.scaled_relation_lattices[p_idx][e - 1].data()),
344 TransposableSubmatrixMut::from(SubmatrixMut::from_1d(&mut result, 1, n)),
345 ZZ,
346 );
347 result
348 };
349
350 let result = G_mod_H_dlog
351 .into_iter()
352 .zip(H_dlog)
353 .map(|(x, y)| x + y)
354 .collect::<Vec<_>>();
355 debug_assert!(group.eq_el(
356 target,
357 &(0..n).fold(group.identity(), |current, i| {
358 group.op(current, group.pow(&gens[i], &ZZ_to_ZZbig.map(result[i])))
359 })
360 ));
361
362 return Some(result);
363 }
364
365 #[stability::unstable(feature = "enable")]
367 pub fn contains(&self, element: &GroupEl<G>) -> bool { self.dlog(element).is_some() }
368
369 #[stability::unstable(feature = "enable")]
373 pub fn dlog(&self, target: &GroupEl<G>) -> Option<Vec<i64>> {
374 let group = &self.parent;
375 let ZZ_to_ZZbig = ZZbig.can_hom(&ZZ).unwrap();
376
377 let n = self.generators.len();
378 if n == 0 {
379 return if group.is_identity(target) {
380 Some(Vec::new())
381 } else {
382 None
383 };
384 }
385
386 let mut current_dlog = (0..n).map(|_| 0).collect::<Vec<_>>();
387 let mut current_order = (0..n).map(|_| 1).collect::<Vec<_>>();
388
389 for p_idx in 0..self.order_factorization.len() {
390 let (p, e) = self.order_factorization[p_idx];
391 let power = ZZbig
392 .checked_div(&self.order_multiple, &ZZbig.pow(int_cast(p, ZZbig, ZZ), e))
393 .unwrap();
394 let padic_dlog = self.padic_dlog(p_idx, e, &group.pow(target, &power))?;
395 for j in 0..n {
396 current_dlog[j] = inv_crt(current_dlog[j], padic_dlog[j], ¤t_order[j], &ZZ.pow(p, e), ZZ);
397 current_order[j] *= ZZ.pow(p, e);
398 }
399 }
400 debug_assert!(group.eq_el(
401 target,
402 &(0..n).fold(group.identity(), |current, i| group.op(
403 current,
404 group.pow(&self.generators[i], &ZZ_to_ZZbig.map(current_dlog[i]))
405 ))
406 ));
407
408 return Some(current_dlog);
409 }
410
411 fn padic_rectangular_form(&self, p_idx: usize) -> Vec<(GroupEl<G>, usize)> {
412 let group = &self.parent;
413 let (p, e) = self.order_factorization[p_idx];
414 let power = ZZbig
415 .checked_div(&self.order_multiple, &ZZbig.pow(int_cast(p, ZZbig, ZZ), e))
416 .unwrap();
417 let n = self.generators.len();
418
419 if n == 0 {
420 return Vec::new();
421 }
422
423 let Zpne = zn_big::Zn::new(ZZbig, ZZbig.pow(int_cast(p, ZZbig, StaticRing::<i64>::RING), e * n));
424 let mod_pne = Zpne.can_hom(&StaticRing::<i64>::RING).unwrap();
425 let relation_lattice = self.scaled_relation_lattices[p_idx][e].data();
426 let mut relation_lattice_mod_pne =
427 OwnedMatrix::from_fn(relation_lattice.row_count(), relation_lattice.col_count(), |k, l| {
428 mod_pne.map(*relation_lattice.at(k, l))
429 });
430 let mut generators = self.generators.iter().map(|g| group.pow(g, &power)).collect::<Vec<_>>();
431
432 struct TransformGenerators<'a, G: AbelianGroupStore> {
433 group: &'a G,
434 generators: &'a mut [GroupEl<G>],
435 }
436 impl<'a, G: AbelianGroupStore> TransformTarget<zn_big::ZnBase<BigIntRing>> for TransformGenerators<'a, G> {
437 fn transform<S: Copy + RingStore<Type = zn_big::ZnBase<BigIntRing>>>(
438 &mut self,
439 ring: S,
440 i: usize,
441 j: usize,
442 transform: &[El<zn_big::Zn<BigIntRing>>; 4],
443 ) {
444 let transform_inv_det = ring
445 .invert(&ring.sub(
446 ring.mul_ref(&transform[0], &transform[3]),
447 ring.mul_ref(&transform[1], &transform[2]),
448 ))
449 .unwrap();
450 let inv_transform = [
451 ring.smallest_positive_lift(ring.mul_ref(&transform[3], &transform_inv_det)),
452 ring.smallest_positive_lift(ring.negate(ring.mul_ref(&transform[1], &transform_inv_det))),
453 ring.smallest_positive_lift(ring.negate(ring.mul_ref(&transform[2], &transform_inv_det))),
454 ring.smallest_positive_lift(ring.mul_ref(&transform[0], &transform_inv_det)),
455 ];
456 let new_gens = (
457 self.group.op(
458 self.group.pow(&self.generators[i], &inv_transform[0]),
459 self.group.pow(&self.generators[j], &inv_transform[1]),
460 ),
461 self.group.op(
462 self.group.pow(&self.generators[i], &inv_transform[2]),
463 self.group.pow(&self.generators[j], &inv_transform[3]),
464 ),
465 );
466 self.generators[i] = new_gens.0;
467 self.generators[j] = new_gens.1;
468 }
469 }
470
471 pre_smith(
472 &Zpne,
473 &mut (),
474 &mut TransformGenerators {
475 group,
476 generators: &mut generators,
477 },
478 relation_lattice_mod_pne.data_mut(),
479 );
480
481 return generators
482 .into_iter()
483 .enumerate()
484 .map(|(i, g)| {
485 (
486 g,
487 int_cast(
488 ZZbig.ideal_gen(
489 Zpne.modulus(),
490 &Zpne.smallest_positive_lift(Zpne.clone_el(relation_lattice_mod_pne.at(i, i))),
491 ),
492 ZZ,
493 ZZbig,
494 ) as usize,
495 )
496 })
497 .collect();
498 }
499
500 #[stability::unstable(feature = "enable")]
503 pub fn rectangular_form<'a>(&'a self) -> Vec<(GroupEl<G>, usize)> {
504 (0..self.order_factorization.len())
505 .flat_map(|p_idx| self.padic_rectangular_form(p_idx))
506 .collect()
507 }
508
509 #[stability::unstable(feature = "enable")]
512 pub fn enumerate_elements<'a>(&'a self) -> impl use<'a, G> + Clone + Iterator<Item = GroupEl<G>> {
513 let rectangular_form = Rc::new(self.rectangular_form());
514 multi_cartesian_product(
515 rectangular_form
516 .iter()
517 .map(|(_, l)| 0..*l)
518 .collect::<Vec<_>>()
519 .into_iter(),
520 move |pows| {
521 pows.iter()
522 .enumerate()
523 .fold(self.parent().identity(), |current, (i, e)| {
524 self.parent().op(
525 current,
526 self.parent()
527 .pow(&rectangular_form[i].0, &int_cast(*e as i64, ZZbig, ZZ)),
528 )
529 })
530 },
531 |_, x| *x,
532 )
533 }
534
535 fn relation_lattice_basis_downscale_p<V>(basis: Submatrix<V, i64>, p: i64) -> OwnedMatrix<i64>
539 where
540 V: AsPointerToSlice<i64>,
541 {
542 let n = basis.row_count();
543 assert_eq!(n, basis.col_count());
544
545 let QQ = RationalField::new(ZZbig);
546 let ZZ_to_QQ = QQ.inclusion().compose(QQ.base_ring().can_hom(&ZZ).unwrap());
547 let as_ZZ = |x| int_cast(ZZbig.checked_div(QQ.num(x), QQ.den(x)).unwrap(), ZZ, ZZbig);
548
549 let mut dual_basis = OwnedMatrix::identity(n, 2 * n, &QQ);
550 let mut Binv = dual_basis.data_mut().submatrix(0..n, n..(2 * n));
551 let mut rhs = OwnedMatrix::identity(n, n, &QQ);
552 QQ.solve_right(
553 OwnedMatrix::from_fn(n, n, |i, j| ZZ_to_QQ.map_ref(basis.at(i, j))).data_mut(),
554 rhs.data_mut(),
555 Binv.reborrow(),
556 )
557 .assert_solved();
558 Binv.reborrow()
559 .row_iter()
560 .flat_map(|row| row.iter_mut())
561 .for_each(|x| ZZ_to_QQ.mul_assign_map(x, p));
562
563 let mut identity = OwnedMatrix::identity(n, n, &QQ);
564 lll(
565 dual_basis.data_mut(),
566 QQ.identity(),
567 &QQ.div(&ZZ_to_QQ.map(9), &ZZ_to_QQ.map(10)),
568 false,
569 &mut (),
570 );
571
572 let mut result_QQ = rhs;
573 QQ.solve_right(
574 dual_basis.data_mut().submatrix(0..n, n..(2 * n)),
575 identity.data_mut(),
576 result_QQ.data_mut(),
577 )
578 .assert_solved();
579
580 let result = OwnedMatrix::from_fn(n, n, |i, j| as_ZZ(result_QQ.at(i, j)));
581
582 return result;
583 }
584}
585
586impl<G: AbelianGroupStore> PartialEq for SubgroupBase<G> {
587 fn eq(&self, other: &Self) -> bool {
588 self.parent().get_group() == other.parent().get_group()
589 && other.generators().iter().all(|g| self.contains(g))
590 && self.generators().iter().all(|g| other.contains(g))
591 }
592}
593
594impl<G: AbelianGroupStore> AbelianGroupBase for SubgroupBase<G> {
595 type Element = GroupEl<G>;
596
597 fn clone_el(&self, x: &Self::Element) -> Self::Element { self.parent().clone_el(x) }
598
599 fn eq_el(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool { self.parent().eq_el(lhs, rhs) }
600
601 fn hash<H: std::hash::Hasher>(&self, x: &Self::Element, hasher: &mut H) { self.parent().hash(x, hasher) }
602
603 fn identity(&self) -> Self::Element { self.parent().identity() }
604
605 fn inv(&self, x: &Self::Element) -> Self::Element { self.parent().inv(x) }
606
607 fn is_identity(&self, x: &Self::Element) -> bool { self.parent().is_identity(x) }
608
609 fn op(&self, lhs: Self::Element, rhs: Self::Element) -> Self::Element { self.parent().op(lhs, rhs) }
610
611 fn pow(&self, x: &Self::Element, e: &El<BigIntRing>) -> Self::Element { self.parent().pow(x, e) }
612
613 fn op_ref(&self, lhs: &Self::Element, rhs: &Self::Element) -> Self::Element { self.parent().op_ref(lhs, rhs) }
614
615 fn op_ref_snd(&self, lhs: Self::Element, rhs: &Self::Element) -> Self::Element {
616 self.parent().op_ref_snd(lhs, rhs)
617 }
618}
619
620impl<G: AbelianGroupStore + Serialize> Serialize for SubgroupBase<G>
621where
622 G::Type: SerializableElementGroup,
623{
624 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
625 where
626 S: serde::Serializer,
627 {
628 #[derive(Serialize)]
629 struct SubgroupData<'a, Gens: Serialize> {
630 order_multiple: SerializeWithRing<'a, BigIntRing>,
631 generators: Gens,
632 group: (),
633 }
634 SerializableNewtypeStruct::new(
635 "Subgroup",
636 (
637 self.parent(),
638 SubgroupData {
639 order_multiple: SerializeWithRing::new(&self.order_multiple, ZZbig),
640 generators: SerializableSeq::new(
641 self.generators
642 .iter()
643 .map(|g| SerializeWithGroup::new(g, self.parent())),
644 ),
645 group: (),
646 },
647 ),
648 )
649 .serialize(serializer)
650 }
651}
652
653impl<'de, G: AbelianGroupStore + Clone + Deserialize<'de>> Deserialize<'de> for SubgroupBase<G>
654where
655 G::Type: SerializableElementGroup,
656{
657 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
658 where
659 D: serde::Deserializer<'de>,
660 {
661 use serde::de::DeserializeSeed;
662
663 struct DeserializeSeedSubgroupData<G: AbelianGroupStore>
664 where
665 G::Type: SerializableElementGroup,
666 {
667 group: G,
668 }
669
670 impl_deserialize_seed_for_dependent_struct! {
671 <{ 'de, G }> pub struct SubgroupData<{'de, G}> using DeserializeSeedSubgroupData<G> {
672 order_multiple: El<BigIntRing>: |_| DeserializeWithRing::new(ZZbig),
673 generators: Vec<GroupEl<G>>: |master: &DeserializeSeedSubgroupData<G>| {
674 let group_clone = master.group.clone();
675 DeserializeSeedSeq::new((0..).map(move |_| DeserializeWithGroup::new(group_clone.clone())), Vec::new(), |mut current, next| { current.push(next); current })
676 },
677 group: G: |master: &DeserializeSeedSubgroupData<G>| {
678 let group_clone = master.group.clone();
679 DeserializeSeedMapped::new(PhantomData::<()>, move |()| group_clone)
680 }
681 } where G: AbelianGroupStore + Clone, G::Type: SerializableElementGroup
682 }
683
684 DeserializeSeedNewtypeStruct::new(
685 "Subgroup",
686 DeserializeSeedDependentTuple::new(PhantomData::<G>, |group| DeserializeSeedSubgroupData { group }),
687 )
688 .deserialize(deserializer)
689 .map(|data| Subgroup::new(data.group, data.order_multiple, data.generators).into())
690 }
691}
692
693impl<G: AbelianGroupStore> Clone for SubgroupBase<G>
694where
695 G: Clone,
696{
697 fn clone(&self) -> Self {
698 Self {
699 parent: self.parent.clone(),
700 generators: self.generators.iter().map(|g| self.parent.clone_el(g)).collect(),
701 order_factorization: self.order_factorization.clone(),
702 order_multiple: self.order_multiple.clone(),
703 scaled_generating_sets: self
704 .scaled_generating_sets
705 .iter()
706 .map(|sets| {
707 sets.iter()
708 .map(|set| set.iter().map(|g| self.parent.clone_el(g)).collect())
709 .collect()
710 })
711 .collect(),
712 scaled_relation_lattices: self
713 .scaled_relation_lattices
714 .iter()
715 .map(|x| x.iter().map(|x| x.clone_matrix(StaticRing::<i64>::RING)).collect())
716 .collect(),
717 }
718 }
719}
720
721impl<R> Subgroup<MultGroup<R>>
722where
723 R: RingStore,
724 R::Type: ZnRing + HashableElRing + DivisibilityRing,
725{
726 #[stability::unstable(feature = "enable")]
727 pub fn for_zn(group: MultGroup<R>, generators: Vec<GroupEl<MultGroup<R>>>) -> Self {
728 let n = generators.len();
729 if n == 0 {
730 let n_factorization = factor(ZZbig, group.underlying_ring().size(ZZbig).unwrap());
731 let mut order_factorization = n_factorization
732 .into_iter()
733 .flat_map(|(p, e)| {
734 factor(ZZbig, ZZbig.sub_ref_fst(&p, ZZbig.one()))
735 .into_iter()
736 .chain([(p, e - 1)])
737 })
738 .collect::<Vec<_>>();
739 order_factorization.sort_unstable_by(|(pl, _), (pr, _)| ZZbig.cmp(pl, pr));
740 order_factorization.dedup_by(|(p1, e1), (p2, e2)| {
741 if ZZbig.eq_el(p1, p2) {
742 *e2 += *e1;
743 true
744 } else {
745 false
746 }
747 });
748 order_factorization.retain(|(_, e)| *e > 0);
749 let order = ZZbig.prod(
750 order_factorization
751 .iter()
752 .map(|(p, e)| ZZbig.pow(ZZbig.clone_el(p), *e)),
753 );
754 let order_factorization = order_factorization
755 .into_iter()
756 .map(|(p, e)| (int_cast(p, ZZ, ZZbig), e))
757 .collect();
758 return Self::from(SubgroupBase {
759 parent: group,
760 generators: Vec::new(),
761 order_multiple: order,
762 order_factorization,
763 scaled_generating_sets: Vec::new(),
764 scaled_relation_lattices: Vec::new(),
765 });
766 } else {
767 let mut result = Self::for_zn(group, Vec::new());
768 for g in generators {
769 result = result.add_generator(g);
770 }
771 return result;
772 }
773 }
774}
775
776#[stability::unstable(feature = "enable")]
841pub fn baby_giant_step<G>(
842 group: G,
843 value: GroupEl<G>,
844 generators: &[GroupEl<G>],
845 dlog_bounds: &[El<BigIntRing>],
846) -> Option<Vec<i64>>
847where
848 G: AbelianGroupStore,
849{
850 let n = generators.len();
851 assert_eq!(n, dlog_bounds.len());
852 if generators.is_empty() {
853 if group.is_identity(&value) {
854 return Some(Vec::new());
855 } else {
856 return None;
857 }
858 }
859 let ns = dlog_bounds
860 .iter()
861 .map(|n| int_cast(root_floor(ZZbig, n.clone(), 2), ZZ, ZZbig) + 1)
862 .collect::<Vec<_>>();
863 let count = int_cast(ZZbig.prod(ns.iter().map(|n| int_cast(*n, ZZbig, ZZ))), ZZ, ZZbig);
864 let mut baby_step_table: HashMap<HashableGroupEl<_>, i64> = HashMap::with_capacity(count as usize);
865
866 {
868 let mut current_els = (0..n).map(|_| group.clone_el(&value)).collect::<Vec<_>>();
869 let mut current_idxs = (0..n).map(|_| 0).collect::<Vec<_>>();
870 for idx in 0..count {
871 _ = baby_step_table.insert(HashableGroupEl::new(&group, group.clone_el(¤t_els[n - 1])), idx);
872
873 let mut i = n - 1;
874 while current_idxs[i] == ns[i] - 1 {
875 if i == 0 {
876 assert!(idx + 1 == count);
877 break;
878 }
879 current_idxs[i] = 0;
880 i -= 1;
881 }
882 current_idxs[i] += 1;
883 current_els[i] = group.op_ref_snd(replace(&mut current_els[i], group.identity()), &generators[i]);
884 for j in (i + 1)..n {
885 current_els[j] = group.clone_el(¤t_els[i]);
886 }
887 }
888 }
889
890 let giant_steps = generators
891 .iter()
892 .zip(ns.iter())
893 .map(|(g, n)| group.pow(g, &int_cast(*n, ZZbig, ZZ)))
894 .collect::<Vec<_>>();
895 {
897 let start_el = giant_steps.iter().fold(group.identity(), |x, y| group.op_ref_snd(x, y));
898 let mut current_els = (0..n).map(|_| group.clone_el(&start_el)).collect::<Vec<_>>();
899 let mut current_idxs = (0..n).map(|_| 1).collect::<Vec<_>>();
900 for idx in 0..count {
901 if let Some(bs_idx) =
902 baby_step_table.get(&HashableGroupEl::new(&group, group.clone_el(¤t_els[n - 1])))
903 {
904 let mut bs_idx = *bs_idx;
905 let mut result = current_idxs.clone();
906 for j in (0..n).rev() {
907 let bs_idxs_j = bs_idx % ns[j];
908 bs_idx /= ns[j];
909 result[j] = result[j] * ns[j] - bs_idxs_j;
910 }
911 if (0..dlog_bounds.len()).all(|j| ZZbig.is_leq(&int_cast(result[j], ZZbig, ZZ), &dlog_bounds[j])) {
912 debug_assert_eq!(n, result.len());
913 return Some(result);
914 }
915 }
916
917 let mut i = n - 1;
918 while current_idxs[i] == ns[i] {
919 if i == 0 {
920 assert!(idx + 1 == count);
921 break;
922 }
923 current_idxs[i] = 1;
924 i -= 1;
925 }
926 current_idxs[i] += 1;
927 current_els[i] = group.op_ref_snd(replace(&mut current_els[i], group.identity()), &giant_steps[i]);
928 for j in (i + 1)..n {
929 current_els[j] = group.clone_el(¤t_els[i]);
930 }
931 }
932 }
933
934 return None;
935}
936
937pub fn finite_field_discrete_log<R: RingStore>(value: El<R>, base: El<R>, Zn: R) -> Option<i64>
942where
943 R::Type: ZnRing + HashableElRing,
944{
945 let group = MultGroup::new(Zn);
946 let generators = vec![group.from_ring_el(base).unwrap()];
947 let subgroup = Subgroup::for_zn(group, generators);
948 return subgroup
949 .dlog(&subgroup.parent().from_ring_el(value).unwrap())
950 .map(|res| res[0]);
951}
952
953pub fn multiplicative_order<R: RingStore>(x: El<R>, Zn: R) -> i64
955where
956 R::Type: ZnRing + HashableElRing,
957{
958 let group = MultGroup::new(Zn);
959 let gen_set = Subgroup::for_zn(group, Vec::new());
960 let Zn = gen_set.parent().underlying_ring();
961
962 let mut result = ZZbig.one();
963 for (p, e) in &gen_set.get_group().order_factorization {
964 let mut current = Zn.pow_gen(
965 Zn.clone_el(&x),
966 &ZZbig
967 .checked_div(
968 &gen_set.get_group().order_multiple,
969 &ZZbig.pow(int_cast(*p, ZZbig, ZZ), *e),
970 )
971 .unwrap(),
972 ZZbig,
973 );
974 while !Zn.is_one(¤t) {
975 current = Zn.pow(current, *p as usize);
976 ZZbig.mul_assign(&mut result, int_cast(*p, ZZbig, ZZ));
977 }
978 }
979 return int_cast(result, ZZ, ZZbig);
980}
981
982#[cfg(test)]
983struct ProdGroupBase<G: AbelianGroupStore, const N: usize>(G);
984
985#[cfg(test)]
986impl<G: AbelianGroupStore, const N: usize> PartialEq for ProdGroupBase<G, N> {
987 fn eq(&self, other: &Self) -> bool { self.0.get_group() == other.0.get_group() }
988}
989
990#[cfg(test)]
991impl<G: AbelianGroupStore, const N: usize> AbelianGroupBase for ProdGroupBase<G, N> {
992 type Element = [GroupEl<G>; N];
993
994 fn clone_el(&self, x: &Self::Element) -> Self::Element { from_fn(|i| self.0.clone_el(&x[i])) }
995
996 fn eq_el(&self, lhs: &Self::Element, rhs: &Self::Element) -> bool { (0..N).all(|i| self.0.eq_el(&lhs[i], &rhs[i])) }
997
998 fn op(&self, lhs: Self::Element, rhs: Self::Element) -> Self::Element {
999 from_fn(|i| self.0.op_ref(&lhs[i], &rhs[i]))
1000 }
1001
1002 fn hash<H: std::hash::Hasher>(&self, x: &Self::Element, hasher: &mut H) {
1003 for i in 0..N {
1004 self.0.hash(&x[i], hasher)
1005 }
1006 }
1007
1008 fn inv(&self, x: &Self::Element) -> Self::Element { from_fn(|i| self.0.inv(&x[i])) }
1009
1010 fn identity(&self) -> Self::Element { from_fn(|_| self.0.identity()) }
1011}
1012
1013#[cfg(test)]
1014use std::array::from_fn;
1015
1016#[cfg(test)]
1017use oorandom::Rand64;
1018
1019#[cfg(test)]
1020use crate::RANDOM_TEST_INSTANCE_COUNT;
1021#[cfg(test)]
1022use crate::algorithms::matmul::ComputeInnerProduct;
1023#[cfg(test)]
1024use crate::assert_matrix_eq;
1025#[cfg(test)]
1026use crate::group::AddGroup;
1027#[cfg(test)]
1028use crate::rings::zn::zn_static::Zn;
1029
1030#[test]
1031fn test_baby_giant_step() {
1032 for base_bound in [21, 26, 31, 37] {
1033 let dlog_bound = [int_cast(base_bound, ZZbig, ZZ)];
1034 let G = AddGroup::new(ZZ);
1035 assert_eq!(Some(vec![6]), baby_giant_step(&G, 6, &[1], &dlog_bound));
1036 assert_eq!(None, baby_giant_step(&G, 0, &[1], &dlog_bound));
1037
1038 let G = AddGroup::new(Zn::<20>::RING);
1039 assert_eq!(Some(vec![20]), baby_giant_step(&G, 0, &[1], &dlog_bound));
1040 assert_eq!(Some(vec![10]), baby_giant_step(&G, 10, &[1], &dlog_bound));
1041 assert_eq!(Some(vec![5]), baby_giant_step(&G, 0, &[16], &dlog_bound));
1042 }
1043
1044 let G = AddGroup::new(ZZ);
1045
1046 assert_eq!(
1048 Some(vec![9 - 1, 6 - 1]),
1049 baby_giant_step(&G, 85, &[10, 1], &from_fn::<_, 2, _>(|_| int_cast(8, ZZbig, ZZ)))
1050 );
1051 assert_eq!(
1053 Some(vec![10 - 2, 5 - 0]),
1054 baby_giant_step(&G, 85, &[10, 1], &from_fn::<_, 2, _>(|_| int_cast(21, ZZbig, ZZ)))
1055 );
1056 assert_eq!(
1058 Some(vec![6 - 0, 30 - 5]),
1059 baby_giant_step(&G, 85, &[10, 1], &from_fn::<_, 2, _>(|_| int_cast(31, ZZbig, ZZ)))
1060 );
1061}
1062
1063#[test]
1064fn test_padic_relation_lattice() {
1065 let G = AddGroup::new(Zn::<81>::RING);
1066
1067 let subgroup = Subgroup::new(&G, int_cast(81, ZZbig, ZZ), vec![1]);
1068 assert_matrix_eq!(ZZ, [[-81]], subgroup.get_group().scaled_relation_lattices[0][4]);
1069 assert_matrix_eq!(ZZ, [[-27]], subgroup.get_group().scaled_relation_lattices[0][3]);
1070 assert_matrix_eq!(ZZ, [[-9]], subgroup.get_group().scaled_relation_lattices[0][2]);
1071 assert_matrix_eq!(ZZ, [[-3]], subgroup.get_group().scaled_relation_lattices[0][1]);
1072 assert_matrix_eq!(ZZ, [[1]], subgroup.get_group().scaled_relation_lattices[0][0]);
1073
1074 let subgroup = Subgroup::new(&G, int_cast(81, ZZbig, ZZ), vec![3, 6]);
1075 let matrix = &subgroup.get_group().scaled_relation_lattices[0][4];
1076 assert_eq!(-27, *matrix.at(0, 0));
1077 assert_eq!(-1, *matrix.at(1, 1));
1078 assert_eq!(
1079 0,
1080 ZZ.get_ring()
1081 .inner_product_ref(matrix.data().row_at(1).iter().zip([3, 6].iter()))
1082 % 81
1083 );
1084
1085 let subgroup = Subgroup::new(&G, int_cast(81, ZZbig, ZZ), vec![3, 9]);
1086 let matrix = &subgroup.get_group().scaled_relation_lattices[0][4];
1087 assert_eq!(-27, *matrix.at(0, 0));
1088 assert_eq!(-1, *matrix.at(1, 1));
1089 assert_eq!(
1090 0,
1091 ZZ.get_ring()
1092 .inner_product_ref(matrix.data().row_at(1).iter().zip([3, 9].iter()))
1093 % 81
1094 );
1095
1096 let subgroup = Subgroup::new(&G, int_cast(81, ZZbig, ZZ), vec![6, 18, 9]);
1097 let matrix = &subgroup.get_group().scaled_relation_lattices[0][4];
1098 assert_eq!(-27, *matrix.at(0, 0));
1099 assert_eq!(-1, *matrix.at(1, 1));
1100 assert_eq!(-1, *matrix.at(2, 2));
1101 assert_eq!(
1102 0,
1103 ZZ.get_ring()
1104 .inner_product_ref(matrix.data().row_at(1).iter().zip([6, 18, 9].iter()))
1105 % 81
1106 );
1107 assert_eq!(
1108 0,
1109 ZZ.get_ring()
1110 .inner_product_ref(matrix.data().row_at(2).iter().zip([6, 18, 9].iter()))
1111 % 81
1112 );
1113
1114 let G = GroupValue::from(ProdGroupBase(AddGroup::new(Zn::<81>::RING)));
1115
1116 let subgroup = Subgroup::new(&G, int_cast(81, ZZbig, ZZ), vec![[1, 4], [1, 1]]);
1117 let matrix = &subgroup.get_group().scaled_relation_lattices[0][4];
1118 assert_eq!(-81, *matrix.at(0, 0));
1119 assert_eq!(-27, *matrix.at(1, 1));
1120 assert_eq!(
1121 0,
1122 ZZ.get_ring()
1123 .inner_product_ref(matrix.data().row_at(1).iter().zip([1, 1].iter()))
1124 % 81
1125 );
1126 assert_eq!(
1127 0,
1128 ZZ.get_ring()
1129 .inner_product_ref(matrix.data().row_at(1).iter().zip([4, 1].iter()))
1130 % 81
1131 );
1132
1133 let G = GroupValue::from(ProdGroupBase(AddGroup::new(Zn::<8>::RING)));
1134
1135 let subgroup = Subgroup::new(&G, int_cast(8, ZZbig, ZZ), vec![[6, 3, 5], [6, 2, 6], [4, 5, 7]]);
1136 let matrix = &subgroup.get_group().scaled_relation_lattices[0][3];
1137 assert_eq!(-8, *matrix.at(0, 0));
1138 assert_eq!(-4, *matrix.at(1, 1));
1139 assert_eq!(-2, *matrix.at(2, 2));
1140 assert_eq!(
1141 0,
1142 ZZ.get_ring()
1143 .inner_product_ref(matrix.data().row_at(1).iter().zip([6, 6, 4].iter()))
1144 % 8
1145 );
1146 assert_eq!(
1147 0,
1148 ZZ.get_ring()
1149 .inner_product_ref(matrix.data().row_at(1).iter().zip([3, 2, 5].iter()))
1150 % 8
1151 );
1152 assert_eq!(
1153 0,
1154 ZZ.get_ring()
1155 .inner_product_ref(matrix.data().row_at(1).iter().zip([5, 6, 7].iter()))
1156 % 8
1157 );
1158 assert_eq!(
1159 0,
1160 ZZ.get_ring()
1161 .inner_product_ref(matrix.data().row_at(2).iter().zip([6, 6, 4].iter()))
1162 % 8
1163 );
1164 assert_eq!(
1165 0,
1166 ZZ.get_ring()
1167 .inner_product_ref(matrix.data().row_at(2).iter().zip([3, 2, 5].iter()))
1168 % 8
1169 );
1170 assert_eq!(
1171 0,
1172 ZZ.get_ring()
1173 .inner_product_ref(matrix.data().row_at(2).iter().zip([5, 6, 7].iter()))
1174 % 8
1175 );
1176}
1177
1178#[test]
1179fn random_test_dlog() {
1180 let ring = Zn::<1400>::RING;
1181 let i = ring.can_hom(&ZZ).unwrap();
1182 let mut rng = Rand64::new(0);
1183 let G = GroupValue::from(ProdGroupBase(AddGroup::new(ring)));
1184 let rand_gs = |rng: &mut Rand64| from_fn::<_, 3, _>(|_| ring.random_element(|| rng.rand_u64()));
1185
1186 for _ in 0..RANDOM_TEST_INSTANCE_COUNT {
1187 let gs = from_fn::<_, 3, _>(|_| rand_gs(&mut rng));
1188 let subgroup = Subgroup::new(&G, int_cast(1400, ZZbig, ZZ), gs.into());
1189
1190 let coeffs = rand_gs(&mut rng);
1191 let val = (0..3).fold(G.identity(), |current, i| {
1192 G.op(current, G.pow(&gs[i], &int_cast(coeffs[i] as i64, ZZbig, ZZ)))
1193 });
1194 let dlog = subgroup.dlog(&val);
1195 println!(
1196 "{:?} * x + {:?} * y + {:?} * z = {:?} mod 1400",
1197 gs[0], gs[1], gs[2], val
1198 );
1199 if let Some(dlog) = dlog {
1200 for k in 0..3 {
1201 assert_el_eq!(
1202 ring,
1203 val[k],
1204 ring.sum([
1205 i.mul_map(gs[0][k], dlog[0]),
1206 i.mul_map(gs[1][k], dlog[1]),
1207 i.mul_map(gs[2][k], dlog[2])
1208 ])
1209 );
1210 }
1211 println!("checked solution");
1212 }
1213 }
1214
1215 for _ in 0..RANDOM_TEST_INSTANCE_COUNT {
1216 let gs = from_fn::<_, 3, _>(|_| rand_gs(&mut rng));
1217 let subgroup = Subgroup::new(&G, int_cast(1400, ZZbig, ZZ), gs.into());
1218
1219 let val = rand_gs(&mut rng);
1220 let dlog = subgroup.dlog(&val);
1221 println!(
1222 "{:?} * x + {:?} * y + {:?} * z = {:?} mod 1400",
1223 gs[0], gs[1], gs[2], val
1224 );
1225 if let Some(dlog) = dlog {
1226 for k in 0..3 {
1227 assert_el_eq!(
1228 ring,
1229 val[k],
1230 ring.sum([
1231 i.mul_map(gs[0][k], dlog[0]),
1232 i.mul_map(gs[1][k], dlog[1]),
1233 i.mul_map(gs[2][k], dlog[2])
1234 ])
1235 );
1236 }
1237 println!("checked solution");
1238 } else {
1239 let mut gen_matrix = OwnedMatrix::from_fn(3, 3, |i, j| gs[j][i]);
1240 let mut value = OwnedMatrix::from_fn(3, 1, |i, _| val[i]);
1241 let mut res = OwnedMatrix::zero(3, 1, ring);
1242 let solved = ring.solve_right(gen_matrix.data_mut(), value.data_mut(), res.data_mut());
1243 println!("[{}, {}, {}]", res.at(0, 0), res.at(1, 0), res.at(2, 0));
1244 if solved.is_solved() {
1245 for k in 0..3 {
1246 assert_el_eq!(
1247 ring,
1248 val[k],
1249 ring.sum([
1250 ring.mul(gs[0][k], *res.at(0, 0)),
1251 ring.mul(gs[1][k], *res.at(1, 0)),
1252 ring.mul(gs[2][k], *res.at(2, 0))
1253 ])
1254 );
1255 }
1256 assert!(solved == crate::algorithms::linsolve::SolveResult::NoSolution);
1257 }
1258 println!("has no solution");
1259 }
1260 }
1261}
1262
1263#[test]
1264fn test_zn_dlog() {
1265 let ring = Zn::<51>::RING;
1266 let g1 = ring.int_hom().map(37);
1267 let g2 = ring.int_hom().map(35);
1268
1269 assert_eq!(0, finite_field_discrete_log(ring.one(), g1, ring).unwrap() % 16);
1270 assert_eq!(0, finite_field_discrete_log(ring.one(), g2, ring).unwrap() % 2);
1271 assert_eq!(1, finite_field_discrete_log(g2, g2, ring).unwrap() % 2);
1272 for i in 0..16 {
1273 assert_eq!(
1274 i,
1275 finite_field_discrete_log(ring.pow(g1, i as usize), g1, ring).unwrap() % 16
1276 );
1277 }
1278 for i in 0..16 {
1279 assert_eq!(
1280 None,
1281 finite_field_discrete_log(ring.mul(ring.pow(g1, i as usize), g2), g1, ring)
1282 );
1283 }
1284
1285 assert_eq!(16, multiplicative_order(g1, ring));
1286 assert_eq!(8, multiplicative_order(ring.pow(g1, 2), ring));
1287 assert_eq!(4, multiplicative_order(ring.pow(g1, 4), ring));
1288 assert_eq!(2, multiplicative_order(ring.pow(g1, 8), ring));
1289 assert_eq!(2, multiplicative_order(g2, ring));
1290}
1291
1292#[test]
1293fn test_zn_subgroup_size() {
1294 let ring = Zn::<153>::RING;
1295 let group = MultGroup::new(ring);
1296 let g1 = group.from_ring_el(ring.int_hom().map(2)).unwrap();
1297 let g2 = group.from_ring_el(ring.int_hom().map(37)).unwrap();
1298
1299 let mut subgroup = Subgroup::for_zn(group.clone(), Vec::new());
1300 assert_el_eq!(ZZbig, ZZbig.int_hom().map(1), subgroup.subgroup_order());
1301
1302 let next_gen = subgroup.parent().clone_el(&g1);
1303 subgroup = subgroup.add_generator(next_gen);
1304 assert_el_eq!(ZZbig, ZZbig.int_hom().map(24), subgroup.subgroup_order());
1305
1306 let next_gen = subgroup.parent().pow(&g1, &ZZbig.int_hom().map(2));
1307 subgroup = subgroup.add_generator(next_gen);
1308 assert_el_eq!(ZZbig, ZZbig.int_hom().map(24), subgroup.subgroup_order());
1309
1310 let next_gen = subgroup.parent().clone_el(&g2);
1311 subgroup = subgroup.add_generator(next_gen);
1312 assert_el_eq!(ZZbig, ZZbig.int_hom().map(96), subgroup.subgroup_order());
1313
1314 let generating_set = Subgroup::for_zn(group, vec![g2]);
1315 assert_el_eq!(ZZbig, ZZbig.int_hom().map(16), generating_set.subgroup_order());
1316}
1317
1318#[test]
1319fn test_enumerate_elements() {
1320 let ring = Zn::<45>::RING;
1321 let group = AddGroup::new(ring);
1322
1323 assert_eq!(
1324 vec![ring.zero()],
1325 Subgroup::new(group.clone(), int_cast(45, ZZbig, ZZ), Vec::new())
1326 .enumerate_elements()
1327 .collect::<Vec<_>>()
1328 );
1329
1330 let subgroup = Subgroup::new(group, int_cast(45, ZZbig, ZZ), vec![9, 15]);
1331 let mut elements = subgroup.enumerate_elements().collect::<Vec<_>>();
1332 elements.sort_unstable();
1333 assert_eq!((0..45).step_by(3).collect::<Vec<_>>(), elements);
1334}