mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-08-01 12:51:30 +00:00
Avoid the use of dynamic dispatch during verification.
This commit is contained in:
@@ -307,14 +307,7 @@ pub fn prepare_verifying_key<E: Engine>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify<E: Engine, C: Input<E>, F: FnOnce(&mut ConstraintSystem<E>) -> C>(
|
pub struct VerifierInput<'a, E: Engine + 'a> {
|
||||||
e: &E,
|
|
||||||
circuit: F,
|
|
||||||
proof: &Proof<E>,
|
|
||||||
pvk: &PreparedVerifyingKey<E>
|
|
||||||
) -> bool
|
|
||||||
{
|
|
||||||
struct VerifierInput<'a, E: Engine + 'a> {
|
|
||||||
e: &'a E,
|
e: &'a E,
|
||||||
acc: E::G1,
|
acc: E::G1,
|
||||||
ic: &'a [<E::G1 as Curve<E>>::Affine],
|
ic: &'a [<E::G1 as Curve<E>>::Affine],
|
||||||
@@ -323,22 +316,6 @@ pub fn verify<E: Engine, C: Input<E>, F: FnOnce(&mut ConstraintSystem<E>) -> C>(
|
|||||||
num_aux: usize
|
num_aux: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E: Engine> PublicConstraintSystem<E> for VerifierInput<'a, E> {
|
|
||||||
fn alloc_input(&mut self, value: E::Fr) -> Variable {
|
|
||||||
if self.ic.len() == 0 {
|
|
||||||
self.insufficient_inputs = true;
|
|
||||||
} else {
|
|
||||||
self.acc.add_assign(self.e, &self.ic[0].mul(self.e, &value));
|
|
||||||
self.ic = &self.ic[1..];
|
|
||||||
}
|
|
||||||
|
|
||||||
let index = self.num_inputs;
|
|
||||||
self.num_inputs += 1;
|
|
||||||
|
|
||||||
Variable(Index::Input(index))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, E: Engine> ConstraintSystem<E> for VerifierInput<'a, E> {
|
impl<'a, E: Engine> ConstraintSystem<E> for VerifierInput<'a, E> {
|
||||||
fn alloc(&mut self, _: E::Fr) -> Variable {
|
fn alloc(&mut self, _: E::Fr) -> Variable {
|
||||||
let index = self.num_aux;
|
let index = self.num_aux;
|
||||||
@@ -359,6 +336,47 @@ pub fn verify<E: Engine, C: Input<E>, F: FnOnce(&mut ConstraintSystem<E>) -> C>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn verify<'a, E: Engine, C: Input<E>, F: FnOnce(&mut VerifierInput<'a, E>) -> C>(
|
||||||
|
e: &'a E,
|
||||||
|
circuit: F,
|
||||||
|
proof: &Proof<E>,
|
||||||
|
pvk: &'a PreparedVerifyingKey<E>
|
||||||
|
) -> bool
|
||||||
|
{
|
||||||
|
struct InputAllocator<T>(T);
|
||||||
|
|
||||||
|
impl<'a, 'b, E: Engine> PublicConstraintSystem<E> for InputAllocator<&'b mut VerifierInput<'a, E>> {
|
||||||
|
fn alloc_input(&mut self, value: E::Fr) -> Variable {
|
||||||
|
if self.0.ic.len() == 0 {
|
||||||
|
self.0.insufficient_inputs = true;
|
||||||
|
} else {
|
||||||
|
self.0.acc.add_assign(self.0.e, &self.0.ic[0].mul(self.0.e, &value));
|
||||||
|
self.0.ic = &self.0.ic[1..];
|
||||||
|
}
|
||||||
|
|
||||||
|
let index = self.0.num_inputs;
|
||||||
|
self.0.num_inputs += 1;
|
||||||
|
|
||||||
|
Variable(Index::Input(index))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b, E: Engine> ConstraintSystem<E> for InputAllocator<&'b mut VerifierInput<'a, E>> {
|
||||||
|
fn alloc(&mut self, num: E::Fr) -> Variable {
|
||||||
|
self.0.alloc(num)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enforce(
|
||||||
|
&mut self,
|
||||||
|
a: LinearCombination<E>,
|
||||||
|
b: LinearCombination<E>,
|
||||||
|
c: LinearCombination<E>
|
||||||
|
)
|
||||||
|
{
|
||||||
|
self.0.enforce(a, b, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut witness = VerifierInput {
|
let mut witness = VerifierInput {
|
||||||
e: e,
|
e: e,
|
||||||
acc: pvk.ic[0].to_jacobian(e),
|
acc: pvk.ic[0].to_jacobian(e),
|
||||||
@@ -368,7 +386,7 @@ pub fn verify<E: Engine, C: Input<E>, F: FnOnce(&mut ConstraintSystem<E>) -> C>(
|
|||||||
num_aux: 0
|
num_aux: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
circuit(&mut witness).synthesize(e, &mut witness);
|
circuit(&mut witness).synthesize(e, &mut InputAllocator(&mut witness));
|
||||||
|
|
||||||
if witness.ic.len() != 0 || witness.insufficient_inputs {
|
if witness.ic.len() != 0 || witness.insufficient_inputs {
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user