1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use {
crate::zk_token_elgamal::pod,
bytemuck::{Pod, Zeroable},
};
#[cfg(not(target_os = "solana"))]
use {
crate::{
encryption::elgamal::{ElGamalKeypair, ElGamalPubkey},
errors::ProofError,
instruction::Verifiable,
sigma_proofs::pubkey_proof::PubkeySigmaProof,
transcript::TranscriptProtocol,
},
merlin::Transcript,
std::convert::TryInto,
};
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct PubkeyValidityData {
pub pubkey: pod::ElGamalPubkey,
pub proof: PubkeyValidityProof, }
#[cfg(not(target_os = "solana"))]
impl PubkeyValidityData {
pub fn new(keypair: &ElGamalKeypair) -> Result<Self, ProofError> {
let pod_pubkey = pod::ElGamalPubkey(keypair.public.to_bytes());
let mut transcript = PubkeyValidityProof::transcript_new(&pod_pubkey);
let proof = PubkeyValidityProof::new(keypair, &mut transcript);
Ok(PubkeyValidityData {
pubkey: pod_pubkey,
proof,
})
}
}
#[cfg(not(target_os = "solana"))]
impl Verifiable for PubkeyValidityData {
fn verify(&self) -> Result<(), ProofError> {
let mut transcript = PubkeyValidityProof::transcript_new(&self.pubkey);
let pubkey = self.pubkey.try_into()?;
self.proof.verify(&pubkey, &mut transcript)
}
}
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
#[allow(non_snake_case)]
pub struct PubkeyValidityProof {
pub proof: pod::PubkeySigmaProof,
}
#[allow(non_snake_case)]
#[cfg(not(target_os = "solana"))]
impl PubkeyValidityProof {
fn transcript_new(pubkey: &pod::ElGamalPubkey) -> Transcript {
let mut transcript = Transcript::new(b"PubkeyProof");
transcript.append_pubkey(b"pubkey", pubkey);
transcript
}
pub fn new(keypair: &ElGamalKeypair, transcript: &mut Transcript) -> Self {
let proof = PubkeySigmaProof::new(keypair, transcript);
Self {
proof: proof.into(),
}
}
pub fn verify(
&self,
pubkey: &ElGamalPubkey,
transcript: &mut Transcript,
) -> Result<(), ProofError> {
let proof: PubkeySigmaProof = self.proof.try_into()?;
proof.verify(pubkey, transcript)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_pubkey_validity_correctness() {
let keypair = ElGamalKeypair::new_rand();
let pubkey_validity_data = PubkeyValidityData::new(&keypair).unwrap();
assert!(pubkey_validity_data.verify().is_ok());
}
}