mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-07-31 12:31:22 +00:00
Merge commit 'd029ddea8396d7a39910028dd5ae436a3bd3e9bb' as 'jubjub'
This commit is contained in:
29
jubjub/tests/common.rs
Normal file
29
jubjub/tests/common.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use jubjub::*;
|
||||
use rand_core::{RngCore, SeedableRng};
|
||||
use rand_xorshift::XorShiftRng;
|
||||
|
||||
pub const NUM_BLACK_BOX_CHECKS: u32 = 2000;
|
||||
|
||||
pub fn new_rng() -> XorShiftRng {
|
||||
XorShiftRng::from_seed([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
||||
}
|
||||
|
||||
pub trait MyRandom {
|
||||
fn new_random<T: RngCore>(rng: &mut T) -> Self;
|
||||
}
|
||||
|
||||
impl MyRandom for Fq {
|
||||
fn new_random<T: RngCore>(rng: &mut T) -> Self {
|
||||
let mut random_bytes = [0u8; 64];
|
||||
rng.fill_bytes(&mut random_bytes);
|
||||
Fq::from_bytes_wide(&random_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl MyRandom for Fr {
|
||||
fn new_random<T: RngCore>(rng: &mut T) -> Self {
|
||||
let mut random_bytes = [0u8; 64];
|
||||
rng.fill_bytes(&mut random_bytes);
|
||||
Fr::from_bytes_wide(&random_bytes)
|
||||
}
|
||||
}
|
120
jubjub/tests/fq_blackbox.rs
Normal file
120
jubjub/tests/fq_blackbox.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
mod common;
|
||||
|
||||
use common::{new_rng, MyRandom, NUM_BLACK_BOX_CHECKS};
|
||||
use jubjub::*;
|
||||
|
||||
#[test]
|
||||
fn test_to_and_from_bytes() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
assert_eq!(a, Fq::from_bytes(&Fq::to_bytes(&a)).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_additive_associativity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
let b = Fq::new_random(&mut rng);
|
||||
let c = Fq::new_random(&mut rng);
|
||||
assert_eq!((a + b) + c, a + (b + c))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_additive_identity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
assert_eq!(a, a + Fq::zero());
|
||||
assert_eq!(a, Fq::zero() + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_subtract_additive_identity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
assert_eq!(a, a - Fq::zero());
|
||||
assert_eq!(a, Fq::zero() - -&a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_additive_inverse() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
let a_neg = -&a;
|
||||
assert_eq!(Fq::zero(), a + a_neg);
|
||||
assert_eq!(Fq::zero(), a_neg + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_additive_commutativity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
let b = Fq::new_random(&mut rng);
|
||||
assert_eq!(a + b, b + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplicative_associativity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
let b = Fq::new_random(&mut rng);
|
||||
let c = Fq::new_random(&mut rng);
|
||||
assert_eq!((a * b) * c, a * (b * c))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplicative_identity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
assert_eq!(a, a * Fq::one());
|
||||
assert_eq!(a, Fq::one() * a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplicative_inverse() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
if a == Fq::zero() {
|
||||
continue;
|
||||
}
|
||||
let a_inv = a.invert().unwrap();
|
||||
assert_eq!(Fq::one(), a * a_inv);
|
||||
assert_eq!(Fq::one(), a_inv * a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplicative_commutativity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
let b = Fq::new_random(&mut rng);
|
||||
assert_eq!(a * b, b * a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiply_additive_identity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fq::new_random(&mut rng);
|
||||
assert_eq!(Fq::zero(), Fq::zero() * a);
|
||||
assert_eq!(Fq::zero(), a * Fq::zero());
|
||||
}
|
||||
}
|
120
jubjub/tests/fr_blackbox.rs
Normal file
120
jubjub/tests/fr_blackbox.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
mod common;
|
||||
|
||||
use common::{new_rng, MyRandom, NUM_BLACK_BOX_CHECKS};
|
||||
use jubjub::*;
|
||||
|
||||
#[test]
|
||||
fn test_to_and_from_bytes() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
assert_eq!(a, Fr::from_bytes(&Fr::to_bytes(&a)).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_additive_associativity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
let b = Fr::new_random(&mut rng);
|
||||
let c = Fr::new_random(&mut rng);
|
||||
assert_eq!((a + b) + c, a + (b + c))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_additive_identity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
assert_eq!(a, a + Fr::zero());
|
||||
assert_eq!(a, Fr::zero() + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_subtract_additive_identity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
assert_eq!(a, a - Fr::zero());
|
||||
assert_eq!(a, Fr::zero() - -&a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_additive_inverse() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
let a_neg = -&a;
|
||||
assert_eq!(Fr::zero(), a + a_neg);
|
||||
assert_eq!(Fr::zero(), a_neg + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_additive_commutativity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
let b = Fr::new_random(&mut rng);
|
||||
assert_eq!(a + b, b + a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplicative_associativity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
let b = Fr::new_random(&mut rng);
|
||||
let c = Fr::new_random(&mut rng);
|
||||
assert_eq!((a * b) * c, a * (b * c))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplicative_identity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
assert_eq!(a, a * Fr::one());
|
||||
assert_eq!(a, Fr::one() * a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplicative_inverse() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
if a == Fr::zero() {
|
||||
continue;
|
||||
}
|
||||
let a_inv = a.invert().unwrap();
|
||||
assert_eq!(Fr::one(), a * a_inv);
|
||||
assert_eq!(Fr::one(), a_inv * a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplicative_commutativity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
let b = Fr::new_random(&mut rng);
|
||||
assert_eq!(a * b, b * a);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiply_additive_identity() {
|
||||
let mut rng = new_rng();
|
||||
for _ in 0..NUM_BLACK_BOX_CHECKS {
|
||||
let a = Fr::new_random(&mut rng);
|
||||
assert_eq!(Fr::zero(), Fr::zero() * a);
|
||||
assert_eq!(Fr::zero(), a * Fr::zero());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user