Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct error type for PlonkVerifierGadget #113

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions plonk/src/circuit/plonk_verifier/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use crate::{
circuit::{plonk_verifier::*, transcript::RescueTranscriptVar},
constants::EXTRA_TRANSCRIPT_MSG_LABEL,
errors::{PlonkError, SnarkError::ParameterError},
errors::PlonkError,
};
use ark_ec::{
short_weierstrass_jacobian::GroupAffine, PairingEngine, SWModelParameters as SWParam,
Expand All @@ -18,6 +18,7 @@ use ark_poly::{EvaluationDomain, Radix2EvaluationDomain};
use ark_std::{format, vec::Vec};
use jf_primitives::rescue::RescueParameter;
use jf_relation::{
errors::{CircuitError, CircuitError::ParameterError},
gadgets::{
ecc::{PointVariable, SWToTEConParam},
ultraplonk::mod_arith::{FpElem, FpElemVar},
Expand Down Expand Up @@ -48,7 +49,7 @@ pub(super) fn aggregate_poly_commitments_circuit<E, F>(
batch_proof: &BatchProofVar<F>,
alpha_bases: &[FpElemVar<F>],
non_native_field_info: NonNativeFieldInfo<F>,
) -> Result<(ScalarsAndBasesVar<F>, Vec<FpElemVar<F>>), PlonkError>
) -> Result<(ScalarsAndBasesVar<F>, Vec<FpElemVar<F>>), CircuitError>
where
E: PairingEngine<Fq = F>,
F: PrimeField,
Expand All @@ -58,8 +59,7 @@ where
"the number of verification keys {} != the number of instances {}",
vks.len(),
batch_proof.len()
))
.into());
)));
}

// Compute the first part of the batched polynomial commitment `[D]1` described in Sec 8.4, step 9 of https://eprint.iacr.org/2019/953.pdf
Expand Down Expand Up @@ -132,7 +132,7 @@ pub(super) fn aggregate_evaluations_circuit<E, F>(
poly_evals_vec: &[ProofEvaluationsVar<F>],
non_native_field_info: NonNativeFieldInfo<F>,
buffer_v_and_uv_basis: &[FpElemVar<F>],
) -> Result<FpElemVar<F>, PlonkError>
) -> Result<FpElemVar<F>, CircuitError>
where
E: PairingEngine<Fq = F>,
F: PrimeField,
Expand Down Expand Up @@ -177,7 +177,7 @@ where
}
// ensure all the buffer has been consumed
if v_and_uv_basis.next().is_some() {
return Err(PlonkError::IteratorOutOfRange);
return Err(PlonkError::IteratorOutOfRange)?;
}
Ok(result)
}
Expand All @@ -191,7 +191,7 @@ pub(super) fn compute_challenges_vars<E, F, P>(
batch_proof: &BatchProofVar<F>,
extra_transcript_init_msg: &Option<Vec<u8>>,
non_native_field_info: NonNativeFieldInfo<F>,
) -> Result<ChallengesFpElemVar<F>, PlonkError>
) -> Result<ChallengesFpElemVar<F>, CircuitError>
alxiong marked this conversation as resolved.
Show resolved Hide resolved
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: RescueParameter + SWToTEConParam,
Expand All @@ -203,8 +203,7 @@ where
verify_keys.len(),
batch_proof.len(),
public_inputs.len(),
))
.into());
)));
}
let mut transcript_var = RescueTranscriptVar::new(circuit);
if let Some(msg) = extra_transcript_init_msg {
Expand Down Expand Up @@ -270,7 +269,7 @@ pub(super) fn prepare_pcs_info_var<E, F, P>(

domain: Radix2EvaluationDomain<E::Fr>,
non_native_field_info: NonNativeFieldInfo<F>,
) -> Result<PcsInfoVar<F>, PlonkError>
) -> Result<PcsInfoVar<F>, CircuitError>
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: RescueParameter + SWToTEConParam,
Expand All @@ -282,8 +281,7 @@ where
verify_keys.len(),
batch_proof.len(),
public_inputs.len(),
))
.into());
)));
}

for (i, (&pub_input, &vk)) in public_inputs.iter().zip(verify_keys.iter()).enumerate() {
Expand All @@ -293,8 +291,7 @@ where
pub_input.len(),
i,
vk.num_inputs,
))
.into());
)));
}

if vk.domain_size != domain.size() {
Expand All @@ -303,8 +300,7 @@ where
vk.domain_size,
i,
domain.size(),
))
.into());
)));
}
}

Expand Down Expand Up @@ -398,7 +394,7 @@ fn add_poly_comm_circuit<F>(
comm: &PointVariable,
r: &FpElemVar<F>,
p: &FpElem<F>,
) -> Result<(), PlonkError>
) -> Result<(), CircuitError>
where
F: PrimeField,
{
Expand All @@ -417,7 +413,7 @@ fn add_pcs_eval_circuit<F>(
random_combiner: &FpElemVar<F>,
eval: &FpElemVar<F>,
p: &FpElem<F>,
) -> Result<(), PlonkError>
) -> Result<(), CircuitError>
where
F: PrimeField,
{
Expand All @@ -434,7 +430,7 @@ fn compute_alpha_basis<F: PrimeField>(
alpha_to_3: FpElemVar<F>,
len: usize,
non_native_field_info: NonNativeFieldInfo<F>,
) -> Result<Vec<FpElemVar<F>>, PlonkError> {
) -> Result<Vec<FpElemVar<F>>, CircuitError> {
let mut res = Vec::new();
let mut alpha_base_elem_var = FpElemVar::<F>::one(
circuit,
Expand Down Expand Up @@ -472,12 +468,12 @@ mod test {

const RANGE_BIT_LEN_FOR_TEST: usize = 16;
#[test]
fn test_compute_challenges_vars_circuit() -> Result<(), PlonkError> {
fn test_compute_challenges_vars_circuit() -> Result<(), CircuitError> {
test_compute_challenges_vars_circuit_helper::<Bls12_377, _, _, Param377, RescueTranscript<_>>(
)
}

fn test_compute_challenges_vars_circuit_helper<E, F, P, Q, T>() -> Result<(), PlonkError>
fn test_compute_challenges_vars_circuit_helper<E, F, P, Q, T>() -> Result<(), CircuitError>
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: RescueParameter + SWToTEConParam,
Expand Down
57 changes: 26 additions & 31 deletions plonk/src/circuit/plonk_verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
// along with the Jellyfish library. If not, see <https://mit-license.org/>.

//! Circuits for Plonk verifiers.
use crate::{
errors::{PlonkError, SnarkError::ParameterError},
proof_system::{structs::VerifyingKey, verifier::Verifier},
};
use crate::proof_system::{structs::VerifyingKey, verifier::Verifier};
use ark_ec::{
short_weierstrass_jacobian::GroupAffine, PairingEngine, SWModelParameters as SWParam,
TEModelParameters as TEParam,
Expand All @@ -17,7 +14,7 @@ use ark_ff::{BigInteger, FpParameters, PrimeField};
use ark_std::{format, string::ToString, vec, vec::Vec};
use jf_primitives::rescue::RescueParameter;
use jf_relation::{
errors::CircuitError,
errors::{CircuitError, CircuitError::ParameterError},
gadgets::{
ecc::{MultiScalarMultiplicationCircuit, Point, PointVariable, SWToTEConParam},
ultraplonk::mod_arith::{FpElem, FpElemVar},
Expand Down Expand Up @@ -58,7 +55,7 @@ impl<E: PairingEngine> VerifyingKeyVar<E> {
pub fn new<F, P>(
circuit: &mut PlonkCircuit<F>,
verify_key: &VerifyingKey<E>,
) -> Result<Self, PlonkError>
) -> Result<Self, CircuitError>
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: PrimeField + SWToTEConParam,
Expand Down Expand Up @@ -103,25 +100,25 @@ impl<E: PairingEngine> VerifyingKeyVar<E> {
&self,
circuit: &mut PlonkCircuit<F>,
other: &Self,
) -> Result<Self, PlonkError>
) -> Result<Self, CircuitError>
where
F: PrimeField,
P: TEParam<BaseField = F>,
{
if self.is_merged || other.is_merged {
return Err(ParameterError("cannot merge a merged key again".to_string()).into());
return Err(ParameterError(
"cannot merge a merged key again".to_string(),
));
}
if self.domain_size != other.domain_size {
return Err(ParameterError(
"cannot merge a verifying key with different domain size".to_string(),
)
.into());
));
}
if self.num_inputs != other.num_inputs {
return Err(ParameterError(
"cannot merge a verifying key with different public input length".to_string(),
)
.into());
));
}
let sigma_comms = self
.sigma_comms
Expand Down Expand Up @@ -157,22 +154,21 @@ impl<E: PairingEngine> VerifyingKeyVar<E> {
shared_public_input_vars: &[FpElemVar<F>],
batch_proof: &BatchProofVar<F>,
blinding_factor: Variable,
) -> Result<(PointVariable, PointVariable), PlonkError>
) -> Result<(PointVariable, PointVariable), CircuitError>
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: RescueParameter + SWToTEConParam,
P: SWParam<BaseField = F> + TEParam,
{
if merged_vks.is_empty() {
return Err(ParameterError("empty merged verification keys".to_string()).into());
return Err(ParameterError("empty merged verification keys".to_string()));
}
if merged_vks.len() != batch_proof.len() {
return Err(ParameterError(format!(
"the number of verification keys {} is different from the number of instances {}.",
merged_vks.len(),
batch_proof.len()
))
.into());
)));
}

let domain_size = merged_vks[0].domain_size;
Expand All @@ -181,8 +177,7 @@ impl<E: PairingEngine> VerifyingKeyVar<E> {
return Err(ParameterError(format!(
"the {}-th verification key's domain size {} is different from {}.",
i, vk.domain_size, domain_size
))
.into());
)));
}
}

Expand Down Expand Up @@ -287,7 +282,7 @@ pub trait BatchableCircuit<F> {
&mut self,
vk_type_a_vars: &[VerifyingKeyVar<E>],
vk_type_b_vars: &[VerifyingKeyVar<E>],
) -> Result<Vec<VerifyingKeyVar<E>>, PlonkError>
) -> Result<Vec<VerifyingKeyVar<E>>, CircuitError>
where
E: PairingEngine,
P: TEParam<BaseField = F>;
Expand All @@ -302,7 +297,7 @@ where
&mut self,
vk_type_a_vars: &[VerifyingKeyVar<E>],
vk_type_b_vars: &[VerifyingKeyVar<E>],
) -> Result<Vec<VerifyingKeyVar<E>>, PlonkError>
) -> Result<Vec<VerifyingKeyVar<E>>, CircuitError>
where
E: PairingEngine,
P: TEParam<BaseField = F>,
Expand All @@ -312,13 +307,13 @@ where
"the number of type A verification key variables {} is different from the number of type B verification key variables {}.",
vk_type_a_vars.len(),
vk_type_b_vars.len())
).into());
));
}
vk_type_a_vars
.iter()
.zip(vk_type_b_vars.iter())
.map(|(vk_b, vk_d)| vk_b.merge::<F, P>(self, vk_d))
.collect::<Result<Vec<_>, PlonkError>>()
.collect::<Result<Vec<_>, CircuitError>>()
}
}

Expand Down Expand Up @@ -346,11 +341,11 @@ mod test {
const RANGE_BIT_LEN_FOR_TEST: usize = 16;

#[test]
fn test_aggregate_vks() -> Result<(), PlonkError> {
fn test_aggregate_vks() -> Result<(), CircuitError> {
test_aggregate_vks_helper::<Bls12_377, Fq377, _, Param377>()
}

fn test_aggregate_vks_helper<E, F, P, Q>() -> Result<(), PlonkError>
fn test_aggregate_vks_helper<E, F, P, Q>() -> Result<(), CircuitError>
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: PrimeField + RescueParameter + SWToTEConParam,
Expand Down Expand Up @@ -396,15 +391,15 @@ mod test {
let vk_type_a_vars = vks_type_a
.iter()
.map(|vk| VerifyingKeyVar::new(&mut circuit, vk))
.collect::<Result<Vec<_>, PlonkError>>()?;
.collect::<Result<Vec<_>, CircuitError>>()?;
for (vk_var, vk) in vk_type_a_vars.iter().zip(vks_type_a.iter()) {
check_vk_equality(&circuit, vk_var, vk);
}

let vk_type_b_vars = vks_type_b
.iter()
.map(|vk| VerifyingKeyVar::new(&mut circuit, vk))
.collect::<Result<Vec<_>, PlonkError>>()?;
.collect::<Result<Vec<_>, CircuitError>>()?;
for (vk_var, vk) in vk_type_b_vars.iter().zip(vks_type_b.iter()) {
check_vk_equality(&circuit, vk_var, vk);
}
Expand Down Expand Up @@ -460,11 +455,11 @@ mod test {
}

#[test]
fn test_partial_verification_circuit() -> Result<(), PlonkError> {
fn test_partial_verification_circuit() -> Result<(), CircuitError> {
test_partial_verification_circuit_helper::<Bls12_377, _, _, Param377, RescueTranscript<_>>()
}

fn test_partial_verification_circuit_helper<E, F, P, Q, T>() -> Result<(), PlonkError>
fn test_partial_verification_circuit_helper<E, F, P, Q, T>() -> Result<(), CircuitError>
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: RescueParameter + SWToTEConParam,
Expand Down Expand Up @@ -697,7 +692,7 @@ mod test {
beta_g_ref: &GroupAffine<P>,
generator_g: &GroupAffine<P>,
blinding_factor: &E::Fr,
) -> Result<(PlonkCircuit<F>, (PointVariable, PointVariable)), PlonkError>
) -> Result<(PlonkCircuit<F>, (PointVariable, PointVariable)), CircuitError>
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: RescueParameter + SWToTEConParam,
Expand Down Expand Up @@ -742,7 +737,7 @@ mod test {
}

#[test]
fn test_variable_independence_for_partial_verification_circuit() -> Result<(), PlonkError> {
fn test_variable_independence_for_partial_verification_circuit() -> Result<(), CircuitError> {
test_variable_independence_for_partial_verification_circuit_helper::<
Bls12_377,
_,
Expand All @@ -753,7 +748,7 @@ mod test {
}

fn test_variable_independence_for_partial_verification_circuit_helper<E, F, P, Q, T>(
) -> Result<(), PlonkError>
) -> Result<(), CircuitError>
where
E: PairingEngine<Fq = F, G1Affine = GroupAffine<P>>,
F: RescueParameter + SWToTEConParam,
Expand Down
Loading