Place d, num_vars, omega in CS.

This commit is contained in:
Sean Bowe 2016-08-06 11:42:21 -06:00
parent ea0b341eed
commit 7468b3b327
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
3 changed files with 33 additions and 31 deletions

View File

@ -60,7 +60,12 @@ pub fn initialize() {
}
}
pub struct CS(*mut libc::c_void);
pub struct CS {
ptr: *mut libc::c_void,
pub d: usize,
pub num_vars: usize,
pub omega: Fr
}
impl CS {
pub fn test_eval(&self, tau: &Fr, At: &[G1], Bt1: &[G1], Bt2: &[G2], Ct: &[G1]) -> bool {
@ -69,7 +74,7 @@ impl CS {
assert_eq!(Bt2.len(), Ct.len());
unsafe {
libsnarkwrap_test_eval(self.0,
libsnarkwrap_test_eval(self.ptr,
tau,
At.len() as u64,
&At[0],
@ -92,7 +97,7 @@ impl CS {
assert_eq!(Bt2.len(), Ct.len());
unsafe {
libsnarkwrap_eval(self.0,
libsnarkwrap_eval(self.ptr,
&Lt1[0],
&Lt2[0],
Lt1.len() as u64,
@ -107,26 +112,31 @@ impl CS {
impl Drop for CS {
fn drop(&mut self) {
unsafe { libsnarkwrap_dropcs(self.0) }
unsafe { libsnarkwrap_dropcs(self.ptr) }
}
}
/// Get the QAP info for the generation routines
pub fn getqap() -> (usize, usize, Fr, CS) {
pub fn getqap() -> CS {
let mut d = 0;
let mut vars = 0;
let mut o = Fr::zero();
let cs = unsafe { libsnarkwrap_getcs(&mut d, &mut vars, &mut o) };
(d as usize, vars as usize, o, CS(cs))
CS {
ptr: cs,
num_vars: vars as usize,
d: d as usize,
omega: o
}
}
/// Check that the lagrange coefficients computed by tau over
/// G1 equal the expected vector.
pub fn compare_tau(v1: &[G1], v2: &[G2], tau: &Fr, cs: &CS) -> bool {
assert_eq!(v1.len(), v2.len());
unsafe { libsnarkwrap_test_compare_tau(&v1[0], &v2[0], tau, v1.len() as u64, cs.0) }
unsafe { libsnarkwrap_test_compare_tau(&v1[0], &v2[0], tau, v1.len() as u64, cs.ptr) }
}
pub trait Pairing<Other: Group> {

View File

@ -85,19 +85,19 @@ mod test {
initialize();
// Get the QAP degree and omega (for FFT evaluation)
let (d, num_vars, omega, cs) = getqap();
let cs = getqap();
// Sample a random tau
let tau = Fr::random();
// Generate powers of tau in G1, from 0 to d exclusive of d
let powers_of_tau_g1 = TauPowers::new(tau).take(d).map(|e| G1::one() * e).collect::<Vec<_>>();
let powers_of_tau_g1 = TauPowers::new(tau).take(cs.d).map(|e| G1::one() * e).collect::<Vec<_>>();
// Generate powers of tau in G2, from 0 to d exclusive of d
let powers_of_tau_g2 = TauPowers::new(tau).take(d).map(|e| G2::one() * e).collect::<Vec<_>>();
let powers_of_tau_g2 = TauPowers::new(tau).take(cs.d).map(|e| G2::one() * e).collect::<Vec<_>>();
// Perform FFT to compute lagrange coeffs in G1/G2
let lc1 = lagrange_coeffs(&powers_of_tau_g1, omega);
let lc2 = lagrange_coeffs(&powers_of_tau_g2, omega);
let lc1 = lagrange_coeffs(&powers_of_tau_g1, cs.omega);
let lc2 = lagrange_coeffs(&powers_of_tau_g2, cs.omega);
{
// Perform G1 FFT with wrong omega
@ -117,10 +117,10 @@ mod test {
assert!(!compare_tau(&lc1, &lc2, &Fr::random(), &cs));
// Evaluate At, Ct in G1 and Bt in G1/G2
let mut At = (0..num_vars).map(|_| G1::zero()).collect::<Vec<_>>();
let mut Bt1 = (0..num_vars).map(|_| G1::zero()).collect::<Vec<_>>();
let mut Bt2 = (0..num_vars).map(|_| G2::zero()).collect::<Vec<_>>();
let mut Ct = (0..num_vars).map(|_| G1::zero()).collect::<Vec<_>>();
let mut At = (0..cs.num_vars).map(|_| G1::zero()).collect::<Vec<_>>();
let mut Bt1 = (0..cs.num_vars).map(|_| G1::zero()).collect::<Vec<_>>();
let mut Bt2 = (0..cs.num_vars).map(|_| G2::zero()).collect::<Vec<_>>();
let mut Ct = (0..cs.num_vars).map(|_| G1::zero()).collect::<Vec<_>>();
cs.eval(&lc1, &lc2, &mut At, &mut Bt1, &mut Bt2, &mut Ct);

View File

@ -20,16 +20,11 @@ pub struct Samples<T> {
pub struct Player {
secrets: Samples<Fr>,
pub d: usize,
pub num_vars: usize,
omega: Fr,
cs: CS
}
impl Player {
pub fn new() -> Player {
let (d, num_vars, omega, cs) = getqap();
Player {
secrets: Samples {
tau: Fr::random_nonzero(),
@ -41,10 +36,7 @@ impl Player {
beta: Fr::random_nonzero(),
gamma: Fr::random_nonzero()
},
d: d,
num_vars: num_vars,
omega: omega,
cs: cs
cs: getqap()
}
}
@ -62,14 +54,14 @@ impl Player {
}
pub fn randompowers(&self, v1: &[G1], v2: &[G2]) -> Result<(Vec<G1>, Vec<G2>), ProtocolError> {
if (v1.len() != v2.len()) || (v1.len() != self.d+1) {
if (v1.len() != v2.len()) || (v1.len() != self.cs.d+1) {
return Err(ProtocolError::InvalidTauPowersSize)
}
let mut t1 = Vec::with_capacity(self.d+1);
let mut t2 = Vec::with_capacity(self.d+1);
let mut t1 = Vec::with_capacity(self.cs.d+1);
let mut t2 = Vec::with_capacity(self.cs.d+1);
for (i, tp) in TauPowers::new(self.secrets.tau).take(self.d+1).enumerate() {
for (i, tp) in TauPowers::new(self.secrets.tau).take(self.cs.d+1).enumerate() {
t1.push(v1[i] * tp);
t2.push(v2[i] * tp);
}
@ -115,8 +107,8 @@ fn randompowers_test() {
use std::iter::repeat;
if i == 0 {
let v1 = repeat(G1::one()).take(p.d + 1).collect::<Vec<_>>();
let v2 = repeat(G2::one()).take(p.d + 1).collect::<Vec<_>>();
let v1 = repeat(G1::one()).take(p.cs.d + 1).collect::<Vec<_>>();
let v2 = repeat(G2::one()).take(p.cs.d + 1).collect::<Vec<_>>();
transcript.push(p.randompowers(&v1, &v2).unwrap());
} else {
let v = p.randompowers(&transcript[i-1].0, &transcript[i-1].1).unwrap();