use crate::{
commitment::HomomorphicCommitment,
keys::{PublicKey, SecretKey},
};
use serde::{Deserialize, Serialize};
use thiserror::Error;
pub const REWIND_PROOF_MESSAGE_LENGTH: usize = 23;
pub const REWIND_CHECK_MESSAGE: &[u8; 2] = b"TR";
pub const REWIND_USER_MESSAGE_LENGTH: usize = 21;
#[derive(Debug, Clone, Error, PartialEq, Deserialize, Serialize)]
pub enum RangeProofError {
#[error("Could not construct range proof")]
ProofConstructionError,
#[error("The deserialization of the range proof failed")]
InvalidProof,
#[error("Invalid input was provided to the RangeProofService constructor")]
InitializationError,
#[error("Invalid range proof provided")]
InvalidRangeProof,
#[error("Invalid range proof rewind, the rewind keys provided must be invalid")]
InvalidRewind,
}
pub trait RangeProofService {
type P: Sized;
type K: SecretKey;
type PK: PublicKey<K = Self::K>;
fn construct_proof(&self, key: &Self::K, value: u64) -> Result<Self::P, RangeProofError>;
fn verify(&self, proof: &Self::P, commitment: &HomomorphicCommitment<Self::PK>) -> bool;
fn range(&self) -> usize;
fn construct_proof_with_rewind_key(
&self,
key: &Self::K,
value: u64,
rewind_key: &Self::K,
rewind_blinding_key: &Self::K,
proof_message: &[u8; REWIND_USER_MESSAGE_LENGTH],
) -> Result<Self::P, RangeProofError>;
fn rewind_proof_value_only(
&self,
proof: &Self::P,
commitment: &HomomorphicCommitment<Self::PK>,
rewind_public_key: &Self::PK,
rewind_blinding_public_key: &Self::PK,
) -> Result<RewindResult, RangeProofError>;
fn rewind_proof_commitment_data(
&self,
proof: &Self::P,
commitment: &HomomorphicCommitment<Self::PK>,
rewind_key: &Self::K,
rewind_blinding_key: &Self::K,
) -> Result<FullRewindResult<Self::K>, RangeProofError>;
}
#[derive(Debug, PartialEq)]
pub struct RewindResult {
pub committed_value: u64,
pub proof_message: [u8; REWIND_USER_MESSAGE_LENGTH],
}
impl RewindResult {
pub fn new(committed_value: u64, proof_message: [u8; REWIND_USER_MESSAGE_LENGTH]) -> Self {
Self {
committed_value,
proof_message,
}
}
}
#[derive(Debug, PartialEq)]
pub struct FullRewindResult<K>
where K: SecretKey
{
pub committed_value: u64,
pub proof_message: [u8; REWIND_USER_MESSAGE_LENGTH],
pub blinding_factor: K,
}
impl<K> FullRewindResult<K>
where K: SecretKey
{
pub fn new(committed_value: u64, proof_message: [u8; REWIND_USER_MESSAGE_LENGTH], blinding_factor: K) -> Self {
Self {
committed_value,
proof_message,
blinding_factor,
}
}
}