rug_gmpmee/lib.rs
1// Copyright © 2024 Denis Morel
2
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the GNU Lesser General Public License as published by the Free
5// Software Foundation, either version 3 of the License, or (at your option) any
6// later version.
7//
8// This program is distributed in the hope that it will be useful, but WITHOUT
9// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11// details.
12//
13// You should have received a copy of the GNU Lesser General Public License and
14// a copy of the GNU General Public License along with this program. If not, see
15// <https://www.gnu.org/licenses/>.
16
17//! The rug-gmpmee crate provides an implementation for [rug](https://docs.rs/rug/latest/rug/) of
18//! the [GMP Modular Exponentiation Extension (GMPMEE)](https://github.com/verificatum/verificatum-gmpmee),
19//! which is a minor extension of [GMP](https://gmplib.org/). It adds simultaneous modular exponentiation
20//! and fixed base modular exponentiation functionality to the set of integer functions (the mpz-functions),
21//! as well as special purpose primality testing routines.
22//!
23//! It contains the following implementations:
24//! - Multi-exponentation (`spowm`)
25//! - Fixed base exponentiation (`fpowm`). It contains a possibility to cache the precomputation table
26//! - Miller-Rabin primality test
27//!
28//! The rub-gmpmee crate is free software: you can redistribute it and/or modify it under the terms of the
29//! GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License,
30//! or (at your option) any later version. See the full text of the [LICENSE](LICENSE.md) for details.
31//!
32//! # Using rug-gmpmee
33//! See the [gmpmee-sys](https://docs.rs/gmpmee-sys) crate.
34
35pub mod fpowm;
36pub mod miller_rabin;
37pub mod spown;
38use fpowm::FPownError;
39use spown::SPownError;
40use std::num::TryFromIntError;
41use thiserror::Error;
42
43#[derive(Error, Debug, Clone, PartialEq, Eq)]
44pub enum GmpMEEError {
45 #[error("Error in parameters of spowm")]
46 SPowmParameters(#[from] SPownError),
47 #[error("Error in parameters of fpown: {0}")]
48 FPowmParameters(#[from] FPownError),
49 #[error("{msg}: {source}")]
50 Cast {
51 msg: String,
52 source: TryFromIntError,
53 },
54}
55
56#[cfg(target_family = "windows")]
57fn usize_to_size_t_type(n: usize) -> Result<i32, TryFromIntError> {
58 n.try_into()
59}
60
61#[cfg(not(target_family = "windows"))]
62fn usize_to_size_t_type(n: usize) -> Result<i64, TryFromIntError> {
63 n.try_into()
64}