ring_math/polynomial_ring.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
use std::fmt::Debug;
use std::fmt::Display;
use std::hash::Hash;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Div;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Neg;
use std::ops::Sub;
use std::ops::SubAssign;
use std::str::FromStr;
use scalarff::BigUint;
use scalarff::FieldElement;
use super::polynomial::Polynomial;
use super::Matrix2D;
use super::Vector;
/// A trait representing a polynomial ring
/// defined as `T[X]/<Self::modulus()>`
/// where T is a FieldElement trait
/// and modulus is a function implemented by the
/// struct implementing PolynomialRingElement
pub trait PolynomialRingElement:
FieldElement
+ Add<Output = Self>
+ AddAssign
+ Div<Output = Self>
+ Mul<Output = Self>
+ MulAssign
+ Neg<Output = Self>
+ Sub<Output = Self>
+ SubAssign
+ FromStr
+ PartialEq
+ Clone
+ Hash
+ Debug
+ From<u64>
+ From<Polynomial<Self::F>>
+ Display
{
type F: FieldElement;
/// Modulus used in remainder division to form
/// the polynomial ring.
///
/// Operations are done modulo this polynomial,
/// in a way similar to scalar fields.
///
/// See the division implementation for more info.
fn modulus() -> Polynomial<Self::F>;
/// Return the Polynomial representation of the current value
/// Used to automatically implement norms and other functions.
fn polynomial(&self) -> &Polynomial<Self::F>;
/// Create a polynomial ring element from a Polynomial
/// with terms in the ring's scalar field.
fn from_polynomial(p: Polynomial<Self::F>) -> Self;
/// Attempt to get a scalar representation of the polynomial.
/// If the polynomial degree is > 0 this method will error.
fn to_scalar(&self) -> anyhow::Result<Self::F> {
if self.polynomial().degree() == 0 {
Ok(self.polynomial().coefficients[0].clone())
} else {
anyhow::bail!("Cannot convert polynomial of degree > 0 to scalar")
}
}
/// Calculate the l1 norm for this polynomial. That is
/// the summation of all coefficients
fn norm_l1(&self) -> BigUint {
self.coef().norm_l1()
}
/// Calculate the l2 norm for this polynomial. That is
/// the square root of the summation of each coefficient squared
///
/// Specifically, we're calculating the square root in the integer
/// field, not the prime field
fn norm_l2(&self) -> BigUint {
self.coef().norm_l2()
}
/// Calculate the l-infinity norm for this polynomial. That is
/// the largest coefficient
fn norm_max(&self) -> BigUint {
self.coef().norm_max()
}
/// Returns a coefficient vector of length equal
/// to the ring modulus degree.
fn coef(&self) -> Vector<Self::F> {
let modulus = Self::modulus();
let target_degree = modulus.degree();
let poly_coefs = self.polynomial().coef_vec().to_vec();
let poly_coefs_len = poly_coefs.len();
Vector::from_vec(
[
poly_coefs,
vec![Self::F::zero(); target_degree - poly_coefs_len],
]
.concat(),
)
}
/// Create a rotated matrix of polynomial coefficients
///
/// from LatticeFold page 9
/// https://eprint.iacr.org/2024/257.pdf
fn rot(&self) -> Matrix2D<Self::F> {
let modulus = Self::modulus();
let degree = modulus.degree();
let mut values = vec![Self::F::zero(); degree * degree];
// TODO: check if this logic is correct
// technically in each row we're multiplying by X
// and then reducing by the modulus. In practice this
// results in coefficients being rotated and inverted.
//
// Test in with various coefficients and moduluses or
// mathematically verify
for i in 0..degree {
let mut coefs = self.coef().to_vec();
coefs.rotate_right(i);
for j in 0..i {
coefs[j] = -coefs[j].clone();
}
for j in 0..degree {
values[j * degree + i] = coefs[j].clone();
}
}
Matrix2D {
dimensions: (degree, degree),
values,
}
}
}
/// Use this to build a concrete instance of a polynomial ring.
///
/// e.g.
/// ```
/// use scalarff::OxfoiFieldElement;
/// use scalarff::FieldElement;
/// use ring_math::Polynomial;
/// use ring_math::PolynomialRingElement;
///
/// ring_math::polynomial_ring!(
/// Poly64,
/// OxfoiFieldElement,
/// {
/// let mut p = Polynomial::new(vec![OxfoiFieldElement::one()]);
/// p.term(&OxfoiFieldElement::one(), 64);
/// p
/// },
/// "Poly64"
/// );
/// ```
#[macro_export]
macro_rules! polynomial_ring {
( $name: ident, $field_element: ident, $modulus: expr, $name_str: expr ) => {
#[derive(Clone, Debug, PartialEq, Eq, std::hash::Hash)]
pub struct $name(pub Polynomial<$field_element>);
impl PolynomialRingElement for $name {
type F = $field_element;
fn modulus() -> Polynomial<Self::F> {
$modulus
}
fn polynomial(&self) -> &Polynomial<Self::F> {
&self.0
}
fn from_polynomial(p: Polynomial<Self::F>) -> Self {
$name(p)
}
}
impl From<Polynomial<$field_element>> for $name {
fn from(p: Polynomial<$field_element>) -> Self {
$name(p.div(&Self::modulus()).1)
}
}
impl FieldElement for $name {
fn zero() -> Self {
$name(Polynomial {
coefficients: vec![$field_element::zero()],
})
}
fn one() -> Self {
$name(Polynomial::identity())
}
fn byte_len() -> usize {
Self::modulus().degree() * $field_element::byte_len()
}
fn serialize(&self) -> String {
self.0
.coefficients
.iter()
.map(|v| v.serialize())
.collect::<Vec<_>>()
.join(",")
}
fn deserialize(str: &str) -> Self {
$name(Polynomial {
coefficients: str
.split(',')
.map(|v| $field_element::deserialize(v))
.collect::<Vec<_>>(),
})
}
fn prime() -> scalarff::BigUint {
panic!("cannot retrieve a scalar prime for a polynomial field");
}
fn name_str() -> &'static str {
$name_str
}
/// Return a constant polynomial with the provided
/// value
fn from_usize(value: usize) -> Self {
$name(Polynomial {
coefficients: vec![$field_element::from_usize(value)],
})
}
fn to_biguint(&self) -> scalarff::BigUint {
panic!("cannot retrieve a scalar representation for a polynomial field element");
}
fn from_biguint(_v: &scalarff::BigUint) -> Self {
panic!();
}
fn from_bytes_le(bytes: &[u8]) -> Self {
$name(Polynomial {
coefficients: bytes
.chunks($field_element::byte_len())
.map(|chunk| $field_element::from_bytes_le(chunk))
.collect::<Vec<_>>(),
})
}
fn to_bytes_le(&self) -> Vec<u8> {
self.0
.coefficients
.iter()
.flat_map(|v| v.to_bytes_le())
.collect()
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for $name {
type Err = ();
fn from_str(_s: &str) -> Result<Self, Self::Err> {
Err(())
}
}
impl From<u64> for $name {
fn from(value: u64) -> Self {
Self::from(Polynomial {
coefficients: vec![$field_element::from(value)],
})
}
}
impl std::ops::Add for $name {
type Output = Self;
fn add(self, other: Self) -> Self {
Self::from(self.0 + other.0)
}
}
impl std::ops::Sub for $name {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self::from(self.0 - other.0)
}
}
impl std::ops::Mul for $name {
type Output = Self;
fn mul(self, other: Self) -> Self {
Self::from(self.0 * other.0)
}
}
impl std::ops::Div for $name {
type Output = Self;
fn div(self, other: Self) -> Self {
// this implementation implies floored division, so discard the remainder
Self::from(self.0.div(&other.0).0)
}
}
impl std::ops::AddAssign for $name {
fn add_assign(&mut self, other: Self) {
*self = self.clone() + other;
}
}
impl std::ops::MulAssign for $name {
fn mul_assign(&mut self, other: Self) {
*self = self.clone() * other;
}
}
impl std::ops::SubAssign for $name {
fn sub_assign(&mut self, other: Self) {
*self = self.clone() - other;
}
}
impl std::ops::Neg for $name {
type Output = Self;
fn neg(self) -> Self {
$name(-self.0.clone())
}
}
};
}
#[cfg(test)]
mod test {
use scalarff::FieldElement;
use scalarff::OxfoiFieldElement;
use super::Polynomial;
use super::PolynomialRingElement;
polynomial_ring!(
Poly64,
OxfoiFieldElement,
{
let mut p = Polynomial::new(vec![OxfoiFieldElement::one()]);
p.term(&OxfoiFieldElement::one(), 64);
p
},
"Poly64"
);
#[test]
fn scalar_math_in_ring() {
for x in 100..500 {
for y in 200..600 {
let z_scalar = OxfoiFieldElement::from(x) * OxfoiFieldElement::from(y);
let z_poly = Poly64::from(x) * Poly64::from(y);
assert_eq!(z_poly.polynomial().degree(), 0);
assert_eq!(z_poly.polynomial().coefficients[0], z_scalar);
}
}
}
#[test]
fn poly_coefs() {
let poly = Poly64::one();
// the coefficient vector should always be equal in length
// to the degree of the polynomial ring modulus
let c = poly.coef();
assert_eq!(c.len(), Poly64::modulus().degree());
}
#[test]
fn poly_rot() {
// Testing the relationship as defined in the LatticeFold paper
// coef(a * b) = rot(a) * coef(b)
// we end up doing multiplication without a division
// reduction step (still O(n^2))
//
// sample two random polynomials
let a = Poly64::sample_uniform(&mut rand::thread_rng());
let b = Poly64::sample_uniform(&mut rand::thread_rng());
// create a rotated matrix of polynomial coefficients
let rot_mat = a.rot();
let b_coef = b.coef();
// check the above
let expected_coef = (a * b).coef();
let actual_coef = rot_mat * b_coef.clone();
for i in 0..b_coef.len() {
assert_eq!(expected_coef[i], actual_coef[i]);
}
}
}