Rename bnwrap to libsnarkwrap.

This commit is contained in:
Sean Bowe 2016-08-02 10:35:28 -06:00
parent e25790b4ca
commit 27ad4bb114
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
8 changed files with 192 additions and 192 deletions

View File

@ -1,136 +0,0 @@
#include <sodium.h>
#include <iostream>
#include <stdexcept>
#include "algebra/curves/alt_bn128/alt_bn128_g1.hpp"
#include <assert.h>
#include "algebra/curves/alt_bn128/alt_bn128_g2.hpp"
#include "algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "algebra/curves/alt_bn128/alt_bn128_pairing.hpp"
#include "algebra/curves/alt_bn128/alt_bn128_pp.hpp"
#include "algebra/curves/public_params.hpp"
#include "relations/arithmetic_programs/qap/qap.hpp"
#include "reductions/r1cs_to_qap/r1cs_to_qap.hpp"
using namespace std;
using namespace libsnark;
typedef Fr<alt_bn128_pp> FieldT;
extern "C" void bnwrap_init() {
libsnark::inhibit_profiling_info = true;
libsnark::inhibit_profiling_counters = true;
assert(sodium_init() != -1);
init_alt_bn128_params();
}
// Fr
extern "C" FieldT bnwrap_Fr_random() {
return FieldT::random_element();
}
extern "C" FieldT bnwrap_Fr_from(const char *a) {
return FieldT(a);
}
extern "C" FieldT bnwrap_Fr_add(const char *a, const char *b) {
return *a + *b;
}
extern "C" FieldT bnwrap_Fr_sub(const char *a, const char *b) {
return *a - *b;
}
extern "C" FieldT bnwrap_Fr_mul(const char *a, const char *b) {
return *a * *b;
}
extern "C" FieldT bnwrap_Fr_neg(const char *a) {
return -(*a);
}
// G1
extern "C" alt_bn128_G1 bnwrap_G1_zero() {
return alt_bn128_G1::zero();
}
extern "C" alt_bn128_G1 bnwrap_G1_one() {
return alt_bn128_G1::one();
}
extern "C" alt_bn128_G1 bnwrap_G1_random() {
return alt_bn128_G1::random_element();
}
extern "C" bool bnwrap_G1_is_zero(alt_bn128_G1 *p) {
return p->is_zero();
}
extern "C" bool bnwrap_G1_is_equal(alt_bn128_G1 *p, alt_bn128_G1 *q) {
return *p == *q;
}
extern "C" alt_bn128_G1 bnwrap_G1_add(alt_bn128_G1 *p, alt_bn128_G1 *q) {
return *p + *q;
}
extern "C" alt_bn128_G1 bnwrap_G1_sub(alt_bn128_G1 *p, alt_bn128_G1 *q) {
return *p - *q;
}
extern "C" alt_bn128_G1 bnwrap_G1_neg(alt_bn128_G1 *p) {
return -(*p);
}
extern "C" alt_bn128_G1 bnwrap_G1_scalarmul(alt_bn128_G1 *p, FieldT *q) {
return (*q) * (*p);
}
// G2
extern "C" alt_bn128_G2 bnwrap_G2_zero() {
return alt_bn128_G2::zero();
}
extern "C" alt_bn128_G2 bnwrap_G2_one() {
return alt_bn128_G2::one();
}
extern "C" alt_bn128_G2 bnwrap_G2_random() {
return alt_bn128_G2::random_element();
}
extern "C" bool bnwrap_G2_is_zero(alt_bn128_G2 *p) {
return p->is_zero();
}
extern "C" bool bnwrap_G2_is_equal(alt_bn128_G2 *p, alt_bn128_G2 *q) {
return *p == *q;
}
extern "C" alt_bn128_G2 bnwrap_G2_add(alt_bn128_G2 *p, alt_bn128_G2 *q) {
return *p + *q;
}
extern "C" alt_bn128_G2 bnwrap_G2_sub(alt_bn128_G2 *p, alt_bn128_G2 *q) {
return *p - *q;
}
extern "C" alt_bn128_G2 bnwrap_G2_neg(alt_bn128_G2 *p) {
return -(*p);
}
extern "C" alt_bn128_G2 bnwrap_G2_scalarmul(alt_bn128_G2 *p, FieldT *q) {
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);
}

View File

@ -24,8 +24,8 @@ fn main() {
.file("libsnark/src/algebra/curves/alt_bn128/alt_bn128_pp.cpp") .file("libsnark/src/algebra/curves/alt_bn128/alt_bn128_pp.cpp")
.file("libsnark/src/common/utils.cpp") .file("libsnark/src/common/utils.cpp")
.file("libsnark/src/common/profiling.cpp") .file("libsnark/src/common/profiling.cpp")
.file("src/bnwrap.cpp") .file("src/libsnarkwrap.cpp")
; ;
cfg.compile("libbnwrap.a"); cfg.compile("libsnarkwrap.a");
} }

View File

@ -8,17 +8,17 @@ use std::ffi::CString;
pub struct Fr([u64; 4]); pub struct Fr([u64; 4]);
extern "C" { extern "C" {
fn bnwrap_Fr_random() -> Fr; fn libsnarkwrap_Fr_random() -> Fr;
fn bnwrap_Fr_from(s: *const c_char) -> Fr; fn libsnarkwrap_Fr_from(s: *const c_char) -> Fr;
fn bnwrap_Fr_add(a: *const Fr, b: *const Fr) -> Fr; fn libsnarkwrap_Fr_add(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_Fr_mul(a: *const Fr, b: *const Fr) -> Fr; fn libsnarkwrap_Fr_mul(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_Fr_sub(a: *const Fr, b: *const Fr) -> Fr; fn libsnarkwrap_Fr_sub(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_Fr_neg(a: *const Fr) -> Fr; fn libsnarkwrap_Fr_neg(a: *const Fr) -> Fr;
} }
impl Fr { impl Fr {
pub fn random() -> Self { pub fn random() -> Self {
unsafe { bnwrap_Fr_random() } unsafe { libsnarkwrap_Fr_random() }
} }
pub fn from_str(s: &str) -> Self { pub fn from_str(s: &str) -> Self {
@ -39,7 +39,7 @@ impl Fr {
let s = CString::new(s).unwrap(); let s = CString::new(s).unwrap();
unsafe { bnwrap_Fr_from(s.as_ptr()) } unsafe { libsnarkwrap_Fr_from(s.as_ptr()) }
} }
} }
@ -47,7 +47,7 @@ impl Add for Fr {
type Output = Fr; type Output = Fr;
fn add(self, other: Fr) -> Fr { fn add(self, other: Fr) -> Fr {
unsafe { bnwrap_Fr_add(&self, &other) } unsafe { libsnarkwrap_Fr_add(&self, &other) }
} }
} }
@ -55,7 +55,7 @@ impl Mul for Fr {
type Output = Fr; type Output = Fr;
fn mul(self, other: Fr) -> Fr { fn mul(self, other: Fr) -> Fr {
unsafe { bnwrap_Fr_mul(&self, &other) } unsafe { libsnarkwrap_Fr_mul(&self, &other) }
} }
} }
@ -63,7 +63,7 @@ impl Sub for Fr {
type Output = Fr; type Output = Fr;
fn sub(self, other: Fr) -> Fr { fn sub(self, other: Fr) -> Fr {
unsafe { bnwrap_Fr_sub(&self, &other) } unsafe { libsnarkwrap_Fr_sub(&self, &other) }
} }
} }
@ -71,6 +71,6 @@ impl Neg for Fr {
type Output = Fr; type Output = Fr;
fn neg(self) -> Fr { fn neg(self) -> Fr {
unsafe { bnwrap_Fr_neg(&self) } unsafe { libsnarkwrap_Fr_neg(&self) }
} }
} }

View File

@ -10,40 +10,40 @@ pub struct G1 {
} }
extern "C" { extern "C" {
fn bnwrap_G1_zero() -> G1; fn libsnarkwrap_G1_zero() -> G1;
fn bnwrap_G1_one() -> G1; fn libsnarkwrap_G1_one() -> G1;
fn bnwrap_G1_random() -> G1; fn libsnarkwrap_G1_random() -> G1;
fn bnwrap_G1_is_zero(p: *const G1) -> bool; fn libsnarkwrap_G1_is_zero(p: *const G1) -> bool;
fn bnwrap_G1_is_equal(p: *const G1, q: *const G1) -> bool; fn libsnarkwrap_G1_is_equal(p: *const G1, q: *const G1) -> bool;
fn bnwrap_G1_add(p: *const G1, q: *const G1) -> G1; fn libsnarkwrap_G1_add(p: *const G1, q: *const G1) -> G1;
fn bnwrap_G1_sub(p: *const G1, q: *const G1) -> G1; fn libsnarkwrap_G1_sub(p: *const G1, q: *const G1) -> G1;
fn bnwrap_G1_neg(p: *const G1) -> G1; fn libsnarkwrap_G1_neg(p: *const G1) -> G1;
fn bnwrap_G1_scalarmul(p: *const G1, s: *const Fr) -> G1; fn libsnarkwrap_G1_scalarmul(p: *const G1, s: *const Fr) -> G1;
} }
impl PartialEq for G1 { impl PartialEq for G1 {
fn eq(&self, other: &G1) -> bool { fn eq(&self, other: &G1) -> bool {
unsafe { bnwrap_G1_is_equal(self, other) } unsafe { libsnarkwrap_G1_is_equal(self, other) }
} }
} }
impl Group for G1 { impl Group for G1 {
fn zero() -> G1 { fn zero() -> G1 {
unsafe { bnwrap_G1_zero() } unsafe { libsnarkwrap_G1_zero() }
} }
fn one() -> G1 { fn one() -> G1 {
unsafe { bnwrap_G1_one() } unsafe { libsnarkwrap_G1_one() }
} }
fn random() -> G1 { fn random() -> G1 {
unsafe { bnwrap_G1_random() } unsafe { libsnarkwrap_G1_random() }
} }
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
unsafe { bnwrap_G1_is_zero(self) } unsafe { libsnarkwrap_G1_is_zero(self) }
} }
} }
@ -51,7 +51,7 @@ impl Add for G1 {
type Output = G1; type Output = G1;
fn add(self, other: G1) -> G1 { fn add(self, other: G1) -> G1 {
unsafe { bnwrap_G1_add(&self, &other) } unsafe { libsnarkwrap_G1_add(&self, &other) }
} }
} }
@ -59,7 +59,7 @@ impl Mul<Fr> for G1 {
type Output = G1; type Output = G1;
fn mul(self, other: Fr) -> G1 { fn mul(self, other: Fr) -> G1 {
unsafe { bnwrap_G1_scalarmul(&self, &other) } unsafe { libsnarkwrap_G1_scalarmul(&self, &other) }
} }
} }
@ -67,7 +67,7 @@ impl Sub for G1 {
type Output = G1; type Output = G1;
fn sub(self, other: G1) -> G1 { fn sub(self, other: G1) -> G1 {
unsafe { bnwrap_G1_sub(&self, &other) } unsafe { libsnarkwrap_G1_sub(&self, &other) }
} }
} }
@ -75,6 +75,6 @@ impl Neg for G1 {
type Output = G1; type Output = G1;
fn neg(self) -> G1 { fn neg(self) -> G1 {
unsafe { bnwrap_G1_neg(&self) } unsafe { libsnarkwrap_G1_neg(&self) }
} }
} }

View File

@ -10,40 +10,40 @@ pub struct G2 {
} }
extern "C" { extern "C" {
fn bnwrap_G2_zero() -> G2; fn libsnarkwrap_G2_zero() -> G2;
fn bnwrap_G2_one() -> G2; fn libsnarkwrap_G2_one() -> G2;
fn bnwrap_G2_random() -> G2; fn libsnarkwrap_G2_random() -> G2;
fn bnwrap_G2_is_zero(p: *const G2) -> bool; fn libsnarkwrap_G2_is_zero(p: *const G2) -> bool;
fn bnwrap_G2_is_equal(p: *const G2, q: *const G2) -> bool; fn libsnarkwrap_G2_is_equal(p: *const G2, q: *const G2) -> bool;
fn bnwrap_G2_add(p: *const G2, q: *const G2) -> G2; fn libsnarkwrap_G2_add(p: *const G2, q: *const G2) -> G2;
fn bnwrap_G2_sub(p: *const G2, q: *const G2) -> G2; fn libsnarkwrap_G2_sub(p: *const G2, q: *const G2) -> G2;
fn bnwrap_G2_neg(p: *const G2) -> G2; fn libsnarkwrap_G2_neg(p: *const G2) -> G2;
fn bnwrap_G2_scalarmul(p: *const G2, s: *const Fr) -> G2; fn libsnarkwrap_G2_scalarmul(p: *const G2, s: *const Fr) -> G2;
} }
impl PartialEq for G2 { impl PartialEq for G2 {
fn eq(&self, other: &G2) -> bool { fn eq(&self, other: &G2) -> bool {
unsafe { bnwrap_G2_is_equal(self, other) } unsafe { libsnarkwrap_G2_is_equal(self, other) }
} }
} }
impl Group for G2 { impl Group for G2 {
fn zero() -> G2 { fn zero() -> G2 {
unsafe { bnwrap_G2_zero() } unsafe { libsnarkwrap_G2_zero() }
} }
fn one() -> G2 { fn one() -> G2 {
unsafe { bnwrap_G2_one() } unsafe { libsnarkwrap_G2_one() }
} }
fn random() -> G2 { fn random() -> G2 {
unsafe { bnwrap_G2_random() } unsafe { libsnarkwrap_G2_random() }
} }
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
unsafe { bnwrap_G2_is_zero(self) } unsafe { libsnarkwrap_G2_is_zero(self) }
} }
} }
@ -51,7 +51,7 @@ impl Add for G2 {
type Output = G2; type Output = G2;
fn add(self, other: G2) -> G2 { fn add(self, other: G2) -> G2 {
unsafe { bnwrap_G2_add(&self, &other) } unsafe { libsnarkwrap_G2_add(&self, &other) }
} }
} }
@ -59,7 +59,7 @@ impl Mul<Fr> for G2 {
type Output = G2; type Output = G2;
fn mul(self, other: Fr) -> G2 { fn mul(self, other: Fr) -> G2 {
unsafe { bnwrap_G2_scalarmul(&self, &other) } unsafe { libsnarkwrap_G2_scalarmul(&self, &other) }
} }
} }
@ -67,7 +67,7 @@ impl Sub for G2 {
type Output = G2; type Output = G2;
fn sub(self, other: G2) -> G2 { fn sub(self, other: G2) -> G2 {
unsafe { bnwrap_G2_sub(&self, &other) } unsafe { libsnarkwrap_G2_sub(&self, &other) }
} }
} }
@ -75,6 +75,6 @@ impl Neg for G2 {
type Output = G2; type Output = G2;
fn neg(self) -> G2 { fn neg(self) -> G2 {
unsafe { bnwrap_G2_neg(&self) } unsafe { libsnarkwrap_G2_neg(&self) }
} }
} }

View File

@ -9,13 +9,13 @@ pub struct Gt {
} }
extern "C" { extern "C" {
fn bnwrap_gt_exp(p: *const Gt, s: *const Fr) -> Gt; fn libsnarkwrap_gt_exp(p: *const Gt, s: *const Fr) -> Gt;
} }
impl Mul<Fr> for Gt { impl Mul<Fr> for Gt {
type Output = Gt; type Output = Gt;
fn mul(self, other: Fr) -> Gt { fn mul(self, other: Fr) -> Gt {
unsafe { bnwrap_gt_exp(&self, &other) } unsafe { libsnarkwrap_gt_exp(&self, &other) }
} }
} }

View File

@ -16,8 +16,8 @@ pub use self::g1::G1;
pub use self::g2::G2; pub use self::g2::G2;
extern "C" { extern "C" {
fn bnwrap_init(); fn libsnarkwrap_init();
fn bnwrap_pairing(p: *const G1, q: *const G2) -> Gt; fn libsnarkwrap_pairing(p: *const G1, q: *const G2) -> Gt;
} }
lazy_static! { lazy_static! {
@ -29,13 +29,13 @@ pub fn initialize() {
let mut l = INIT_LOCK.lock().unwrap(); let mut l = INIT_LOCK.lock().unwrap();
if !*l { if !*l {
unsafe { bnwrap_init(); } unsafe { libsnarkwrap_init(); }
*l = true; *l = true;
} }
} }
pub fn pairing(p: &G1, q: &G2) -> Gt { pub fn pairing(p: &G1, q: &G2) -> Gt {
unsafe { bnwrap_pairing(p, q) } unsafe { libsnarkwrap_pairing(p, q) }
} }
pub trait Group: Sized + pub trait Group: Sized +

136
snark/src/libsnarkwrap.cpp Normal file
View File

@ -0,0 +1,136 @@
#include <sodium.h>
#include <iostream>
#include <stdexcept>
#include "algebra/curves/alt_bn128/alt_bn128_g1.hpp"
#include <assert.h>
#include "algebra/curves/alt_bn128/alt_bn128_g2.hpp"
#include "algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "algebra/curves/alt_bn128/alt_bn128_pairing.hpp"
#include "algebra/curves/alt_bn128/alt_bn128_pp.hpp"
#include "algebra/curves/public_params.hpp"
#include "relations/arithmetic_programs/qap/qap.hpp"
#include "reductions/r1cs_to_qap/r1cs_to_qap.hpp"
using namespace std;
using namespace libsnark;
typedef Fr<alt_bn128_pp> FieldT;
extern "C" void libsnarkwrap_init() {
libsnark::inhibit_profiling_info = true;
libsnark::inhibit_profiling_counters = true;
assert(sodium_init() != -1);
init_alt_bn128_params();
}
// Fr
extern "C" FieldT libsnarkwrap_Fr_random() {
return FieldT::random_element();
}
extern "C" FieldT libsnarkwrap_Fr_from(const char *a) {
return FieldT(a);
}
extern "C" FieldT libsnarkwrap_Fr_add(const char *a, const char *b) {
return *a + *b;
}
extern "C" FieldT libsnarkwrap_Fr_sub(const char *a, const char *b) {
return *a - *b;
}
extern "C" FieldT libsnarkwrap_Fr_mul(const char *a, const char *b) {
return *a * *b;
}
extern "C" FieldT libsnarkwrap_Fr_neg(const char *a) {
return -(*a);
}
// G1
extern "C" alt_bn128_G1 libsnarkwrap_G1_zero() {
return alt_bn128_G1::zero();
}
extern "C" alt_bn128_G1 libsnarkwrap_G1_one() {
return alt_bn128_G1::one();
}
extern "C" alt_bn128_G1 libsnarkwrap_G1_random() {
return alt_bn128_G1::random_element();
}
extern "C" bool libsnarkwrap_G1_is_zero(alt_bn128_G1 *p) {
return p->is_zero();
}
extern "C" bool libsnarkwrap_G1_is_equal(alt_bn128_G1 *p, alt_bn128_G1 *q) {
return *p == *q;
}
extern "C" alt_bn128_G1 libsnarkwrap_G1_add(alt_bn128_G1 *p, alt_bn128_G1 *q) {
return *p + *q;
}
extern "C" alt_bn128_G1 libsnarkwrap_G1_sub(alt_bn128_G1 *p, alt_bn128_G1 *q) {
return *p - *q;
}
extern "C" alt_bn128_G1 libsnarkwrap_G1_neg(alt_bn128_G1 *p) {
return -(*p);
}
extern "C" alt_bn128_G1 libsnarkwrap_G1_scalarmul(alt_bn128_G1 *p, FieldT *q) {
return (*q) * (*p);
}
// G2
extern "C" alt_bn128_G2 libsnarkwrap_G2_zero() {
return alt_bn128_G2::zero();
}
extern "C" alt_bn128_G2 libsnarkwrap_G2_one() {
return alt_bn128_G2::one();
}
extern "C" alt_bn128_G2 libsnarkwrap_G2_random() {
return alt_bn128_G2::random_element();
}
extern "C" bool libsnarkwrap_G2_is_zero(alt_bn128_G2 *p) {
return p->is_zero();
}
extern "C" bool libsnarkwrap_G2_is_equal(alt_bn128_G2 *p, alt_bn128_G2 *q) {
return *p == *q;
}
extern "C" alt_bn128_G2 libsnarkwrap_G2_add(alt_bn128_G2 *p, alt_bn128_G2 *q) {
return *p + *q;
}
extern "C" alt_bn128_G2 libsnarkwrap_G2_sub(alt_bn128_G2 *p, alt_bn128_G2 *q) {
return *p - *q;
}
extern "C" alt_bn128_G2 libsnarkwrap_G2_neg(alt_bn128_G2 *p) {
return -(*p);
}
extern "C" alt_bn128_G2 libsnarkwrap_G2_scalarmul(alt_bn128_G2 *p, FieldT *q) {
return (*q) * (*p);
}
// Pairing
extern "C" alt_bn128_GT libsnarkwrap_gt_exp(alt_bn128_GT *p, FieldT *s) {
return (*p) ^ (*s);
}
extern "C" alt_bn128_GT libsnarkwrap_pairing(alt_bn128_G1 *p, alt_bn128_G2 *q) {
return alt_bn128_reduced_pairing(*p, *q);
}