From 22d7f5b26a5d414e3be4ae5f11386ab144a109eb Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Wed, 4 Dec 2024 17:47:47 +0100 Subject: [PATCH] wip on primtive root with Hensel lifting --- Cargo.lock | 202 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/dft.rs | 1 + src/dft/primitive_root.rs | 0 src/modulus/prime.rs | 67 ++++++++++++- 5 files changed, 268 insertions(+), 5 deletions(-) create mode 100644 src/dft.rs create mode 100644 src/dft/primitive_root.rs diff --git a/Cargo.lock b/Cargo.lock index a8cc55b..7f9b2ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,50 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "libc" +version = "0.2.167" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" + [[package]] name = "math" version = "0.1.0" @@ -15,6 +59,21 @@ dependencies = [ "num-bigint", "num-traits", "primality-test", + "prime_factorization", +] + +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", ] [[package]] @@ -27,6 +86,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -36,6 +104,28 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -45,8 +135,120 @@ dependencies = [ "autocfg", ] +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "primality-test" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98439e9658b9548a33abdab8c82532554dc08e49ddc5398a9262222fb360ae24" + +[[package]] +name = "prime_factorization" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61b43cd4d5e49fa3c769f72033129f07eeaa102c3db2aa11be0c7f1a0cb50f0c" +dependencies = [ + "itertools", + "num", + "rand", +] + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index afe4f4d..f90a3f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" [dependencies] primality-test = "0.3.0" num-bigint = "0.4.6" -num-traits = "0.2.19" \ No newline at end of file +num-traits = "0.2.19" +prime_factorization = "1.0.4" \ No newline at end of file diff --git a/src/dft.rs b/src/dft.rs new file mode 100644 index 0000000..27eaa65 --- /dev/null +++ b/src/dft.rs @@ -0,0 +1 @@ +pub(crate) mod primitive_root; \ No newline at end of file diff --git a/src/dft/primitive_root.rs b/src/dft/primitive_root.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/modulus/prime.rs b/src/modulus/prime.rs index 70a3f53..dad4ded 100644 --- a/src/modulus/prime.rs +++ b/src/modulus/prime.rs @@ -1,12 +1,24 @@ use primality_test::is_prime; +use prime_factorization::Factorization; -pub struct Prime { - q: u64, +pub struct Prime { + q: O, /// q_base^q_powers + q_base: O, + q_powers: O, + factors: Vec, /// distinct factors of q-1 + nth_root: O, } -impl Prime { - pub fn new(q: u64) -> Self{ +impl Prime{ + pub fn new(q_base: u64, q_power: u64) -> Self{ assert!(is_prime(q) && q > 2); + assert!() + + q_exp + for i in 0..q_power{ + + } + Self::new_unchecked(q) } @@ -16,4 +28,51 @@ impl Prime { q, } } + + /// Returns returns Phi(BaseModulus^BaseModulusPower) + pub fn phi() -> u64 { + + } + + /// Returns the smallest primitive root. The unique factors + /// can be given as argument to avoid factorization of q-1. + pub fn primitive_root(&self) -> u64{ + if self.factors.len() != 0{ + self.check_factors(); + }else{ + let factors = Factorization::run(q).prime_factor_repr(); + let mut distincts_factors: Vec = Vec::with_capacity(factors.len()); + for factor in factors.iter(){ + distincts_factors.push(factor.0) + } + self.factors = distincts_factors + } + + let log_nth_root = 64 - self.q.leading_zeros() as usize; + + 0 + } + + pub fn check_factors(&self){ + + if self.factors.len() == 0{ + panic!("invalid factor list: empty") + } + + let mut q = self.q; + + for &factor in &self.factors{ + if !is_prime(factor){ + panic!("invalid factor list: factor {} is not prime", factor) + } + + while q%factor != 0{ + q /= factor + } + } + + if q != 1{ + panic!("invalid factor list: does not fully divide q: q % (alll factors) = {}", q) + } + } } \ No newline at end of file