#![allow(incomplete_features)] #![feature(generic_const_exprs)] #![feature(const_trait_impl)] use rand::SeedableRng; use cryptix_bigint::property::IsBigInt; use cryptix_blmq::{BLMQ, fr::FrElement}; use cryptix_ecc::CurvePoint; use cryptix_field::field::montgomery::MontgomeryOps; use cryptix_bn254::pairing::{e2::BN254Fp2, o_ate::OptimalAte, Pairing, e1::BN254Fp}; 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, ]; const ID: &[u8] = b"M4tsuri"; const MSG: &[u8] = b"This message is signed by M4tsuri"; #[test] fn test_blmq() { let rng = rand_chacha::ChaCha12Rng::from_seed(SEED); let mut blmq = BLMQ::new(rng); let (_pk, sk) = blmq.extract(ID); println!("Private Key: {sk:#?}"); let sig = blmq.sign(sk, MSG); println!("Signature: {sig:#?}"); assert!(blmq.verify(&sig, ID, MSG)); println!("正确签名,正确ID,正确消息,验证通过"); assert!(!blmq.verify(&sig, b"M5tsuri", MSG)); println!("正确签名,错误ID,正确消息,验证失败"); assert!(!blmq.verify(&sig, ID, b"This message is not signed")); println!("正确签名,正确ID,错误消息,验证失败"); let mut wrong_sig = sig; wrong_sig.s.x.0.0 = wrong_sig.s.x.0.0.set_bit( 51, !wrong_sig.s.x.0.0.bit(51) ); // bit flip assert!(!blmq.verify(&wrong_sig, ID, MSG)); println!("错误签名,正确ID,正确消息,验证失败"); println!("\n\n\n\n\n\n\n\n\n\n\n"); } #[test] fn test_pairing_bilinear() { let mut rng = rand_chacha::ChaCha8Rng::from_seed(SEED); for _ in 0..5 { // e(Q_1, Q_2)^r e(Q_1, Q_2)^h = e(S, MQ_2 + R) // S = (r + h)(M + s)^-1Q_1 // R = sQ_2 // // l: e((r + h)Q_1, Q_2) // r: e((r + h)(M + s)^-1Q_1, (M + s)Q_2) // = e((r + h)Q_1, Q_2) let r = FrElement::rand(&mut rng).repr(); let p_pub = BN254Fp2::GENERATOR.scalar_mul(r); assert!(p_pub.on_curve()); let r_r = FrElement::rand(&mut rng).repr(); let r_q2 = BN254Fp2::GENERATOR.scalar_mul(r_r); assert!(r_q2.on_curve()); let r = (p_pub + r_q2).normalize(); assert!(r.on_curve()); // r = p_pub + r_q2, g = e(Q_1, (r + r_r)Q_2) let g = OptimalAte::pairing(BN254Fp::GENERATOR, r); // w = e() let r_s = BN254Fp::GENERATOR.scalar_mul(r_r).normalize(); assert!(r_s.on_curve()); let h = OptimalAte::pairing(r_s, BN254Fp2::GENERATOR); let f = OptimalAte::pairing(BN254Fp::GENERATOR, p_pub); let w = h.mont_mul(f); assert_eq!(w, g) } }