Skip to content

Commit e385879

Browse files
authored
feat(ring): introduce RingElement API and bump version to 1.4.0 (#53)
* feat(ring): introduce `RingElement` API and bump version to `1.4.0` - Add `RingElement<C>` for polynomial arithmetic in $R_q = \mathbb{Z}_q[X]/(X^N + 1)$ with dual-state (coefficient/NTT) representation and lazy conversion. - Make `lumen_math::ring` module public for direct access. - Update CHANGELOG, README, and tests to document and validate the new API. - Bump crate version to `1.4.0`. * feat(ring): add `modify_data` for safe coefficient modifications, refactor addition/subtraction logic, and expand tests - Introduced `modify_data` method for safe mutation of polynomial coefficients, enforcing state consistency. - Refactored addition and subtraction methods to simplify RHS state conversion logic. - Expanded test coverage to validate edge cases, state transitions, format implementations, and API behavior.
1 parent 3734f43 commit e385879

9 files changed

Lines changed: 1318 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.4.0] - 2026-01-06
9+
10+
### Added
11+
12+
- **Ring Element for Lattice Cryptography**: New `RingElement<C>` type for polynomials in $R_q = \mathbb{Z}_q[X]/(X^N + 1)$
13+
- **Dual-state representation**: Coefficient form ↔ NTT form with lazy conversion
14+
- **Arithmetic operations**: `Add`, `Sub`, `Mul`, `Neg` for both owned and reference types
15+
- **State management**: `to_ntt()`, `to_coefficient()`, `into_ntt()`, `into_coefficient()`
16+
- **Shared context**: Uses `Arc<NttContext<C>>` for efficient NTT table sharing
17+
- **Query methods**: `state()`, `coefficients()`, `ntt_values()`, `is_zero()`, `scale()`
18+
- Exports: `RingElement`, `RingElementState` from `lumen_math::ring`
19+
20+
### Improved
21+
22+
- **Public ring module**: `lumen_math::ring` is now public for direct access
23+
824
## [1.3.0] - 2025-12-30
925

1026
### Changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lumen-math"
3-
version = "1.3.0"
3+
version = "1.4.0"
44
edition = "2024"
55

66
[dependencies]

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A high-performance mathematical library for Rust, designed for cryptographic app
1515
- **Number Theoretic Transform (NTT)**: Fast polynomial multiplication using NTT (O(n log n)) with Cooley-Tukey algorithm.
1616
- **Negacyclic NTT**: Specialized NTT for lattice-based cryptography (Kyber/Dilithium) over rings $Z_q[X]/(X^N + 1)$.
1717
- **Small-Modulus Fields**: Optimized native `u32`/`u64` arithmetic with Barrett reduction for Kyber/Dilithium.
18+
- **Ring Elements**: `RingElement<C>` with dual-state (coefficient/NTT) representation and lazy conversion.
1819
- **Hardware Acceleration**: AVX2 optimized backend for specific operations on x86_64 architectures (e.g., XOR, conditional selection).
1920
- **GMP Integration**: Optional backend using GMP for verification and comparison (enabled via `gmp` feature).
2021
- **Cryptographic Protocols**: Implementation of Extended Euclidean Algorithm (GCD) and Chinese Remainder Theorem (CRT).
@@ -95,6 +96,25 @@ let d2 = DilithiumFieldElement::new(67890);
9596
let d_prod = d1 * d2;
9697
```
9798

99+
### Ring Elements (Polynomial Rings)
100+
101+
For operations in polynomial rings $R_q = \mathbb{Z}_q[X]/(X^N + 1)$:
102+
103+
```rust
104+
use lumen_math::{RingElement, NttContext, DefaultFieldConfig, fp};
105+
use std::sync::Arc;
106+
107+
// Create shared NTT context
108+
let ctx = Arc::new(NttContext::<DefaultFieldConfig>::new(256));
109+
110+
// Create ring elements
111+
let a = RingElement::new(vec![fp!(1u64); 256], ctx.clone());
112+
let b = RingElement::one(ctx);
113+
114+
// Arithmetic (auto-converts between coefficient/NTT forms)
115+
let product = a * b; // Uses NTT multiplication
116+
```
117+
98118
### Field Arithmetic (Generic)
99119

100120
```rust
@@ -152,6 +172,8 @@ The library is structured into several core modules:
152172
- **`protocol`**: Cryptographic primitives.
153173
- `gcd`: Extended Euclidean Algorithm.
154174
- `crt`: Chinese Remainder Theorem solver.
175+
- **`ring`**: Ring elements for lattice-based cryptography.
176+
- `element`: `RingElement<C>` with dual-state (coefficient/NTT) representation.
155177
- **`traits`**: Core traits defining the interface for big integers and fields.
156178

157179
## Performance

THEORY.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,23 @@ $$
249249
| **Kyber** | 3329 | 256 | 17 | 289 |
250250
| **Dilithium** | 8380417 | 256 | 1753 | 3073009 |
251251

252+
### Ring Elements
253+
254+
The `RingElement<C>` type encapsulates polynomials in the ring $R_q$ with lazy state management:
255+
256+
**State Representation:**
257+
- **Coefficient form**: $[c_0, c_1, \ldots, c_{N-1}]$ — natural representation
258+
- **NTT form**: $[\hat{c}_0, \hat{c}_1, \ldots, \hat{c}_{N-1}]$ — transformed for multiplication
259+
260+
**Lazy Conversion:**
261+
- Addition/Subtraction: Operates in coefficient form (auto-converts if needed)
262+
- Multiplication: Operates in NTT form (pointwise multiplication is $O(N)$)
263+
- Negation: Works in either form (coefficient-wise negation)
264+
265+
**Efficiency:**
266+
- Shared `NttContext<C>` via `Arc` avoids recomputing twiddle factors
267+
- State tracking prevents redundant conversions
268+
252269
---
253270

254271
## Number Theory Algorithms

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod big_int;
1212
pub mod field;
1313
pub mod poly;
1414
pub mod protocol;
15+
pub mod ring;
1516
pub mod traits;
1617

1718
pub use lumen_math_macros::FieldConfig;
@@ -45,6 +46,9 @@ pub use crate::poly::ntt::{NttContext, intt_negacyclic, mul_negacyclic, ntt_nega
4546
// Traits
4647
pub use traits::{BigInt, Digest};
4748

49+
// Ring elements for lattice crypto
50+
pub use crate::ring::{RingElement, RingElementState};
51+
4852
/// Computes N' for Montgomery reduction where P * N' = -1 mod 2^1024.
4953
///
5054
/// This is a convenience re-export of `MontgomeryContext::compute_n_prime`.

0 commit comments

Comments
 (0)