Skip to main content

qfall_math/integer_mod_q/mat_zq/
unsafe_functions.rs

1// Copyright 2025 Jan 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 flint3_sys::{
17    fmpz_mat_clear, {fmpz_mod_ctx, fmpz_mod_mat_struct},
18};
19
20unsafe_getter!(MatZq, matrix, fmpz_mod_mat_struct);
21unsafe_getter_indirect!(MatZq, modulus, get_fmpz_mod_ctx, fmpz_mod_ctx);
22
23unsafe_setter!(MatZq, matrix, fmpz_mod_mat_struct, fmpz_mat_clear);
24unsafe_setter_indirect!(MatZq, modulus, set_fmpz_mod_ctx, fmpz_mod_ctx);
25
26#[cfg(test)]
27mod test_get_fmpz_mod_mat_struct {
28    use super::MatZq;
29    use crate::traits::MatrixDimensions;
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.c = 5;
42
43        assert_eq!(5, mat.get_num_columns());
44    }
45}
46
47#[cfg(test)]
48mod test_set_fmpz_mod_mat_struct {
49    use super::MatZq;
50    use crate::integer_mod_q::Modulus;
51    use flint3_sys::fmpz_mod_mat_init;
52    use std::{mem::MaybeUninit, str::FromStr};
53
54    /// Checks availability of the setter for [`MatZq::matrix`]
55    /// and its ability to modify [`MatZq`].
56    #[test]
57    fn availability_and_modification() {
58        let mut mat = MatZq::from_str("[[3]] mod 7").unwrap();
59        let mut flint_struct = MaybeUninit::uninit();
60        let modulus = Modulus::from(7);
61        let flint_struct = unsafe {
62            fmpz_mod_mat_init(
63                flint_struct.as_mut_ptr(),
64                1,
65                1,
66                modulus.get_fmpz_mod_ctx_struct(),
67            );
68            flint_struct.assume_init()
69        };
70
71        unsafe {
72            mat.set_fmpz_mod_mat_struct(flint_struct);
73        };
74
75        assert_eq!(MatZq::new(1, 1, 7), mat);
76    }
77}