mirror of https://github.com/zcash/mpc.git
Pairings (closes #3)
This commit is contained in:
parent
731860e477
commit
b47d8193ad
|
@ -17,12 +17,18 @@ using namespace libsnark;
|
||||||
typedef Fr<alt_bn128_pp> FieldT;
|
typedef Fr<alt_bn128_pp> FieldT;
|
||||||
|
|
||||||
extern "C" void bnwrap_init() {
|
extern "C" void bnwrap_init() {
|
||||||
|
libsnark::inhibit_profiling_info = true;
|
||||||
|
libsnark::inhibit_profiling_counters = true;
|
||||||
assert(sodium_init() != -1);
|
assert(sodium_init() != -1);
|
||||||
init_alt_bn128_params();
|
init_alt_bn128_params();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fr
|
// Fr
|
||||||
|
|
||||||
|
extern "C" FieldT bnwrap_Fr_random() {
|
||||||
|
return FieldT::random_element();
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" FieldT bnwrap_Fr_from(const char *a) {
|
extern "C" FieldT bnwrap_Fr_from(const char *a) {
|
||||||
return FieldT(a);
|
return FieldT(a);
|
||||||
}
|
}
|
||||||
|
@ -118,3 +124,13 @@ extern "C" alt_bn128_G2 bnwrap_G2_neg(alt_bn128_G2 *p) {
|
||||||
extern "C" alt_bn128_G2 bnwrap_G2_scalarmul(alt_bn128_G2 *p, FieldT *q) {
|
extern "C" alt_bn128_G2 bnwrap_G2_scalarmul(alt_bn128_G2 *p, FieldT *q) {
|
||||||
return (*q) * (*p);
|
return (*q) * (*p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pairing
|
||||||
|
|
||||||
|
extern "C" alt_bn128_GT bnwrap_gt_exp(alt_bn128_GT *p, FieldT *s) {
|
||||||
|
return (*p) ^ (*s);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" alt_bn128_GT bnwrap_pairing(alt_bn128_G1 *p, alt_bn128_G2 *q) {
|
||||||
|
return alt_bn128_reduced_pairing(*p, *q);
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ use std::ffi::CString;
|
||||||
pub struct Fr([u64; 4]);
|
pub struct Fr([u64; 4]);
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
fn bnwrap_Fr_random() -> Fr;
|
||||||
fn bnwrap_Fr_from(s: *const c_char) -> Fr;
|
fn bnwrap_Fr_from(s: *const c_char) -> Fr;
|
||||||
fn bnwrap_Fr_add(a: *const Fr, b: *const Fr) -> Fr;
|
fn bnwrap_Fr_add(a: *const Fr, b: *const Fr) -> Fr;
|
||||||
fn bnwrap_Fr_mul(a: *const Fr, b: *const Fr) -> Fr;
|
fn bnwrap_Fr_mul(a: *const Fr, b: *const Fr) -> Fr;
|
||||||
|
@ -16,6 +17,10 @@ extern "C" {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Fr {
|
impl Fr {
|
||||||
|
pub fn random() -> Self {
|
||||||
|
unsafe { bnwrap_Fr_random() }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_str(s: &str) -> Self {
|
pub fn from_str(s: &str) -> Self {
|
||||||
for c in s.chars() {
|
for c in s.chars() {
|
||||||
if c != '0' &&
|
if c != '0' &&
|
||||||
|
|
|
@ -1,6 +1,21 @@
|
||||||
|
use std::ops::Mul;
|
||||||
|
use super::Fr;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct Gt {
|
pub struct Gt {
|
||||||
a: [u64; 4 * 6],
|
a: [u64; 4 * 6],
|
||||||
b: [u64; 4 * 6]
|
b: [u64; 4 * 6]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
fn bnwrap_gt_exp(p: *const Gt, s: *const Fr) -> Gt;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<Fr> for Gt {
|
||||||
|
type Output = Gt;
|
||||||
|
|
||||||
|
fn mul(self, other: Fr) -> Gt {
|
||||||
|
unsafe { bnwrap_gt_exp(&self, &other) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ pub use self::g2::G2;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn bnwrap_init();
|
fn bnwrap_init();
|
||||||
|
fn bnwrap_pairing(p: *const G1, q: *const G2) -> Gt;
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
@ -29,6 +30,10 @@ pub fn initialize() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn pairing(p: &G1, q: &G2) -> Gt {
|
||||||
|
unsafe { bnwrap_pairing(p, q) }
|
||||||
|
}
|
||||||
|
|
||||||
pub trait GroupElement: Sized +
|
pub trait GroupElement: Sized +
|
||||||
Copy +
|
Copy +
|
||||||
Clone +
|
Clone +
|
||||||
|
@ -43,6 +48,27 @@ pub trait GroupElement: Sized +
|
||||||
fn is_zero(&self) -> bool;
|
fn is_zero(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pairing_test() {
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
for _ in 0..50 {
|
||||||
|
let p = G1::random();
|
||||||
|
let q = G2::random();
|
||||||
|
let s = Fr::random();
|
||||||
|
|
||||||
|
let sp = p * s;
|
||||||
|
let sq = q * s;
|
||||||
|
|
||||||
|
let a = pairing(&p, &q) * s;
|
||||||
|
let b = pairing(&sp, &q);
|
||||||
|
let c = pairing(&p, &sq);
|
||||||
|
|
||||||
|
assert!(a == b);
|
||||||
|
assert!(b == c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod test_groups {
|
mod test_groups {
|
||||||
use super::{Fr, G1, G2, initialize, GroupElement};
|
use super::{Fr, G1, G2, initialize, GroupElement};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue