qfall_math/integer_mod_q/mat_zq/
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::MatZq;
13use crate::macros::unsafe_passthrough::{
14    unsafe_getter, unsafe_getter_indirect, unsafe_setter, unsafe_setter_indirect,
15};
16use flint_sys::{
17    fmpz_mod::fmpz_mod_ctx,
18    fmpz_mod_mat::{fmpz_mod_mat_clear, fmpz_mod_mat_struct},
19};
20
21unsafe_getter!(MatZq, matrix, fmpz_mod_mat_struct);
22unsafe_getter_indirect!(MatZq, modulus, get_fmpz_mod_ctx, fmpz_mod_ctx);
23
24unsafe_setter!(MatZq, matrix, fmpz_mod_mat_struct, fmpz_mod_mat_clear);
25unsafe_setter_indirect!(MatZq, modulus, set_fmpz_mod_ctx, fmpz_mod_ctx);
26
27#[cfg(test)]
28mod test_get_fmpz_mod_mat_struct {
29    use super::MatZq;
30    use std::str::FromStr;
31
32    /// Checks availability of the getter for [`MatZq::matrix`]
33    /// and its ability to be modified.
34    #[test]
35    #[allow(unused_mut)]
36    fn availability_and_modification() {
37        let mut mat = MatZq::from_str("[[3]] mod 7").unwrap();
38
39        let mut fmpz_mat = unsafe { mat.get_fmpz_mod_mat_struct() };
40
41        fmpz_mat.mod_[0].0 = 5;
42
43        assert_eq!(MatZq::from_str("[[3]] mod 5").unwrap(), mat);
44    }
45}
46
47#[cfg(test)]
48mod test_set_fmpz_mod_mat_struct {
49    use super::MatZq;
50    use flint_sys::{fmpz::fmpz, fmpz_mod_mat::fmpz_mod_mat_init};
51    use std::{mem::MaybeUninit, str::FromStr};
52
53    /// Checks availability of the setter for [`MatZq::matrix`]
54    /// and its ability to modify [`MatZq`].
55    #[test]
56    fn availability_and_modification() {
57        let mut mat = MatZq::from_str("[[3]] mod 7").unwrap();
58        let mut flint_struct = MaybeUninit::uninit();
59        let flint_struct = unsafe {
60            fmpz_mod_mat_init(flint_struct.as_mut_ptr(), 1, 1, &fmpz(7));
61            flint_struct.assume_init()
62        };
63
64        unsafe {
65            mat.set_fmpz_mod_mat_struct(flint_struct);
66        };
67
68        assert_eq!(MatZq::new(1, 1, 7), mat);
69    }
70}