-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add `FiniteGroup` trait * update tests * add docs * move ecdsa to generic group * remove additivegroup * fix doc test * update readme * move to algebra module * add mul group * fix docs * update docs * add `Group` and `AbelianGroup` trait * separate Field and FiniteField traits * satisfy lint * add permutation group example * change example name and AbelianGroup impl
- Loading branch information
1 parent
32111ee
commit 3fd764b
Showing
42 changed files
with
875 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,8 @@ dump | |
*.swp | ||
*.swo | ||
|
||
# coverage | ||
lcov.info | ||
## macOS | ||
.DS_Store | ||
|
||
# sage | ||
*.sage.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
//! Implements [dihedral][dihedral] group of degree 3 and order 6 using ronkathon's [`Group`] and | ||
//! [`FiniteGroup`] trait. | ||
//! | ||
//! Consider a symmetric group containing all permutation of 3 distinct | ||
//! elements: `[a, b, c]`. Total number of elements is 3! = 6. Each element of the group is a | ||
//! permutation operation. | ||
//! | ||
//! ## Example | ||
//! Let `a=[2, 1, 3]` be an element of the group, when applied to any 3-length vector performs the | ||
//! swap of 1st and 2nd element. So, `RGB->GRB`. | ||
//! | ||
//! ## Operation | ||
//! Group operation is defined as combined action of performing permutation twice, i.e. take `x,y` | ||
//! two distinct element of the group. `a·b` is applying permutation `b` first then `a`. | ||
//! | ||
//! [dihedral]: https://en.wikipedia.org/wiki/Dihedral_group_of_order_6 | ||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; | ||
|
||
use ronkathon::algebra::{ | ||
group::{FiniteGroup, Group}, | ||
Finite, | ||
}; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
struct DihedralGroup { | ||
mapping: [usize; 3], | ||
} | ||
|
||
impl Finite for DihedralGroup { | ||
const ORDER: usize = 6; | ||
} | ||
|
||
impl Group for DihedralGroup { | ||
type Scalar = usize; | ||
|
||
const IDENTITY: Self = Self::new([0, 1, 2]); | ||
|
||
fn op(&self, rhs: &Self) -> Self { | ||
let mut new_mapping = [0; 3]; | ||
for (i, &j) in self.mapping.iter().enumerate() { | ||
new_mapping[i] = rhs.mapping[j]; | ||
} | ||
|
||
Self::new(new_mapping) | ||
} | ||
|
||
fn inverse(&self) -> Option<Self> { | ||
let mut inverse_mapping = [0; 3]; | ||
for (i, &j) in self.mapping.iter().enumerate() { | ||
inverse_mapping[j] = i; | ||
} | ||
|
||
Some(Self::new(inverse_mapping)) | ||
} | ||
|
||
fn scalar_mul(&self, scalar: Self::Scalar) -> Self { | ||
let mut res = *self; | ||
for _ in 0..scalar { | ||
res = res.op(self); | ||
} | ||
res | ||
} | ||
} | ||
|
||
impl DihedralGroup { | ||
const fn new(mapping: [usize; 3]) -> Self { Self { mapping } } | ||
} | ||
|
||
impl FiniteGroup for DihedralGroup {} | ||
|
||
impl Default for DihedralGroup { | ||
fn default() -> Self { Self::IDENTITY } | ||
} | ||
|
||
impl Add for DihedralGroup { | ||
type Output = Self; | ||
|
||
fn add(self, rhs: Self) -> Self::Output { Self::op(&self, &rhs) } | ||
} | ||
|
||
impl AddAssign for DihedralGroup { | ||
fn add_assign(&mut self, rhs: Self) { *self = *self + rhs; } | ||
} | ||
|
||
impl Neg for DihedralGroup { | ||
type Output = Self; | ||
|
||
fn neg(self) -> Self::Output { Self::inverse(&self).expect("inverse does not exist") } | ||
} | ||
|
||
impl Sub for DihedralGroup { | ||
type Output = Self; | ||
|
||
fn sub(self, rhs: Self) -> Self::Output { self + -rhs } | ||
} | ||
|
||
impl SubAssign for DihedralGroup { | ||
fn sub_assign(&mut self, rhs: Self) { *self = *self - rhs; } | ||
} | ||
|
||
impl Mul<usize> for DihedralGroup { | ||
type Output = Self; | ||
|
||
fn mul(self, rhs: usize) -> Self::Output { Self::scalar_mul(&self, rhs) } | ||
} | ||
|
||
impl MulAssign<usize> for DihedralGroup { | ||
fn mul_assign(&mut self, rhs: usize) { *self = *self * rhs; } | ||
} | ||
|
||
fn main() { | ||
let ident = DihedralGroup::default(); | ||
let a = DihedralGroup::new([1, 0, 2]); | ||
let b = DihedralGroup::new([0, 2, 1]); | ||
|
||
// closure | ||
let ab = a.op(&b); | ||
let ba = b.op(&a); | ||
|
||
// identity | ||
assert_eq!(a, ident.op(&a)); | ||
assert_eq!(b, ident.op(&b)); | ||
|
||
// non-abelian property | ||
assert_ne!(ab, ba); | ||
|
||
// group element order | ||
assert_eq!(a.order(), 2); | ||
assert_eq!(ab.order(), 3); | ||
assert_eq!(ba.order(), 3); | ||
|
||
// inverse | ||
assert_eq!(a.op(&a.inverse().unwrap()), ident); | ||
assert_eq!(ab.inverse().unwrap(), ba); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Algebra | ||
|
||
[Groups](https://en.wikipedia.org/wiki/Group_(mathematics)) $G$ are algebraic structures which are set and has a binary operation $\cdot$ that combines two elements $a, b$ of the set to produce a third element $a\cdot b$ in the set. | ||
The operation is said to have following properties: | ||
1. Closure: $a\cdot b=c\in G$ | ||
2. Associative: $(a\cdot b)\cdot c = a\cdot(b\cdot c)$ | ||
3. Existence of Identity element: $a\cdot 0 = 0\cdot a = a$ | ||
4. Existence of inverse element for every element of the set: $a\cdot b=0$ | ||
5. Commutativity: Groups which satisfy an additional property: *commutativity* on the set of | ||
elements are known as **Abelian groups**, $a\cdot b=b\cdot a$. | ||
|
||
Examples: | ||
- $Z$ is a group under addition defined as $(Z,+)$ | ||
- $(Z/nZ)^{\times}$ is a finite group under multiplication | ||
- Equilateral triangles for a non-abelian group of order 6 under symmetry. | ||
- Invertible $n\times n$ form a non-abelian group under multiplication. | ||
|
||
[Rings](https://en.wikipedia.org/wiki/Ring_(mathematics)) are algebraic structures with two binary operations $R(+, \cdot)$ satisfying the following properties: | ||
1. Abelian group under + | ||
2. Monoid under $\cdot$ | ||
3. Distributive with respect to $\cdot$ | ||
|
||
Examples: | ||
- $Z/nZ$ form a ring | ||
- a polynomial with integer coefficients: $Z[x]$ satisfy ring axioms | ||
|
||
[Fields](https://en.wikipedia.org/wiki/Field_(mathematics)) are algebraic structures with two binary operations $F(+,\cdot)$ such that: | ||
- Abelian group under $+$ | ||
- Non-zero numbers $F-\{0\}$ form abelian group under $\cdot$ | ||
- Multiplicative distribution over $+$ | ||
|
||
> Fields can also be defined as commutative ring $(R,+,\cdot)$ with existence of inverse under $\cdot$. Thus, every Field can be classified as Ring. | ||
Examples: | ||
- $\mathbb{Q,R,C}$, i.e. set of rational, real and complex numbers are all Fields. | ||
- $\mathbb{Z}$ is **not** a field. | ||
- $Z/nZ$ form a finite field under modulo addition and multiplication. | ||
|
||
[Finite fields](https://en.wikipedia.org/wiki/Finite_field) are field with a finite order and are instrumental in cryptographic systems. They are used in elliptic curve cryptography, RSA, and many other cryptographic systems. | ||
This module provides a generic implementation of finite fields via two traits and two structs. | ||
It is designed to be easy to use and understand, and to be flexible enough to extend yourself. | ||
|
||
## Constant (Compile Time) Implementations | ||
Note that traits defined in this module are tagged with `#[const_trait]` which implies that each method and associated constant is implemented at compile time. | ||
This is done purposefully as it allows for generic implementations of these fields to be constructed when you compile `ronkathon` rather than computed at runtime. | ||
In principle, this means the code runs faster, but will compile slower, but the tradeoff is that the cryptographic system is faster and extensible. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.