From 1246d84267a00f5a540473f30fa0b8150e54ac1f Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 28 May 2023 10:34:56 +0200 Subject: [PATCH] gallois sucks --- src/math/ecc.rs | 7 ++++--- src/math/gallois.rs | 40 ++++++++++++++++++++++++++++++++++++++-- src/math/gcd.rs | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/math/ecc.rs b/src/math/ecc.rs index 62ae2fa..d690b0f 100644 --- a/src/math/ecc.rs +++ b/src/math/ecc.rs @@ -26,16 +26,16 @@ pub const INFINITY_POINT: ElipticCurvePoint = ElipticCurvePoint { /// /// real curves not supported, only in Gallois Fields pub struct ElipticCurve { - f: Option, + f: GalloisField, a: i128, b: i128, points: Vec, verbose: bool, - INFINITY_POINT: ElipticCurvePoint + INFINITY_POINT: ElipticCurvePoint, } impl ElipticCurve { - pub fn new(f: Option, a: i128, b: i128, verbose: bool) -> Self { + pub fn new(f: GalloisField, a: i128, b: i128, verbose: bool) -> Self { let e = ElipticCurve { f, a, @@ -48,6 +48,7 @@ impl ElipticCurve { } } + #[derive(Debug, Clone)] #[pyclass] /// represent a specific eliptic curves point diff --git a/src/math/gallois.rs b/src/math/gallois.rs index 38e5cbb..12a74c7 100644 --- a/src/math/gallois.rs +++ b/src/math/gallois.rs @@ -2,6 +2,12 @@ /// calculation in a gallois field /// /// This module contains functions that can be used to calculate things in a gallois field +/// TODO I'm not sure how accurate it is to call this stuff a gallois field. +/// They should normally be based on some relation and not use numbers? +/// It does also not even come close to statisfying the characteristic of prime powers q = p^k.as +/// base => p = 0 +/// +/// Something is wrong here. /// /// Author: Christoph J. Scherr /// License: MIT @@ -15,6 +21,8 @@ use num::Integer; use pyo3::{prelude::*, exceptions::PyValueError}; +use primes::{Sieve, PrimeSet, is_prime}; + /////////////////////////////////////////////////////////////////////////////////////////////////// #[derive(Debug)] /// used when trying to find a root for a number which does not have a root. @@ -59,9 +67,8 @@ pub struct GalloisField { impl GalloisField { /// make a new gallois field pub fn new(base: u128, verbose: bool) -> Self { - let field = GalloisField{ + let mut field = GalloisField{ base, - // TODO: calculate the characteristic cha: 0, verbose }; @@ -296,6 +303,25 @@ impl GalloisField { return Ok((w1, w2)); } } + + /// calculate the characteristic of the field + pub fn calc_char(mut self) -> u128 { + if self.verbose { + seperator(); + println!("calculating characteristic of F_{}", self.base); + seperator(); + } + let mut i = 1u128; + while self.reduce(i) > 0 { + if self.verbose { + println!("{i}.\t {i} = {} (mod {})", self.reduce(i), self.base) + } + i += 1; + } + + self.cha = i; + return i; + } } #[pymethods] @@ -378,3 +404,13 @@ fn test_gallois_inverse() { // TODO add a test for a field that has a non prime base } + +#[test] +fn test_calc_char() { + assert_eq!(GalloisField::new(16, true).calc_char(), 2); + assert_eq!(GalloisField::new(81, true).calc_char(), 81); + assert_eq!(GalloisField::new(1151, true).calc_char(), 1151); + assert_eq!(GalloisField::new(8, true).calc_char(), 2); + assert_eq!(GalloisField::new(2, true).calc_char(), 2); + assert_eq!(GalloisField::new(60, true).calc_char(), 3); +} diff --git a/src/math/gcd.rs b/src/math/gcd.rs index ffc9f91..d91725c 100644 --- a/src/math/gcd.rs +++ b/src/math/gcd.rs @@ -35,7 +35,7 @@ pub fn alt_egcd(mut a: i128, mut b: i128, recursion: bool) -> Vec { return vec![b, 0, 1] } let v = alt_egcd(b % a, a, true); - let mut result = vec![ + let result = vec![ v[0], v[2] - (b.checked_div(a).unwrap()) * v[1], v[1],