From 746eb56e0f48c1e8cd358d34b5bd0644a61b7775 Mon Sep 17 00:00:00 2001 From: Alex Vlasov Date: Tue, 5 Feb 2019 23:10:14 +0300 Subject: [PATCH] verifying key is now self-sufficient for exports --- src/sonic/helped/batch.rs | 54 ++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/src/sonic/helped/batch.rs b/src/sonic/helped/batch.rs index 0196161..445f7e5 100644 --- a/src/sonic/helped/batch.rs +++ b/src/sonic/helped/batch.rs @@ -10,9 +10,17 @@ use ff::{Field}; use pairing::{Engine, CurveAffine, CurveProjective}; + +use crate::SynthesisError; + +use crate::sonic::cs::{Backend, SynthesisDriver}; +use crate::sonic::cs::{Circuit}; + use crate::sonic::srs::SRS; use crate::sonic::util::multiexp; +use std::marker::PhantomData; + // One of the primary functions of the `Batch` abstraction is handling // Kate commitment openings: // @@ -128,20 +136,50 @@ impl Batch { } -pub struct VerifyingKey { +pub struct VerifyingKey { pub alpha_x: E::G2Affine, pub alpha: E::G2Affine, pub neg_h: E::G2Affine, - pub neg_x_n_minus_d: E::G2Affine + pub neg_x_n_minus_d: E::G2Affine, + pub k_map: Vec, + + pub n: usize, + + pub q: usize } impl VerifyingKey { - pub fn new(srs: &SRS, n: usize) -> Self { - Self { + pub fn new, S: SynthesisDriver>(circuit: C, srs: &SRS) -> Result { + struct Preprocess { + k_map: Vec, + n: usize, + q: usize, + _marker: PhantomData + } + + impl<'a, E: Engine> Backend for &'a mut Preprocess { + fn new_k_power(&mut self, index: usize) { + self.k_map.push(index); + } + + fn new_multiplication_gate(&mut self) { + self.n += 1; + } + + fn new_linear_constraint(&mut self) { + self.q += 1; + } + } + + let mut preprocess = Preprocess { k_map: vec![], n: 0, q: 0, _marker: PhantomData }; + + S::synthesize(&mut preprocess, &circuit)?; + + Ok(Self { alpha_x: srs.h_positive_x_alpha[1], alpha: srs.h_positive_x_alpha[0], @@ -154,11 +192,15 @@ impl VerifyingKey { }, neg_x_n_minus_d: { - let mut tmp = srs.h_negative_x[srs.d - n]; + let mut tmp = srs.h_negative_x[srs.d - preprocess.n]; tmp.negate(); tmp }, - } + + k_map: preprocess.k_map, + n: preprocess.n, + q: preprocess.q + }) } } \ No newline at end of file