-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmod.rs
More file actions
71 lines (62 loc) · 1.94 KB
/
mod.rs
File metadata and controls
71 lines (62 loc) · 1.94 KB
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
#![doc = include_str!("./README.md")]
pub mod prime;
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use super::Finite;
#[const_trait]
/// Group trait defined by a binary operation, identity element and inverse.
pub trait Group:
std::fmt::Debug
+ Sized
+ Add<Output = Self>
+ AddAssign
+ Sub<Output = Self>
+ SubAssign
+ Neg
+ Mul<Self::Scalar, Output = Self>
+ MulAssign<Self::Scalar>
+ Clone
+ Copy
+ Default
+ Eq {
/// Scalar defined in the group
type Scalar;
/// Identity element of group
const IDENTITY: Self;
/// operation defined for the group, can be `+` for additive group and `·` for multiplicative
/// group
fn op(&self, rhs: &Self) -> Self;
/// Inverse of group element: a·i = [`FiniteGroup::IDENTITY`]
fn inverse(&self) -> Option<Self>;
/// Multiplication with the scalar element of the group, i.e. repeatedly applying group
/// [`FiniteGroup::operation`] `scalar` number of times.
fn scalar_mul(&self, scalar: Self::Scalar) -> Self;
}
/// Group trait with finite number of elements
pub trait FiniteGroup: Finite + Group {
/// order of group element, i.e. order of finite cyclic subgroup that the element belongs to
fn order(&self) -> usize {
let mut order = 1;
let mut elem = *self;
for _ in 0..Self::ORDER {
// check if elem is the identity
if elem == Self::IDENTITY {
return order;
}
// apply operation and increment order
elem = Self::op(&elem, self);
order += 1;
}
order
}
}
/// Defines a group with commutative operation: `a·b=b·a`
pub trait AbelianGroup: Group {
/// Returns whether the group is an abelian group
fn is_abelian(a: &Self, b: &Self) { assert!(Self::op(a, b) == Self::op(b, a)) }
}
#[const_trait]
/// Finite cyclic group trait defined by a generator element and order of the group
pub trait FiniteCyclicGroup: FiniteGroup + AbelianGroup {
/// primitive element of group
const GENERATOR: Self;
}