mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-02-15 11:15:48 +00:00
Add pairing_with API.
This commit is contained in:
parent
f51a383ef1
commit
41834bc4b2
@ -7,7 +7,8 @@ macro_rules! curve_impl {
|
|||||||
$basefield:ident,
|
$basefield:ident,
|
||||||
$scalarfield:ident,
|
$scalarfield:ident,
|
||||||
$uncompressed:ident,
|
$uncompressed:ident,
|
||||||
$compressed:ident
|
$compressed:ident,
|
||||||
|
$pairing:ident
|
||||||
) => {
|
) => {
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||||
pub struct $affine {
|
pub struct $affine {
|
||||||
@ -113,6 +114,8 @@ macro_rules! curve_impl {
|
|||||||
type Projective = $projective;
|
type Projective = $projective;
|
||||||
type Uncompressed = $uncompressed;
|
type Uncompressed = $uncompressed;
|
||||||
type Compressed = $compressed;
|
type Compressed = $compressed;
|
||||||
|
type Pair = $pairing;
|
||||||
|
type PairingResult = Fq12;
|
||||||
|
|
||||||
fn zero() -> Self {
|
fn zero() -> Self {
|
||||||
$affine {
|
$affine {
|
||||||
@ -155,6 +158,10 @@ macro_rules! curve_impl {
|
|||||||
$prepared::from_affine(*self)
|
$prepared::from_affine(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pairing_with(&self, other: &Self::Pair) -> Self::PairingResult {
|
||||||
|
self.perform_pairing(other)
|
||||||
|
}
|
||||||
|
|
||||||
fn into_projective(&self) -> $projective {
|
fn into_projective(&self) -> $projective {
|
||||||
(*self).into()
|
(*self).into()
|
||||||
}
|
}
|
||||||
@ -574,10 +581,11 @@ macro_rules! curve_impl {
|
|||||||
|
|
||||||
pub mod g1 {
|
pub mod g1 {
|
||||||
use rand::{Rand, Rng};
|
use rand::{Rand, Rng};
|
||||||
use super::super::{Fq, Fr, FrRepr, FqRepr};
|
use super::g2::G2Affine;
|
||||||
use ::{CurveProjective, CurveAffine, PrimeField, SqrtField, PrimeFieldRepr, Field, BitIterator, EncodedPoint, GroupDecodingError};
|
use super::super::{Fq, Fr, FrRepr, FqRepr, Fq12};
|
||||||
|
use ::{CurveProjective, CurveAffine, PrimeField, SqrtField, PrimeFieldRepr, Field, BitIterator, EncodedPoint, GroupDecodingError, Engine};
|
||||||
|
|
||||||
curve_impl!("G1", G1, G1Affine, G1Prepared, Fq, Fr, G1Uncompressed, G1Compressed);
|
curve_impl!("G1", G1, G1Affine, G1Prepared, Fq, Fr, G1Uncompressed, G1Compressed, G2Affine);
|
||||||
|
|
||||||
#[derive(Copy)]
|
#[derive(Copy)]
|
||||||
pub struct G1Uncompressed([u8; 96]);
|
pub struct G1Uncompressed([u8; 96]);
|
||||||
@ -828,6 +836,10 @@ pub mod g1 {
|
|||||||
fn get_coeff_b() -> Fq {
|
fn get_coeff_b() -> Fq {
|
||||||
super::super::fq::B_COEFF
|
super::super::fq::B_COEFF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn perform_pairing(&self, other: &G2Affine) -> Fq12 {
|
||||||
|
super::super::Bls12::pairing(*self, *other)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl G1 {
|
impl G1 {
|
||||||
@ -1122,10 +1134,11 @@ pub mod g1 {
|
|||||||
|
|
||||||
pub mod g2 {
|
pub mod g2 {
|
||||||
use rand::{Rand, Rng};
|
use rand::{Rand, Rng};
|
||||||
use super::super::{Fq2, Fr, Fq, FrRepr, FqRepr};
|
use super::super::{Fq2, Fr, Fq, FrRepr, FqRepr, Fq12};
|
||||||
use ::{CurveProjective, CurveAffine, PrimeField, SqrtField, PrimeFieldRepr, Field, BitIterator, EncodedPoint, GroupDecodingError};
|
use super::g1::G1Affine;
|
||||||
|
use ::{CurveProjective, CurveAffine, PrimeField, SqrtField, PrimeFieldRepr, Field, BitIterator, EncodedPoint, GroupDecodingError, Engine};
|
||||||
|
|
||||||
curve_impl!("G2", G2, G2Affine, G2Prepared, Fq2, Fr, G2Uncompressed, G2Compressed);
|
curve_impl!("G2", G2, G2Affine, G2Prepared, Fq2, Fr, G2Uncompressed, G2Compressed, G1Affine);
|
||||||
|
|
||||||
#[derive(Copy)]
|
#[derive(Copy)]
|
||||||
pub struct G2Uncompressed([u8; 192]);
|
pub struct G2Uncompressed([u8; 192]);
|
||||||
@ -1403,6 +1416,10 @@ pub mod g2 {
|
|||||||
c1: super::super::fq::B_COEFF
|
c1: super::super::fq::B_COEFF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn perform_pairing(&self, other: &G1Affine) -> Fq12 {
|
||||||
|
super::super::Bls12::pairing(*other, *self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl G2 {
|
impl G2 {
|
||||||
|
@ -42,13 +42,13 @@ pub trait Engine {
|
|||||||
type G1: CurveProjective<Base=Self::Fq, Scalar=Self::Fr, Affine=Self::G1Affine> + From<Self::G1Affine>;
|
type G1: CurveProjective<Base=Self::Fq, Scalar=Self::Fr, Affine=Self::G1Affine> + From<Self::G1Affine>;
|
||||||
|
|
||||||
/// The affine representation of an element in G1.
|
/// The affine representation of an element in G1.
|
||||||
type G1Affine: CurveAffine<Base=Self::Fq, Scalar=Self::Fr, Projective=Self::G1> + From<Self::G1>;
|
type G1Affine: CurveAffine<Base=Self::Fq, Scalar=Self::Fr, Projective=Self::G1, Pair=Self::G2Affine, PairingResult=Self::Fqk> + From<Self::G1>;
|
||||||
|
|
||||||
/// The projective representation of an element in G2.
|
/// The projective representation of an element in G2.
|
||||||
type G2: CurveProjective<Base=Self::Fqe, Scalar=Self::Fr, Affine=Self::G2Affine> + From<Self::G2Affine>;
|
type G2: CurveProjective<Base=Self::Fqe, Scalar=Self::Fr, Affine=Self::G2Affine> + From<Self::G2Affine>;
|
||||||
|
|
||||||
/// The affine representation of an element in G2.
|
/// The affine representation of an element in G2.
|
||||||
type G2Affine: CurveAffine<Base=Self::Fqe, Scalar=Self::Fr, Projective=Self::G2> + From<Self::G2>;
|
type G2Affine: CurveAffine<Base=Self::Fqe, Scalar=Self::Fr, Projective=Self::G2, Pair=Self::G1Affine, PairingResult=Self::Fqk> + From<Self::G2>;
|
||||||
|
|
||||||
/// The base field that hosts G1.
|
/// The base field that hosts G1.
|
||||||
type Fq: PrimeField + SqrtField;
|
type Fq: PrimeField + SqrtField;
|
||||||
@ -172,6 +172,8 @@ pub trait CurveAffine: Copy +
|
|||||||
type Prepared: Clone + Send + Sync + 'static;
|
type Prepared: Clone + Send + Sync + 'static;
|
||||||
type Uncompressed: EncodedPoint<Affine=Self>;
|
type Uncompressed: EncodedPoint<Affine=Self>;
|
||||||
type Compressed: EncodedPoint<Affine=Self>;
|
type Compressed: EncodedPoint<Affine=Self>;
|
||||||
|
type Pair: CurveAffine<Pair=Self>;
|
||||||
|
type PairingResult: Field;
|
||||||
|
|
||||||
/// Returns the additive identity.
|
/// Returns the additive identity.
|
||||||
fn zero() -> Self;
|
fn zero() -> Self;
|
||||||
@ -192,6 +194,9 @@ pub trait CurveAffine: Copy +
|
|||||||
/// Prepares this element for pairing purposes.
|
/// Prepares this element for pairing purposes.
|
||||||
fn prepare(&self) -> Self::Prepared;
|
fn prepare(&self) -> Self::Prepared;
|
||||||
|
|
||||||
|
/// Perform a pairing
|
||||||
|
fn pairing_with(&self, other: &Self::Pair) -> Self::PairingResult;
|
||||||
|
|
||||||
/// Converts this element into its affine representation.
|
/// Converts this element into its affine representation.
|
||||||
fn into_projective(&self) -> Self::Projective;
|
fn into_projective(&self) -> Self::Projective;
|
||||||
|
|
||||||
|
@ -6,6 +6,14 @@ pub fn engine_tests<E: Engine>()
|
|||||||
{
|
{
|
||||||
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
|
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
|
||||||
|
|
||||||
|
for _ in 0..10 {
|
||||||
|
let a = E::G1::rand(&mut rng).into_affine();
|
||||||
|
let b = E::G2::rand(&mut rng).into_affine();
|
||||||
|
|
||||||
|
assert!(a.pairing_with(&b) == b.pairing_with(&a));
|
||||||
|
assert!(a.pairing_with(&b) == E::pairing(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
for _ in 0..1000 {
|
for _ in 0..1000 {
|
||||||
let z1 = E::G1Affine::zero().prepare();
|
let z1 = E::G1Affine::zero().prepare();
|
||||||
let z2 = E::G2Affine::zero().prepare();
|
let z2 = E::G2Affine::zero().prepare();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user