mirror of https://github.com/zcash/mpc.git
returning qap
This commit is contained in:
parent
4f85755795
commit
bc6fd2683e
|
@ -18,8 +18,13 @@ pub use self::g2::G2;
|
|||
extern "C" {
|
||||
fn libsnarkwrap_init();
|
||||
fn libsnarkwrap_pairing(p: *const G1, q: *const G2) -> Gt;
|
||||
fn libsnarkwrap_getqap(d: *mut libc::uint32_t, omega: *mut Fr);
|
||||
fn libsnarkwrap_test_compare_tau(i: *const G1, tau: *const Fr, d: libc::uint32_t) -> bool;
|
||||
fn libsnarkwrap_getqap(d: *mut libc::uint32_t, omega: *mut Fr) -> *mut libc::c_void;
|
||||
fn libsnarkwrap_dropqap(qap: *mut libc::c_void);
|
||||
fn libsnarkwrap_test_compare_tau(
|
||||
i: *const G1,
|
||||
tau: *const Fr,
|
||||
d: libc::uint32_t,
|
||||
qap: *const libc::c_void) -> bool;
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
|
@ -36,20 +41,29 @@ pub fn initialize() {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct QAP(*mut libc::c_void);
|
||||
|
||||
impl Drop for QAP {
|
||||
fn drop(&mut self) {
|
||||
unsafe { libsnarkwrap_dropqap(self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the QAP info for the generation routines
|
||||
pub fn getqap() -> (usize, Fr) {
|
||||
pub fn getqap() -> (usize, Fr, QAP) {
|
||||
let mut d = 0;
|
||||
let mut o = Fr::zero();
|
||||
|
||||
unsafe { libsnarkwrap_getqap(&mut d, &mut o); }
|
||||
|
||||
(d as usize, o)
|
||||
let qap = unsafe { libsnarkwrap_getqap(&mut d, &mut o) };
|
||||
|
||||
(d as usize, o, QAP(qap))
|
||||
}
|
||||
|
||||
/// Check that the lagrange coefficients computed by tau over
|
||||
/// G1 equal the expected vector.
|
||||
pub fn compare_tau(v: &[G1], tau: &Fr) -> bool {
|
||||
unsafe { libsnarkwrap_test_compare_tau(&v[0], tau, v.len() as u32) }
|
||||
pub fn compare_tau(v: &[G1], tau: &Fr, qap: &QAP) -> bool {
|
||||
unsafe { libsnarkwrap_test_compare_tau(&v[0], tau, v.len() as u32, qap.0) }
|
||||
}
|
||||
|
||||
pub trait Pairing<Other: Group> {
|
||||
|
|
|
@ -172,9 +172,7 @@ extern "C" curve_GT libsnarkwrap_pairing(const curve_G1 *p, const curve_G2 *q) {
|
|||
|
||||
// QAP
|
||||
|
||||
qap_instance<curve_Fr> get_qap(
|
||||
std::shared_ptr<basic_radix2_domain<curve_Fr>> &domain
|
||||
)
|
||||
qap_instance<curve_Fr> get_qap()
|
||||
{
|
||||
// Generate a dummy circuit
|
||||
auto example = generate_r1cs_example_with_field_input<curve_Fr>(250, 4);
|
||||
|
@ -188,33 +186,35 @@ qap_instance<curve_Fr> get_qap(
|
|||
// Degree of the QAP must be a power of 2
|
||||
assert(qap.degree() == 256);
|
||||
|
||||
// Assume radix2 evaluation domain
|
||||
domain = std::static_pointer_cast<basic_radix2_domain<curve_Fr>>(qap.domain);
|
||||
|
||||
return qap;
|
||||
}
|
||||
|
||||
extern "C" void libsnarkwrap_getqap(uint32_t *d, curve_Fr *omega)
|
||||
extern "C" void* libsnarkwrap_getqap(uint32_t *d, curve_Fr *omega)
|
||||
{
|
||||
std::shared_ptr<basic_radix2_domain<curve_Fr>> domain;
|
||||
auto qap = get_qap(domain);
|
||||
auto qap = new qap_instance<curve_Fr>(get_qap());
|
||||
|
||||
// Assume radix2 evaluation domain
|
||||
*omega = std::static_pointer_cast<basic_radix2_domain<curve_Fr>>(qap->domain)->omega;
|
||||
*d = qap->degree();
|
||||
|
||||
return qap;
|
||||
}
|
||||
|
||||
*omega = domain->omega;
|
||||
*d = qap.degree();
|
||||
extern "C" void libsnarkwrap_dropqap(qap_instance<curve_Fr> *qap)
|
||||
{
|
||||
delete qap;
|
||||
}
|
||||
|
||||
extern "C" bool libsnarkwrap_test_compare_tau(
|
||||
const curve_G1 *inputs,
|
||||
const curve_Fr *tau,
|
||||
uint32_t d
|
||||
uint32_t d,
|
||||
const qap_instance<curve_Fr> *qap
|
||||
)
|
||||
{
|
||||
std::shared_ptr<basic_radix2_domain<curve_Fr>> domain;
|
||||
auto qap = get_qap(domain);
|
||||
|
||||
auto coeffs = domain->lagrange_coeffs(*tau);
|
||||
auto coeffs = qap->domain->lagrange_coeffs(*tau);
|
||||
assert(coeffs.size() == d);
|
||||
assert(qap.degree() == d);
|
||||
assert(qap->degree() == d);
|
||||
|
||||
bool res = true;
|
||||
for (size_t i = 0; i < d; i++) {
|
||||
|
|
|
@ -46,7 +46,7 @@ mod test {
|
|||
initialize();
|
||||
|
||||
// Get the QAP degree and omega (for FFT evaluation)
|
||||
let (d, omega) = getqap();
|
||||
let (d, omega, qap) = getqap();
|
||||
|
||||
// Sample a random tau
|
||||
let tau = Fr::random();
|
||||
|
@ -62,9 +62,9 @@ mod test {
|
|||
.collect::<Vec<_>>();
|
||||
|
||||
// Compare against libsnark
|
||||
assert!(compare_tau(&lc, &tau));
|
||||
assert!(compare_tau(&lc, &tau, &qap));
|
||||
|
||||
// Wrong tau
|
||||
assert!(!compare_tau(&lc, &Fr::random()));
|
||||
assert!(!compare_tau(&lc, &Fr::random(), &qap));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue