use krusty_kms_common::{
AuditProof, ElGamalCiphertext, ElGamalProof, Poe2Proof, PoeProof, ProofOfBit, ProofOfTransfer,
Range, Result,
};
use starknet_types_core::curve::ProjectivePoint;
use starknet_types_core::felt::Felt;
pub fn serialize_projective_point(point: &ProjectivePoint) -> Result<(Felt, Felt)> {
let affine = point
.to_affine()
.map_err(|_| krusty_kms_common::KmsError::CryptoError("Invalid point".to_string()))?;
Ok((affine.x(), affine.y()))
}
pub fn deserialize_projective_point(x: Felt, y: Felt) -> Result<ProjectivePoint> {
ProjectivePoint::from_affine(x, y).map_err(|_| {
krusty_kms_common::KmsError::CryptoError("Invalid point coordinates".to_string())
})
}
pub fn serialize_poe_proof(proof: &PoeProof) -> Result<Vec<Felt>> {
let a_x = Felt::from_hex(&proof.a.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid point x: {}", e)))?;
let a_y = Felt::from_hex(&proof.a.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid point y: {}", e)))?;
let s_felt = Felt::from_hex(&proof.s).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid s scalar: {}", e))
})?;
Ok(vec![a_x, a_y, s_felt])
}
pub fn serialize_poe2_proof(proof: &Poe2Proof) -> Result<Vec<Felt>> {
let a_x = Felt::from_hex(&proof.a.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid point x: {}", e)))?;
let a_y = Felt::from_hex(&proof.a.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid point y: {}", e)))?;
let s1_felt = Felt::from_hex(&proof.s1).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid s1 scalar: {}", e))
})?;
let s2_felt = Felt::from_hex(&proof.s2).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid s2 scalar: {}", e))
})?;
Ok(vec![a_x, a_y, s1_felt, s2_felt])
}
pub fn serialize_elgamal_proof(proof: &ElGamalProof) -> Result<Vec<Felt>> {
let al_x = Felt::from_hex(&proof.al.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AL.x: {}", e)))?;
let al_y = Felt::from_hex(&proof.al.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AL.y: {}", e)))?;
let ar_x = Felt::from_hex(&proof.ar.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AR.x: {}", e)))?;
let ar_y = Felt::from_hex(&proof.ar.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AR.y: {}", e)))?;
let sb_felt = Felt::from_hex(&proof.sb).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid sb scalar: {}", e))
})?;
let sr_felt = Felt::from_hex(&proof.sr).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid sr scalar: {}", e))
})?;
Ok(vec![al_x, al_y, ar_x, ar_y, sb_felt, sr_felt])
}
pub fn u128_to_u256(value: u128) -> (Felt, Felt) {
(Felt::from(value), Felt::ZERO)
}
pub fn u256_to_u128(low: Felt, high: Felt) -> Result<u128> {
if high != Felt::ZERO {
return Err(krusty_kms_common::KmsError::CryptoError(
"Value too large for u128".to_string(),
));
}
let bytes = low.to_bytes_be();
let mut u128_bytes = [0u8; 16];
u128_bytes.copy_from_slice(&bytes[16..32]);
Ok(u128::from_be_bytes(u128_bytes))
}
pub fn serialize_ae_balance(ciphertext_bytes: &[u8], nonce_bytes: &[u8]) -> Result<Vec<Felt>> {
if ciphertext_bytes.len() != 64 {
return Err(krusty_kms_common::KmsError::CryptoError(format!(
"Ciphertext must be 64 bytes, got {}",
ciphertext_bytes.len()
)));
}
if nonce_bytes.len() != 24 {
return Err(krusty_kms_common::KmsError::CryptoError(format!(
"Nonce must be 24 bytes, got {}",
nonce_bytes.len()
)));
}
let ct_felts = bytes_to_u512(ciphertext_bytes);
let mut nonce_padded = [0u8; 32];
nonce_padded[..24].copy_from_slice(nonce_bytes);
let nonce_felts = bytes_to_u256(&nonce_padded);
Ok(vec![
ct_felts.0,
ct_felts.1,
ct_felts.2,
ct_felts.3,
nonce_felts.0,
nonce_felts.1,
])
}
fn bytes_to_u512(bytes: &[u8]) -> (Felt, Felt, Felt, Felt) {
let mut limb0 = [0u8; 16];
let mut limb1 = [0u8; 16];
let mut limb2 = [0u8; 16];
let mut limb3 = [0u8; 16];
limb0.copy_from_slice(&bytes[0..16]);
limb1.copy_from_slice(&bytes[16..32]);
limb2.copy_from_slice(&bytes[32..48]);
limb3.copy_from_slice(&bytes[48..64]);
(
Felt::from(u128::from_be_bytes(limb0)),
Felt::from(u128::from_be_bytes(limb1)),
Felt::from(u128::from_be_bytes(limb2)),
Felt::from(u128::from_be_bytes(limb3)),
)
}
fn bytes_to_u256(bytes: &[u8]) -> (Felt, Felt) {
let mut low_bytes = [0u8; 16];
let mut high_bytes = [0u8; 16];
low_bytes.copy_from_slice(&bytes[16..32]); high_bytes.copy_from_slice(&bytes[0..16]);
(
Felt::from(u128::from_be_bytes(low_bytes)), Felt::from(u128::from_be_bytes(high_bytes)), )
}
#[must_use]
pub fn serialize_cairo_some<F>(data: F) -> Vec<Felt>
where
F: FnOnce() -> Vec<Felt>,
{
let mut result = vec![Felt::ZERO]; result.extend(data());
result
}
#[must_use]
pub fn serialize_cairo_none() -> Vec<Felt> {
vec![Felt::ONE] }
pub fn serialize_audit_proof(proof: &AuditProof) -> Result<Vec<Felt>> {
let ax_x = Felt::from_hex(&proof.ax.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid Ax.x: {}", e)))?;
let ax_y = Felt::from_hex(&proof.ax.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid Ax.y: {}", e)))?;
let al0_x = Felt::from_hex(&proof.al0.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AL0.x: {}", e)))?;
let al0_y = Felt::from_hex(&proof.al0.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AL0.y: {}", e)))?;
let al1_x = Felt::from_hex(&proof.al1.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AL1.x: {}", e)))?;
let al1_y = Felt::from_hex(&proof.al1.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AL1.y: {}", e)))?;
let ar1_x = Felt::from_hex(&proof.ar1.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AR1.x: {}", e)))?;
let ar1_y = Felt::from_hex(&proof.ar1.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid AR1.y: {}", e)))?;
let sx_felt = Felt::from_hex(&proof.sx).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid sx scalar: {}", e))
})?;
let sb_felt = Felt::from_hex(&proof.sb).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid sb scalar: {}", e))
})?;
let sr_felt = Felt::from_hex(&proof.sr).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid sr scalar: {}", e))
})?;
Ok(vec![
ax_x, ax_y, al0_x, al0_y, al1_x, al1_y, ar1_x, ar1_y, sx_felt, sb_felt, sr_felt,
])
}
pub fn serialize_cipher_balance(cipher: &ElGamalCiphertext) -> Result<Vec<Felt>> {
let (l_x, l_y) = serialize_projective_point(&cipher.l)?;
let (r_x, r_y) = serialize_projective_point(&cipher.r)?;
Ok(vec![l_x, l_y, r_x, r_y])
}
pub fn serialize_bit_proof(proof: &ProofOfBit) -> Result<Vec<Felt>> {
let a0_x = Felt::from_hex(&proof.a0.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid A0.x: {}", e)))?;
let a0_y = Felt::from_hex(&proof.a0.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid A0.y: {}", e)))?;
let a1_x = Felt::from_hex(&proof.a1.x)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid A1.x: {}", e)))?;
let a1_y = Felt::from_hex(&proof.a1.y)
.map_err(|e| krusty_kms_common::KmsError::CryptoError(format!("Invalid A1.y: {}", e)))?;
let c0_felt = Felt::from_hex(&proof.c0).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid c0 scalar: {}", e))
})?;
let s0_felt = Felt::from_hex(&proof.s0).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid s0 scalar: {}", e))
})?;
let s1_felt = Felt::from_hex(&proof.s1).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid s1 scalar: {}", e))
})?;
Ok(vec![a0_x, a0_y, a1_x, a1_y, c0_felt, s0_felt, s1_felt])
}
pub fn serialize_range(range: &Range) -> Result<Vec<Felt>> {
let mut felts = Vec::new();
felts.push(Felt::from(range.commitments.len()));
for commitment in &range.commitments {
let x = Felt::from_hex(&commitment.x).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid commitment.x: {}", e))
})?;
let y = Felt::from_hex(&commitment.y).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid commitment.y: {}", e))
})?;
felts.push(x);
felts.push(y);
}
felts.push(Felt::from(range.proofs.len()));
for proof in &range.proofs {
let proof_felts = serialize_bit_proof(proof)?;
felts.extend(proof_felts);
}
Ok(felts)
}
pub fn serialize_proof_of_transfer(proof: &ProofOfTransfer) -> Result<Vec<Felt>> {
let mut felts = Vec::new();
for point_ref in [
&proof.a_x,
&proof.a_r,
&proof.a_r2,
&proof.a_b,
&proof.a_b2,
&proof.a_v,
&proof.a_v2,
&proof.a_bar,
] {
let x = Felt::from_hex(&point_ref.x).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid point.x: {}", e))
})?;
let y = Felt::from_hex(&point_ref.y).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid point.y: {}", e))
})?;
felts.push(x);
felts.push(y);
}
for scalar_str in [&proof.s_x, &proof.s_r, &proof.s_b, &proof.s_b2, &proof.s_r2] {
let scalar_felt = Felt::from_hex(scalar_str).map_err(|e| {
krusty_kms_common::KmsError::CryptoError(format!("Invalid scalar: {}", e))
})?;
felts.push(scalar_felt);
}
let range_felts = serialize_range(&proof.range)?;
felts.extend(range_felts);
let range2_felts = serialize_range(&proof.range2)?;
felts.extend(range2_felts);
Ok(felts)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize_projective_point() {
let g_x =
Felt::from_hex("0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca")
.unwrap();
let g_y =
Felt::from_hex("0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f")
.unwrap();
let point = ProjectivePoint::from_affine(g_x, g_y).unwrap();
let (x, y) = serialize_projective_point(&point).unwrap();
assert_eq!(x, g_x);
assert_eq!(y, g_y);
}
#[test]
fn test_deserialize_projective_point() {
let g_x =
Felt::from_hex("0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca")
.unwrap();
let g_y =
Felt::from_hex("0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f")
.unwrap();
let point = deserialize_projective_point(g_x, g_y).unwrap();
let affine = point.to_affine().unwrap();
assert_eq!(affine.x(), g_x);
assert_eq!(affine.y(), g_y);
}
#[test]
fn test_roundtrip_point() {
let g_x =
Felt::from_hex("0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca")
.unwrap();
let g_y =
Felt::from_hex("0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f")
.unwrap();
let point1 = deserialize_projective_point(g_x, g_y).unwrap();
let (x, y) = serialize_projective_point(&point1).unwrap();
let point2 = deserialize_projective_point(x, y).unwrap();
let affine1 = point1.to_affine().unwrap();
let affine2 = point2.to_affine().unwrap();
assert_eq!(affine1.x(), affine2.x());
assert_eq!(affine1.y(), affine2.y());
}
#[test]
fn test_serialize_poe_proof() {
use krusty_kms_common::SerializablePoint;
let proof = PoeProof {
a: SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
s: "0x7b".to_string(), c: "unused".to_string(),
};
let serialized = serialize_poe_proof(&proof).unwrap();
assert_eq!(serialized.len(), 3);
assert_eq!(serialized[0], Felt::from(1u64));
assert_eq!(serialized[1], Felt::from(2u64));
assert_eq!(serialized[2], Felt::from(123u64));
}
#[test]
fn test_serialize_poe2_proof() {
use krusty_kms_common::SerializablePoint;
let proof = Poe2Proof {
a: SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
s1: "0x7b".to_string(), s2: "0x1c8".to_string(), c: "unused".to_string(),
};
let serialized = serialize_poe2_proof(&proof).unwrap();
assert_eq!(serialized.len(), 4);
assert_eq!(serialized[0], Felt::from(1u64));
assert_eq!(serialized[1], Felt::from(2u64));
assert_eq!(serialized[2], Felt::from(123u64));
assert_eq!(serialized[3], Felt::from(456u64));
}
#[test]
fn test_serialize_elgamal_proof() {
use krusty_kms_common::SerializablePoint;
let proof = ElGamalProof {
al: SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
ar: SerializablePoint {
x: "0x3".to_string(),
y: "0x4".to_string(),
},
sb: "0x6f".to_string(), sr: "0xde".to_string(), c: "unused".to_string(),
};
let serialized = serialize_elgamal_proof(&proof).unwrap();
assert_eq!(serialized.len(), 6);
assert_eq!(serialized[0], Felt::from(1u64));
assert_eq!(serialized[1], Felt::from(2u64));
assert_eq!(serialized[2], Felt::from(3u64));
assert_eq!(serialized[3], Felt::from(4u64));
assert_eq!(serialized[4], Felt::from(111u64));
assert_eq!(serialized[5], Felt::from(222u64));
}
#[test]
fn test_u128_to_u256() {
let value = 123456789u128;
let (low, high) = u128_to_u256(value);
assert_eq!(low, Felt::from(value));
assert_eq!(high, Felt::ZERO);
}
#[test]
fn test_u256_to_u128() {
let value = 123456789u128;
let (low, high) = u128_to_u256(value);
let result = u256_to_u128(low, high).unwrap();
assert_eq!(result, value);
}
#[test]
fn test_u256_to_u128_overflow() {
let result = u256_to_u128(Felt::ZERO, Felt::ONE);
assert!(result.is_err());
}
#[test]
fn test_cairo_option_some() {
let data = serialize_cairo_some(|| vec![Felt::from(42u64), Felt::from(43u64)]);
assert_eq!(data.len(), 3);
assert_eq!(data[0], Felt::ZERO); assert_eq!(data[1], Felt::from(42u64));
assert_eq!(data[2], Felt::from(43u64));
}
#[test]
fn test_cairo_option_none() {
let data = serialize_cairo_none();
assert_eq!(data.len(), 1);
assert_eq!(data[0], Felt::ONE); }
#[test]
fn test_serialize_ae_balance() {
let ciphertext = [0x42u8; 64];
let nonce = [0x99u8; 24];
let result = serialize_ae_balance(&ciphertext, &nonce).unwrap();
assert_eq!(result.len(), 6);
for felt in &result {
assert_ne!(*felt, Felt::ZERO);
}
}
#[test]
fn test_serialize_ae_balance_invalid_sizes() {
let ciphertext_short = [0u8; 32];
let nonce = [0u8; 24];
assert!(serialize_ae_balance(&ciphertext_short, &nonce).is_err());
let ciphertext = [0u8; 64];
let nonce_short = [0u8; 16];
assert!(serialize_ae_balance(&ciphertext, &nonce_short).is_err());
}
#[test]
fn test_bytes_to_u512_roundtrip() {
let original_bytes = [0x12u8; 64];
let (f0, f1, f2, f3) = bytes_to_u512(&original_bytes);
let expected_u128 = u128::from_be_bytes([0x12u8; 16]);
assert_eq!(f0, Felt::from(expected_u128));
assert_eq!(f1, Felt::from(expected_u128));
assert_eq!(f2, Felt::from(expected_u128));
assert_eq!(f3, Felt::from(expected_u128));
}
#[test]
fn test_bytes_to_u256_roundtrip() {
let bytes = [0xAAu8; 32];
let (low, high) = bytes_to_u256(&bytes);
let expected_u128 = u128::from_be_bytes([0xAAu8; 16]);
assert_eq!(low, Felt::from(expected_u128));
assert_eq!(high, Felt::from(expected_u128));
}
#[test]
fn test_serialize_projective_point_invalid() {
let point = ProjectivePoint::identity();
let result = serialize_projective_point(&point);
assert!(result.is_err());
}
#[test]
fn test_deserialize_projective_point_invalid() {
let invalid_x = Felt::from(1u64);
let invalid_y = Felt::from(2u64);
let result = deserialize_projective_point(invalid_x, invalid_y);
assert!(result.is_err());
}
#[test]
fn test_serialize_poe_proof_invalid_hex() {
use krusty_kms_common::SerializablePoint;
let proof = PoeProof {
a: SerializablePoint {
x: "invalid_hex".to_string(),
y: "0x2".to_string(),
},
s: "0x7b".to_string(),
c: "unused".to_string(),
};
let result = serialize_poe_proof(&proof);
assert!(result.is_err());
}
#[test]
fn test_serialize_poe2_proof_invalid_s1() {
use krusty_kms_common::SerializablePoint;
let proof = Poe2Proof {
a: SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
s1: "invalid_hex".to_string(),
s2: "0x1c8".to_string(),
c: "unused".to_string(),
};
let result = serialize_poe2_proof(&proof);
assert!(result.is_err());
}
#[test]
fn test_serialize_elgamal_proof_invalid_sb() {
use krusty_kms_common::SerializablePoint;
let proof = ElGamalProof {
al: SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
ar: SerializablePoint {
x: "0x3".to_string(),
y: "0x4".to_string(),
},
sb: "invalid_hex".to_string(),
sr: "0xde".to_string(),
c: "unused".to_string(),
};
let result = serialize_elgamal_proof(&proof);
assert!(result.is_err());
}
#[test]
fn test_serialize_audit_proof() {
use krusty_kms_common::SerializablePoint;
let proof = AuditProof {
ax: SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
al0: SerializablePoint {
x: "0x3".to_string(),
y: "0x4".to_string(),
},
al1: SerializablePoint {
x: "0x5".to_string(),
y: "0x6".to_string(),
},
ar1: SerializablePoint {
x: "0x7".to_string(),
y: "0x8".to_string(),
},
sx: "0x9".to_string(),
sb: "0xa".to_string(),
sr: "0xb".to_string(),
c: "unused".to_string(),
};
let serialized = serialize_audit_proof(&proof).unwrap();
assert_eq!(serialized.len(), 11);
assert_eq!(serialized[0], Felt::from(1u64)); assert_eq!(serialized[1], Felt::from(2u64)); assert_eq!(serialized[8], Felt::from(9u64)); assert_eq!(serialized[9], Felt::from(10u64)); assert_eq!(serialized[10], Felt::from(11u64)); }
#[test]
fn test_serialize_audit_proof_invalid_hex() {
use krusty_kms_common::SerializablePoint;
let proof = AuditProof {
ax: SerializablePoint {
x: "invalid".to_string(),
y: "0x2".to_string(),
},
al0: SerializablePoint {
x: "0x3".to_string(),
y: "0x4".to_string(),
},
al1: SerializablePoint {
x: "0x5".to_string(),
y: "0x6".to_string(),
},
ar1: SerializablePoint {
x: "0x7".to_string(),
y: "0x8".to_string(),
},
sx: "0x9".to_string(),
sb: "0xa".to_string(),
sr: "0xb".to_string(),
c: "unused".to_string(),
};
let result = serialize_audit_proof(&proof);
assert!(result.is_err());
}
#[test]
fn test_serialize_cipher_balance() {
let g_x =
Felt::from_hex("0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca")
.unwrap();
let g_y =
Felt::from_hex("0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f")
.unwrap();
let point = ProjectivePoint::from_affine(g_x, g_y).unwrap();
let cipher = ElGamalCiphertext {
l: point.clone(),
r: point,
};
let serialized = serialize_cipher_balance(&cipher).unwrap();
assert_eq!(serialized.len(), 4);
assert_eq!(serialized[0], g_x);
assert_eq!(serialized[1], g_y);
assert_eq!(serialized[2], g_x);
assert_eq!(serialized[3], g_y);
}
#[test]
fn test_serialize_cipher_balance_invalid() {
let cipher = ElGamalCiphertext {
l: ProjectivePoint::identity(),
r: ProjectivePoint::identity(),
};
let result = serialize_cipher_balance(&cipher);
assert!(result.is_err());
}
#[test]
fn test_serialize_bit_proof() {
use krusty_kms_common::SerializablePoint;
let proof = ProofOfBit {
a0: SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
a1: SerializablePoint {
x: "0x3".to_string(),
y: "0x4".to_string(),
},
c0: "0x5".to_string(),
s0: "0x6".to_string(),
s1: "0x7".to_string(),
};
let serialized = serialize_bit_proof(&proof).unwrap();
assert_eq!(serialized.len(), 7);
assert_eq!(serialized[0], Felt::from(1u64));
assert_eq!(serialized[6], Felt::from(7u64));
}
#[test]
fn test_serialize_bit_proof_invalid_hex() {
use krusty_kms_common::SerializablePoint;
let proof = ProofOfBit {
a0: SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
a1: SerializablePoint {
x: "0x3".to_string(),
y: "0x4".to_string(),
},
c0: "invalid".to_string(),
s0: "0x6".to_string(),
s1: "0x7".to_string(),
};
let result = serialize_bit_proof(&proof);
assert!(result.is_err());
}
#[test]
fn test_serialize_range() {
use krusty_kms_common::SerializablePoint;
let range = Range {
commitments: vec![
SerializablePoint {
x: "0x1".to_string(),
y: "0x2".to_string(),
},
SerializablePoint {
x: "0x3".to_string(),
y: "0x4".to_string(),
},
],
proofs: vec![
ProofOfBit {
a0: SerializablePoint {
x: "0xa".to_string(),
y: "0xb".to_string(),
},
a1: SerializablePoint {
x: "0xc".to_string(),
y: "0xd".to_string(),
},
c0: "0xe".to_string(),
s0: "0xf".to_string(),
s1: "0x10".to_string(),
},
ProofOfBit {
a0: SerializablePoint {
x: "0x11".to_string(),
y: "0x12".to_string(),
},
a1: SerializablePoint {
x: "0x13".to_string(),
y: "0x14".to_string(),
},
c0: "0x15".to_string(),
s0: "0x16".to_string(),
s1: "0x17".to_string(),
},
],
};
let serialized = serialize_range(&range).unwrap();
assert_eq!(serialized.len(), 20);
assert_eq!(serialized[0], Felt::from(2u64)); assert_eq!(serialized[5], Felt::from(2u64)); }
#[test]
fn test_serialize_range_invalid_commitment() {
use krusty_kms_common::SerializablePoint;
let range = Range {
commitments: vec![SerializablePoint {
x: "invalid".to_string(),
y: "0x2".to_string(),
}],
proofs: vec![],
};
let result = serialize_range(&range);
assert!(result.is_err());
}
#[test]
fn test_u128_to_u256_zero() {
let (low, high) = u128_to_u256(0);
assert_eq!(low, Felt::ZERO);
assert_eq!(high, Felt::ZERO);
}
#[test]
fn test_u128_to_u256_max() {
let (low, high) = u128_to_u256(u128::MAX);
assert_eq!(low, Felt::from(u128::MAX));
assert_eq!(high, Felt::ZERO);
}
#[test]
fn test_u256_to_u128_zero() {
let result = u256_to_u128(Felt::ZERO, Felt::ZERO).unwrap();
assert_eq!(result, 0);
}
}