Skip to content

Commit

Permalink
Trim ring when too many keys
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy committed Jul 10, 2024
1 parent efd5a7e commit cf8d875
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
30 changes: 16 additions & 14 deletions src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,28 @@ where
self.piop_params.keyset_part_size
}

pub fn prover_key(&self, pks: &[AffinePoint<S>]) -> Result<ProverKey<S>, Error> {
if pks.len() > self.max_ring_size() {
return Err(Error::InvalidData);
}
/// Construct a `ProverKey` instance for the given ring.
///
/// Note: if `pks.len() > self.max_ring_size()` the extra keys in the tail are ignored.
pub fn prover_key(&self, pks: &[AffinePoint<S>]) -> ProverKey<S> {
let pks = &pks[..pks.len().min(self.max_ring_size())];
#[cfg(feature = "parallel")]
let pks = pks.par_iter().map(|p| p.into_sw()).collect();
let pks: Vec<_> = pks.par_iter().map(|p| p.into_sw()).collect();
#[cfg(not(feature = "parallel"))]
let pks = pks.iter().map(|p| p.into_sw()).collect();
Ok(ring_proof::index(self.pcs_params.clone(), &self.piop_params, pks).0)
let pks: Vec<_> = pks.iter().map(|p| p.into_sw()).collect();
ring_proof::index(&self.pcs_params, &self.piop_params, &pks).0
}

pub fn verifier_key(&self, pks: &[AffinePoint<S>]) -> Result<VerifierKey<S>, Error> {
if pks.len() > self.max_ring_size() {
return Err(Error::InvalidData);
}
/// Construct a `VerifierKey` instance for the given ring.
///
/// Note: if `pks.len() > self.max_ring_size()` the extra keys in the tail are ignored.
pub fn verifier_key(&self, pks: &[AffinePoint<S>]) -> VerifierKey<S> {
let pks = &pks[..pks.len().min(self.max_ring_size())];
#[cfg(feature = "parallel")]
let pks = pks.par_iter().map(|p| p.into_sw()).collect();
let pks: Vec<_> = pks.par_iter().map(|p| p.into_sw()).collect();
#[cfg(not(feature = "parallel"))]
let pks = pks.iter().map(|p| p.into_sw()).collect();
Ok(ring_proof::index(self.pcs_params.clone(), &self.piop_params, pks).1)
let pks: Vec<_> = pks.iter().map(|p| p.into_sw()).collect();
ring_proof::index(&self.pcs_params, &self.piop_params, &pks).1
}

pub fn prover(&self, prover_key: ProverKey<S>, key_index: usize) -> RingProver<S> {
Expand Down
4 changes: 2 additions & 2 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ where
let mut pks = random_vec::<AffinePoint<S>>(ring_size, Some(rng));
pks[prover_idx] = public.0;

let prover_key = ring_ctx.prover_key(&pks).unwrap();
let prover_key = ring_ctx.prover_key(&pks);
let prover = ring_ctx.prover(prover_key, prover_idx);
let proof = secret.prove(input, output, b"foo", &prover);

let verifier_key = ring_ctx.verifier_key(&pks).unwrap();
let verifier_key = ring_ctx.verifier_key(&pks);
let verifier = ring_ctx.verifier(verifier_key);
let result = Public::verify(input, output, b"foo", &proof, &verifier);
assert!(result.is_ok());
Expand Down

0 comments on commit cf8d875

Please sign in to comment.