g2poly-1.1.0/.cargo_vcs_info.json0000644000000001440000000000100122460ustar { "git": { "sha1": "c3a3a1578db31be1774f63f9f628e1fa2b8957a7" }, "path_in_vcs": "g2poly" }g2poly-1.1.0/.gitignore000064400000000000000000000000321046102023000130220ustar 00000000000000/target **/*.rs.bk .idea/ g2poly-1.1.0/Cargo.toml0000644000000016300000000000100102450ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "g2poly" version = "1.1.0" authors = ["WanzenBug "] description = """ Primitive implementation of polynomials over the field GF(2) """ documentation = "https://docs.rs/g2poly" readme = "README.md" keywords = [ "finite-field", "galois", ] categories = [ "no-std", "algorithms", ] license = "MIT/Apache-2.0" repository = "https://github.com/WanzenBug/g2p" [lib] path = "src/lib.rs" g2poly-1.1.0/Cargo.toml.orig000064400000000000000000000006641046102023000137340ustar 00000000000000[package] name = "g2poly" version = "1.1.0" authors = ["WanzenBug "] edition = "2018" readme = "./README.md" license = "MIT/Apache-2.0" repository = "https://github.com/WanzenBug/g2p" documentation = "https://docs.rs/g2poly" description = """ Primitive implementation of polynomials over the field GF(2) """ categories = [ "no-std", "algorithms" ] keywords = [ "finite-field", "galois"] [lib] path = "src/lib.rs" g2poly-1.1.0/LICENSE-APACHE000064400000000000000000000252351046102023000127720ustar 00000000000000Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and 2. You must cause any modified files to carry prominent notices stating that You changed the files; and 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 4. If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2018 Moritz 'WanzenBug' Wanzenböck Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. g2poly-1.1.0/LICENSE-MIT000064400000000000000000000021111046102023000124660ustar 00000000000000The MIT License (MIT) Copyright (c) 2018 Moritz 'WanzenBug' Wanzenböck Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. g2poly-1.1.0/README.md000064400000000000000000000022241046102023000123160ustar 00000000000000# g2poly A small library to handle polynomials of degree < 64 over the finite field GF(2). The main motivation for this library is generating finite fields of the form GF(2^p). Elements of GF(2^p) can be expressed as polynomials over GF(2) with degree < p. These finite fields are used in cryptographic algorithms as well as error detecting / correcting codes. [Documentation](https://docs.rs/g2poly) # Example ```rust use g2poly; let a = g2poly::G2Poly(0b10011); assert_eq!(format!("{}", a), "G2Poly { x^4 + x + 1 }"); let b = g2poly::G2Poly(0b1); assert_eq!(a + b, g2poly::G2Poly(0b10010)); // Since products could overflow in u64, the product is defined as a u128 assert_eq!(a * a, g2poly::G2PolyProd(0b100000101)); // This can be reduced using another polynomial let s = a * a % g2poly::G2Poly(0b1000000); assert_eq!(s, g2poly::G2Poly(0b101)); ``` ## License // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. g2poly-1.1.0/src/lib.rs000064400000000000000000000425131046102023000127470ustar 00000000000000// Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! # g2poly //! //! A small library to handle polynomials of degree < 64 over the finite field GF(2). //! //! The main motivation for this library is generating finite fields of the form GF(2^p). //! Elements of GF(2^p) can be expressed as polynomials over GF(2) with degree < p. These //! finite fields are used in cryptographic algorithms as well as error detecting / correcting //! codes. //! //! # Example //! //! ```rust //! use g2poly; //! //! let a = g2poly::G2Poly(0b10011); //! assert_eq!(format!("{}", a), "G2Poly { x^4 + x + 1 }"); //! let b = g2poly::G2Poly(0b1); //! assert_eq!(a + b, g2poly::G2Poly(0b10010)); //! //! // Since products could overflow in u64, the product is defined as a u128 //! assert_eq!(a * a, g2poly::G2PolyProd(0b100000101)); //! //! // This can be reduced using another polynomial //! let s = a * a % g2poly::G2Poly(0b1000000); //! assert_eq!(s, g2poly::G2Poly(0b101)); //! ``` use core::{ ops, fmt, cmp, }; /// Main type exported by this library /// /// The polynomial is represented as the bits of the inner `u64`. The least significant bit /// represents `c_0` in `c_n * x^n + c_(n-1) * x^(n-1) + ... + c_0 * x^0`, the next bit c_1 and so on. /// /// ```rust /// # use g2poly::G2Poly; /// assert_eq!(format!("{}", G2Poly(0b101)), "G2Poly { x^2 + 1 }"); /// ``` /// /// 3 main operations [`+`](#impl-Add), [`-`](#impl-Sub) and /// [`*`](#impl-Mul) are implemented, as well as [`%`](#impl-Rem) for remainder /// calculation. Note that multiplication generates a `G2PolyProd` so there is no risk of /// overflow. /// /// Division is left out as there is generally not needed for common use cases. This may change in a /// later release. #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] pub struct G2Poly(pub u64); /// The result of multiplying two `G2Poly` /// /// This type is used to represent the result of multiplying two `G2Poly`s. Since this could /// overflow when relying on just a `u64`, this type uses an internal `u128`. The only operation /// implemented on this type is [`%`](#impl-Rem) which reduces the result back to a /// `G2Poly`. /// /// ```rust /// # use g2poly::{G2Poly, G2PolyProd}; /// let a = G2Poly(0xff_00_00_00_00_00_00_00); /// assert_eq!(a * a, G2PolyProd(0x55_55_00_00_00_00_00_00_00_00_00_00_00_00_00_00)); /// assert_eq!(a * a % G2Poly(0b100), G2Poly(0)); /// ``` #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] pub struct G2PolyProd(pub u128); impl G2PolyProd { /// Convert to G2Poly /// /// # Panics /// Panics, if the internal representation exceeds the maximum value for G2Poly. /// /// # Example /// ```rust /// # use g2poly::G2Poly; /// /// let a = G2Poly(0x40_00_00_00_00_00_00_00) * G2Poly(2); /// assert_eq!(G2Poly(0x80_00_00_00_00_00_00_00), a.to_poly()); /// /// // Next line would panics! /// // (G2Poly(0x40_00_00_00_00_00_00_00) * G2Poly(4)).to_poly(); /// ``` pub fn to_poly(self) -> G2Poly { self.try_to_poly().expect("Tried to convert product bigger than G2Poly max") } /// Convert to G2Poly if possible /// /// In case the value would not fit into `G2Poly`, return `None` /// /// # Example /// ```rust /// # use g2poly::G2Poly; /// assert_eq!((G2Poly(0x40_00_00_00_00_00_00_00) * G2Poly(2)).try_to_poly(), Some(G2Poly(0x80_00_00_00_00_00_00_00))); /// assert_eq!((G2Poly(0x40_00_00_00_00_00_00_00) * G2Poly(4)).try_to_poly(), None); /// ``` pub fn try_to_poly(self) -> Option { if self.0 <= u64::max_value() as u128 { Some(G2Poly(self.0 as u64)) } else { None } } } impl fmt::Debug for G2Poly { fn fmt<'a>(&self, f: &mut fmt::Formatter<'a>) -> fmt::Result { write!(f, "G2Poly {{ {:b} }}", self.0) } } impl fmt::Debug for G2PolyProd { fn fmt<'a>(&self, f: &mut fmt::Formatter<'a>) -> fmt::Result { write!(f, "G2PolyProd {{ {:b} }}", self.0) } } impl fmt::Display for G2Poly { fn fmt<'a>(&self, f: &mut fmt::Formatter<'a>) -> fmt::Result { if self.0 == 0 { return write!(f, "G2Poly {{ 0 }}"); } write!(f, "G2Poly {{ ")?; let start = 63 - self.0.leading_zeros(); let mut check = 1 << start; let mut append = false; for p in (0..=start).rev() { if check & self.0 > 0 { if append { write!(f, " + ")?; } if p == 0 { write!(f, "1")?; } else if p == 1 { write!(f, "x")?; } else { write!(f, "x^{}", p)?; } append = true; } check >>= 1; } write!(f, " }}") } } impl ops::Mul for G2Poly { type Output = G2PolyProd; fn mul(self, rhs: G2Poly) -> G2PolyProd { let mut result = 0; let smaller = cmp::min(self.0, rhs.0); let mut bigger = cmp::max(self.0, rhs.0) as u128; let end = 64 - smaller.leading_zeros(); let mut bitpos = 1; for _ in 0..end { if bitpos & smaller > 0 { result ^= bigger; } bigger <<= 1; bitpos <<= 1; } G2PolyProd(result) } } impl ops::Rem for G2Poly { type Output = G2Poly; fn rem(self, rhs: G2Poly) -> G2Poly { G2PolyProd(self.0 as u128) % rhs } } impl ops::Div for G2Poly { type Output = G2Poly; /// Calculate the polynomial quotient /// /// For `a / b` calculate the value `q` in `a = q * b + r` such that /// |r| < |b|. /// /// # Example /// ```rust /// # use g2poly::G2Poly; /// let a = G2Poly(0b0100_0000_0101); /// let b = G2Poly(0b1010); /// /// assert_eq!(G2Poly(0b101_01010), a / b); /// ``` fn div(self, rhs: G2Poly) -> G2Poly { let divisor = rhs.0; let divisor_degree_p1 = 64 - divisor.leading_zeros(); assert!(divisor_degree_p1 > 0); let mut quotient = 0; let mut rem = self.0; let mut rem_degree_p1 = 64 - self.0.leading_zeros(); while divisor_degree_p1 <= rem_degree_p1 { let shift_len = rem_degree_p1 - divisor_degree_p1; quotient |= 1 << shift_len; rem ^= divisor << shift_len; rem_degree_p1 = 64 - rem.leading_zeros(); } G2Poly(quotient) } } impl ops::Add for G2Poly { type Output = G2Poly; #[allow(clippy::suspicious_arithmetic_impl)] fn add(self, rhs: G2Poly) -> G2Poly { G2Poly(self.0 ^ rhs.0) } } impl ops::Sub for G2Poly { type Output = G2Poly; #[allow(clippy::suspicious_arithmetic_impl)] fn sub(self, rhs: G2Poly) -> G2Poly { G2Poly(self.0 ^ rhs.0) } } impl ops::Rem for G2PolyProd { type Output = G2Poly; /// Calculate the polynomial remainder of the product of polynomials /// /// When calculating a % b this computes the value of r in /// `a = q * b + r` such that |r| < |b|. /// /// # Example /// ```rust /// # use g2poly::G2Poly; /// let a = G2Poly(0x12_34_56_78_9A_BC_DE); /// let m = G2Poly(0x00_00_00_01_00_00); /// assert!((a * a % m).degree().expect("Positive degree") < m.degree().expect("Positive degree")); /// assert_eq!(G2Poly(0b0101_0001_0101_0100), a * a % m); /// ``` fn rem(self, rhs: G2Poly) -> G2Poly { let module = rhs.0 as u128; let mod_degree_p1 = 128 - module.leading_zeros(); assert!(mod_degree_p1 > 0); let mut rem = self.0; let mut rem_degree_p1 = 128 - rem.leading_zeros(); while mod_degree_p1 <= rem_degree_p1 { let shift_len = rem_degree_p1 - mod_degree_p1; rem ^= module << shift_len; rem_degree_p1 = 128 - rem.leading_zeros(); } // NB: rem_degree < mod_degree implies that rem < mod so it fits in u64 G2Poly(rem as u64) } } /// Calculate the greatest common divisor of `a` and `b` /// /// This uses the classic euclidean algorithm to determine the greatest common divisor of two /// polynomials. /// /// # Example /// ```rust /// # use g2poly::{G2Poly, gcd}; /// let a = G2Poly(0b11011); /// let b = G2Poly(0b100001); /// assert_eq!(gcd(a, b), G2Poly(0b11)); /// assert_eq!(gcd(b, a), G2Poly(0b11)); /// ``` pub fn gcd(a: G2Poly, b: G2Poly) -> G2Poly { let (mut a, mut b) = (cmp::max(a, b), cmp::min(a, b)); while b != G2Poly(0) { let new_b = a % b; a = b; b = new_b; } a } /// Calculate the greatest common divisor with Bézout coefficients /// /// Uses the extended euclidean algorithm to calculate the greatest common divisor of two /// polynomials. Also returns the Bézout coefficients x and y such that /// > gcd(a, b) == a * x + b * x /// /// # Example /// ```rust /// # use g2poly::{G2Poly, extended_gcd}; /// /// let a = G2Poly(0b11011); /// let b = G2Poly(0b100001); /// let (gcd, x, y) = extended_gcd(a, b); /// assert_eq!(gcd, G2Poly(0b11)); /// assert_eq!((a * x).to_poly() + (b * y).to_poly(), G2Poly(0b11)); /// ``` pub fn extended_gcd(a: G2Poly, b: G2Poly) -> (G2Poly, G2Poly, G2Poly) { let mut s = G2Poly(0); let mut old_s = G2Poly(1); let mut t = G2Poly(1); let mut old_t = G2Poly(0); let mut r = b; let mut old_r = a; while r != G2Poly(0) { let quotient = old_r / r; let tmp = old_r - (quotient * r).to_poly(); old_r = r; r = tmp; let tmp = old_s - (quotient * s).to_poly(); old_s = s; s = tmp; let tmp = old_t - (quotient * t).to_poly(); old_t = t; t = tmp; } (old_r, old_s, old_t) } impl G2Poly { /// The constant `1` polynomial. /// /// This is the multiplicative identity. (a * UNIT = a) pub const UNIT: Self = G2Poly(1); /// The constant `0 polynomial` /// /// This is the additive identity (a + ZERO = a) pub const ZERO: Self = G2Poly(0); /// The `x` polynomial. /// /// Useful for quickly generating `x^n` values. pub const X: Self = G2Poly(2); /// Quickly calculate p^n mod m /// /// Uses [square-and-multiply](https://en.wikipedia.org/wiki/Exponentiation_by_squaring) to /// quickly exponentiate a polynomial. /// /// # Example /// ```rust /// # use g2poly::G2Poly; /// let p = G2Poly(0b1011); /// assert_eq!(p.pow_mod(127, G2Poly(0b1101)), G2Poly(0b110)); /// ``` pub fn pow_mod(self, power: u64, modulus: G2Poly) -> G2Poly { let mut init = G2Poly::UNIT; // max starts with only the highest bit set let mut max = 0x80_00_00_00_00_00_00_00; assert_eq!(max << 1, 0); while max > 0 { let square = init * init; init = square % modulus; if power & max > 0 { let mult = init * self; init = mult % modulus; } max >>= 1; } init } /// Determine if the given polynomial is irreducible. /// /// Irreducible polynomials not be expressed as the product of other irreducible polynomials /// (except `1` and itself). This uses [Rabin's tests](https://en.wikipedia.org/wiki/Factorization_of_polynomials_over_finite_fields#Rabin's_test_of_irreducibility) /// to check if the given polynomial is irreducible. /// /// # Example /// ```rust /// # use g2poly::G2Poly; /// let p = G2Poly(0b101); /// assert!(!p.is_irreducible()); // x^2 + 1 == (x + 1)^2 in GF(2) /// let p = G2Poly(0b111); /// assert!(p.is_irreducible()); /// ``` pub fn is_irreducible(self) -> bool { const PRIMES_LE_63: [u64; 11] = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, ]; // Zero is not irreducible if self == G2Poly::ZERO { return false; } // Degrees let n = self.degree().expect("Already checked for zero"); let distinct_prime_coprod = PRIMES_LE_63.iter() .filter(|&&p| p <= n) .filter(|&&p| n % p == 0) .map(|&p| n / p); for p in distinct_prime_coprod { let q_to_the_p = 1 << p; let h = G2Poly::X.pow_mod(q_to_the_p, self) - (G2Poly(2) % self); if gcd(self, h) != G2Poly(1) { return false; } } let g = G2Poly::X.pow_mod(1 << n, self) - G2Poly(2) % self; g == G2Poly::ZERO } /// Get the degree of the polynomial /// /// Returns `None` for the 0 polynomial (which has degree `-infinity`), /// otherwise is guaranteed to return `Some(d)` with `d` the degree. /// /// ```rust /// # use g2poly::G2Poly; /// let z = G2Poly::ZERO; /// assert_eq!(z.degree(), None); /// let s = G2Poly(0b101); /// assert_eq!(s.degree(), Some(2)); /// ``` pub fn degree(self) -> Option { 63_u32.checked_sub(self.0.leading_zeros()).map(|n| n as u64) } /// Checks if a polynomial generates the multiplicative group mod m. /// /// The field GF(2^p) can be interpreted as all polynomials of degree < p, with all operations /// carried over from polynomials. Multiplication is done mod m, where m is some irreducible /// polynomial of degree p. The multiplicative group is cyclic, so there is an element `a` so /// that all elements != can be expressed as a^n for some n < 2^p - 1. /// /// This checks if the given polynomial is such a generator element mod m. /// /// # Example /// ```rust /// # use g2poly::G2Poly; /// let m = G2Poly(0b10011101); /// // The element `x` generates the whole group. /// assert!(G2Poly::X.is_generator(m)); /// ``` pub fn is_generator(self, module: G2Poly) -> bool { assert!(module.is_irreducible()); let order = module.degree().expect("Module is not 0"); let p_minus_1 = (1 << order) - 1; let mut g_pow = self; for _ in 1..p_minus_1 { if g_pow == G2Poly::UNIT { return false; } g_pow = (g_pow * self) % module; } true } } #[cfg(test)] mod tests { use super::*; #[test] fn test_debug_format() { let a = 0; let b = 0b0110; let c = 1; let d = 49; assert_eq!(format!("{:?}", G2Poly(a)), "G2Poly { 0 }"); assert_eq!(format!("{:?}", G2Poly(b)), "G2Poly { 110 }"); assert_eq!(format!("{:?}", G2Poly(c)), "G2Poly { 1 }"); assert_eq!(format!("{:?}", G2Poly(d)), "G2Poly { 110001 }"); } #[test] fn test_display_format() { let a = 0; let b = 0b0110; let c = 1; let d = 49; assert_eq!(format!("{}", G2Poly(a)), "G2Poly { 0 }"); assert_eq!(format!("{}", G2Poly(b)), "G2Poly { x^2 + x }"); assert_eq!(format!("{}", G2Poly(c)), "G2Poly { 1 }"); assert_eq!(format!("{}", G2Poly(d)), "G2Poly { x^5 + x^4 + 1 }"); } #[test] fn test_poly_prod() { let e = G2Poly(1); let a = G2Poly(0b01101); let b = G2Poly(0b11111); let c = G2Poly(0xff_ff_ff_ff_ff_ff_ff_ff); assert_eq!(e * e, G2PolyProd(1)); assert_eq!(a * e, G2PolyProd(0b01101)); assert_eq!(b * e, G2PolyProd(0b11111)); assert_eq!(c * e, G2PolyProd(0xff_ff_ff_ff_ff_ff_ff_ff)); assert_eq!(a * b, G2PolyProd(0b10011011)); assert_eq!(a * c, G2PolyProd(0b1001111111111111111111111111111111111111111111111111111111111111011)); assert_eq!(c * c, G2PolyProd(0b1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101)); } #[test] fn test_poly_rem() { let a = G2PolyProd(0b00111); let b = G2PolyProd(0b10101); let m = G2Poly(0b01001); assert_eq!(a % m, G2Poly(0b00111)); assert_eq!(b % m, G2Poly(0b0111)); } #[test] fn test_irreducible_check() { let a = G2Poly(0b11); let b = G2Poly(0b1101); let c = G2Poly(0x80_00_00_00_80_00_00_01); let z = G2Poly(0b1001); let y = G2Poly(0x80_00_00_00_80_00_00_03); assert!(a.is_irreducible()); assert!(b.is_irreducible()); assert!(c.is_irreducible()); assert!(!z.is_irreducible()); assert!(!y.is_irreducible()); } #[test] fn test_generator_check() { // Rijndael's field let m = G2Poly(0b100011011); let g = G2Poly(0b11); assert!(g.is_generator(m)); } #[test] #[should_panic] fn test_generator_check_fail() { let m = G2Poly(0b101); let g = G2Poly(0b1); g.is_generator(m); } #[test] fn test_poly_div() { let a = G2Poly(0b10000001001); let b = G2Poly(0b1010); assert_eq!(G2Poly(0b10101011), a / b); } #[test] fn test_extended_euclid() { let m = G2Poly(0b10000001001); let a = G2Poly(10); let (gcd, x, _) = extended_gcd(a, m); assert_eq!(G2Poly(1), gcd); assert_eq!(G2Poly(1), a * x % m); } }