Skip to content

Commit 05ae532

Browse files
committed
Operator operloading for polynomial
Signed-off-by: lovesh <[email protected]>
1 parent 665c9f3 commit 05ae532

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "amcl_wrapper"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
authors = ["lovesh harchandani <[email protected]>"]
55
description = "Wapper over Milagro Cryptographic Library (version 3)"
66
repository = "https://github.com/lovesh/amcl_rust_wrapper"

src/univar_poly.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ops::{Index, IndexMut};
1+
use std::ops::{Add, Index, IndexMut, Mul, Sub};
22

33
use crate::field_elem::{FieldElement, FieldElementVector};
44
use crate::rayon::iter::IntoParallelRefIterator;
@@ -216,6 +216,29 @@ impl IndexMut<usize> for UnivarPolynomial {
216216

217217
impl Eq for UnivarPolynomial {}
218218

219+
impl<'a> Add<&'a UnivarPolynomial> for &UnivarPolynomial {
220+
type Output = UnivarPolynomial;
221+
fn add(self, other: &'a UnivarPolynomial) -> UnivarPolynomial {
222+
UnivarPolynomial::sum(self, other)
223+
}
224+
}
225+
226+
impl<'a> Sub<&'a UnivarPolynomial> for &UnivarPolynomial {
227+
type Output = UnivarPolynomial;
228+
229+
fn sub(self, other: &'a UnivarPolynomial) -> UnivarPolynomial {
230+
UnivarPolynomial::difference(self, other)
231+
}
232+
}
233+
234+
impl<'a> Mul<&'a UnivarPolynomial> for &UnivarPolynomial {
235+
type Output = UnivarPolynomial;
236+
237+
fn mul(self, other: &'a UnivarPolynomial) -> UnivarPolynomial {
238+
UnivarPolynomial::multiply(self, other)
239+
}
240+
}
241+
219242
/// Creates a new univariate polynomial from given coefficients from lower to higher degree terms
220243
#[macro_export]
221244
macro_rules! univar_polynomial {
@@ -412,6 +435,9 @@ mod tests {
412435
assert_eq!(product[1], FieldElement::zero());
413436
assert_eq!(product[2], FieldElement::one());
414437

438+
// Test overloaded operator
439+
assert_eq!(product, &left * &right);
440+
415441
// (x + 1) * (2x + 1) = 2x^2 + 3x + 1
416442
// 1 + x
417443
let left = UnivarPolynomial(FieldElementVector::from(vec![
@@ -430,6 +456,9 @@ mod tests {
430456
assert_eq!(product[1], FieldElement::from(3u64));
431457
assert_eq!(product[2], FieldElement::from(2u64));
432458

459+
// Test overloaded operator
460+
assert_eq!(product, &left * &right);
461+
433462
// (x^2 + 1) * (x^3 + 4) = x^5 + x^3 + 4x^2 + 4
434463
// 1 + x^2
435464
let left = UnivarPolynomial(FieldElementVector::from(vec![
@@ -453,6 +482,9 @@ mod tests {
453482
assert_eq!(product[3], FieldElement::one());
454483
assert_eq!(product[4], FieldElement::zero());
455484
assert_eq!(product[5], FieldElement::one());
485+
486+
// Test overloaded operator
487+
assert_eq!(product, &left * &right);
456488
}
457489

458490
#[test]
@@ -524,8 +556,15 @@ mod tests {
524556
// sum is commutative
525557
assert_eq!(sum, UnivarPolynomial::sum(&right, &left));
526558

559+
// Test overloaded operator
560+
assert_eq!(sum, &left + &right);
561+
527562
// sum - left == right
528563
let mut diff_1 = UnivarPolynomial::difference(&sum, &right);
564+
565+
// Test overloaded operator
566+
assert_eq!(diff_1, &sum - &right);
567+
529568
// Since degree of difference is same as degree of `sum` but the higher degree coeffs
530569
// of difference will be 0. Remove those 0s (after checking that they really are 0) and
531570
// then do equality comparison with `left`
@@ -537,6 +576,10 @@ mod tests {
537576

538577
// sum - right == left
539578
let mut diff_2 = UnivarPolynomial::difference(&sum, &left);
579+
580+
// Test overloaded operator
581+
assert_eq!(diff_2, &sum - &left);
582+
540583
// Since degree of difference is same as degree of `sum` but the higher degree coeffs
541584
// of difference will be 0. Remove those 0s (after checking that they really are 0) and
542585
// then do equality comparison with `right`
@@ -570,6 +613,9 @@ mod tests {
570613
// product / right == left
571614
let quotient_2 = UnivarPolynomial::long_division(&product, &right).0;
572615
assert_eq!(quotient_2, left);
616+
617+
// Test overloaded operator
618+
assert_eq!(product, &left * &right);
573619
}
574620
}
575621

0 commit comments

Comments
 (0)