diff --git a/snark/src/bnwrap.cpp b/snark/src/bnwrap.cpp deleted file mode 100644 index 30c8713..0000000 --- a/snark/src/bnwrap.cpp +++ /dev/null @@ -1,136 +0,0 @@ -#include -#include -#include -#include "algebra/curves/alt_bn128/alt_bn128_g1.hpp" -#include -#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 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); -} diff --git a/snark/src/build.rs b/snark/src/build.rs index 058af45..48ee771 100644 --- a/snark/src/build.rs +++ b/snark/src/build.rs @@ -24,8 +24,8 @@ fn main() { .file("libsnark/src/algebra/curves/alt_bn128/alt_bn128_pp.cpp") .file("libsnark/src/common/utils.cpp") .file("libsnark/src/common/profiling.cpp") - .file("src/bnwrap.cpp") + .file("src/libsnarkwrap.cpp") ; - cfg.compile("libbnwrap.a"); + cfg.compile("libsnarkwrap.a"); } diff --git a/snark/src/fr.rs b/snark/src/fr.rs index 9c36a6e..dd2ee0b 100644 --- a/snark/src/fr.rs +++ b/snark/src/fr.rs @@ -8,17 +8,17 @@ use std::ffi::CString; pub struct Fr([u64; 4]); extern "C" { - fn bnwrap_Fr_random() -> Fr; - fn bnwrap_Fr_from(s: *const c_char) -> 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_sub(a: *const Fr, b: *const Fr) -> Fr; - fn bnwrap_Fr_neg(a: *const Fr) -> Fr; + fn libsnarkwrap_Fr_random() -> Fr; + fn libsnarkwrap_Fr_from(s: *const c_char) -> Fr; + fn libsnarkwrap_Fr_add(a: *const Fr, b: *const Fr) -> Fr; + fn libsnarkwrap_Fr_mul(a: *const Fr, b: *const Fr) -> Fr; + fn libsnarkwrap_Fr_sub(a: *const Fr, b: *const Fr) -> Fr; + fn libsnarkwrap_Fr_neg(a: *const Fr) -> Fr; } impl Fr { pub fn random() -> Self { - unsafe { bnwrap_Fr_random() } + unsafe { libsnarkwrap_Fr_random() } } pub fn from_str(s: &str) -> Self { @@ -39,7 +39,7 @@ impl Fr { 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; 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; 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; 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; fn neg(self) -> Fr { - unsafe { bnwrap_Fr_neg(&self) } + unsafe { libsnarkwrap_Fr_neg(&self) } } } diff --git a/snark/src/g1.rs b/snark/src/g1.rs index 3f6cf17..bc38e8c 100644 --- a/snark/src/g1.rs +++ b/snark/src/g1.rs @@ -10,40 +10,40 @@ pub struct G1 { } extern "C" { - fn bnwrap_G1_zero() -> G1; - fn bnwrap_G1_one() -> G1; - fn bnwrap_G1_random() -> G1; + fn libsnarkwrap_G1_zero() -> G1; + fn libsnarkwrap_G1_one() -> G1; + fn libsnarkwrap_G1_random() -> G1; - fn bnwrap_G1_is_zero(p: *const G1) -> bool; - fn bnwrap_G1_is_equal(p: *const G1, q: *const G1) -> bool; + fn libsnarkwrap_G1_is_zero(p: *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 bnwrap_G1_sub(p: *const G1, q: *const G1) -> G1; - fn bnwrap_G1_neg(p: *const G1) -> G1; - fn bnwrap_G1_scalarmul(p: *const G1, s: *const Fr) -> G1; + fn libsnarkwrap_G1_add(p: *const G1, q: *const G1) -> G1; + fn libsnarkwrap_G1_sub(p: *const G1, q: *const G1) -> G1; + fn libsnarkwrap_G1_neg(p: *const G1) -> G1; + fn libsnarkwrap_G1_scalarmul(p: *const G1, s: *const Fr) -> G1; } impl PartialEq for G1 { fn eq(&self, other: &G1) -> bool { - unsafe { bnwrap_G1_is_equal(self, other) } + unsafe { libsnarkwrap_G1_is_equal(self, other) } } } impl Group for G1 { fn zero() -> G1 { - unsafe { bnwrap_G1_zero() } + unsafe { libsnarkwrap_G1_zero() } } fn one() -> G1 { - unsafe { bnwrap_G1_one() } + unsafe { libsnarkwrap_G1_one() } } fn random() -> G1 { - unsafe { bnwrap_G1_random() } + unsafe { libsnarkwrap_G1_random() } } 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; fn add(self, other: G1) -> G1 { - unsafe { bnwrap_G1_add(&self, &other) } + unsafe { libsnarkwrap_G1_add(&self, &other) } } } @@ -59,7 +59,7 @@ impl Mul for G1 { type Output = 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; 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; fn neg(self) -> G1 { - unsafe { bnwrap_G1_neg(&self) } + unsafe { libsnarkwrap_G1_neg(&self) } } } diff --git a/snark/src/g2.rs b/snark/src/g2.rs index 71e9307..f2592e3 100644 --- a/snark/src/g2.rs +++ b/snark/src/g2.rs @@ -10,40 +10,40 @@ pub struct G2 { } extern "C" { - fn bnwrap_G2_zero() -> G2; - fn bnwrap_G2_one() -> G2; - fn bnwrap_G2_random() -> G2; + fn libsnarkwrap_G2_zero() -> G2; + fn libsnarkwrap_G2_one() -> G2; + fn libsnarkwrap_G2_random() -> G2; - fn bnwrap_G2_is_zero(p: *const G2) -> bool; - fn bnwrap_G2_is_equal(p: *const G2, q: *const G2) -> bool; + fn libsnarkwrap_G2_is_zero(p: *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 bnwrap_G2_sub(p: *const G2, q: *const G2) -> G2; - fn bnwrap_G2_neg(p: *const G2) -> G2; - fn bnwrap_G2_scalarmul(p: *const G2, s: *const Fr) -> G2; + fn libsnarkwrap_G2_add(p: *const G2, q: *const G2) -> G2; + fn libsnarkwrap_G2_sub(p: *const G2, q: *const G2) -> G2; + fn libsnarkwrap_G2_neg(p: *const G2) -> G2; + fn libsnarkwrap_G2_scalarmul(p: *const G2, s: *const Fr) -> G2; } impl PartialEq for G2 { fn eq(&self, other: &G2) -> bool { - unsafe { bnwrap_G2_is_equal(self, other) } + unsafe { libsnarkwrap_G2_is_equal(self, other) } } } impl Group for G2 { fn zero() -> G2 { - unsafe { bnwrap_G2_zero() } + unsafe { libsnarkwrap_G2_zero() } } fn one() -> G2 { - unsafe { bnwrap_G2_one() } + unsafe { libsnarkwrap_G2_one() } } fn random() -> G2 { - unsafe { bnwrap_G2_random() } + unsafe { libsnarkwrap_G2_random() } } 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; fn add(self, other: G2) -> G2 { - unsafe { bnwrap_G2_add(&self, &other) } + unsafe { libsnarkwrap_G2_add(&self, &other) } } } @@ -59,7 +59,7 @@ impl Mul for G2 { type Output = 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; 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; fn neg(self) -> G2 { - unsafe { bnwrap_G2_neg(&self) } + unsafe { libsnarkwrap_G2_neg(&self) } } } diff --git a/snark/src/gt.rs b/snark/src/gt.rs index 314e915..bc7926c 100644 --- a/snark/src/gt.rs +++ b/snark/src/gt.rs @@ -9,13 +9,13 @@ pub struct Gt { } 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 for Gt { type Output = Gt; fn mul(self, other: Fr) -> Gt { - unsafe { bnwrap_gt_exp(&self, &other) } + unsafe { libsnarkwrap_gt_exp(&self, &other) } } } diff --git a/snark/src/lib.rs b/snark/src/lib.rs index 227ab30..b217939 100644 --- a/snark/src/lib.rs +++ b/snark/src/lib.rs @@ -16,8 +16,8 @@ pub use self::g1::G1; pub use self::g2::G2; extern "C" { - fn bnwrap_init(); - fn bnwrap_pairing(p: *const G1, q: *const G2) -> Gt; + fn libsnarkwrap_init(); + fn libsnarkwrap_pairing(p: *const G1, q: *const G2) -> Gt; } lazy_static! { @@ -29,13 +29,13 @@ pub fn initialize() { let mut l = INIT_LOCK.lock().unwrap(); if !*l { - unsafe { bnwrap_init(); } + unsafe { libsnarkwrap_init(); } *l = true; } } pub fn pairing(p: &G1, q: &G2) -> Gt { - unsafe { bnwrap_pairing(p, q) } + unsafe { libsnarkwrap_pairing(p, q) } } pub trait Group: Sized + diff --git a/snark/src/libsnarkwrap.cpp b/snark/src/libsnarkwrap.cpp new file mode 100644 index 0000000..084a0a0 --- /dev/null +++ b/snark/src/libsnarkwrap.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include "algebra/curves/alt_bn128/alt_bn128_g1.hpp" +#include +#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 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); +}