add random point functions

This commit is contained in:
exfinen
2023-09-21 20:33:32 +09:00
parent bed243c062
commit 3385554ad5
6 changed files with 64 additions and 19 deletions

1
Cargo.lock generated
View File

@@ -86,6 +86,7 @@ dependencies = [
"autocfg",
"num-integer",
"num-traits",
"rand",
]
[[package]]

View File

@@ -7,7 +7,7 @@ edition = "2021"
bitvec = "1"
hex = "0.4.3"
nom = "7.1.1"
num-bigint = "0.4.3"
num-bigint = { version = "0.4.3", features = ["rand"] }
num-traits = "0.2"
once_cell = "1.18.0"
rand = "0.8.5"

View File

@@ -13,6 +13,8 @@ To build a zk library from scratch keeping the implementation as easily understa
- Ed25519 public key generation, signing and signature verification on Curve25519 curve
- Bulletproofs (inner product argument range proof)
- Pinnocio (equation parser, R1CS, QAP)
- Miller's Algorithm
- Weil Pairing on BLS12-381
## What's NOT implemented so far
- Big number

View File

@@ -11,7 +11,11 @@ use crate::{
zero::Zero,
},
};
use num_bigint::BigUint;
use num_bigint::{
BigUint,
RandBigInt,
};
use num_traits::Zero as NumTraitsZero;
use std::{
fmt,
ops::{Add, Mul, Neg},
@@ -87,6 +91,12 @@ impl G1Point {
&ns[80..96],
)
}
pub fn get_random_point() -> AffinePoint {
let mut rng = rand::thread_rng();
let n = rng.gen_biguint_range(&NumTraitsZero::zero(), &CURVE_GROUP.order);
G1Point::g() * &CURVE_GROUP.elem(&n)
}
}
impl RationalPoint for G1Point {

View File

@@ -13,7 +13,11 @@ use crate::{
zero::Zero,
},
};
use num_bigint::BigUint;
use num_bigint::{
BigUint,
RandBigInt,
};
use num_traits::Zero as NumTraitsZero;
use std::{
ops::{Add, Mul, Neg},
sync::Arc,
@@ -62,6 +66,12 @@ impl G2Point {
G2Point::Rational { x, y } => G2Point::new(&x, &y.inv()),
}
}
pub fn get_random_point() -> AffinePoint {
let mut rng = rand::thread_rng();
let n = rng.gen_biguint_range(&NumTraitsZero::zero(), &CURVE_GROUP.order);
G2Point::g() * &CURVE_GROUP.elem(&n)
}
}
macro_rules! impl_neg {

View File

@@ -81,31 +81,53 @@ impl WeilPairing {
mod tests {
use super::*;
#[test]
fn do_it() {
let wp = WeilPairing::new();
let p = G1Point::g();
let p2 = &p + &p;
let q = G2Point::g();
fn test_pairing(wp: &WeilPairing, p: &G1Point, q: &G2Point) -> bool {
let p2 = p + p;
// test e(p + p2, q) = e(p, q) e(p2, q)
println!("Calculating e(p + p2, q)...");
let lhs = wp.calculate(&(&p + &p2), &q);
// println!("Calculating e(p + p2, q)...");
let lhs = wp.calculate(&(p + &p2), q);
println!("Calculating e(p, q)...");
let rhs1 = wp.calculate(&p, &q);
// println!("Calculating e(p, q)...");
let rhs1 = wp.calculate(p, q);
println!("Calculating e(p2, q)...");
let rhs2 = wp.calculate(&p2, &q);
// println!("Calculating e(p2, q)...");
let rhs2 = wp.calculate(&p2, q);
let rhs = rhs1 * rhs2;
println!("lhs = {:?}", &lhs);
println!("rhs = {:?}", &rhs);
// println!("lhs = {:?}", &lhs);
// println!("rhs = {:?}", &rhs);
assert!(lhs == rhs);
lhs == rhs
}
#[test]
fn test_pairing_with_generators() {
let wp = WeilPairing::new();
let p = G1Point::g();
let q = G2Point::g();
let res = test_pairing(&wp, &p, &q);
assert!(res);
}
//#[test]
fn test_pairing_with_random_points() {
let mut errors = 0;
for i in 0..1000 {
println!("iteration {}", i);
let wp = WeilPairing::new();
let p = G1Point::get_random_point();
let q = G2Point::get_random_point();
let res = test_pairing(&wp, &p, &q);
if res == false {
println!("----> iteration {} failed!", i);
errors += 1;
}
}
println!("{} tests failed!", errors);
}
}