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

feat: add method to verify consistent precomputed data #561

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 7 additions & 7 deletions vid/src/advz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ mod tests {

#[test]
fn sad_path_verify_share_corrupt_share() {
let (mut advz, bytes_random) = advz_init();
let (mut advz, bytes_random, _) = advz_init();
let disperse = advz.disperse(bytes_random).unwrap();
let (shares, common, commit) = (disperse.shares, disperse.common, disperse.commit);

Expand Down Expand Up @@ -1088,7 +1088,7 @@ mod tests {

#[test]
fn sad_path_verify_share_corrupt_commit() {
let (mut advz, bytes_random) = advz_init();
let (mut advz, bytes_random, _) = advz_init();
let disperse = advz.disperse(bytes_random).unwrap();
let (shares, common, commit) = (disperse.shares, disperse.common, disperse.commit);

Expand Down Expand Up @@ -1134,7 +1134,7 @@ mod tests {

#[test]
fn sad_path_verify_share_corrupt_share_and_commit() {
let (mut advz, bytes_random) = advz_init();
let (mut advz, bytes_random, _) = advz_init();
let disperse = advz.disperse(bytes_random).unwrap();
let (mut shares, mut common, commit) = (disperse.shares, disperse.common, disperse.commit);

Expand All @@ -1159,8 +1159,8 @@ mod tests {

#[test]
fn sad_path_recover_payload_corrupt_shares() {
let (mut advz, bytes_random) = advz_init();
let disperse = advz.disperse(&bytes_random).unwrap();
let (mut advz, bytes_random, _) = advz_init();
let disperse = advz.disperse(bytes_random.clone()).unwrap();
let (shares, common) = (disperse.shares, disperse.common);

{
Expand Down Expand Up @@ -1219,13 +1219,13 @@ mod tests {
/// Returns the following tuple:
/// 1. An initialized [`Advz`] instance.
/// 2. A `Vec<u8>` filled with random bytes.
pub(super) fn advz_init() -> (Advz<Bls12_381, Sha256>, Vec<u8>) {
pub(super) fn advz_init() -> (Advz<Bls12_381, Sha256>, Vec<u8>, u32) {
let (recovery_threshold, num_storage_nodes) = (4, 6);
let mut rng = jf_utils::test_rng();
let srs = init_srs(recovery_threshold as usize, &mut rng);
let advz = Advz::new(num_storage_nodes, recovery_threshold, srs).unwrap();
let bytes_random = init_random_payload(4000, &mut rng);
(advz, bytes_random)
(advz, bytes_random, num_storage_nodes)
}

/// Convenience wrapper to assert [`VidError::Argument`] return value.
Expand Down
41 changes: 38 additions & 3 deletions vid/src/advz/precomputable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ use crate::{
MaybeGPU, Pairing, PolynomialMultiplier, UnivariateKzgPCS,
},
precomputable::Precomputable,
vid, VidDisperse, VidResult,
vid, VidDisperse, VidError, VidResult,
};
use alloc::string::ToString;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{end_timer, start_timer, vec::Vec};
use jf_merkle_tree::{MerkleCommitment, MerkleTreeScheme};
use jf_pcs::{prelude::Commitment, PolynomialCommitmentScheme, UnivariatePCS};

use jf_utils::canonical;
use serde::{Deserialize, Serialize};

use super::Advz;

impl<E, H, T> Precomputable for AdvzInternal<E, H, T>
where
E: Pairing,
Expand Down Expand Up @@ -139,6 +141,26 @@ where
commit,
})
}

fn is_consistent_precompute(
commit: &Self::Commit,
precompute_data: &Self::PrecomputeData,
payload_byte_len: u32,
num_storage_nodes: u32,
) -> VidResult<()> {
if *commit
!= Advz::<E, H>::derive_commit(
&precompute_data.poly_commits,
payload_byte_len,
num_storage_nodes,
)?
{
return Err(VidError::Argument(
"precompute data inconsistent with commit".to_string(),
));
}
Ok(())
}
}

#[derive(
Expand Down Expand Up @@ -217,7 +239,7 @@ mod tests {

#[test]
fn commit_disperse_recover_with_precomputed_data() {
let (advz, bytes_random) = advz_init();
let (advz, bytes_random, _) = advz_init();
let (commit, data) = advz.commit_only_precompute(&bytes_random).unwrap();
let disperse = advz.disperse_precompute(&bytes_random, &data).unwrap();
let (shares, common) = (disperse.shares, disperse.common);
Expand All @@ -231,4 +253,17 @@ mod tests {
.expect("recover_payload should succeed");
assert_eq!(bytes_recovered, bytes_random);
}

#[test]
fn commit_and_verify_consistent_precomputed_data() {
let (advz, bytes_random, num_storage_nodes) = advz_init();
let (commit, data) = advz.commit_only_precompute(&bytes_random).unwrap();
assert!(Advz::is_consistent_precompute(
&commit,
&data,
bytes_random.len() as u32,
num_storage_nodes
)
.is_ok())
}
}
9 changes: 9 additions & 0 deletions vid/src/precomputable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ pub trait Precomputable: VidScheme {
) -> VidResult<VidDisperse<Self>>
where
B: AsRef<[u8]>;

/// Check that a [`Precomputable::PrecomputeData`] is consistent with a
/// [`VidScheme::Commit`].
fn is_consistent_precompute(
commit: &Self::Commit,
precompute_data: &Self::PrecomputeData,
payload_byte_len: u32,
num_storage_nodes: u32,
) -> VidResult<()>;
}
Loading