Skip to content

Commit

Permalink
Implementing divide, truediv, mod for polynomial struct
Browse files Browse the repository at this point in the history
  • Loading branch information
elielnfinic committed Mar 19, 2024
1 parent a221547 commit d32e0d3
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 17 deletions.
2 changes: 1 addition & 1 deletion baby-stark-math-lib/src/linear_multiiplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub fn vector_scalar_multiplication(vector : Vec<i128>, scalar : i128) -> Vec<i1
vector.into_iter().map(|elt|{
elt * scalar
}).collect::<Vec<i128>>()
}
}
14 changes: 13 additions & 1 deletion baby-stark/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl FieldElement{
}
}

// TO BE IMPLEMENTED
pub fn __truediv__(self, right : FieldElement) -> FieldElement{
FieldElement::new()
}
Expand All @@ -59,7 +60,8 @@ impl FieldElement{
let acc = FieldElement::from(1, self.field.clone());
let val = FieldElement::from(self.value, self.field);

// TBD
// TBD


}

Expand Down Expand Up @@ -182,6 +184,16 @@ impl Field{
}


pub fn vector_multiplication_field_scalar ( vector : &Vec<FieldElement>, scalar : i128) -> Vec<FieldElement>{
let mut res : Vec<FieldElement> = vec![];
for field_element in vector.iter() {
res.push(FieldElement::from(field_element.value * scalar, vector.get(0).unwrap().field));
}

res
}


#[cfg(test)]
mod tests{
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion baby-stark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use baby_stark_math_lib;
use crate::field::FieldElement;

mod field;
// mod polynomial;
mod polynomial;

fn main() {
println!("Hello, world!");
Expand Down
108 changes: 94 additions & 14 deletions baby-stark/src/polynomial.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use baby_stark_math_lib::linear_multiiplication::vector_scalar_multiplication;
use core::num;
use std::cmp::max;

use crate::field::{self, FieldElement};
use crate::field::{self, vector_multiplication_field_scalar, FieldElement};

#[derive(Debug)]
pub fn multi_poly_and_scalar(poly : Polynomial, scalar : i128) -> Polynomial{
Polynomial{
coeficients : vector_multiplication_field_scalar(&poly.coeficients, scalar)
}
}

#[derive(Debug, Clone)]
pub struct Polynomial {
coeficients: Vec<FieldElement>,
}
Expand Down Expand Up @@ -42,17 +49,22 @@ impl Polynomial {
)
}

// TO FIX : ADD RETURN ELEMENT
pub fn __add__(self, other: Polynomial) -> Polynomial {
if self.degree() == -1 {
if self.clone().degree() == -1 {
return other;
} else if other.degree() == -1 {
} else if other.clone().degree() == -1 {
return self;
}
let field = self.coeficients.get(0).unwrap().field;
// let field = self.coeficients.get(0).unwrap().field;

let zero_vec = [field.zero(); 10].to_vec();
let largest_vec = max(self.coeficients.len(), other.coeficients.len());
let coeffs = vector_scalar_multiplication(zero_vec, largest_vec as i128);
// let zero_vec = [field.zero(); 10].to_vec();
// let largest_vec = max(self.coeficients.len(), other.coeficients.len());
// // let coeffs = vector_scalar_multiplication(zero_vec, largest_vec as i128);

Polynomial{
coeficients : vec![]
}
}

pub fn __sub__(self, other: Polynomial) -> Polynomial {
Expand All @@ -73,16 +85,16 @@ impl Polynomial {
}

pub fn __eq__(self, other: Polynomial) -> bool {
if self.degree() != other.degree() {
if self.clone().degree() != other.clone().degree() {
return false;
}
if self.degree() == -1 {
if self.clone().degree() == -1 {
return true;
}
self.coeficients
self.clone().coeficients
.iter()
.zip(other.coeficients.iter())
.all(|(a, b)| a == b)
.all(|(a, b)| (*a).__eq__(*b))
}

pub fn __neq__(self, other: Polynomial) -> bool {
Expand All @@ -96,7 +108,75 @@ impl Polynomial {
return false;
}

pub fn leading_coefficient(self) {
self.coeficients[self.degree()]
// TO BE FIXED
pub fn leading_coefficient(self) -> FieldElement{
// self.coeficients[self.degree()]
self.coeficients[0_usize]
}

pub fn divide(numerator : Polynomial, denominator : Polynomial) -> Option<(Polynomial, Polynomial)>{
if denominator.clone().degree() == -1 {
return None;
}

if numerator.clone().degree() < denominator.clone().degree() {
return Some((Polynomial::from(vec![]), numerator));
}

let field = denominator.clone().coeficients.get(0).unwrap().field;
let mut remainder = Polynomial::from(numerator.coeficients.clone());

let mut quotient_coefficients : Vec<FieldElement> = vec![];

let degree_diff = numerator.clone().degree() - denominator.clone().degree() + 1;

for i in 0 .. degree_diff {
quotient_coefficients.push(field.zero());
}

for i in 0 .. degree_diff {
if remainder.clone().degree() < denominator.clone().degree() {
break;
}

let coefficent = remainder.clone().leading_coefficient().__truediv__(denominator.clone().leading_coefficient());
let shift = remainder.clone().degree() - denominator.clone().degree();

let poly_product = multi_poly_and_scalar(Polynomial::from(vec![field.clone().zero()]), shift);

let subsctractee = (poly_product.__add__(Polynomial::from(vec![coefficent]))).__mul__(denominator.clone());

quotient_coefficients[shift as usize] = coefficent;

remainder = remainder.__sub__(subsctractee)
}

let quotient = Polynomial::from(quotient_coefficients);

Some((quotient, remainder))
}

pub fn __truediv__(self, other : Polynomial) -> Result<Polynomial, String> {
if other.clone().is_zero() {
return Err("CANT_DIVIDE_BY_0".to_string());
}
let divide_result = Polynomial::divide(self, other);
match divide_result {
Some(divide_result) => Ok(divide_result.0),
None => Err("COULD_NOT_DIVIDE".to_string())
}
}

pub fn __mod__(self, other : Polynomial) -> Result<Polynomial, String> {
let divide_result = Polynomial::divide(self, other);
match divide_result {
Some(divide_result) => Ok(divide_result.1),
None => Err("COULD_NOT_GET_MODULO".to_string())
}
}

pub fn __xor__(self, exponent : i128) -> Polynomial {
// TBD
Polynomial::from(vec![FieldElement::new()])
}
}

0 comments on commit d32e0d3

Please sign in to comment.