Allow invalid length inputs in rust tests (#171)

* Allow bad input values in rust tests

* Clean up a little

* Re-enable feature checks & consolidate

* Use more pattern matching

* Consolidate imports

* Split some long lines into shorter ones

* Clean up test loops

* Simplify get_output() for some funcs
This commit is contained in:
Justin Traglia 2023-03-06 02:52:36 -07:00 committed by GitHub
parent 8ade71a137
commit 572507ce77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 150 additions and 148 deletions

View File

@ -110,7 +110,7 @@ impl Drop for KZGSettings {
} }
impl Blob { impl Blob {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> { pub fn from_bytes(bytes: &[u8]) -> Result<Box<Self>, Error> {
if bytes.len() != BYTES_PER_BLOB { if bytes.len() != BYTES_PER_BLOB {
return Err(Error::InvalidBytesLength(format!( return Err(Error::InvalidBytesLength(format!(
"Invalid byte length. Expected {} got {}", "Invalid byte length. Expected {} got {}",
@ -120,7 +120,7 @@ impl Blob {
} }
let mut new_bytes = [0; BYTES_PER_BLOB]; let mut new_bytes = [0; BYTES_PER_BLOB];
new_bytes.copy_from_slice(bytes); new_bytes.copy_from_slice(bytes);
Ok(Self { bytes: new_bytes }) Ok(Box::new(Self { bytes: new_bytes }))
} }
} }
@ -461,18 +461,17 @@ mod tests {
assert!(trusted_setup_file.exists()); assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap(); let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(BLOB_TO_KZG_COMMITMENT_TESTS) for test_file in glob::glob(BLOB_TO_KZG_COMMITMENT_TESTS).unwrap() {
.unwrap() let yaml_data = fs::read_to_string(test_file.unwrap()).unwrap();
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
let test: blob_to_kzg_commitment_test::Test = serde_yaml::from_str(&yaml_data).unwrap(); let test: blob_to_kzg_commitment_test::Test = serde_yaml::from_str(&yaml_data).unwrap();
let res = KZGCommitment::blob_to_kzg_commitment(test.input.get_blob(), &kzg_settings); let Ok(blob) = test.input.get_blob() else {
assert!(test.get_output().is_none());
continue;
};
if res.is_ok() { match KZGCommitment::blob_to_kzg_commitment(*blob, &kzg_settings) {
assert_eq!(res.unwrap().bytes, test.get_output().unwrap().bytes) Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
} else { _ => assert!(test.get_output().is_none()),
assert!(test.get_output().is_none())
} }
} }
} }
@ -484,22 +483,17 @@ mod tests {
assert!(trusted_setup_file.exists()); assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap(); let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(COMPUTE_KZG_PROOF_TESTS) for test_file in glob::glob(COMPUTE_KZG_PROOF_TESTS).unwrap() {
.unwrap() let yaml_data = fs::read_to_string(test_file.unwrap()).unwrap();
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
let test: compute_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap(); let test: compute_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap();
let res = KZGProof::compute_kzg_proof( let (Ok(blob), Ok(z)) = (test.input.get_blob(), test.input.get_z()) else {
test.input.get_blob(), assert!(test.get_output().is_none());
test.input.get_z(), continue;
&kzg_settings, };
);
if res.is_ok() { match KZGProof::compute_kzg_proof(*blob, z, &kzg_settings) {
assert_eq!(res.unwrap().bytes, test.get_output().unwrap().bytes) Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
} else { _ => assert!(test.get_output().is_none()),
assert!(test.get_output().is_none())
} }
} }
} }
@ -511,18 +505,17 @@ mod tests {
assert!(trusted_setup_file.exists()); assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap(); let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(COMPUTE_BLOB_KZG_PROOF_TESTS) for test_file in glob::glob(COMPUTE_BLOB_KZG_PROOF_TESTS).unwrap() {
.unwrap() let yaml_data = fs::read_to_string(test_file.unwrap()).unwrap();
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
let test: compute_blob_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap(); let test: compute_blob_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap();
let res = KZGProof::compute_blob_kzg_proof(test.input.get_blob(), &kzg_settings); let Ok(blob) = test.input.get_blob() else {
assert!(test.get_output().is_none());
continue;
};
if res.is_ok() { match KZGProof::compute_blob_kzg_proof(*blob, &kzg_settings) {
assert_eq!(res.unwrap().bytes, test.get_output().unwrap().bytes) Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
} else { _ => assert!(test.get_output().is_none()),
assert!(test.get_output().is_none())
} }
} }
} }
@ -534,24 +527,22 @@ mod tests {
assert!(trusted_setup_file.exists()); assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap(); let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(VERIFY_KZG_PROOF_TESTS) for test_file in glob::glob(VERIFY_KZG_PROOF_TESTS).unwrap() {
.unwrap() let yaml_data = fs::read_to_string(test_file.unwrap()).unwrap();
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
let test: verify_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap(); let test: verify_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap();
let res = KZGProof::verify_kzg_proof( let (Ok(commitment), Ok(z), Ok(y), Ok(proof)) = (
test.input.get_commitment(), test.input.get_commitment(),
test.input.get_z(), test.input.get_z(),
test.input.get_y(), test.input.get_y(),
test.input.get_proof(), test.input.get_proof()
&kzg_settings, ) else {
); assert!(test.get_output().is_none());
continue;
};
if res.is_ok() { match KZGProof::verify_kzg_proof(commitment, z, y, proof, &kzg_settings) {
assert_eq!(res.unwrap(), test.get_output().unwrap()) Ok(res) => assert_eq!(res, test.get_output().unwrap()),
} else { _ => assert!(test.get_output().is_none()),
assert!(test.get_output().is_none())
} }
} }
} }
@ -563,23 +554,21 @@ mod tests {
assert!(trusted_setup_file.exists()); assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap(); let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(VERIFY_BLOB_KZG_PROOF_TESTS) for test_file in glob::glob(VERIFY_BLOB_KZG_PROOF_TESTS).unwrap() {
.unwrap() let yaml_data = fs::read_to_string(test_file.unwrap()).unwrap();
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
let test: verify_blob_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap(); let test: verify_blob_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap();
let res = KZGProof::verify_blob_kzg_proof( let (Ok(blob), Ok(commitment), Ok(proof)) = (
test.input.get_blob(), test.input.get_blob(),
test.input.get_commitment(), test.input.get_commitment(),
test.input.get_proof(), test.input.get_proof()
&kzg_settings, ) else {
); assert!(test.get_output().is_none());
continue;
};
if res.is_ok() { match KZGProof::verify_blob_kzg_proof(*blob, commitment, proof, &kzg_settings) {
assert_eq!(res.unwrap(), test.get_output().unwrap()) Ok(res) => assert_eq!(res, test.get_output().unwrap()),
} else { _ => assert!(test.get_output().is_none()),
assert!(test.get_output().is_none())
} }
} }
} }
@ -591,23 +580,30 @@ mod tests {
assert!(trusted_setup_file.exists()); assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap(); let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS) for test_file in glob::glob(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS).unwrap() {
.unwrap() let yaml_data = fs::read_to_string(test_file.unwrap()).unwrap();
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
let test: verify_blob_kzg_proof_batch::Test = serde_yaml::from_str(&yaml_data).unwrap(); let test: verify_blob_kzg_proof_batch::Test = serde_yaml::from_str(&yaml_data).unwrap();
let res = KZGProof::verify_blob_kzg_proof_batch( let (Ok(blobs), Ok(commitments), Ok(proofs)) = (
test.input.get_blobs().as_slice(), test.input.get_blobs(),
test.input.get_commitments().as_slice(), test.input.get_commitments(),
test.input.get_proofs().as_slice(), test.input.get_proofs()
&kzg_settings, ) else {
); assert!(test.get_output().is_none());
continue;
};
if res.is_ok() { match KZGProof::verify_blob_kzg_proof_batch(
assert_eq!(res.unwrap(), test.get_output().unwrap()) blobs
} else { .into_iter()
assert!(test.get_output().is_none()) .map(|b| *b)
.collect::<Vec<Blob>>()
.as_slice(),
commitments.as_slice(),
proofs.as_slice(),
&kzg_settings,
) {
Ok(res) => assert_eq!(res, test.get_output().unwrap()),
_ => assert!(test.get_output().is_none()),
} }
} }
} }

View File

@ -1,7 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::Blob; use crate::{Blob, Bytes48, Error};
use crate::Bytes48;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -10,8 +9,10 @@ pub struct Input<'a> {
} }
impl Input<'_> { impl Input<'_> {
pub fn get_blob(&self) -> Blob { pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
Blob::from_bytes(&hex::decode(self.blob.replace("0x", "")).unwrap()).unwrap() let hex_str = self.blob.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Blob::from_bytes(&bytes)
} }
} }
@ -25,13 +26,9 @@ pub struct Test<'a> {
impl Test<'_> { impl Test<'_> {
pub fn get_output(&self) -> Option<Bytes48> { pub fn get_output(&self) -> Option<Bytes48> {
if self.output.is_some() { self.output
Some( .map(|s| s.replace("0x", ""))
Bytes48::from_bytes(&hex::decode(self.output.unwrap().replace("0x", "")).unwrap()) .map(|hex_str| hex::decode(hex_str).unwrap())
.unwrap(), .map(|bytes| Bytes48::from_bytes(&bytes).unwrap())
)
} else {
None
}
} }
} }

View File

@ -1,7 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::Blob; use crate::{Blob, Bytes48, Error};
use crate::Bytes48;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -10,8 +9,10 @@ pub struct Input<'a> {
} }
impl Input<'_> { impl Input<'_> {
pub fn get_blob(&self) -> Blob { pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
Blob::from_bytes(&hex::decode(self.blob.replace("0x", "")).unwrap()).unwrap() let hex_str = self.blob.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Blob::from_bytes(&bytes)
} }
} }
@ -25,13 +26,9 @@ pub struct Test<'a> {
impl Test<'_> { impl Test<'_> {
pub fn get_output(&self) -> Option<Bytes48> { pub fn get_output(&self) -> Option<Bytes48> {
if self.output.is_some() { self.output
Some( .map(|s| s.replace("0x", ""))
Bytes48::from_bytes(&hex::decode(self.output.unwrap().replace("0x", "")).unwrap()) .map(|hex_str| hex::decode(hex_str).unwrap())
.unwrap(), .map(|bytes| Bytes48::from_bytes(&bytes).unwrap())
)
} else {
None
}
} }
} }

View File

@ -1,8 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::Blob; use crate::{Blob, Bytes32, Bytes48, Error};
use crate::Bytes32;
use crate::Bytes48;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -12,12 +10,16 @@ pub struct Input<'a> {
} }
impl Input<'_> { impl Input<'_> {
pub fn get_blob(&self) -> Blob { pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
Blob::from_bytes(&hex::decode(self.blob.replace("0x", "")).unwrap()).unwrap() let hex_str = self.blob.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Blob::from_bytes(&bytes)
} }
pub fn get_z(&self) -> Bytes32 { pub fn get_z(&self) -> Result<Bytes32, Error> {
Bytes32::from_bytes(&hex::decode(self.z.replace("0x", "")).unwrap()).unwrap() let hex_str = self.z.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Bytes32::from_bytes(&bytes)
} }
} }
@ -31,13 +33,9 @@ pub struct Test<'a> {
impl Test<'_> { impl Test<'_> {
pub fn get_output(&self) -> Option<Bytes48> { pub fn get_output(&self) -> Option<Bytes48> {
if self.output.is_some() { self.output
Some( .map(|s| s.replace("0x", ""))
Bytes48::from_bytes(&hex::decode(self.output.unwrap().replace("0x", "")).unwrap()) .map(|hex_str| hex::decode(hex_str).unwrap())
.unwrap(), .map(|bytes| Bytes48::from_bytes(&bytes).unwrap())
)
} else {
None
}
} }
} }

View File

@ -1,7 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::Blob; use crate::{Blob, Bytes48, Error};
use crate::Bytes48;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -12,16 +11,22 @@ pub struct Input<'a> {
} }
impl Input<'_> { impl Input<'_> {
pub fn get_blob(&self) -> Blob { pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
Blob::from_bytes(&hex::decode(self.blob.replace("0x", "")).unwrap()).unwrap() let hex_str = self.blob.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Blob::from_bytes(&bytes)
} }
pub fn get_commitment(&self) -> Bytes48 { pub fn get_commitment(&self) -> Result<Bytes48, Error> {
Bytes48::from_bytes(&hex::decode(self.commitment.replace("0x", "")).unwrap()).unwrap() let hex_str = self.commitment.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
pub fn get_proof(&self) -> Bytes48 { pub fn get_proof(&self) -> Result<Bytes48, Error> {
Bytes48::from_bytes(&hex::decode(self.proof.replace("0x", "")).unwrap()).unwrap() let hex_str = self.proof.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
} }

View File

@ -1,7 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::Blob; use crate::{Blob, Bytes48, Error};
use crate::Bytes48;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -12,28 +11,31 @@ pub struct Input {
} }
impl Input { impl Input {
pub fn get_blobs(&self) -> Vec<Blob> { pub fn get_blobs(&self) -> Result<Vec<Box<Blob>>, Error> {
self.blobs self.blobs
.iter() .iter()
.map(|f| hex::decode(f.replace("0x", "")).unwrap()) .map(|s| s.replace("0x", ""))
.map(|bytes| Blob::from_bytes(bytes.as_slice()).unwrap()) .map(|hex_str| hex::decode(hex_str).unwrap())
.collect::<Vec<Blob>>() .map(|bytes| Blob::from_bytes(bytes.as_slice()))
.collect::<Result<Vec<Box<Blob>>, Error>>()
} }
pub fn get_commitments(&self) -> Vec<Bytes48> { pub fn get_commitments(&self) -> Result<Vec<Bytes48>, Error> {
self.commitments self.commitments
.iter() .iter()
.map(|f| hex::decode(f.replace("0x", "")).unwrap()) .map(|s| s.replace("0x", ""))
.map(|bytes| Bytes48::from_bytes(bytes.as_slice()).unwrap()) .map(|hex_str| hex::decode(hex_str).unwrap())
.collect::<Vec<Bytes48>>() .map(|bytes| Bytes48::from_bytes(bytes.as_slice()))
.collect::<Result<Vec<Bytes48>, Error>>()
} }
pub fn get_proofs(&self) -> Vec<Bytes48> { pub fn get_proofs(&self) -> Result<Vec<Bytes48>, Error> {
self.proofs self.proofs
.iter() .iter()
.map(|f| hex::decode(f.replace("0x", "")).unwrap()) .map(|s| s.replace("0x", ""))
.map(|bytes| Bytes48::from_bytes(bytes.as_slice()).unwrap()) .map(|hex_str| hex::decode(hex_str).unwrap())
.collect::<Vec<Bytes48>>() .map(|bytes| Bytes48::from_bytes(bytes.as_slice()))
.collect::<Result<Vec<Bytes48>, Error>>()
} }
} }

View File

@ -1,7 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::Bytes32; use crate::{Bytes32, Bytes48, Error};
use crate::Bytes48;
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -13,20 +12,28 @@ pub struct Input<'a> {
} }
impl Input<'_> { impl Input<'_> {
pub fn get_commitment(&self) -> Bytes48 { pub fn get_commitment(&self) -> Result<Bytes48, Error> {
Bytes48::from_bytes(&hex::decode(self.commitment.replace("0x", "")).unwrap()).unwrap() let hex_str = self.commitment.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
pub fn get_z(&self) -> Bytes32 { pub fn get_z(&self) -> Result<Bytes32, Error> {
Bytes32::from_bytes(&hex::decode(self.z.replace("0x", "")).unwrap()).unwrap() let hex_str = self.z.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Bytes32::from_bytes(&bytes)
} }
pub fn get_y(&self) -> Bytes32 { pub fn get_y(&self) -> Result<Bytes32, Error> {
Bytes32::from_bytes(&hex::decode(self.y.replace("0x", "")).unwrap()).unwrap() let hex_str = self.y.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Bytes32::from_bytes(&bytes)
} }
pub fn get_proof(&self) -> Bytes48 { pub fn get_proof(&self) -> Result<Bytes48, Error> {
Bytes48::from_bytes(&hex::decode(self.proof.replace("0x", "")).unwrap()).unwrap() let hex_str = self.proof.replace("0x", "");
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
} }