solana/zk-token-sdk/src/transcript.rs

109 lines
3.5 KiB
Rust
Raw Normal View History

2021-12-03 09:06:31 -08:00
use {
2022-01-04 04:31:24 -08:00
crate::errors::TranscriptError,
2021-12-03 09:06:31 -08:00
curve25519_dalek::{ristretto::CompressedRistretto, scalar::Scalar, traits::IsIdentity},
merlin::Transcript,
};
2021-09-29 21:45:35 -07:00
pub trait TranscriptProtocol {
2021-10-11 11:25:16 -07:00
/// Append a domain separator for an `n`-bit rangeproof for ElGamalKeypair
/// ciphertext using a decryption key
2021-09-29 21:45:35 -07:00
fn rangeproof_from_key_domain_sep(&mut self, n: u64);
2021-10-11 11:25:16 -07:00
/// Append a domain separator for an `n`-bit rangeproof for ElGamalKeypair
/// ciphertext using an opening
2021-09-29 21:45:35 -07:00
fn rangeproof_from_opening_domain_sep(&mut self, n: u64);
/// Append a domain separator for a length-`n` inner product proof.
fn innerproduct_domain_sep(&mut self, n: u64);
/// Append a domain separator for close account proof.
fn close_account_proof_domain_sep(&mut self);
/// Append a domain separator for update account public key proof.
fn update_account_public_key_proof_domain_sep(&mut self);
2021-09-29 21:45:35 -07:00
/// Append a domain separator for withdraw proof.
fn withdraw_proof_domain_sep(&mut self);
2021-12-09 07:56:33 -08:00
/// Append a domain separator for transfer proof.
fn transfer_proof_domain_sep(&mut self);
2021-09-29 21:45:35 -07:00
/// Append a `scalar` with the given `label`.
fn append_scalar(&mut self, label: &'static [u8], scalar: &Scalar);
/// Append a `point` with the given `label`.
fn append_point(&mut self, label: &'static [u8], point: &CompressedRistretto);
/// Check that a point is not the identity, then append it to the
/// transcript. Otherwise, return an error.
fn validate_and_append_point(
&mut self,
label: &'static [u8],
point: &CompressedRistretto,
2022-01-04 04:31:24 -08:00
) -> Result<(), TranscriptError>;
2021-09-29 21:45:35 -07:00
/// Compute a `label`ed challenge variable.
fn challenge_scalar(&mut self, label: &'static [u8]) -> Scalar;
}
impl TranscriptProtocol for Transcript {
fn rangeproof_from_key_domain_sep(&mut self, n: u64) {
self.append_message(b"dom-sep", b"rangeproof from opening v1");
self.append_u64(b"n", n);
}
fn rangeproof_from_opening_domain_sep(&mut self, n: u64) {
self.append_message(b"dom-sep", b"rangeproof from opening v1");
self.append_u64(b"n", n);
}
fn innerproduct_domain_sep(&mut self, n: u64) {
self.append_message(b"dom-sep", b"ipp v1");
self.append_u64(b"n", n);
}
fn close_account_proof_domain_sep(&mut self) {
self.append_message(b"dom-sep", b"CloseAccountProof");
}
fn update_account_public_key_proof_domain_sep(&mut self) {
self.append_message(b"dom-sep", b"UpdateAccountPublicKeyProof");
2021-09-29 21:45:35 -07:00
}
fn withdraw_proof_domain_sep(&mut self) {
self.append_message(b"dom-sep", b"WithdrawProof");
2021-09-29 21:45:35 -07:00
}
2021-12-09 07:56:33 -08:00
fn transfer_proof_domain_sep(&mut self) {
self.append_message(b"dom-sep", b"TransferProof");
2021-09-29 21:45:35 -07:00
}
fn append_scalar(&mut self, label: &'static [u8], scalar: &Scalar) {
self.append_message(label, scalar.as_bytes());
}
fn append_point(&mut self, label: &'static [u8], point: &CompressedRistretto) {
self.append_message(label, point.as_bytes());
}
fn validate_and_append_point(
&mut self,
label: &'static [u8],
point: &CompressedRistretto,
2022-01-04 04:31:24 -08:00
) -> Result<(), TranscriptError> {
2021-09-29 21:45:35 -07:00
if point.is_identity() {
2022-01-04 04:31:24 -08:00
Err(TranscriptError::ValidationError)
2021-09-29 21:45:35 -07:00
} else {
self.append_message(label, point.as_bytes());
Ok(())
}
}
fn challenge_scalar(&mut self, label: &'static [u8]) -> Scalar {
let mut buf = [0u8; 64];
self.challenge_bytes(label, &mut buf);
Scalar::from_bytes_mod_order_wide(&buf)
}
}