use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct PedersenCommitment {
pub commitment: String,
#[serde(default, skip_serializing)]
pub blinding_hint: String,
}
impl PedersenCommitment {
const MAX_COMMITMENT_LEN: usize = 128;
const MAX_BLINDING_HINT_LEN: usize = 128;
pub fn validate(&self) -> Result<(), String> {
if self.commitment.len() > Self::MAX_COMMITMENT_LEN {
return Err(format!(
"PedersenCommitment commitment length {} exceeds max {}",
self.commitment.len(),
Self::MAX_COMMITMENT_LEN,
));
}
if crate::core::has_dangerous_chars(&self.commitment) {
return Err(
"PedersenCommitment commitment contains control or format characters".to_string(),
);
}
if self.blinding_hint.len() > Self::MAX_BLINDING_HINT_LEN {
return Err(format!(
"PedersenCommitment blinding_hint length {} exceeds max {}",
self.blinding_hint.len(),
Self::MAX_BLINDING_HINT_LEN,
));
}
if crate::core::has_dangerous_chars(&self.blinding_hint) {
return Err(
"PedersenCommitment blinding_hint contains control or format characters"
.to_string(),
);
}
Ok(())
}
}
impl fmt::Debug for PedersenCommitment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PedersenCommitment")
.field("commitment", &self.commitment)
.field("blinding_hint", &"[REDACTED]")
.finish()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ZkBatchProof {
pub proof: String,
pub batch_id: String,
pub entry_range: (u64, u64),
pub merkle_root: String,
pub first_prev_hash: String,
pub final_entry_hash: String,
pub created_at: String,
pub entry_count: usize,
}
impl ZkBatchProof {
const MAX_PROOF_LEN: usize = 65_536;
const MAX_BATCH_ID_LEN: usize = 256;
const MAX_HASH_LEN: usize = 256;
const MAX_TIMESTAMP_LEN: usize = 64;
const MAX_ENTRY_COUNT: usize = 10_000;
pub fn validate(&self) -> Result<(), String> {
if self.proof.len() > Self::MAX_PROOF_LEN {
return Err(format!(
"ZkBatchProof proof length {} exceeds max {}",
self.proof.len(),
Self::MAX_PROOF_LEN,
));
}
if crate::core::has_dangerous_chars(&self.proof) {
return Err("ZkBatchProof proof contains control or format characters".to_string());
}
if self.batch_id.len() > Self::MAX_BATCH_ID_LEN {
return Err(format!(
"ZkBatchProof batch_id length {} exceeds max {}",
self.batch_id.len(),
Self::MAX_BATCH_ID_LEN,
));
}
if crate::core::has_dangerous_chars(&self.batch_id) {
return Err("ZkBatchProof batch_id contains control or format characters".to_string());
}
if self.merkle_root.len() > Self::MAX_HASH_LEN {
return Err(format!(
"ZkBatchProof merkle_root length {} exceeds max {}",
self.merkle_root.len(),
Self::MAX_HASH_LEN,
));
}
if crate::core::has_dangerous_chars(&self.merkle_root) {
return Err(
"ZkBatchProof merkle_root contains control or format characters".to_string(),
);
}
if self.first_prev_hash.len() > Self::MAX_HASH_LEN {
return Err(format!(
"ZkBatchProof first_prev_hash length {} exceeds max {}",
self.first_prev_hash.len(),
Self::MAX_HASH_LEN,
));
}
if crate::core::has_dangerous_chars(&self.first_prev_hash) {
return Err(
"ZkBatchProof first_prev_hash contains control or format characters".to_string(),
);
}
if self.final_entry_hash.len() > Self::MAX_HASH_LEN {
return Err(format!(
"ZkBatchProof final_entry_hash length {} exceeds max {}",
self.final_entry_hash.len(),
Self::MAX_HASH_LEN,
));
}
if crate::core::has_dangerous_chars(&self.final_entry_hash) {
return Err(
"ZkBatchProof final_entry_hash contains control or format characters".to_string(),
);
}
if self.created_at.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"ZkBatchProof created_at length {} exceeds max {}",
self.created_at.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
if crate::core::has_dangerous_chars(&self.created_at) {
return Err(
"ZkBatchProof created_at contains control or format characters".to_string(),
);
}
if self.entry_range.0 > self.entry_range.1 {
return Err(format!(
"ZkBatchProof entry_range start {} exceeds end {}",
self.entry_range.0, self.entry_range.1,
));
}
if self.entry_count > Self::MAX_ENTRY_COUNT {
return Err(format!(
"ZkBatchProof entry_count {} exceeds max {}",
self.entry_count,
Self::MAX_ENTRY_COUNT,
));
}
let expected_count =
usize::try_from((self.entry_range.1 - self.entry_range.0).saturating_add(1))
.unwrap_or(usize::MAX);
if self.entry_count != expected_count {
return Err(format!(
"ZkBatchProof entry_count {} does not match entry_range span {} (range {}-{})",
self.entry_count, expected_count, self.entry_range.0, self.entry_range.1,
));
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ZkVerifyResult {
pub valid: bool,
pub batch_id: String,
pub entry_range: (u64, u64),
pub verified_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
impl ZkVerifyResult {
const MAX_BATCH_ID_LEN: usize = 256;
const MAX_TIMESTAMP_LEN: usize = 64;
const MAX_ERROR_LEN: usize = 4096;
pub fn validate(&self) -> Result<(), String> {
if self.batch_id.len() > Self::MAX_BATCH_ID_LEN {
return Err(format!(
"ZkVerifyResult batch_id length {} exceeds max {}",
self.batch_id.len(),
Self::MAX_BATCH_ID_LEN,
));
}
if crate::core::has_dangerous_chars(&self.batch_id) {
return Err(
"ZkVerifyResult batch_id contains control or format characters".to_string(),
);
}
if self.verified_at.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"ZkVerifyResult verified_at length {} exceeds max {}",
self.verified_at.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
if crate::core::has_dangerous_chars(&self.verified_at) {
return Err(
"ZkVerifyResult verified_at contains control or format characters".to_string(),
);
}
if let Some(ref err) = self.error {
if err.len() > Self::MAX_ERROR_LEN {
return Err(format!(
"ZkVerifyResult error length {} exceeds max {}",
err.len(),
Self::MAX_ERROR_LEN,
));
}
if crate::core::has_dangerous_chars(err) {
return Err(
"ZkVerifyResult error contains control or format characters".to_string()
);
}
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ZkSchedulerStatus {
pub active: bool,
pub pending_witnesses: usize,
pub completed_proofs: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_proved_sequence: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_proof_at: Option<String>,
}
impl ZkSchedulerStatus {
const MAX_TIMESTAMP_LEN: usize = 64;
pub fn validate(&self) -> Result<(), String> {
if let Some(ref ts) = self.last_proof_at {
if ts.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"ZkSchedulerStatus last_proof_at length {} exceeds max {}",
ts.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
if crate::core::has_dangerous_chars(ts) {
return Err(
"ZkSchedulerStatus last_proof_at contains control or format characters"
.to_string(),
);
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn valid_batch_proof() -> ZkBatchProof {
ZkBatchProof {
proof: "deadbeef".to_string(),
batch_id: "batch-001".to_string(),
entry_range: (1, 10),
merkle_root: "aabbccdd".to_string(),
first_prev_hash: "00001111".to_string(),
final_entry_hash: "ffffeeee".to_string(),
created_at: "2025-06-01T12:00:00Z".to_string(),
entry_count: 10,
}
}
#[test]
fn test_pedersen_commitment_validate_valid_ok() {
let pc = PedersenCommitment {
commitment: "a".repeat(64),
blinding_hint: "b".repeat(64),
};
assert!(pc.validate().is_ok());
}
#[test]
fn test_pedersen_commitment_validate_commitment_too_long_rejected() {
let pc = PedersenCommitment {
commitment: "x".repeat(129),
blinding_hint: String::new(),
};
let err = pc.validate().unwrap_err();
assert!(err.contains("commitment length"), "got: {err}");
}
#[test]
fn test_pedersen_commitment_validate_blinding_hint_too_long_rejected() {
let pc = PedersenCommitment {
commitment: "ok".to_string(),
blinding_hint: "y".repeat(129),
};
let err = pc.validate().unwrap_err();
assert!(err.contains("blinding_hint length"), "got: {err}");
}
#[test]
fn test_pedersen_commitment_validate_commitment_control_chars_rejected() {
let pc = PedersenCommitment {
commitment: "abc\x00def".to_string(),
blinding_hint: String::new(),
};
let err = pc.validate().unwrap_err();
assert!(err.contains("commitment contains control"), "got: {err}");
}
#[test]
fn test_pedersen_commitment_validate_blinding_hint_control_chars_rejected() {
let pc = PedersenCommitment {
commitment: "ok".to_string(),
blinding_hint: "hint\x07bell".to_string(),
};
let err = pc.validate().unwrap_err();
assert!(err.contains("blinding_hint contains control"), "got: {err}");
}
#[test]
fn test_pedersen_commitment_debug_redacts_blinding_hint() {
let pc = PedersenCommitment {
commitment: "abc123".to_string(),
blinding_hint: "supersecret".to_string(),
};
let debug_str = format!("{pc:?}");
assert!(debug_str.contains("[REDACTED]"));
assert!(!debug_str.contains("supersecret"));
}
#[test]
fn test_zk_batch_proof_validate_valid_ok() {
assert!(valid_batch_proof().validate().is_ok());
}
#[test]
fn test_zk_batch_proof_validate_proof_too_long_rejected() {
let mut bp = valid_batch_proof();
bp.proof = "p".repeat(65_537);
let err = bp.validate().unwrap_err();
assert!(err.contains("proof length"), "got: {err}");
}
#[test]
fn test_zk_batch_proof_validate_proof_control_chars_rejected() {
let mut bp = valid_batch_proof();
bp.proof = "abc\x01def".to_string();
let err = bp.validate().unwrap_err();
assert!(err.contains("proof contains control"), "got: {err}");
}
#[test]
fn test_zk_batch_proof_validate_batch_id_control_chars_rejected() {
let mut bp = valid_batch_proof();
bp.batch_id = "id\x00null".to_string();
let err = bp.validate().unwrap_err();
assert!(err.contains("batch_id contains control"), "got: {err}");
}
#[test]
fn test_zk_batch_proof_validate_entry_range_inverted_rejected() {
let mut bp = valid_batch_proof();
bp.entry_range = (10, 1);
let err = bp.validate().unwrap_err();
assert!(err.contains("entry_range start"), "got: {err}");
}
#[test]
fn test_zk_batch_proof_validate_entry_count_too_large_rejected() {
let mut bp = valid_batch_proof();
bp.entry_count = 10_001;
bp.entry_range = (1, 10_001);
let err = bp.validate().unwrap_err();
assert!(err.contains("entry_count"), "got: {err}");
}
#[test]
fn test_zk_batch_proof_validate_entry_count_mismatch_rejected() {
let mut bp = valid_batch_proof();
bp.entry_count = 5; let err = bp.validate().unwrap_err();
assert!(
err.contains("does not match entry_range span"),
"got: {err}"
);
}
#[test]
fn test_zk_batch_proof_validate_merkle_root_too_long_rejected() {
let mut bp = valid_batch_proof();
bp.merkle_root = "m".repeat(257);
let err = bp.validate().unwrap_err();
assert!(err.contains("merkle_root length"), "got: {err}");
}
#[test]
fn test_zk_batch_proof_validate_hash_fields_control_chars_rejected() {
let mut bp = valid_batch_proof();
bp.first_prev_hash = "hash\x02bad".to_string();
assert!(bp
.validate()
.unwrap_err()
.contains("first_prev_hash contains control"));
let mut bp2 = valid_batch_proof();
bp2.final_entry_hash = "hash\x03bad".to_string();
assert!(bp2
.validate()
.unwrap_err()
.contains("final_entry_hash contains control"));
let mut bp3 = valid_batch_proof();
bp3.merkle_root = "root\x04bad".to_string();
assert!(bp3
.validate()
.unwrap_err()
.contains("merkle_root contains control"));
}
#[test]
fn test_zk_verify_result_validate_valid_ok() {
let vr = ZkVerifyResult {
valid: true,
batch_id: "batch-001".to_string(),
entry_range: (1, 10),
verified_at: "2025-06-01T12:00:00Z".to_string(),
error: None,
};
assert!(vr.validate().is_ok());
}
#[test]
fn test_zk_verify_result_validate_error_too_long_rejected() {
let vr = ZkVerifyResult {
valid: false,
batch_id: "b1".to_string(),
entry_range: (1, 1),
verified_at: "2025-01-01T00:00:00Z".to_string(),
error: Some("e".repeat(4097)),
};
assert!(vr.validate().unwrap_err().contains("error length"));
}
#[test]
fn test_zk_verify_result_validate_error_control_chars_rejected() {
let vr = ZkVerifyResult {
valid: false,
batch_id: "b1".to_string(),
entry_range: (1, 1),
verified_at: "2025-01-01T00:00:00Z".to_string(),
error: Some("err\x00msg".to_string()),
};
assert!(vr
.validate()
.unwrap_err()
.contains("error contains control"));
}
#[test]
fn test_zk_scheduler_status_validate_valid_ok() {
let ss = ZkSchedulerStatus {
active: true,
pending_witnesses: 5,
completed_proofs: 10,
last_proved_sequence: Some(100),
last_proof_at: Some("2025-06-01T12:00:00Z".to_string()),
};
assert!(ss.validate().is_ok());
}
#[test]
fn test_zk_scheduler_status_validate_timestamp_too_long_rejected() {
let ss = ZkSchedulerStatus {
active: false,
pending_witnesses: 0,
completed_proofs: 0,
last_proved_sequence: None,
last_proof_at: Some("t".repeat(65)),
};
assert!(ss.validate().unwrap_err().contains("last_proof_at length"));
}
#[test]
fn test_zk_scheduler_status_validate_timestamp_control_chars_rejected() {
let ss = ZkSchedulerStatus {
active: false,
pending_witnesses: 0,
completed_proofs: 0,
last_proved_sequence: None,
last_proof_at: Some("2025-01-01\x00T00:00:00Z".to_string()),
};
assert!(ss.validate().unwrap_err().contains("control or format"));
}
}