Skip to content

Commit

Permalink
Experiment with boxed blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Jul 3, 2023
1 parent 13cec82 commit 12e9c7f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 31 deletions.
60 changes: 40 additions & 20 deletions bindings/rust/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl KZGProof {
}

pub fn compute_kzg_proof(
blob: Blob,
blob: Box<Blob>,
z_bytes: Bytes32,
kzg_settings: &KZGSettings,
) -> Result<(Self, Bytes32), Error> {
Expand All @@ -287,7 +287,7 @@ impl KZGProof {
let res = compute_kzg_proof(
kzg_proof.as_mut_ptr(),
y_out.as_mut_ptr(),
&blob,
&*blob,
&z_bytes,
kzg_settings,
);
Expand All @@ -300,15 +300,15 @@ impl KZGProof {
}

pub fn compute_blob_kzg_proof(
blob: Blob,
blob: Box<Blob>,
commitment_bytes: Bytes48,
kzg_settings: &KZGSettings,
) -> Result<Self, Error> {
let mut kzg_proof = MaybeUninit::<KZGProof>::uninit();
unsafe {
let res = compute_blob_kzg_proof(
kzg_proof.as_mut_ptr(),
&blob,
&*blob,
&commitment_bytes,
kzg_settings,
);
Expand Down Expand Up @@ -346,7 +346,7 @@ impl KZGProof {
}

pub fn verify_blob_kzg_proof(
blob: Blob,
blob: Box<Blob>,
commitment_bytes: Bytes48,
proof_bytes: Bytes48,
kzg_settings: &KZGSettings,
Expand All @@ -355,7 +355,7 @@ impl KZGProof {
unsafe {
let res = verify_blob_kzg_proof(
verified.as_mut_ptr(),
&blob,
&*blob,
&commitment_bytes,
&proof_bytes,
kzg_settings,
Expand All @@ -369,7 +369,7 @@ impl KZGProof {
}

pub fn verify_blob_kzg_proof_batch(
blobs: &[Blob],
blobs: Box<&[Blob]>,
commitments_bytes: &[Bytes48],
proofs_bytes: &[Bytes48],
kzg_settings: &KZGSettings,
Expand All @@ -389,13 +389,14 @@ impl KZGProof {
)));
}
let mut verified: MaybeUninit<bool> = MaybeUninit::uninit();
let blob_slice = &**blobs;
unsafe {
let res = verify_blob_kzg_proof_batch(
verified.as_mut_ptr(),
blobs.as_ptr(),
blob_slice.as_ptr(),
commitments_bytes.as_ptr(),
proofs_bytes.as_ptr(),
blobs.len(),
blob_slice.len(),
kzg_settings,
);
if let C_KZG_RET::C_KZG_OK = res {
Expand Down Expand Up @@ -429,12 +430,12 @@ impl KZGCommitment {
hex::encode(self.bytes)
}

pub fn blob_to_kzg_commitment(blob: Blob, kzg_settings: &KZGSettings) -> Result<Self, Error> {
pub fn blob_to_kzg_commitment(blob: Box<Blob>, kzg_settings: &KZGSettings) -> Result<Self, Error> {
let mut kzg_commitment: MaybeUninit<KZGCommitment> = MaybeUninit::uninit();
unsafe {
let res = blob_to_kzg_commitment(
kzg_commitment.as_mut_ptr(),
blob.as_ptr() as *const Blob,
(*blob).bytes.as_ptr() as *const Blob,
kzg_settings,
);
if let C_KZG_RET::C_KZG_OK = res {
Expand Down Expand Up @@ -464,6 +465,12 @@ impl From<[u8; BYTES_PER_BLOB]> for Blob {
}
}

impl From<[u8; BYTES_PER_BLOB]> for Box<Blob> {
fn from(value: [u8; BYTES_PER_BLOB]) -> Self {
Box::new(Blob { bytes: value })
}
}

impl From<[u8; 32]> for Bytes32 {
fn from(value: [u8; 32]) -> Self {
Self { bytes: value }
Expand Down Expand Up @@ -541,15 +548,15 @@ mod tests {
verify_blob_kzg_proof, verify_blob_kzg_proof_batch, verify_kzg_proof,
};

fn generate_random_blob(rng: &mut ThreadRng) -> Blob {
fn generate_random_blob(rng: &mut ThreadRng) -> Box<Blob> {
let mut arr = [0u8; BYTES_PER_BLOB];
rng.fill(&mut arr[..]);
// Ensure that the blob is canonical by ensuring that
// each field element contained in the blob is < BLS_MODULUS
for i in 0..FIELD_ELEMENTS_PER_BLOB {
arr[i * BYTES_PER_FIELD_ELEMENT] = 0;
}
arr.into()
Box::new(arr.into())
}

fn test_simple(trusted_setup_file: PathBuf) {
Expand All @@ -558,27 +565,30 @@ mod tests {
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();

let num_blobs: usize = rng.gen_range(1..16);
let mut blobs: Vec<Blob> = (0..num_blobs)
let mut blobs: Vec<Box<Blob>> = (0..num_blobs)
.map(|_| generate_random_blob(&mut rng))
.collect();

let commitments: Vec<Bytes48> = blobs
.iter()
.map(|blob| KZGCommitment::blob_to_kzg_commitment(blob.clone(), &kzg_settings).unwrap())
.map(|blob| KZGCommitment::blob_to_kzg_commitment((*blob).clone(), &kzg_settings).unwrap())
.map(|commitment| commitment.to_bytes())
.collect();

let proofs: Vec<Bytes48> = blobs
.iter()
.zip(commitments.iter())
.map(|(blob, commitment)| {
KZGProof::compute_blob_kzg_proof(blob.clone(), *commitment, &kzg_settings).unwrap()
KZGProof::compute_blob_kzg_proof((*blob).clone(), *commitment, &kzg_settings).unwrap()
})
.map(|proof| proof.to_bytes())
.collect();

let blobs_copy: Vec<Blob> = blobs.iter().map(|b| (**b).clone()).collect();
let blobs_slice: &[Blob] = &blobs_copy;
let blobs_boxed_slice: Box<&[Blob]> = Box::new(blobs_slice);
assert!(KZGProof::verify_blob_kzg_proof_batch(
&blobs,
blobs_boxed_slice,
&commitments,
&proofs,
&kzg_settings
Expand All @@ -587,16 +597,22 @@ mod tests {

blobs.pop();

let blobs_copy: Vec<Blob> = blobs.iter().map(|b| (**b).clone()).collect();
let blobs_slice: &[Blob] = &blobs_copy;
let blobs_boxed_slice: Box<&[Blob]> = Box::new(blobs_slice);
let error =
KZGProof::verify_blob_kzg_proof_batch(&blobs, &commitments, &proofs, &kzg_settings)
KZGProof::verify_blob_kzg_proof_batch(blobs_boxed_slice, &commitments, &proofs, &kzg_settings)
.unwrap_err();
assert!(matches!(error, Error::MismatchLength(_)));

let incorrect_blob = generate_random_blob(&mut rng);
blobs.push(incorrect_blob);

let blobs_copy: Vec<Blob> = blobs.iter().map(|b| (**b).clone()).collect();
let blobs_slice: &[Blob] = &blobs_copy;
let blobs_boxed_slice: Box<&[Blob]> = Box::new(blobs_slice);
assert!(!KZGProof::verify_blob_kzg_proof_batch(
&blobs,
blobs_boxed_slice,
&commitments,
&proofs,
&kzg_settings
Expand Down Expand Up @@ -795,8 +811,12 @@ mod tests {
continue;
};

let blobs_copy: Vec<Blob> = blobs.iter().map(|b| (**b).clone()).collect();
let blobs_slice: &[Blob] = &blobs_copy;
let blobs_boxed_slice: Box<&[Blob]> = Box::new(blobs_slice);

match KZGProof::verify_blob_kzg_proof_batch(
&blobs,
blobs_boxed_slice,
&commitments,
&proofs,
&kzg_settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ pub struct Input<'a> {
}

impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> {
Blob::from_hex(self.blob)
pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
let blob = Blob::from_hex(self.blob)?;
Ok(Box::new(blob))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub struct Input<'a> {
}

impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> {
Blob::from_hex(self.blob)
pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
let blob = Blob::from_hex(self.blob)?;
Ok(Box::new(blob))
}

pub fn get_commitment(&self) -> Result<Bytes48, Error> {
Expand Down
5 changes: 3 additions & 2 deletions bindings/rust/src/bindings/test_formats/compute_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub struct Input<'a> {
}

impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> {
Blob::from_hex(self.blob)
pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
let blob = Blob::from_hex(self.blob)?;
Ok(Box::new(blob))
}

pub fn get_z(&self) -> Result<Bytes32, Error> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ pub struct Input<'a> {
}

impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> {
Blob::from_hex(self.blob)
pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
let blob = Blob::from_hex(self.blob)?;
Ok(Box::new(blob))
}

pub fn get_commitment(&self) -> Result<Bytes48, Error> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ pub struct Input {
}

impl Input {
pub fn get_blobs(&self) -> Result<Vec<Blob>, Error> {
let mut v: Vec<Blob> = Vec::new();
pub fn get_blobs(&self) -> Result<Vec<Box<Blob>>, Error> {
let mut v: Vec<Box<Blob>> = Vec::new();
for blob in &self.blobs {
v.push(Blob::from_hex(blob)?);
let boxed_blob = Box::new(Blob::from_hex(blob)?);
v.push(boxed_blob);
}
return Ok(v);
}
Expand Down

0 comments on commit 12e9c7f

Please sign in to comment.