cryptix-field 0.1.0

A library for group, ring and field arithmetics based on cryptix-bigint
Documentation
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

mod bn254;
use bn254::{U256, BN254};

const TEST_COUNT: usize = 500;
const SEED: [u8; 32] = [
    1, 0, 52, 0, 0, 0, 0, 0, 
    1, 0, 10, 0, 22, 32, 0, 0, 
    2, 0, 55, 49, 0, 11, 0, 0, 
    3, 0, 0, 0, 0, 0, 2, 92,
];

#[cfg(feature = "rand")]
#[test]
fn test_mont_rdc() {
    use rand::SeedableRng;
    use cryptix_field::field::{primefield::FpElement, montgomery::MontgomeryOps};
    let mut rng = rand_chacha::ChaCha8Rng::from_seed(SEED);
    for _ in 0..TEST_COUNT {
        let a: FpElement<U256, BN254> = FpElement::rand(&mut rng).mont_form();
        let b = FpElement::rand(&mut rng).mont_form();

        let c = (a * b).mont_rdc().mont_rdc();
        let d = a.mont_mul(b).mont_rdc();

        assert_eq!(c, d)
    }
}

#[cfg(feature = "rand")]
#[test]
fn test_mont_inv() {
    use rand::SeedableRng;
    use cryptix_field::field::{primefield::FpElement, montgomery::MontgomeryOps, MulIdentity};
    let mut rng = rand_chacha::ChaCha8Rng::from_seed(SEED);
    for _ in 0..TEST_COUNT {
        let a: FpElement<U256, BN254> = FpElement::rand(&mut rng).mont_form();
        let ainv = a.mont_inv();
        let c = a.mont_mul(ainv);

        assert_eq!(c.mont_rdc(), FpElement::<U256, BN254>::ONE);
    }
}

#[cfg(feature = "rand")]
#[test]
fn test_mont_basic() {
    use rand::SeedableRng;
    use cryptix_field::field::{primefield::FpElement, montgomery::MontgomeryOps, MulIdentity};
    
    let mut rng = rand_chacha::ChaCha8Rng::from_seed(SEED);
    for _ in 0..TEST_COUNT {
        let a: FpElement<U256, BN254> = FpElement::rand(&mut rng).mont_form();
        let b = FpElement::rand(&mut rng).mont_form();

        let res1 = a.mont_mul(a).mont_mul(b);
        let res2 = a.mont_mul(b).mont_mul(a);
        assert_eq!(res1, res2);

        let two = FpElement::ONE + FpElement::ONE;
        let res1 = a.mont_mul(two);
        let res2 = (a + a).mont_rdc();

        assert_eq!(res1, res2);
    }
}