cryptix-bn254 0.1.0

A library for bn254 elliptic curve related algorithms
Documentation
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use cryptix_bigint::bigint;
use cryptix_ecc::CurvePoint;
use cryptix_bn254::fp2;
use cryptix_bn254::galoisfield::U256;
use cryptix_bn254::pairing::e1::BN254Fp;
use cryptix_bn254::pairing::e2::BN254Fp2;

#[test]
fn test_e2_oncurve() {
    assert!(BN254Fp2::GENERATOR.on_curve(), "Q not on curve.")
}

#[test]
fn test_e2_add() {
    let p = BN254Fp2::GENERATOR;
    assert!(p.on_curve(), "P not on curve");
    
    let p2 = p.double();
    let two = bigint!(U256, "02");
    let p2m = p.scalar_mul(two);
    assert!(p2m.on_curve());
    let p2a = p + p;
    let p2ma = p.mix_add(p);
    let (p2dl, _) = p.dbl_line(BN254Fp::GENERATOR);
    
    assert!(p2.on_curve(), "2P Jacobian not on curve");
    let q = p2.normalize();
    assert!(q.on_curve(), "2P Affine not on curve");
    assert_eq!(p2dl.normalize(), q.normalize());
    assert_eq!(p2a, p2);
    assert_eq!(p2ma, p2);
    assert_eq!(p2m, p2);

    let r = q + p;
    let (p3mal, _) = p.mix_add_line(q, BN254Fp::GENERATOR);
    assert!(p3mal.on_curve());
    assert!(r.on_curve(), "3P Jacobian not on curve");
    let r = r.normalize();
    assert!(r.on_curve(), "3P Affine not on curve");
    assert_eq!(r, p3mal.normalize());

    let s = r.mix_add(p);
    assert!(s.on_curve(), "4P Jacobian not on curve");
    let s = s.normalize();
    assert!(s.on_curve(), "4P Affine not on curve");
}

#[test]
fn test_e2_mix_add_line() {
    let t = BN254Fp2 {
        x: fp2!(
            "1b3e30e52cbb6e15f8e94cff831fd919bc1494806b1029c358957a89aac84ece", 
            "0b082b9c6ea3839607565cbd456af0087a42c3229a8f6805845938a47fc0ea52"
        ),
        y: fp2!(
            "0ed28c4805cabb45a52936fdabf67026e04311647e9a443be7776a2b38ce085f", 
            "09ad6fa10f50a3c344b8ac4434daf47ca204423895a85548121faa5c0489a7c9"
        ),
        z: fp2!(
            "1516dbc4ccb31f7999eff8af8be7cfd2bc35ad2745bb64ddd5921659194a8624", 
            "0fc4c4fa7d18d48de30c4d4e8508b244322d90ec90720cbaa45f520f3806dd2e"
        )
    };
    let q = BN254Fp2 {
        x: fp2!(
            "238aee80a5aeb84c465ed20a220b3345fee51e0eba4062526484e3ffc87a01b0", 
            "078e83af5690a50cbbbee634d7682ca8c502b0c980966161531684813402ad35"
        ),
        y: fp2!(
            "16f0824d035e18a1a6ad95b169c639c9da65b0d3886a2cec876c329ac8c1106b", 
            "1adca320ab6cc027326937968034b42edb24c987b19ad8e01990cb64018955a7"
        ),
        z: fp2!(
            "0c2b7de587161f0b83cf04b4c0be66a967e9c00d0e8380b915bbe4223c9290f0", 
            "216984f88a0831e8570d81f5517a4d2685a83567a5ca31e6fb8d670668adea25"
        )
    };
    assert!(t.on_curve());
    assert!(q.on_curve());

    let (tmalq, _) = t.normalize().mix_add_line(q, BN254Fp::GENERATOR);
    assert!(tmalq.on_curve());
    assert_eq!((t + q).normalize(), tmalq.normalize());
    println!("{:?}", t.mix_add(t.normalize()).normalize());
    println!("{:?}", t.double().normalize());
    // println!("{:?}", (t + t).normalize());
    // println!("{:?}", t.dbl_line(E1Point::GENERATOR).0.normalize());
    // assert!(t.mix_add(t).on_curve());
}

#[test]
fn test_e2_mul() {
    let k = bigint!(U256, "2123432234324423423434243432");

    let p = BN254Fp2::GENERATOR.scalar_mul(k);
    assert!(p.on_curve());
}