Skip to content

Commit

Permalink
feat: basic polynomial arithmetic (#48)
Browse files Browse the repository at this point in the history
* feat: basic polynomial arithmetic

* fix: remove polynomial const generics
  • Loading branch information
Autoparallel authored May 9, 2024
1 parent 2637594 commit eea6702
Show file tree
Hide file tree
Showing 7 changed files with 663 additions and 222 deletions.
22 changes: 21 additions & 1 deletion math/field.sage
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Define a finite field, our `PlutoField` which is
# Define a finite field, our `GF101` which is
# a field of 101 elements (101 being prime).
F = GF(101)
print(F)
Expand Down Expand Up @@ -39,6 +39,26 @@ omega_n = primitive_element ^ quotient
print("The", n, "th root of unity is: ", omega_n)

# Check that this is actually a root of unity:
assert omega_n^n == 1
print(omega_n, "^", n, " = ", omega_n^n)
######################################################################

######################################################################
# Let's find a mth root of unity (for l = 2)
# First, check that m divides 101 - 1 = 100
l = 2
assert (101 - 1) % l == 0
quotient = (101 - 1) // l
print("The quotient is: ", quotient)

# Find a primitive root of unity using the formula:
# omega = primitive_element^quotient
omega_l = primitive_element^quotient
print("The ", l, "th root of unity is: ", omega_l)

# Check that this is actually a root of unity:
assert omega_l^l == 1
print(omega_l, "^", l, " = ", omega_l^l)
assert omega_n ^ n == 1
print(omega_n, "^", n, " = ", omega_n ^ n)
######################################################################
Expand Down
36 changes: 36 additions & 0 deletions math/polynomial.sage
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Define a finite field, our `GF101` which is
# a field of 101 elements (101 being prime).
F = GF(101)

# Define the polynomial ring over GF(101)
R.<x> = PolynomialRing(F)

# Create the polynomials
a = 4*x^3 + 3*x^2 + 2*x^1 + 1
b = 9*x^4 + 8*x^3 + 7*x^2 + 6*x^1 + 5

# Perform polynomial division
q_ab, r_ab = a.quo_rem(b)

# Print the results
print("For a / b :")
print("Quotient:", q_ab)
print("Remainder:", r_ab)

# Perform polynomial division
q_ba, r_ba = b.quo_rem(a)

# Print the results
print("\nFor b / a :")
print("Quotient:", q_ba)
print("Remainder:", r_ba)

# Polynomials easy to do by hand
p = x^2 + 2*x + 1
q = x + 1

# Perform polynomial division
quot, rem = p.quo_rem(q)
print("\nFor p / q :")
print("Quotient:", quot)
print("Remainder:", rem)
10 changes: 6 additions & 4 deletions src/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign},
};

use super::*;

pub mod gf_101;
pub mod gf_101_2;

Expand Down Expand Up @@ -74,10 +76,10 @@ pub trait FiniteField:
// - There exists a multiplicative subgroup generated by a primitive element 'a'.
//
// According to the Sylow theorems (https://en.wikipedia.org/wiki/Sylow_theorems):
// A non-trivial multiplicative subgroup of prime order 'q' exists if and only if
// 'p - 1' is divisible by 'q'.
// The primitive q-th root of unity 'w' is defined as: w = a^((p - 1) / q),
// and the roots of unity are generated by 'w', such that {w^i | i in [0, q - 1]}.
// A non-trivial multiplicative subgroup of prime order 'n' exists if and only if
// 'p - 1' is divisible by 'n'.
// The primitive n-th root of unity 'w' is defined as: w = a^((p - 1) / n),
// and the roots of unity are generated by 'w', such that {w^i | i in [0, n - 1]}.
fn primitive_root_of_unity(n: Self::Storage) -> Self {
let p_minus_one = Self::ORDER - Self::Storage::from(1);
assert!(p_minus_one % n == 0.into(), "n must divide p - 1");
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ pub mod curves;
pub mod field;
pub mod kzg;
pub mod polynomial;

use core::{
fmt,
hash::Hash,
iter::{Product, Sum},
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign},
};
217 changes: 0 additions & 217 deletions src/polynomial.rs

This file was deleted.

Loading

0 comments on commit eea6702

Please sign in to comment.