Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zk-sdk] Remove source, destination, and auditor variable names #1533

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub struct BatchedGroupedCiphertext2HandlesValidityProofData {
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct BatchedGroupedCiphertext2HandlesValidityProofContext {
pub destination_pubkey: PodElGamalPubkey, // 32 bytes
pub first_pubkey: PodElGamalPubkey, // 32 bytes

pub auditor_pubkey: PodElGamalPubkey, // 32 bytes
pub second_pubkey: PodElGamalPubkey, // 32 bytes

pub grouped_ciphertext_lo: PodGroupedElGamalCiphertext2Handles, // 96 bytes

Expand All @@ -57,32 +57,32 @@ pub struct BatchedGroupedCiphertext2HandlesValidityProofContext {
#[cfg(not(target_os = "solana"))]
impl BatchedGroupedCiphertext2HandlesValidityProofData {
pub fn new(
destination_pubkey: &ElGamalPubkey,
auditor_pubkey: &ElGamalPubkey,
first_pubkey: &ElGamalPubkey,
second_pubkey: &ElGamalPubkey,
grouped_ciphertext_lo: &GroupedElGamalCiphertext<2>,
grouped_ciphertext_hi: &GroupedElGamalCiphertext<2>,
amount_lo: u64,
amount_hi: u64,
opening_lo: &PedersenOpening,
opening_hi: &PedersenOpening,
) -> Result<Self, ProofGenerationError> {
let pod_destination_pubkey = PodElGamalPubkey(destination_pubkey.into());
let pod_auditor_pubkey = PodElGamalPubkey(auditor_pubkey.into());
let pod_first_pubkey = PodElGamalPubkey(first_pubkey.into());
let pod_second_pubkey = PodElGamalPubkey(second_pubkey.into());
let pod_grouped_ciphertext_lo = (*grouped_ciphertext_lo).into();
let pod_grouped_ciphertext_hi = (*grouped_ciphertext_hi).into();

let context = BatchedGroupedCiphertext2HandlesValidityProofContext {
destination_pubkey: pod_destination_pubkey,
auditor_pubkey: pod_auditor_pubkey,
first_pubkey: pod_first_pubkey,
second_pubkey: pod_second_pubkey,
grouped_ciphertext_lo: pod_grouped_ciphertext_lo,
grouped_ciphertext_hi: pod_grouped_ciphertext_hi,
};

let mut transcript = context.new_transcript();

let proof = BatchedGroupedCiphertext2HandlesValidityProof::new(
destination_pubkey,
auditor_pubkey,
first_pubkey,
second_pubkey,
amount_lo,
amount_hi,
opening_lo,
Expand All @@ -108,31 +108,31 @@ impl ZkProofData<BatchedGroupedCiphertext2HandlesValidityProofContext>
fn verify_proof(&self) -> Result<(), ProofVerificationError> {
let mut transcript = self.context.new_transcript();

let destination_pubkey = self.context.destination_pubkey.try_into()?;
let auditor_pubkey = self.context.auditor_pubkey.try_into()?;
let first_pubkey = self.context.first_pubkey.try_into()?;
let second_pubkey = self.context.second_pubkey.try_into()?;
let grouped_ciphertext_lo: GroupedElGamalCiphertext<2> =
self.context.grouped_ciphertext_lo.try_into()?;
let grouped_ciphertext_hi: GroupedElGamalCiphertext<2> =
self.context.grouped_ciphertext_hi.try_into()?;

let destination_handle_lo = grouped_ciphertext_lo.handles.first().unwrap();
let auditor_handle_lo = grouped_ciphertext_lo.handles.get(1).unwrap();
let first_handle_lo = grouped_ciphertext_lo.handles.first().unwrap();
let second_handle_lo = grouped_ciphertext_lo.handles.get(1).unwrap();

let destination_handle_hi = grouped_ciphertext_hi.handles.first().unwrap();
let auditor_handle_hi = grouped_ciphertext_hi.handles.get(1).unwrap();
let first_handle_hi = grouped_ciphertext_hi.handles.first().unwrap();
let second_handle_hi = grouped_ciphertext_hi.handles.get(1).unwrap();

let proof: BatchedGroupedCiphertext2HandlesValidityProof = self.proof.try_into()?;

proof
.verify(
&destination_pubkey,
&auditor_pubkey,
&first_pubkey,
&second_pubkey,
&grouped_ciphertext_lo.commitment,
&grouped_ciphertext_hi.commitment,
destination_handle_lo,
destination_handle_hi,
auditor_handle_lo,
auditor_handle_hi,
first_handle_lo,
first_handle_hi,
second_handle_lo,
second_handle_hi,
&mut transcript,
)
.map_err(|e| e.into())
Expand All @@ -145,8 +145,8 @@ impl BatchedGroupedCiphertext2HandlesValidityProofContext {
let mut transcript =
Transcript::new(b"batched-grouped-ciphertext-validity-2-handles-instruction");

transcript.append_message(b"destination-pubkey", bytes_of(&self.destination_pubkey));
transcript.append_message(b"auditor-pubkey", bytes_of(&self.auditor_pubkey));
transcript.append_message(b"first-pubkey", bytes_of(&self.first_pubkey));
transcript.append_message(b"second-pubkey", bytes_of(&self.second_pubkey));
transcript.append_message(
b"grouped-ciphertext-lo",
bytes_of(&self.grouped_ciphertext_lo),
Expand All @@ -169,33 +169,27 @@ mod test {

#[test]
fn test_ciphertext_validity_proof_instruction_correctness() {
let destination_keypair = ElGamalKeypair::new_rand();
let destination_pubkey = destination_keypair.pubkey();
let first_keypair = ElGamalKeypair::new_rand();
let first_pubkey = first_keypair.pubkey();

let auditor_keypair = ElGamalKeypair::new_rand();
let auditor_pubkey = auditor_keypair.pubkey();
let second_keypair = ElGamalKeypair::new_rand();
let second_pubkey = second_keypair.pubkey();

let amount_lo: u64 = 11;
let amount_hi: u64 = 22;

let opening_lo = PedersenOpening::new_rand();
let opening_hi = PedersenOpening::new_rand();

let grouped_ciphertext_lo = GroupedElGamal::encrypt_with(
[destination_pubkey, auditor_pubkey],
amount_lo,
&opening_lo,
);
let grouped_ciphertext_lo =
GroupedElGamal::encrypt_with([first_pubkey, second_pubkey], amount_lo, &opening_lo);

let grouped_ciphertext_hi = GroupedElGamal::encrypt_with(
[destination_pubkey, auditor_pubkey],
amount_hi,
&opening_hi,
);
let grouped_ciphertext_hi =
GroupedElGamal::encrypt_with([first_pubkey, second_pubkey], amount_hi, &opening_hi);

let proof_data = BatchedGroupedCiphertext2HandlesValidityProofData::new(
destination_pubkey,
auditor_pubkey,
first_pubkey,
second_pubkey,
&grouped_ciphertext_lo,
&grouped_ciphertext_hi,
amount_lo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ pub struct BatchedGroupedCiphertext3HandlesValidityProofData {
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct BatchedGroupedCiphertext3HandlesValidityProofContext {
pub source_pubkey: PodElGamalPubkey, // 32 bytes
pub first_pubkey: PodElGamalPubkey, // 32 bytes

pub destination_pubkey: PodElGamalPubkey, // 32 bytes
pub second_pubkey: PodElGamalPubkey, // 32 bytes

pub auditor_pubkey: PodElGamalPubkey, // 32 bytes
pub third_pubkey: PodElGamalPubkey, // 32 bytes

pub grouped_ciphertext_lo: PodGroupedElGamalCiphertext3Handles, // 128 bytes

Expand All @@ -59,36 +59,36 @@ pub struct BatchedGroupedCiphertext3HandlesValidityProofContext {
#[cfg(not(target_os = "solana"))]
impl BatchedGroupedCiphertext3HandlesValidityProofData {
pub fn new(
source_pubkey: &ElGamalPubkey,
destination_pubkey: &ElGamalPubkey,
auditor_pubkey: &ElGamalPubkey,
first_pubkey: &ElGamalPubkey,
second_pubkey: &ElGamalPubkey,
third_pubkey: &ElGamalPubkey,
grouped_ciphertext_lo: &GroupedElGamalCiphertext<3>,
grouped_ciphertext_hi: &GroupedElGamalCiphertext<3>,
amount_lo: u64,
amount_hi: u64,
opening_lo: &PedersenOpening,
opening_hi: &PedersenOpening,
) -> Result<Self, ProofGenerationError> {
let pod_source_pubkey = PodElGamalPubkey(source_pubkey.into());
let pod_destination_pubkey = PodElGamalPubkey(destination_pubkey.into());
let pod_auditor_pubkey = PodElGamalPubkey(auditor_pubkey.into());
let pod_first_pubkey = PodElGamalPubkey(first_pubkey.into());
let pod_second_pubkey = PodElGamalPubkey(second_pubkey.into());
let pod_third_pubkey = PodElGamalPubkey(third_pubkey.into());
let pod_grouped_ciphertext_lo = (*grouped_ciphertext_lo).into();
let pod_grouped_ciphertext_hi = (*grouped_ciphertext_hi).into();

let context = BatchedGroupedCiphertext3HandlesValidityProofContext {
source_pubkey: pod_source_pubkey,
destination_pubkey: pod_destination_pubkey,
auditor_pubkey: pod_auditor_pubkey,
first_pubkey: pod_first_pubkey,
second_pubkey: pod_second_pubkey,
third_pubkey: pod_third_pubkey,
grouped_ciphertext_lo: pod_grouped_ciphertext_lo,
grouped_ciphertext_hi: pod_grouped_ciphertext_hi,
};

let mut transcript = context.new_transcript();

let proof = BatchedGroupedCiphertext3HandlesValidityProof::new(
source_pubkey,
destination_pubkey,
auditor_pubkey,
first_pubkey,
second_pubkey,
third_pubkey,
amount_lo,
amount_hi,
opening_lo,
Expand All @@ -114,37 +114,37 @@ impl ZkProofData<BatchedGroupedCiphertext3HandlesValidityProofContext>
fn verify_proof(&self) -> Result<(), ProofVerificationError> {
let mut transcript = self.context.new_transcript();

let source_pubkey = self.context.source_pubkey.try_into()?;
let destination_pubkey = self.context.destination_pubkey.try_into()?;
let auditor_pubkey = self.context.auditor_pubkey.try_into()?;
let first_pubkey = self.context.first_pubkey.try_into()?;
let second_pubkey = self.context.second_pubkey.try_into()?;
let third_pubkey = self.context.third_pubkey.try_into()?;
let grouped_ciphertext_lo: GroupedElGamalCiphertext<3> =
self.context.grouped_ciphertext_lo.try_into()?;
let grouped_ciphertext_hi: GroupedElGamalCiphertext<3> =
self.context.grouped_ciphertext_hi.try_into()?;

let source_handle_lo = grouped_ciphertext_lo.handles.first().unwrap();
let destination_handle_lo = grouped_ciphertext_lo.handles.get(1).unwrap();
let auditor_handle_lo = grouped_ciphertext_lo.handles.get(2).unwrap();
let first_handle_lo = grouped_ciphertext_lo.handles.first().unwrap();
let second_handle_lo = grouped_ciphertext_lo.handles.get(1).unwrap();
let third_handle_lo = grouped_ciphertext_lo.handles.get(2).unwrap();

let source_handle_hi = grouped_ciphertext_hi.handles.first().unwrap();
let destination_handle_hi = grouped_ciphertext_hi.handles.get(1).unwrap();
let auditor_handle_hi = grouped_ciphertext_hi.handles.get(2).unwrap();
let first_handle_hi = grouped_ciphertext_hi.handles.first().unwrap();
let second_handle_hi = grouped_ciphertext_hi.handles.get(1).unwrap();
let third_handle_hi = grouped_ciphertext_hi.handles.get(2).unwrap();

let proof: BatchedGroupedCiphertext3HandlesValidityProof = self.proof.try_into()?;

proof
.verify(
&source_pubkey,
&destination_pubkey,
&auditor_pubkey,
&first_pubkey,
&second_pubkey,
&third_pubkey,
&grouped_ciphertext_lo.commitment,
&grouped_ciphertext_hi.commitment,
source_handle_lo,
source_handle_hi,
destination_handle_lo,
destination_handle_hi,
auditor_handle_lo,
auditor_handle_hi,
first_handle_lo,
first_handle_hi,
second_handle_lo,
second_handle_hi,
third_handle_lo,
third_handle_hi,
&mut transcript,
)
.map_err(|e| e.into())
Expand All @@ -157,9 +157,9 @@ impl BatchedGroupedCiphertext3HandlesValidityProofContext {
let mut transcript =
Transcript::new(b"batched-grouped-ciphertext-validity-3-handles-instruction");

transcript.append_message(b"source-pubkey", bytes_of(&self.source_pubkey));
transcript.append_message(b"destination-pubkey", bytes_of(&self.destination_pubkey));
transcript.append_message(b"auditor-pubkey", bytes_of(&self.auditor_pubkey));
transcript.append_message(b"first-pubkey", bytes_of(&self.first_pubkey));
transcript.append_message(b"second-pubkey", bytes_of(&self.second_pubkey));
transcript.append_message(b"third-pubkey", bytes_of(&self.third_pubkey));
transcript.append_message(
b"grouped-ciphertext-lo",
bytes_of(&self.grouped_ciphertext_lo),
Expand All @@ -182,14 +182,14 @@ mod test {

#[test]
fn test_ciphertext_validity_proof_instruction_correctness() {
let source_keypair = ElGamalKeypair::new_rand();
let source_pubkey = source_keypair.pubkey();
let first_keypair = ElGamalKeypair::new_rand();
let first_pubkey = first_keypair.pubkey();

let destination_keypair = ElGamalKeypair::new_rand();
let destination_pubkey = destination_keypair.pubkey();
let second_keypair = ElGamalKeypair::new_rand();
let second_pubkey = second_keypair.pubkey();

let auditor_keypair = ElGamalKeypair::new_rand();
let auditor_pubkey = auditor_keypair.pubkey();
let third_keypair = ElGamalKeypair::new_rand();
let third_pubkey = third_keypair.pubkey();

let amount_lo: u64 = 11;
let amount_hi: u64 = 22;
Expand All @@ -198,21 +198,21 @@ mod test {
let opening_hi = PedersenOpening::new_rand();

let grouped_ciphertext_lo = GroupedElGamal::encrypt_with(
[source_pubkey, destination_pubkey, auditor_pubkey],
[first_pubkey, second_pubkey, third_pubkey],
amount_lo,
&opening_lo,
);

let grouped_ciphertext_hi = GroupedElGamal::encrypt_with(
[source_pubkey, destination_pubkey, auditor_pubkey],
[first_pubkey, second_pubkey, third_pubkey],
amount_hi,
&opening_hi,
);

let proof_data = BatchedGroupedCiphertext3HandlesValidityProofData::new(
source_pubkey,
destination_pubkey,
auditor_pubkey,
first_pubkey,
second_pubkey,
third_pubkey,
&grouped_ciphertext_lo,
&grouped_ciphertext_hi,
amount_lo,
Expand Down
Loading
Loading