use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Groth16Proof {
pub data: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PublicInputs {
pub model_hash: [u8; 32],
pub input_hash: [u8; 32],
pub output: Vec<u8>,
pub class_label: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationBundle {
pub proof: Groth16Proof,
pub public_inputs: PublicInputs,
}
impl PublicInputs {
pub fn to_bytes(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(64 + self.output.len() + 8);
out.extend_from_slice(&self.model_hash);
out.extend_from_slice(&self.input_hash);
out.extend_from_slice(&self.output);
out.extend_from_slice(&self.class_label.to_le_bytes());
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn public_inputs_serialize_with_prefix() {
let pi = PublicInputs {
model_hash: [1u8; 32],
input_hash: [2u8; 32],
output: vec![9u8; 8],
class_label: 0,
};
let bytes = pi.to_bytes();
assert_eq!(bytes.len(), 80);
assert_eq!(&bytes[0..32], &[1u8; 32]);
assert_eq!(&bytes[32..64], &[2u8; 32]);
assert_eq!(&bytes[64..72], &[9u8; 8]);
assert_eq!(&bytes[72..80], &[0u8; 8]);
}
#[test]
fn groth16_proof_size_target() {
let proof = Groth16Proof {
data: vec![0u8; 256],
};
assert!(
proof.data.len() < 500,
"Groth16 proof size {} exceeds 500 byte target",
proof.data.len()
);
}
#[test]
fn groth16_proof_round_trip_serialization() {
let original = Groth16Proof {
data: vec![
1u8; 256 ],
};
let serialized = bincode::serialize(&original).expect("Groth16Proof serialization failed");
let deserialized: Groth16Proof =
bincode::deserialize(&serialized).expect("Groth16Proof deserialization failed");
assert_eq!(original.data, deserialized.data);
}
#[test]
fn verification_bundle_round_trip_serialization() {
let bundle = VerificationBundle {
proof: Groth16Proof {
data: vec![1u8; 256],
},
public_inputs: PublicInputs {
model_hash: [1u8; 32],
input_hash: [2u8; 32],
output: vec![3u8; 8],
class_label: 4,
},
};
let serialized =
bincode::serialize(&bundle).expect("VerificationBundle serialization failed");
let deserialized: VerificationBundle =
bincode::deserialize(&serialized).expect("VerificationBundle deserialization failed");
assert_eq!(bundle.proof.data, deserialized.proof.data);
assert_eq!(
bundle.public_inputs.model_hash,
deserialized.public_inputs.model_hash
);
assert_eq!(
bundle.public_inputs.input_hash,
deserialized.public_inputs.input_hash
);
assert_eq!(
bundle.public_inputs.output,
deserialized.public_inputs.output
);
}
}