qfall_math/integer_mod_q/modulus/
unsafe_functions.rs

1// Copyright © 2025 Niklas Siemer
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! This module contains public functions that enable access to underlying
10//! [FLINT](https://flintlib.org/) structs. Therefore, they require to be unsafe.
11
12use super::Modulus;
13use crate::macros::unsafe_passthrough::unsafe_getter_mod;
14use flint_sys::fmpz_mod::{fmpz_mod_ctx, fmpz_mod_ctx_clear};
15
16unsafe_getter_mod!(Modulus, modulus, fmpz_mod_ctx);
17
18impl Modulus {
19    /// Sets a mutable reference to the field `modulus` of type [`Modulus`] to a given `fmpz_mod_ctx`.
20    ///
21    /// Parameters:
22    /// - `flint_struct`: value to set the attribute to
23    ///
24    /// **WARNING:** The set struct is part of [`flint_sys`].
25    /// Any changes to this object are unsafe and may introduce memory leaks.
26    /// Please be aware that most moduli are shared across multiple instances and all
27    /// modifications of this struct will affect any other instance with a reference to this object.
28    ///
29    /// This function is a passthrough to enable users of this library to use [`flint_sys`]
30    /// and with that [FLINT](https://flintlib.org/) functions that might not be covered in our library yet.
31    /// If this is the case, please consider contributing to this open-source project
32    /// by opening a Pull Request at [qfall_math](https://github.com/qfall/math)
33    /// to provide this feature in the future.
34    ///
35    /// # Safety
36    /// Ensure that the old `modulus` does not share any memory with any moduli
37    /// that might be used in the future. The memory of the old `modulus` is freed
38    /// using this function.
39    ///
40    /// Any [`flint_sys`] struct and function is part of a FFI to the C-library `FLINT`.
41    /// As `FLINT` is a C-library, it does not provide all memory safety features
42    /// that Rust and our Wrapper provide.
43    /// Thus, using functions of [`flint_sys`] can introduce memory leaks.
44    pub unsafe fn set_fmpz_mod_ctx(&mut self, flint_struct: fmpz_mod_ctx) {
45        let modulus = std::rc::Rc::<fmpz_mod_ctx>::get_mut(&mut self.modulus).unwrap();
46
47        // free memory of old modulus before new values of modulus are copied
48        unsafe { fmpz_mod_ctx_clear(modulus) };
49
50        modulus.add_fxn = flint_struct.add_fxn;
51        modulus.mod_ = flint_struct.mod_;
52        modulus.mul_fxn = flint_struct.mul_fxn;
53        modulus.n = flint_struct.n;
54        modulus.n_limbs = flint_struct.n_limbs;
55        modulus.ninv_limbs = flint_struct.ninv_limbs;
56        modulus.sub_fxn = flint_struct.sub_fxn;
57    }
58}
59
60#[cfg(test)]
61mod test_get_fmpz_mod_ctx {
62    use super::Modulus;
63
64    /// Checks availability of the getter for [`Modulus::modulus`]
65    /// and its ability to be modified.
66    #[test]
67    #[allow(unused_mut)]
68    fn availability_and_modification() {
69        let mut modulus = Modulus::from(3);
70
71        let mut fmpz_mod = unsafe { modulus.get_fmpz_mod_ctx() };
72
73        fmpz_mod.n[0].0 = 2;
74
75        assert_eq!(Modulus::from(2), modulus);
76    }
77}
78
79#[cfg(test)]
80mod test_set_fmpz_mod_ctx {
81    use super::Modulus;
82    use flint_sys::{fmpz::fmpz, fmpz_mod::fmpz_mod_ctx_init};
83    use std::mem::MaybeUninit;
84
85    /// Checks availability of the setter for [`Modulus::modulus`]
86    /// and its ability to modify [`Modulus`].
87    #[test]
88    #[allow(unused_mut)]
89    fn availability_and_modification() {
90        let mut modulus = Modulus::from(3);
91
92        let mut flint_struct = MaybeUninit::uninit();
93        let mut flint_struct = unsafe {
94            fmpz_mod_ctx_init(flint_struct.as_mut_ptr(), &fmpz(2));
95            flint_struct.assume_init()
96        };
97
98        unsafe { modulus.set_fmpz_mod_ctx(flint_struct) };
99
100        assert_eq!(Modulus::from(2), modulus);
101    }
102}