From 1ec3e8e004b7688749244d6b3452df48f021aac6 Mon Sep 17 00:00:00 2001 From: porcuquine Date: Mon, 31 Aug 2020 21:01:52 -0700 Subject: [PATCH] Eliminate wasteful public-input conversions. --- storage-proofs/core/src/gadgets/por.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/storage-proofs/core/src/gadgets/por.rs b/storage-proofs/core/src/gadgets/por.rs index a3cc78f92..c9a2a866e 100644 --- a/storage-proofs/core/src/gadgets/por.rs +++ b/storage-proofs/core/src/gadgets/por.rs @@ -1,11 +1,13 @@ +use std::convert::TryFrom; use std::marker::PhantomData; use anyhow::ensure; use bellperson::gadgets::boolean::{AllocatedBit, Boolean}; use bellperson::gadgets::{multipack, num}; use bellperson::{Circuit, ConstraintSystem, SynthesisError}; +use ff::PrimeField; use generic_array::typenum::Unsigned; -use paired::bls12_381::{Bls12, Fr}; +use paired::bls12_381::{Bls12, Fr, FrRepr}; use crate::compound_proof::{CircuitComponent, CompoundProof}; use crate::error::Result; @@ -306,10 +308,19 @@ impl<'a, Tree: 'static + MerkleTreeTrait> CompoundProof<'a, PoR, PoRCircui pub_params: & as ProofScheme<'a>>::PublicParams, _k: Option, ) -> Result> { + ensure!( + pub_inputs.challenge < pub_params.leaves, + "Challenge out of range" + ); let mut inputs = Vec::new(); - let path_bits = challenge_into_auth_path_bits(pub_inputs.challenge, pub_params.leaves); - inputs.extend(multipack::compute_multipacking::(&path_bits)); + // Inputs are (currently, inefficiently) packed with one `Fr` per challenge. + // Boolean/bit auth paths trivially correspond to the challenged node's index within a sector. + // Defensively convert the challenge with `try_from` as a reminder that we must not truncate. + let input_fr = Fr::from_repr(FrRepr::from( + u64::try_from(pub_inputs.challenge).expect("challenge type too wide"), + ))?; + inputs.push(input_fr); if let Some(commitment) = pub_inputs.commitment { ensure!(!pub_params.private, "Params must be public");