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 {
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 {
return Err(Error::InvalidBytesLength(format!(
"Invalid byte length. Expected {} got {}",
@ -120,7 +120,7 @@ impl Blob {
}
let mut new_bytes = [0; BYTES_PER_BLOB];
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());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(BLOB_TO_KZG_COMMITMENT_TESTS)
.unwrap()
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
for test_file in glob::glob(BLOB_TO_KZG_COMMITMENT_TESTS).unwrap() {
let yaml_data = fs::read_to_string(test_file.unwrap()).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() {
assert_eq!(res.unwrap().bytes, test.get_output().unwrap().bytes)
} else {
assert!(test.get_output().is_none())
match KZGCommitment::blob_to_kzg_commitment(*blob, &kzg_settings) {
Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
_ => assert!(test.get_output().is_none()),
}
}
}
@ -484,22 +483,17 @@ mod tests {
assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(COMPUTE_KZG_PROOF_TESTS)
.unwrap()
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
for test_file in glob::glob(COMPUTE_KZG_PROOF_TESTS).unwrap() {
let yaml_data = fs::read_to_string(test_file.unwrap()).unwrap();
let test: compute_kzg_proof::Test = serde_yaml::from_str(&yaml_data).unwrap();
let res = KZGProof::compute_kzg_proof(
test.input.get_blob(),
test.input.get_z(),
&kzg_settings,
);
let (Ok(blob), Ok(z)) = (test.input.get_blob(), test.input.get_z()) else {
assert!(test.get_output().is_none());
continue;
};
if res.is_ok() {
assert_eq!(res.unwrap().bytes, test.get_output().unwrap().bytes)
} else {
assert!(test.get_output().is_none())
match KZGProof::compute_kzg_proof(*blob, z, &kzg_settings) {
Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
_ => assert!(test.get_output().is_none()),
}
}
}
@ -511,18 +505,17 @@ mod tests {
assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(COMPUTE_BLOB_KZG_PROOF_TESTS)
.unwrap()
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
for test_file in glob::glob(COMPUTE_BLOB_KZG_PROOF_TESTS).unwrap() {
let yaml_data = fs::read_to_string(test_file.unwrap()).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() {
assert_eq!(res.unwrap().bytes, test.get_output().unwrap().bytes)
} else {
assert!(test.get_output().is_none())
match KZGProof::compute_blob_kzg_proof(*blob, &kzg_settings) {
Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
_ => assert!(test.get_output().is_none()),
}
}
}
@ -534,24 +527,22 @@ mod tests {
assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(VERIFY_KZG_PROOF_TESTS)
.unwrap()
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
for test_file in glob::glob(VERIFY_KZG_PROOF_TESTS).unwrap() {
let yaml_data = fs::read_to_string(test_file.unwrap()).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_z(),
test.input.get_y(),
test.input.get_proof(),
&kzg_settings,
);
test.input.get_proof()
) else {
assert!(test.get_output().is_none());
continue;
};
if res.is_ok() {
assert_eq!(res.unwrap(), test.get_output().unwrap())
} else {
assert!(test.get_output().is_none())
match KZGProof::verify_kzg_proof(commitment, z, y, proof, &kzg_settings) {
Ok(res) => assert_eq!(res, test.get_output().unwrap()),
_ => assert!(test.get_output().is_none()),
}
}
}
@ -563,23 +554,21 @@ mod tests {
assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(VERIFY_BLOB_KZG_PROOF_TESTS)
.unwrap()
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
for test_file in glob::glob(VERIFY_BLOB_KZG_PROOF_TESTS).unwrap() {
let yaml_data = fs::read_to_string(test_file.unwrap()).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_commitment(),
test.input.get_proof(),
&kzg_settings,
);
test.input.get_proof()
) else {
assert!(test.get_output().is_none());
continue;
};
if res.is_ok() {
assert_eq!(res.unwrap(), test.get_output().unwrap())
} else {
assert!(test.get_output().is_none())
match KZGProof::verify_blob_kzg_proof(*blob, commitment, proof, &kzg_settings) {
Ok(res) => assert_eq!(res, test.get_output().unwrap()),
_ => assert!(test.get_output().is_none()),
}
}
}
@ -591,23 +580,30 @@ mod tests {
assert!(trusted_setup_file.exists());
let kzg_settings = KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap();
let tests = glob::glob(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS)
.unwrap()
.map(|t| t.unwrap());
for test_file in tests {
let yaml_data = fs::read_to_string(test_file).unwrap();
for test_file in glob::glob(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS).unwrap() {
let yaml_data = fs::read_to_string(test_file.unwrap()).unwrap();
let test: verify_blob_kzg_proof_batch::Test = serde_yaml::from_str(&yaml_data).unwrap();
let res = KZGProof::verify_blob_kzg_proof_batch(
test.input.get_blobs().as_slice(),
test.input.get_commitments().as_slice(),
test.input.get_proofs().as_slice(),
&kzg_settings,
);
let (Ok(blobs), Ok(commitments), Ok(proofs)) = (
test.input.get_blobs(),
test.input.get_commitments(),
test.input.get_proofs()
) else {
assert!(test.get_output().is_none());
continue;
};
if res.is_ok() {
assert_eq!(res.unwrap(), test.get_output().unwrap())
} else {
assert!(test.get_output().is_none())
match KZGProof::verify_blob_kzg_proof_batch(
blobs
.into_iter()
.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)]
use crate::Blob;
use crate::Bytes48;
use crate::{Blob, Bytes48, Error};
use serde::Deserialize;
#[derive(Deserialize)]
@ -10,8 +9,10 @@ pub struct Input<'a> {
}
impl Input<'_> {
pub fn get_blob(&self) -> Blob {
Blob::from_bytes(&hex::decode(self.blob.replace("0x", "")).unwrap()).unwrap()
pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
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<'_> {
pub fn get_output(&self) -> Option<Bytes48> {
if self.output.is_some() {
Some(
Bytes48::from_bytes(&hex::decode(self.output.unwrap().replace("0x", "")).unwrap())
.unwrap(),
)
} else {
None
}
self.output
.map(|s| s.replace("0x", ""))
.map(|hex_str| hex::decode(hex_str).unwrap())
.map(|bytes| Bytes48::from_bytes(&bytes).unwrap())
}
}

View File

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

View File

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

View File

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

View File

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