lambdaworks_math/cyclic_group.rs
1use crate::unsigned_integer::traits::IsUnsignedInteger;
2
3pub trait IsGroup: Clone + PartialEq + Eq {
4 /// Returns the neutral element of the group. The equality
5 /// `neutral_element().operate_with(g) == g` must hold
6 /// for every group element `g`.
7 fn neutral_element() -> Self;
8
9 /// Check if an element the neutral element.
10 fn is_neutral_element(&self) -> bool {
11 self == &Self::neutral_element()
12 }
13
14 /// Applies the group operation `times` times with itself
15 /// The operation can be addition or multiplication depending on
16 /// the notation of the particular group.
17 fn operate_with_self<T: IsUnsignedInteger>(&self, mut exponent: T) -> Self {
18 let mut result = Self::neutral_element();
19 let mut base = self.clone();
20
21 while exponent != T::from(0) {
22 if exponent & T::from(1) == T::from(1) {
23 result = Self::operate_with(&result, &base);
24 }
25 exponent >>= 1;
26 base = Self::operate_with(&base, &base);
27 }
28 result
29 }
30
31 /// Applies the group operation between `self` and `other`.
32 /// The operation can be addition or multiplication depending on
33 /// the notation of the particular group.
34 fn operate_with(&self, other: &Self) -> Self;
35
36 /// Provides the inverse of the group element.
37 /// This is the unique y such that for any x
38 /// x.operate_with(y) returns the neutral element
39 fn neg(&self) -> Self;
40}