Skip to content

Commit f36a62d

Browse files
[zk-sdk] Add to_bytes and from_bytes functions for proof context types for wasm target (anza-xyz#4645)
* add `encryption_with_u64` for wasm grouped ciphertext encryption * add `to_bytes` and `from_bytes` functions for context types * cargo fmt * use macros for `to_bytes` and `from_bytes`
1 parent 667fb3e commit f36a62d

File tree

12 files changed

+142
-63
lines changed

12 files changed

+142
-63
lines changed

zk-sdk/src/encryption/grouped_elgamal.rs

+29
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ mod grouped_elgamal_wasm {
243243
amount,
244244
))
245245
}
246+
247+
#[wasm_bindgen(js_name = encryptionWithU64)]
248+
pub fn encryption_with_u64(
249+
first_pubkey: &ElGamalPubkey,
250+
second_pubkey: &ElGamalPubkey,
251+
amount: u64,
252+
opening: &PedersenOpening,
253+
) -> Self {
254+
Self(GroupedElGamal::<2>::encrypt_with(
255+
[first_pubkey, second_pubkey],
256+
amount,
257+
opening,
258+
))
259+
}
246260
}
247261

248262
#[wasm_bindgen]
@@ -262,6 +276,21 @@ mod grouped_elgamal_wasm {
262276
amount,
263277
))
264278
}
279+
280+
#[wasm_bindgen(js_name = encryptionWithU64)]
281+
pub fn encryption_with_u64(
282+
first_pubkey: &ElGamalPubkey,
283+
second_pubkey: &ElGamalPubkey,
284+
third_pubkey: &ElGamalPubkey,
285+
amount: u64,
286+
opening: &PedersenOpening,
287+
) -> Self {
288+
Self(GroupedElGamal::<3>::encrypt_with(
289+
[first_pubkey, second_pubkey, third_pubkey],
290+
amount,
291+
opening,
292+
))
293+
}
265294
}
266295
}
267296

zk-sdk/src/zk_elgamal_proof_program/proof_data/batched_grouped_ciphertext_validity/handles_2.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use {
1515
elgamal::PodElGamalPubkey, grouped_elgamal::PodGroupedElGamalCiphertext2Handles,
1616
},
1717
sigma_proofs::pod::PodBatchedGroupedCiphertext2HandlesValidityProof,
18-
zk_elgamal_proof_program::proof_data::{ProofType, ZkProofData},
18+
zk_elgamal_proof_program::proof_data::{pod::impl_wasm_to_bytes, ProofType, ZkProofData},
1919
},
2020
bytemuck_derive::{Pod, Zeroable},
2121
};
@@ -27,7 +27,10 @@ use {
2727
pedersen::PedersenOpening,
2828
},
2929
sigma_proofs::batched_grouped_ciphertext_validity::BatchedGroupedCiphertext2HandlesValidityProof,
30-
zk_elgamal_proof_program::errors::{ProofGenerationError, ProofVerificationError},
30+
zk_elgamal_proof_program::{
31+
errors::{ProofGenerationError, ProofVerificationError},
32+
proof_data::errors::ProofDataError,
33+
},
3134
},
3235
bytemuck::bytes_of,
3336
merlin::Transcript,
@@ -145,13 +148,10 @@ impl BatchedGroupedCiphertext2HandlesValidityProofData {
145148

146149
Ok(Self { context, proof })
147150
}
148-
149-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toBytes))]
150-
pub fn to_bytes(&self) -> Box<[u8]> {
151-
bytes_of(self).into()
152-
}
153151
}
154152

153+
impl_wasm_to_bytes!(TYPE = BatchedGroupedCiphertext2HandlesValidityProofData);
154+
155155
impl ZkProofData<BatchedGroupedCiphertext2HandlesValidityProofContext>
156156
for BatchedGroupedCiphertext2HandlesValidityProofData
157157
{
@@ -217,6 +217,8 @@ impl BatchedGroupedCiphertext2HandlesValidityProofContext {
217217
}
218218
}
219219

220+
impl_wasm_to_bytes!(TYPE = BatchedGroupedCiphertext2HandlesValidityProofContext);
221+
220222
#[cfg(test)]
221223
mod test {
222224
use {

zk-sdk/src/zk_elgamal_proof_program/proof_data/batched_grouped_ciphertext_validity/handles_3.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use {
1515
elgamal::PodElGamalPubkey, grouped_elgamal::PodGroupedElGamalCiphertext3Handles,
1616
},
1717
sigma_proofs::pod::PodBatchedGroupedCiphertext3HandlesValidityProof,
18-
zk_elgamal_proof_program::proof_data::{ProofType, ZkProofData},
18+
zk_elgamal_proof_program::proof_data::{pod::impl_wasm_to_bytes, ProofType, ZkProofData},
1919
},
2020
bytemuck_derive::{Pod, Zeroable},
2121
};
@@ -27,7 +27,10 @@ use {
2727
pedersen::PedersenOpening,
2828
},
2929
sigma_proofs::batched_grouped_ciphertext_validity::BatchedGroupedCiphertext3HandlesValidityProof,
30-
zk_elgamal_proof_program::errors::{ProofGenerationError, ProofVerificationError},
30+
zk_elgamal_proof_program::{
31+
errors::{ProofGenerationError, ProofVerificationError},
32+
proof_data::errors::ProofDataError,
33+
},
3134
},
3235
bytemuck::bytes_of,
3336
merlin::Transcript,
@@ -155,13 +158,10 @@ impl BatchedGroupedCiphertext3HandlesValidityProofData {
155158

156159
Ok(Self { context, proof })
157160
}
158-
159-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toBytes))]
160-
pub fn to_bytes(&self) -> Box<[u8]> {
161-
bytes_of(self).into()
162-
}
163161
}
164162

163+
impl_wasm_to_bytes!(TYPE = BatchedGroupedCiphertext3HandlesValidityProofData);
164+
165165
impl ZkProofData<BatchedGroupedCiphertext3HandlesValidityProofContext>
166166
for BatchedGroupedCiphertext3HandlesValidityProofData
167167
{
@@ -234,6 +234,8 @@ impl BatchedGroupedCiphertext3HandlesValidityProofContext {
234234
}
235235
}
236236

237+
impl_wasm_to_bytes!(TYPE = BatchedGroupedCiphertext3HandlesValidityProofContext);
238+
237239
#[cfg(test)]
238240
mod test {
239241
use {

zk-sdk/src/zk_elgamal_proof_program/proof_data/ciphertext_ciphertext_equality.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use {
1111
crate::{
1212
encryption::pod::elgamal::{PodElGamalCiphertext, PodElGamalPubkey},
1313
sigma_proofs::pod::PodCiphertextCiphertextEqualityProof,
14-
zk_elgamal_proof_program::proof_data::{ProofType, ZkProofData},
14+
zk_elgamal_proof_program::proof_data::{pod::impl_wasm_to_bytes, ProofType, ZkProofData},
1515
},
1616
bytemuck_derive::{Pod, Zeroable},
1717
};
@@ -23,7 +23,10 @@ use {
2323
pedersen::PedersenOpening,
2424
},
2525
sigma_proofs::ciphertext_ciphertext_equality::CiphertextCiphertextEqualityProof,
26-
zk_elgamal_proof_program::errors::{ProofGenerationError, ProofVerificationError},
26+
zk_elgamal_proof_program::{
27+
errors::{ProofGenerationError, ProofVerificationError},
28+
proof_data::errors::ProofDataError,
29+
},
2730
},
2831
bytemuck::bytes_of,
2932
merlin::Transcript,
@@ -95,13 +98,10 @@ impl CiphertextCiphertextEqualityProofData {
9598

9699
Ok(Self { context, proof })
97100
}
98-
99-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toBytes))]
100-
pub fn to_bytes(&self) -> Box<[u8]> {
101-
bytes_of(self).into()
102-
}
103101
}
104102

103+
impl_wasm_to_bytes!(TYPE = CiphertextCiphertextEqualityProofData);
104+
105105
impl ZkProofData<CiphertextCiphertextEqualityProofContext>
106106
for CiphertextCiphertextEqualityProofData
107107
{
@@ -148,6 +148,8 @@ impl CiphertextCiphertextEqualityProofContext {
148148
}
149149
}
150150

151+
impl_wasm_to_bytes!(TYPE = CiphertextCiphertextEqualityProofContext);
152+
151153
#[cfg(test)]
152154
mod test {
153155
use super::*;

zk-sdk/src/zk_elgamal_proof_program/proof_data/ciphertext_commitment_equality.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use {
1414
pedersen::PodPedersenCommitment,
1515
},
1616
sigma_proofs::pod::PodCiphertextCommitmentEqualityProof,
17-
zk_elgamal_proof_program::proof_data::{ProofType, ZkProofData},
17+
zk_elgamal_proof_program::proof_data::{pod::impl_wasm_to_bytes, ProofType, ZkProofData},
1818
},
1919
bytemuck_derive::{Pod, Zeroable},
2020
};
@@ -26,7 +26,10 @@ use {
2626
pedersen::{PedersenCommitment, PedersenOpening},
2727
},
2828
sigma_proofs::ciphertext_commitment_equality::CiphertextCommitmentEqualityProof,
29-
zk_elgamal_proof_program::errors::{ProofGenerationError, ProofVerificationError},
29+
zk_elgamal_proof_program::{
30+
errors::{ProofGenerationError, ProofVerificationError},
31+
proof_data::errors::ProofDataError,
32+
},
3033
},
3134
bytemuck::bytes_of,
3235
merlin::Transcript,
@@ -88,13 +91,10 @@ impl CiphertextCommitmentEqualityProofData {
8891
proof: proof.into(),
8992
})
9093
}
91-
92-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toBytes))]
93-
pub fn to_bytes(&self) -> Box<[u8]> {
94-
bytes_of(self).into()
95-
}
9694
}
9795

96+
impl_wasm_to_bytes!(TYPE = CiphertextCommitmentEqualityProofData);
97+
9898
impl ZkProofData<CiphertextCommitmentEqualityProofContext>
9999
for CiphertextCommitmentEqualityProofData
100100
{
@@ -131,6 +131,8 @@ impl CiphertextCommitmentEqualityProofContext {
131131
}
132132
}
133133

134+
impl_wasm_to_bytes!(TYPE = CiphertextCommitmentEqualityProofContext);
135+
134136
#[cfg(test)]
135137
mod test {
136138
use {

zk-sdk/src/zk_elgamal_proof_program/proof_data/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use thiserror::Error;
2+
#[cfg(target_arch = "wasm32")]
3+
use wasm_bindgen::prelude::*;
24

35
#[derive(Error, Clone, Debug, Eq, PartialEq)]
46
pub enum ProofDataError {
@@ -12,4 +14,13 @@ pub enum ProofDataError {
1214
Overflow,
1315
#[error("invalid proof type")]
1416
InvalidProofType,
17+
#[error("deserialization failed")]
18+
Deserialization,
19+
}
20+
21+
#[cfg(target_arch = "wasm32")]
22+
impl From<ProofDataError> for JsValue {
23+
fn from(err: ProofDataError) -> Self {
24+
js_sys::Error::new(&err.to_string()).into()
25+
}
1526
}

zk-sdk/src/zk_elgamal_proof_program/proof_data/grouped_ciphertext_validity/handles_2.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use {
1515
elgamal::PodElGamalPubkey, grouped_elgamal::PodGroupedElGamalCiphertext2Handles,
1616
},
1717
sigma_proofs::pod::PodGroupedCiphertext2HandlesValidityProof,
18-
zk_elgamal_proof_program::proof_data::{ProofType, ZkProofData},
18+
zk_elgamal_proof_program::proof_data::{pod::impl_wasm_to_bytes, ProofType, ZkProofData},
1919
},
2020
bytemuck_derive::{Pod, Zeroable},
2121
};
@@ -27,7 +27,10 @@ use {
2727
pedersen::PedersenOpening,
2828
},
2929
sigma_proofs::grouped_ciphertext_validity::GroupedCiphertext2HandlesValidityProof,
30-
zk_elgamal_proof_program::errors::{ProofGenerationError, ProofVerificationError},
30+
zk_elgamal_proof_program::{
31+
errors::{ProofGenerationError, ProofVerificationError},
32+
proof_data::errors::ProofDataError,
33+
},
3134
},
3235
bytemuck::bytes_of,
3336
merlin::Transcript,
@@ -129,13 +132,10 @@ impl GroupedCiphertext2HandlesValidityProofData {
129132

130133
Ok(Self { context, proof })
131134
}
132-
133-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toBytes))]
134-
pub fn to_bytes(&self) -> Box<[u8]> {
135-
bytes_of(self).into()
136-
}
137135
}
138136

137+
impl_wasm_to_bytes!(TYPE = GroupedCiphertext2HandlesValidityProofData);
138+
139139
impl ZkProofData<GroupedCiphertext2HandlesValidityProofContext>
140140
for GroupedCiphertext2HandlesValidityProofData
141141
{
@@ -185,6 +185,8 @@ impl GroupedCiphertext2HandlesValidityProofContext {
185185
}
186186
}
187187

188+
impl_wasm_to_bytes!(TYPE = GroupedCiphertext2HandlesValidityProofContext);
189+
188190
#[cfg(test)]
189191
mod test {
190192
use {

zk-sdk/src/zk_elgamal_proof_program/proof_data/grouped_ciphertext_validity/handles_3.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use {
1515
elgamal::PodElGamalPubkey, grouped_elgamal::PodGroupedElGamalCiphertext3Handles,
1616
},
1717
sigma_proofs::pod::PodGroupedCiphertext3HandlesValidityProof,
18-
zk_elgamal_proof_program::proof_data::{ProofType, ZkProofData},
18+
zk_elgamal_proof_program::proof_data::{pod::impl_wasm_to_bytes, ProofType, ZkProofData},
1919
},
2020
bytemuck_derive::{Pod, Zeroable},
2121
};
@@ -27,7 +27,10 @@ use {
2727
pedersen::PedersenOpening,
2828
},
2929
sigma_proofs::grouped_ciphertext_validity::GroupedCiphertext3HandlesValidityProof,
30-
zk_elgamal_proof_program::errors::{ProofGenerationError, ProofVerificationError},
30+
zk_elgamal_proof_program::{
31+
errors::{ProofGenerationError, ProofVerificationError},
32+
proof_data::errors::ProofDataError,
33+
},
3134
},
3235
bytemuck::bytes_of,
3336
merlin::Transcript,
@@ -139,13 +142,10 @@ impl GroupedCiphertext3HandlesValidityProofData {
139142

140143
Ok(Self { context, proof })
141144
}
142-
143-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toBytes))]
144-
pub fn to_bytes(&self) -> Box<[u8]> {
145-
bytes_of(self).into()
146-
}
147145
}
148146

147+
impl_wasm_to_bytes!(TYPE = GroupedCiphertext3HandlesValidityProofData);
148+
149149
impl ZkProofData<GroupedCiphertext3HandlesValidityProofContext>
150150
for GroupedCiphertext3HandlesValidityProofData
151151
{
@@ -200,6 +200,8 @@ impl GroupedCiphertext3HandlesValidityProofContext {
200200
}
201201
}
202202

203+
impl_wasm_to_bytes!(TYPE = GroupedCiphertext3HandlesValidityProofContext);
204+
203205
#[cfg(test)]
204206
mod test {
205207
use {

zk-sdk/src/zk_elgamal_proof_program/proof_data/percentage_with_cap.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use {
1313
crate::{
1414
encryption::pedersen::{PedersenCommitment, PedersenOpening},
1515
sigma_proofs::percentage_with_cap::PercentageWithCapProof,
16-
zk_elgamal_proof_program::errors::{ProofGenerationError, ProofVerificationError},
16+
zk_elgamal_proof_program::{
17+
errors::{ProofGenerationError, ProofVerificationError},
18+
proof_data::errors::ProofDataError,
19+
},
1720
},
1821
bytemuck::bytes_of,
1922
merlin::Transcript,
@@ -24,7 +27,7 @@ use {
2427
encryption::pod::pedersen::PodPedersenCommitment,
2528
pod::PodU64,
2629
sigma_proofs::pod::PodPercentageWithCapProof,
27-
zk_elgamal_proof_program::proof_data::{ProofType, ZkProofData},
30+
zk_elgamal_proof_program::proof_data::{pod::impl_wasm_to_bytes, ProofType, ZkProofData},
2831
},
2932
bytemuck_derive::{Pod, Zeroable},
3033
};
@@ -110,13 +113,10 @@ impl PercentageWithCapProofData {
110113

111114
Ok(Self { context, proof })
112115
}
113-
114-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toBytes))]
115-
pub fn to_bytes(&self) -> Box<[u8]> {
116-
bytes_of(self).into()
117-
}
118116
}
119117

118+
impl_wasm_to_bytes!(TYPE = PercentageWithCapProofData);
119+
120120
impl ZkProofData<PercentageWithCapProofContext> for PercentageWithCapProofData {
121121
const PROOF_TYPE: ProofType = ProofType::PercentageWithCap;
122122

@@ -161,6 +161,8 @@ impl PercentageWithCapProofContext {
161161
}
162162
}
163163

164+
impl_wasm_to_bytes!(TYPE = PercentageWithCapProofContext);
165+
164166
#[cfg(test)]
165167
mod test {
166168
use {super::*, crate::encryption::pedersen::Pedersen, curve25519_dalek::scalar::Scalar};

0 commit comments

Comments
 (0)