Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qfall-math"
version = "0.1.2"
version = "0.2.0"
edition = "2024"
rust-version = "1.87" # due to wit_bindgen dependency
description = "Mathematical foundations for rapid prototyping of lattice-based cryptography"
Expand All @@ -14,19 +14,19 @@ autobenches = false

[dependencies]
criterion = { version = "0.8", features = ["html_reports"] }
flint-sys = "0.7"
libc = "0.2"
paste = "1"
paste = "1.0"
rand = "0.10"
rand_distr = "0.6"
regex = "1"
serde = {version="1", features=["derive"]}
serde_json = "1"
string-builder = "0.2"
regex = "1.13"
serde = {version="1.0", features=["derive"]}
serde_json = "1.0"
string-builder = "0.2.0"
thiserror = "2"
lazy_static = "1"
probability = "0.20"
derive_more = { version = "2", features = ["display"] }
derive_more = { version = "2.1", features = ["display"] }
flint3-sys = { version="3.6.0", features=["gmp-mpfr-sys"]}

[profile.bench]
debug = true
Expand Down
3 changes: 3 additions & 0 deletions src/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ pub use poly_over_z::PolyOverZ;
pub(crate) use poly_over_z::fmpz_poly_helpers;
pub use z::Z;
pub(crate) use z::fmpz_helpers;

pub(crate) use mat_z::debug_fmpz_mat_struct;
pub(crate) use z::debug_fmpz;
16 changes: 13 additions & 3 deletions src/integer/mat_poly_over_z.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! This implementation uses the [FLINT](https://flintlib.org/) library.

use crate::utils::parse::partial_string;
use flint_sys::fmpz_poly_mat::fmpz_poly_mat_struct;
use flint3_sys::fmpz_poly_mat_struct;
use std::fmt;

mod arithmetic;
Expand Down Expand Up @@ -88,10 +88,20 @@ impl fmt::Debug for MatPolyOverZ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"MatPolyOverZ: {{matrix: {}, storage: {:?}}}",
"MatPolyOverZ: {{matrix: {}, storage: {{ matrix: {}}}}}",
// printing the entire matrix is not meaningful for large matrices
partial_string(self, 3, 3),
self.matrix
debug_fmpz_poly_mat_struct(&self.matrix)
)
}
}

pub(crate) fn debug_fmpz_poly_mat_struct(value: &fmpz_poly_mat_struct) -> String {
format!(
"fmpz_poly_mat_struct {{ entries: {:#x}, r: {}, c: {}, stride: {} }}",
value.entries.addr(),
value.r,
value.c,
value.stride
)
}
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/arithmetic/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::macros::arithmetics::{
arithmetic_trait_mixed_borrowed_owned,
};
use crate::traits::MatrixDimensions;
use flint_sys::fmpz_poly_mat::fmpz_poly_mat_add;
use flint3_sys::fmpz_poly_mat_add;
use std::ops::{Add, AddAssign};

impl AddAssign<&MatPolyOverZ> for MatPolyOverZ {
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/arithmetic/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::macros::arithmetics::{
arithmetic_trait_borrowed_to_owned, arithmetic_trait_mixed_borrowed_owned,
};
use crate::traits::MatrixDimensions;
use flint_sys::fmpz_poly_mat::fmpz_poly_mat_mul_KS;
use flint3_sys::fmpz_poly_mat_mul_KS;
use std::ops::Mul;

impl Mul for &MatPolyOverZ {
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/arithmetic/mul_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::macros::arithmetics::{
};
use crate::macros::for_others::implement_for_others;
use crate::traits::MatrixDimensions;
use flint_sys::fmpz_poly_mat::{fmpz_poly_mat_scalar_mul_fmpz, fmpz_poly_mat_scalar_mul_fmpz_poly};
use flint3_sys::{fmpz_poly_mat_scalar_mul_fmpz, fmpz_poly_mat_scalar_mul_fmpz_poly};
use std::ops::{Mul, MulAssign};

impl Mul<&Z> for &MatPolyOverZ {
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/arithmetic/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::macros::arithmetics::{
arithmetic_trait_mixed_borrowed_owned,
};
use crate::traits::MatrixDimensions;
use flint_sys::fmpz_poly_mat::fmpz_poly_mat_sub;
use flint3_sys::fmpz_poly_mat_sub;
use std::ops::{Sub, SubAssign};

impl SubAssign<&MatPolyOverZ> for MatPolyOverZ {
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
macros::compare_base::compare_base_default,
traits::CompareBase,
};
use flint_sys::fmpz_poly_mat::fmpz_poly_mat_equal;
use flint3_sys::fmpz_poly_mat_equal;

impl PartialEq for MatPolyOverZ {
/// Checks if two matrices over [`PolyOverZ`](crate::integer::PolyOverZ) are equal. Used by the `==` and `!=` operators.
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
error::MathError,
traits::{Concatenate, MatrixDimensions},
};
use flint_sys::fmpz_poly_mat::{fmpz_poly_mat_concat_horizontal, fmpz_poly_mat_concat_vertical};
use flint3_sys::{fmpz_poly_mat_concat_horizontal, fmpz_poly_mat_concat_vertical};

impl Concatenate for &MatPolyOverZ {
type Output = MatPolyOverZ;
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use super::MatPolyOverZ;
use crate::utils::index::evaluate_indices;
use flint_sys::fmpz_poly_mat::{fmpz_poly_mat_init, fmpz_poly_mat_one};
use flint3_sys::{fmpz_poly_mat_init, fmpz_poly_mat_one};
use std::{fmt::Display, mem::MaybeUninit};

impl MatPolyOverZ {
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
integer::{MatZ, Z},
traits::{Evaluate, MatrixDimensions},
};
use flint_sys::fmpz_poly_mat::fmpz_poly_mat_evaluate_fmpz;
use flint3_sys::fmpz_poly_mat_evaluate_fmpz;

impl<Integer: Into<Z>> Evaluate<Integer, MatZ> for MatPolyOverZ {
/// Evaluates a [`MatPolyOverZ`] on a given input entrywise.
Expand Down
31 changes: 17 additions & 14 deletions src/integer/mat_poly_over_z/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use crate::{
integer::PolyOverZ,
traits::{MatrixDimensions, MatrixGetEntry, MatrixGetSubmatrix},
};
use flint_sys::{
fmpz_poly::{fmpz_poly_set, fmpz_poly_struct},
fmpz_poly_mat::{
use flint3_sys::{
fmpz_poly_set, fmpz_poly_struct,
{
fmpz_poly_mat_entry, fmpz_poly_mat_init_set, fmpz_poly_mat_window_clear,
fmpz_poly_mat_window_init,
},
Expand Down Expand Up @@ -171,15 +171,18 @@ impl MatPolyOverZ {
///
/// let fmpz_entries = mat.collect_entries();
/// ```
pub(crate) fn collect_entries(&self) -> Vec<fmpz_poly_struct> {
let mut entries: Vec<fmpz_poly_struct> =
pub(crate) fn collect_entries<'a>(&self) -> Vec<&'a fmpz_poly_struct> {
let mut entries: Vec<&'a fmpz_poly_struct> =
Vec::with_capacity((self.get_num_rows() * self.get_num_columns()) as usize);

for row in 0..self.get_num_rows() {
for col in 0..self.get_num_columns() {
// efficiently get entry without cloning the entry itself
let entry = unsafe { *fmpz_poly_mat_entry(&self.matrix, row, col) };
entries.push(entry);
unsafe {
let entry_ptr = fmpz_poly_mat_entry(&self.matrix, row, col);
let entry_ref: &'a fmpz_poly_struct = &*entry_ptr;
entries.push(entry_ref);
}
}
}

Expand Down Expand Up @@ -592,16 +595,16 @@ mod test_collect_entries {
let entries_2 = mat_2.collect_entries();

assert_eq!(entries_1.len(), 6);
assert_eq!(unsafe { *entries_1[0].coeffs }.0, 1);
assert!(unsafe { *entries_1[2].coeffs }.0 >= 2_i64.pow(62));
assert!(unsafe { *entries_1[3].coeffs }.0 >= 2_i64.pow(62));
assert_eq!(unsafe { *entries_1[4].coeffs }.0, -3);
assert_eq!(unsafe { *entries_1[0].coeffs }, 1);
assert!(unsafe { *entries_1[2].coeffs } >= 2_i64.pow(62));
assert!(unsafe { *entries_1[3].coeffs } >= 2_i64.pow(62));
assert_eq!(unsafe { *entries_1[4].coeffs }, -3);

assert_eq!(entries_2.len(), 2);
assert_eq!(unsafe { *entries_2[0].coeffs.offset(0) }.0, -1);
assert_eq!(unsafe { *entries_2[0].coeffs.offset(0) }, -1);
assert_eq!(entries_2[0].length, 1);
assert_eq!(unsafe { *entries_2[1].coeffs.offset(0) }.0, 1);
assert_eq!(unsafe { *entries_2[1].coeffs.offset(1) }.0, 2);
assert_eq!(unsafe { *entries_2[1].coeffs.offset(0) }, 1);
assert_eq!(unsafe { *entries_2[1].coeffs.offset(1) }, 2);
assert_eq!(entries_2[1].length, 2);
}
}
51 changes: 31 additions & 20 deletions src/integer/mat_poly_over_z/ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use super::MatPolyOverZ;
use crate::traits::MatrixDimensions;
use flint_sys::fmpz_poly_mat::{fmpz_poly_mat_clear, fmpz_poly_mat_set};
use flint3_sys::{fmpz_poly_mat_clear, fmpz_poly_mat_set};

impl Clone for MatPolyOverZ {
/// Clones the given element and returns a deep clone of the [`MatPolyOverZ`] element.
Expand All @@ -30,7 +30,7 @@ impl Clone for MatPolyOverZ {
// we can unwrap since we know, that the number of rows and columns is positive and fits into an [`i64`]
let mut clone = MatPolyOverZ::new(self.get_num_rows(), self.get_num_columns());

unsafe { fmpz_poly_mat_set(&mut clone.matrix, &mut self.matrix.to_owned()) }
unsafe { fmpz_poly_mat_set(&mut clone.matrix, &self.matrix) }

clone
}
Expand Down Expand Up @@ -78,20 +78,26 @@ mod test_clone {
// an i64, both should be a pointer and their values should differ
unsafe {
assert_ne!(
(*(*poly_1.matrix.entries).coeffs.offset(0)).0,
(*(*poly_2.matrix.entries).coeffs.offset(0)).0
(*(*poly_1.matrix.entries).coeffs.offset(0)),
(*(*poly_2.matrix.entries).coeffs.offset(0))
);
}
unsafe {
assert_ne!(
(*(*poly_1.matrix.entries).coeffs.offset(1)).0,
(*(*poly_2.matrix.entries).coeffs.offset(1)).0
(*(*poly_1.matrix.entries).coeffs.offset(1)),
(*(*poly_2.matrix.entries).coeffs.offset(1))
);
}

// check if length of polynomial is correctly cloned
assert_eq!(unsafe { *poly_1.matrix.entries.offset(0) }.length, 2);
assert_eq!(unsafe { *poly_2.matrix.entries.offset(0) }.length, 2);
assert_eq!(
unsafe { std::ptr::read(poly_1.matrix.entries.offset(0)) }.length,
2
);
assert_eq!(
unsafe { std::ptr::read(poly_2.matrix.entries.offset(0)) }.length,
2
);

assert_eq!(poly_1, poly_2);
}
Expand All @@ -110,20 +116,26 @@ mod test_clone {
// both should be stored directly on stack and their values should be equal
unsafe {
assert_eq!(
(*(*poly_1.matrix.entries).coeffs.offset(0)).0,
(*(*poly_2.matrix.entries).coeffs.offset(0)).0
(*(*poly_1.matrix.entries).coeffs.offset(0)),
(*(*poly_2.matrix.entries).coeffs.offset(0))
);
}
unsafe {
assert_eq!(
(*(*poly_1.matrix.entries).coeffs.offset(1)).0,
(*(*poly_2.matrix.entries).coeffs.offset(1)).0
(*(*poly_1.matrix.entries).coeffs.offset(1)),
(*(*poly_2.matrix.entries).coeffs.offset(1))
);
}

// check if length of polynomial is correctly cloned
assert_eq!(unsafe { *poly_1.matrix.entries.offset(0) }.length, 2);
assert_eq!(unsafe { *poly_2.matrix.entries.offset(0) }.length, 2);
assert_eq!(
unsafe { std::ptr::read(poly_1.matrix.entries.offset(0)) }.length,
2
);
assert_eq!(
unsafe { std::ptr::read(poly_2.matrix.entries.offset(0)) }.length,
2
);

assert_eq!(poly_1, poly_2);
}
Expand Down Expand Up @@ -151,7 +163,7 @@ mod test_drop {
/// Creates and drops a [`MatPolyOverZ`], and returns the storage points in memory
fn create_and_drop_poly_over_z() -> i64 {
let a = MatPolyOverZ::from_str(&format!("[[1 {}]]", u64::MAX)).unwrap();
unsafe { *(*a.matrix.entries).coeffs.offset(0) }.0
unsafe { *(*a.matrix.entries).coeffs.offset(0) }
}

/// Check whether freed memory is reused afterwards
Expand All @@ -166,13 +178,12 @@ mod test_drop {
assert!(set.capacity() < 5);

let a = MatPolyOverZ::from_str(&format!("[[2 {} {}]]", u64::MAX - 1, u64::MAX)).unwrap();
let storage_point = unsafe { *(*a.matrix.entries).coeffs.offset(0) }.0;
let storage_point = unsafe { *(*a.matrix.entries).coeffs.offset(0) };

// memory slots differ due to previously created large integer
let d = MatPolyOverZ::from_str(&format!("[[2 {} {}]]", u64::MAX - 1, u64::MAX)).unwrap();
assert_ne!(
storage_point,
unsafe { *(*d.matrix.entries).coeffs.offset(0) }.0
);
assert_ne!(storage_point, unsafe {
*(*d.matrix.entries).coeffs.offset(0)
});
}
}
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
integer::Z,
traits::{MatrixDimensions, MatrixGetEntry},
};
use flint_sys::fmpz_poly_mat::{fmpz_poly_mat_is_one, fmpz_poly_mat_is_zero, fmpz_poly_mat_rank};
use flint3_sys::{fmpz_poly_mat_is_one, fmpz_poly_mat_is_zero, fmpz_poly_mat_rank};

impl MatPolyOverZ {
/// Checks if a [`MatPolyOverZ`] is a identity matrix, i.e.
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
integer::{PolyOverZ, poly_over_z::fmpz_poly_helpers::reduce_fmpz_poly_by_poly_over_z},
traits::MatrixDimensions,
};
use flint_sys::fmpz_poly_mat::fmpz_poly_mat_entry;
use flint3_sys::fmpz_poly_mat_entry;

impl MatPolyOverZ {
/// Entrywise reduces a matrix of polynomials by a polynomial `modulus`.
Expand Down
6 changes: 3 additions & 3 deletions src/integer/mat_poly_over_z/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ use crate::{
traits::{MatrixDimensions, MatrixSetEntry, MatrixSetSubmatrix, MatrixSwaps},
utils::index::{evaluate_index_for_vector, evaluate_indices_for_matrix},
};
use flint_sys::{
fmpz_poly::{fmpz_poly_set, fmpz_poly_swap},
fmpz_poly_mat::{
use flint3_sys::{
{
fmpz_poly_mat_entry, fmpz_poly_mat_set, fmpz_poly_mat_window_clear,
fmpz_poly_mat_window_init,
},
{fmpz_poly_set, fmpz_poly_swap},
};
use std::{fmt::Display, mem::MaybeUninit};

Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
integer::PolyOverZ,
traits::{MatrixDimensions, MatrixGetEntry, Tensor},
};
use flint_sys::{fmpz_poly::fmpz_poly_mul, fmpz_poly_mat::fmpz_poly_mat_entry};
use flint3_sys::{fmpz_poly_mat_entry, fmpz_poly_mul};

impl Tensor for MatPolyOverZ {
/// Computes the tensor product of `self` with `other`.
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

//! This module contains the implementation of the `trace` function.

use flint_sys::fmpz_poly_mat::fmpz_poly_mat_trace;
use flint3_sys::fmpz_poly_mat_trace;

use super::MatPolyOverZ;
use crate::{error::MathError, integer::PolyOverZ, traits::MatrixDimensions};
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/transpose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use super::MatPolyOverZ;
use crate::traits::MatrixDimensions;
use flint_sys::fmpz_poly_mat::fmpz_poly_mat_transpose;
use flint3_sys::fmpz_poly_mat_transpose;

impl MatPolyOverZ {
/// Returns the transposed form of the given matrix, i.e. rows get transformed to columns
Expand Down
Loading
Loading