qfall/math

GitHub: qfall/math

基于格的密码学数学库的内存安全 Rust 包装器,提供高效可靠的数学原语。

Stars: 29 | Forks: 2

# qFALL-math [github](https://github.com/qfall/math) [crates.io](https://crates.io/crates/qfall-math) [docs.rs](https://docs.rs/qfall-math) [tutorial](https://qfall.github.io/book) [build](https://github.com/qfall/math/actions/workflows/main.yml) [license](https://github.com/qfall/math/blob/dev/LICENSE) `qFALL` 是一个基于格的密码学原型库。 这个 `math` 库是 Rust 中 [FLINT](https://flintlib.org/) 的内存安全包装器,提供了许多在基于格的密码学中常用的额外功能。该库是 [qFALL 项目](https://qfall.github.io) 的基础,包含用于基于格的密码学原型的其他库。 ## 快速开始 首先,请确保你使用的是类 Unix 发行版(Linux 或 MacOS)。如果使用 Windows,请设置 [WSL](https://learn.microsoft.com/en-us/windows/wsl/install)。由于该库依赖于 FLINT,因此这是必需的。 然后,请确保你的 `rustc --version` 是 `1.87` 或更高版本。 此外,还需要安装 `m4`、C 编译器(如 `gcc`)和 `make`。 ``` sudo apt-get install m4 gcc make ``` 然后,你可以通过执行以下命令将此库添加到你的项目中。 ``` cargo add qfall-math ``` - 在 [我们的网站](https://qfall.github.io/) 上查找更多信息。同时查看 [`qfall-tools`](https://crates.io/crates/qfall-tools) 和 [`qfall-schemes`](https://crates.io/crates/qfall-schemes)。 - 阅读该库的 [文档](https://docs.rs/qfall-math)。 - 我们推荐 [我们的教程](https://qfall.github.io/book) 来开始使用 qFALL。 ## qFALL-math 提供了什么? 我们想指出一些对基于格的密码学特别重要的支持功能。 - 均匀分布、离散高斯分布和二项式采样 - 支持多种范数 - 求解线性方程组 - 支持数论变换(NTT) 此外,该库通过支持张量乘法、序列化、矩阵拼接等功能简化了原型的实现。 算术运算、比较和转换在多种类型上得到支持。你可以在下方找到所有支持的数据类型。 ### 整数 - [`Z`](https://docs.rs/qfall-math/latest/qfall_math/integer/struct.Z.html):表示 $\mathbb Z$。 - [`MatZ`](https://docs.rs/qfall-math/latest/qfall_math/integer/struct.MatZ.html):表示 $\mathbb Z$ 上的矩阵。 - [`PolyOverZ`](https://docs.rs/qfall-math/latest/qfall_math/integer/struct.PolyOverZ.html):表示系数在 $\mathbb Z$ 上的多项式。 - [`MatPolyOverZ`](https://docs.rs/qfall-math/latest/qfall_math/integer/struct.MatPolyOverZ.html):表示系数在 $\mathbb Z$ 上的多项式矩阵。 ### 模 q 的整数 - [`Zq`](https://docs.rs/qfall-math/latest/qfall_math/integer_mod_q/struct.Zq.html):表示 $\mathbb Z_q$。 - [`MatZq`](https://docs.rs/qfall-math/latest/qfall_math/integer_mod_q/struct.MatZq.html):表示 $\mathbb Z_q$ 上的矩阵。 - [`PolyOverZq`](https://docs.rs/qfall-math/latest/qfall_math/integer_mod_q/struct.PolyOverZq.html):表示系数在 $\mathbb Z_q$ 上的多项式。 - [`PolynomialRingZq`](https://docs.rs/qfall-math/latest/qfall_math/integer_mod_q/struct.PolynomialRingZq.html):表示商环 $\mathbb Z_q[X]/f(X)$。 - [`MatPolynomialRingZq`](https://docs.rs/qfall-math/latest/qfall_math/integer_mod_q/struct.MatPolynomialRingZq.html):表示商环 $\mathbb Z_q[X]/f(X)$ 上的矩阵。 - [`NTTPolynomialRingZq`](https://docs.rs/qfall-math/latest/qfall_math/integer_mod_q/struct.NTTPolynomialRingZq.html):表示 NTT 形式的商环 $\mathbb Z_q[X]/f(X)$。 - [`MatNTTPolynomialRingZq`](https://docs.rs/qfall-math/latest/qfall_math/integer_mod_q/struct.MatNTTPolynomialRingZq.html):表示 NTT 形式的商环 $\mathbb Z_q[X]/f(X)$ 上的矩阵。 ### 有理数 - [`Q`](https://docs.rs/qfall-math/latest/qfall_math/rational/struct.Q.html):表示 $\mathbb Q$。 - [`MatQ`](https://docs.rs/qfall-math/latest/qfall_math/rational/struct.MatQ.html):表示 $\mathbb Q$ 上的矩阵。 - [`PolyOverQ`](https://docs.rs/qfall-math/latest/qfall_math/rational/struct.PolyOverQ.html):表示系数在 $\mathbb Q$ 上的多项式。 ## 快速示例 ``` use qfall_math::{integer_mod_q::MatZq, integer::MatZ}; let (n, m, q) = (256, 1024, 3329); let (center, sigma) = (0.0, 8.0); let mat_a = MatZq::sample_uniform(n, m, q); let vec_s = MatZ::sample_uniform(n, 1, 0, 2).unwrap(); let vec_e = MatZ::sample_discrete_gauss(m, 1, center, sigma).unwrap(); // SIS-Instance: t = A * e mod q let vec_t = &mat_a * &vec_e; // LWE-Instance: b^T = s^T * A + e^T mod q let vec_b = vec_s.transpose() * mat_a + vec_e.transpose(); ``` ## 已知问题 请通过 [GitHub 问题跟踪器](https://github.com/qfall/math/issues) 报告错误。 ## 引用 请使用以下 BibTeX 条目来引用 [qFALL](https://qfall.github.io)。 ``` @misc{qfall, author = {Marvin Beckmann and Phil Milewski and Laurens Porzenheim and Marcel Luca Schmidt and Jan Niklas Siemer}, title = {{qFALL} – {Rapid Prototyping of Lattice-based Cryptography}}, howpublished = {Cryptology {ePrint} Archive, Paper 2026/069}, year = {2026}, url = {https://eprint.iacr.org/2026/069} } ``` ## 依赖项 该项目使用基于 C 的优化数学库 [FLINT](https://flintlib.org/)。我们已对 FLINT 的使用进行了广泛测试,以确保使用该库不会引入内存泄漏。 如果需要 FLINT 支持但本库未提供的功能,该库提供了对 FLINT 结构体的 `unsafe` 直接访问和操作功能。 此外,我们使用 [serde](https://crates.io/crates/serde) 和 [serde_json](https://crates.io/crates/serde_json) 对对象进行(反)序列化。该库使用 [criterion](https://crates.io/crates/criterion) 进行基准测试。最后但同样重要的是,我们的采样算法使用 [rand](https://crates.io/crates/rand)-crate 生成均匀随机比特。完整列表可以在我们的 `Cargo.toml` 文件中找到。 ## 许可证 该库根据 [Mozilla Public License Version 2.0](https://github.com/qfall/math/blob/dev/LICENSE) 分发。 此弱复制左许可证的条件是使已许可文件的源代码及其修改版本在相同许可证下(或在某些情况下,在 GNU 许可证下)可用。必须保留版权和许可证声明。贡献者提供专利权利的明确授权。然而,较大作品使用已许可作品时,可以按照不同条款分发,且无需提供源代码用于添加到较大作品中的文件。
标签:Crates.io, CVE, FLINT, GitHub Actions, MPL 2.0, qFALL, Rust, 内存安全, 加密库, 区块链安全, 可视化界面, 同态加密, 后量子密码, 多项式算术, 大数计算, 安全原型, 密码学原型, 密钥交换, 开源, 开源框架, 快速原型, 持续集成, 教程, 数字签名, 数学基础, 数学库, 文档, 晶格密码, 格密码, 模运算, 网络流量审计, 自动笔记, 通知系统, 零知识证明