Cleanup some rust tests (#195)

This commit is contained in:
Justin Traglia 2023-03-10 03:42:17 -06:00 committed by GitHub
parent 4211d4b427
commit da83e45e9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 69 deletions

View File

@ -122,6 +122,10 @@ impl Blob {
new_bytes.copy_from_slice(bytes); new_bytes.copy_from_slice(bytes);
Ok(Self { bytes: new_bytes }) Ok(Self { bytes: new_bytes })
} }
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
Self::from_bytes(&hex::decode(&hex_str[2..]).unwrap())
}
} }
impl Bytes32 { impl Bytes32 {
@ -137,6 +141,10 @@ impl Bytes32 {
new_bytes.copy_from_slice(bytes); new_bytes.copy_from_slice(bytes);
Ok(Self { bytes: new_bytes }) Ok(Self { bytes: new_bytes })
} }
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
Self::from_bytes(&hex::decode(&hex_str[2..]).unwrap())
}
} }
impl Bytes48 { impl Bytes48 {
@ -152,6 +160,10 @@ impl Bytes48 {
new_bytes.copy_from_slice(bytes); new_bytes.copy_from_slice(bytes);
Ok(Self { bytes: new_bytes }) Ok(Self { bytes: new_bytes })
} }
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
Self::from_bytes(&hex::decode(&hex_str[2..]).unwrap())
}
} }
impl KZGProof { impl KZGProof {

View File

@ -10,9 +10,7 @@ pub struct Input<'a> {
impl Input<'_> { impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> { pub fn get_blob(&self) -> Result<Blob, Error> {
let hex_str = self.blob.replace("0x", ""); Blob::from_hex(self.blob)
let bytes = hex::decode(hex_str).unwrap();
Blob::from_bytes(&bytes)
} }
} }
@ -26,9 +24,6 @@ pub struct Test<'a> {
impl Test<'_> { impl Test<'_> {
pub fn get_output(&self) -> Option<Bytes48> { pub fn get_output(&self) -> Option<Bytes48> {
self.output self.output.map(|s| Bytes48::from_hex(s).unwrap())
.map(|s| s.replace("0x", ""))
.map(|hex_str| hex::decode(hex_str).unwrap())
.map(|bytes| Bytes48::from_bytes(&bytes).unwrap())
} }
} }

View File

@ -11,15 +11,11 @@ pub struct Input<'a> {
impl Input<'_> { impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> { pub fn get_blob(&self) -> Result<Blob, Error> {
let hex_str = self.blob.replace("0x", ""); Blob::from_hex(self.blob)
let bytes = hex::decode(hex_str).unwrap();
Blob::from_bytes(&bytes)
} }
pub fn get_commitment(&self) -> Result<Bytes48, Error> { pub fn get_commitment(&self) -> Result<Bytes48, Error> {
let hex_str = self.commitment.replace("0x", ""); Bytes48::from_hex(self.commitment)
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
} }
@ -33,9 +29,6 @@ pub struct Test<'a> {
impl Test<'_> { impl Test<'_> {
pub fn get_output(&self) -> Option<Bytes48> { pub fn get_output(&self) -> Option<Bytes48> {
self.output self.output.map(|s| Bytes48::from_hex(s).unwrap())
.map(|s| s.replace("0x", ""))
.map(|hex_str| hex::decode(hex_str).unwrap())
.map(|bytes| Bytes48::from_bytes(&bytes).unwrap())
} }
} }

View File

@ -11,15 +11,11 @@ pub struct Input<'a> {
impl Input<'_> { impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> { pub fn get_blob(&self) -> Result<Blob, Error> {
let hex_str = self.blob.replace("0x", ""); Blob::from_hex(self.blob)
let bytes = hex::decode(hex_str).unwrap();
Blob::from_bytes(&bytes)
} }
pub fn get_z(&self) -> Result<Bytes32, Error> { pub fn get_z(&self) -> Result<Bytes32, Error> {
let hex_str = self.z.replace("0x", ""); Bytes32::from_hex(self.z)
let bytes = hex::decode(hex_str).unwrap();
Bytes32::from_bytes(&bytes)
} }
} }
@ -33,18 +29,11 @@ pub struct Test<'a> {
impl Test<'_> { impl Test<'_> {
pub fn get_output(&self) -> Option<(Bytes48, Bytes32)> { pub fn get_output(&self) -> Option<(Bytes48, Bytes32)> {
if self.output.is_none() { self.output.map(|(proof, y)| {
return None; (
} Bytes48::from_hex(proof).unwrap(),
Bytes32::from_hex(y).unwrap(),
let proof_hex = self.output.as_ref().unwrap().0.replace("0x", ""); )
let proof_bytes = hex::decode(proof_hex).unwrap(); })
let proof = Bytes48::from_bytes(&proof_bytes).unwrap();
let z_hex = self.output.as_ref().unwrap().1.replace("0x", "");
let z_bytes = hex::decode(z_hex).unwrap();
let z = Bytes32::from_bytes(&z_bytes).unwrap();
Some((proof, z))
} }
} }

View File

@ -12,21 +12,15 @@ pub struct Input<'a> {
impl Input<'_> { impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> { pub fn get_blob(&self) -> Result<Blob, Error> {
let hex_str = self.blob.replace("0x", ""); Blob::from_hex(self.blob)
let bytes = hex::decode(hex_str).unwrap();
Blob::from_bytes(&bytes)
} }
pub fn get_commitment(&self) -> Result<Bytes48, Error> { pub fn get_commitment(&self) -> Result<Bytes48, Error> {
let hex_str = self.commitment.replace("0x", ""); Bytes48::from_hex(self.commitment)
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
pub fn get_proof(&self) -> Result<Bytes48, Error> { pub fn get_proof(&self) -> Result<Bytes48, Error> {
let hex_str = self.proof.replace("0x", ""); Bytes48::from_hex(self.proof)
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
} }

View File

@ -13,32 +13,23 @@ pub struct Input {
impl Input { impl Input {
pub fn get_blobs(&self) -> Result<Vec<Blob>, Error> { pub fn get_blobs(&self) -> Result<Vec<Blob>, Error> {
let mut v: Vec<Blob> = Vec::new(); let mut v: Vec<Blob> = Vec::new();
for blob in &self.blobs { for blob in &self.blobs {
let blob_hex = blob.replace("0x", ""); v.push(Blob::from_hex(blob)?);
let blob_bytes = hex::decode(blob_hex).unwrap();
let b = Blob::from_bytes(blob_bytes.as_slice())?;
v.push(b);
} }
return Ok(v); return Ok(v);
} }
pub fn get_commitments(&self) -> Result<Vec<Bytes48>, Error> { pub fn get_commitments(&self) -> Result<Vec<Bytes48>, Error> {
self.commitments self.commitments
.iter() .iter()
.map(|s| s.replace("0x", "")) .map(|s| Bytes48::from_hex(s))
.map(|hex_str| hex::decode(hex_str).unwrap())
.map(|bytes| Bytes48::from_bytes(bytes.as_slice()))
.collect::<Result<Vec<Bytes48>, Error>>() .collect::<Result<Vec<Bytes48>, Error>>()
} }
pub fn get_proofs(&self) -> Result<Vec<Bytes48>, Error> { pub fn get_proofs(&self) -> Result<Vec<Bytes48>, Error> {
self.proofs self.proofs
.iter() .iter()
.map(|s| s.replace("0x", "")) .map(|s| Bytes48::from_hex(s))
.map(|hex_str| hex::decode(hex_str).unwrap())
.map(|bytes| Bytes48::from_bytes(bytes.as_slice()))
.collect::<Result<Vec<Bytes48>, Error>>() .collect::<Result<Vec<Bytes48>, Error>>()
} }
} }

View File

@ -13,27 +13,19 @@ pub struct Input<'a> {
impl Input<'_> { impl Input<'_> {
pub fn get_commitment(&self) -> Result<Bytes48, Error> { pub fn get_commitment(&self) -> Result<Bytes48, Error> {
let hex_str = self.commitment.replace("0x", ""); Bytes48::from_hex(self.commitment)
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
pub fn get_z(&self) -> Result<Bytes32, Error> { pub fn get_z(&self) -> Result<Bytes32, Error> {
let hex_str = self.z.replace("0x", ""); Bytes32::from_hex(self.z)
let bytes = hex::decode(hex_str).unwrap();
Bytes32::from_bytes(&bytes)
} }
pub fn get_y(&self) -> Result<Bytes32, Error> { pub fn get_y(&self) -> Result<Bytes32, Error> {
let hex_str = self.y.replace("0x", ""); Bytes32::from_hex(self.y)
let bytes = hex::decode(hex_str).unwrap();
Bytes32::from_bytes(&bytes)
} }
pub fn get_proof(&self) -> Result<Bytes48, Error> { pub fn get_proof(&self) -> Result<Bytes48, Error> {
let hex_str = self.proof.replace("0x", ""); Bytes48::from_hex(self.proof)
let bytes = hex::decode(hex_str).unwrap();
Bytes48::from_bytes(&bytes)
} }
} }