-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a2a16a
commit 16514ff
Showing
1 changed file
with
39 additions
and
10 deletions.
There are no files selected for viewing
49 changes: 39 additions & 10 deletions
49
applied-crypto-references/aleo-cryptography/src/algebra.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,58 @@ | ||
///! This module explores Aleo's basic algebraic structures and their properties | ||
//Field and Ring elements | ||
use snarkvm::utilities::{ | ||
BigInteger384, BigInteger | ||
}; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
//Field and Ring elements | ||
use snarkvm::utilities::{ | ||
BigInteger384, BigInteger | ||
}; | ||
|
||
#[test] | ||
fn additions_overflow() { | ||
let mut a = BigInteger384::new([u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]); | ||
let mut b = BigInteger384::from(7); | ||
a.add_nocarry(&b); | ||
let b = BigInteger384::from(7); | ||
let add_overflowed = a.add_nocarry(&b); | ||
let d = BigInteger384::from(6); | ||
assert_eq!(a, d); | ||
assert!(add_overflowed); | ||
|
||
} | ||
|
||
#[test] | ||
fn subs_overflow() { | ||
let mut a = BigInteger384::from(0); | ||
let mut b = BigInteger384::from(1); | ||
a.sub_noborrow(&b); | ||
let b = BigInteger384::from(1); | ||
let sub_overflowed = a.sub_noborrow(&b); | ||
let d = BigInteger384::new([u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]); | ||
assert_eq!(a, d); | ||
assert!(sub_overflowed); | ||
} | ||
|
||
#[test] | ||
fn mul_test() { | ||
let mut a = BigInteger384::from(2000000); | ||
a.muln(1); | ||
let mut b = BigInteger384::from(2000000); | ||
b.muln(2); | ||
let mut c = BigInteger384::from(2000000); | ||
c.muln(4); | ||
assert_eq!(a, BigInteger384::from(4000000)); | ||
assert_eq!(b, BigInteger384::from(8000000)); | ||
assert_eq!(c, BigInteger384::from(32000000)); | ||
|
||
let mut c = BigInteger384::from(2000000); | ||
c.mul2(); | ||
assert_eq!(c, BigInteger384::from(4000000)); | ||
|
||
} | ||
|
||
#[test] | ||
fn mul_saturation() { | ||
// Create a number that is within 1 multiplication of overflowing | ||
let mut a = BigInteger384::new([u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX - 4]); | ||
let u384_max = BigInteger384::new([u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX, u64::MAX]); | ||
a.mul2(); | ||
println!("a: {}", a); | ||
println!("u384_max: {}", u384_max); | ||
} | ||
} |