Skip to content

Commit 1d37eb9

Browse files
authored
fix: support builds targeting wasm32 (#298)
* fix: support builds targetting `wasm32` - Introduced conditional compilation for WASM target architecture in burn.rs, mint.rs, transfer.rs, and transfer_with_fee.rs. Fixes #293 * ci: add `wasm` build check for `spl-token-2022` - Introduced a new job in the GitHub Actions workflow to build the Token-2022 for the WASM target architecture. This should ensure no future regressions. * style: update formatting * chore: add WASM build script for programs - Removed the old `wasm:build` script and introduced `programs:build-wasm` to streamline the build process for WASM targets. - Added a new `build-wasm.mjs` script to handle the WASM build using Cargo. * ci: update build script
1 parent db15252 commit 1d37eb9

File tree

9 files changed

+137
-11
lines changed

9 files changed

+137
-11
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ jobs:
286286
- name: Build Token-2022
287287
run: pnpm programs:build
288288

289+
- name: Build Token-2022 Wasm
290+
run: pnpm programs:build-wasm
291+
289292
- name: Build ElGamal Registry
290293
run: pnpm confidential-transfer:elgamal-registry:build
291294

confidential-transfer/proof-generation/src/burn.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(target_arch = "wasm32")]
2+
use solana_zk_sdk::encryption::grouped_elgamal::GroupedElGamalCiphertext3Handles;
13
use {
24
crate::{
35
encryption::BurnAmountCiphertext, errors::TokenProofGenerationError,
@@ -53,14 +55,33 @@ pub fn burn_split_proof_data(
5355
supply_elgamal_pubkey,
5456
auditor_elgamal_pubkey,
5557
);
58+
#[cfg(not(target_arch = "wasm32"))]
59+
let grouped_ciphertext_lo = burn_amount_ciphertext_lo.0;
60+
#[cfg(target_arch = "wasm32")]
61+
let grouped_ciphertext_lo = GroupedElGamalCiphertext3Handles::encryption_with_u64(
62+
source_elgamal_keypair.pubkey(),
63+
supply_elgamal_pubkey,
64+
auditor_elgamal_pubkey,
65+
burn_amount_lo,
66+
&burn_amount_opening_lo,
67+
);
5668

5769
let (burn_amount_ciphertext_hi, burn_amount_opening_hi) = BurnAmountCiphertext::new(
5870
burn_amount_hi,
5971
source_elgamal_keypair.pubkey(),
6072
supply_elgamal_pubkey,
6173
auditor_elgamal_pubkey,
6274
);
63-
75+
#[cfg(not(target_arch = "wasm32"))]
76+
let grouped_ciphertext_hi = burn_amount_ciphertext_hi.0;
77+
#[cfg(target_arch = "wasm32")]
78+
let grouped_ciphertext_hi = GroupedElGamalCiphertext3Handles::encryption_with_u64(
79+
source_elgamal_keypair.pubkey(),
80+
supply_elgamal_pubkey,
81+
auditor_elgamal_pubkey,
82+
burn_amount_hi,
83+
&burn_amount_opening_hi,
84+
);
6485
// decrypt the current available balance at the source
6586
let current_decrypted_available_balance = current_decryptable_available_balance
6687
.decrypt(source_aes_key)
@@ -108,8 +129,8 @@ pub fn burn_split_proof_data(
108129
source_elgamal_keypair.pubkey(),
109130
supply_elgamal_pubkey,
110131
auditor_elgamal_pubkey,
111-
&burn_amount_ciphertext_lo.0,
112-
&burn_amount_ciphertext_hi.0,
132+
&grouped_ciphertext_lo,
133+
&grouped_ciphertext_hi,
113134
burn_amount_lo,
114135
burn_amount_hi,
115136
&burn_amount_opening_lo,

confidential-transfer/proof-generation/src/mint.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(target_arch = "wasm32")]
2+
use solana_zk_sdk::encryption::grouped_elgamal::GroupedElGamalCiphertext3Handles;
13
use {
24
crate::{
35
encryption::MintAmountCiphertext, errors::TokenProofGenerationError,
@@ -52,13 +54,33 @@ pub fn mint_split_proof_data(
5254
supply_elgamal_keypair.pubkey(),
5355
auditor_elgamal_pubkey,
5456
);
57+
#[cfg(not(target_arch = "wasm32"))]
58+
let grouped_ciphertext_lo = mint_amount_grouped_ciphertext_lo.0;
59+
#[cfg(target_arch = "wasm32")]
60+
let grouped_ciphertext_lo = GroupedElGamalCiphertext3Handles::encryption_with_u64(
61+
destination_elgamal_pubkey,
62+
supply_elgamal_keypair.pubkey(),
63+
auditor_elgamal_pubkey,
64+
mint_amount_lo,
65+
&mint_amount_opening_lo,
66+
);
5567

5668
let (mint_amount_grouped_ciphertext_hi, mint_amount_opening_hi) = MintAmountCiphertext::new(
5769
mint_amount_hi,
5870
destination_elgamal_pubkey,
5971
supply_elgamal_keypair.pubkey(),
6072
auditor_elgamal_pubkey,
6173
);
74+
#[cfg(not(target_arch = "wasm32"))]
75+
let grouped_ciphertext_hi = mint_amount_grouped_ciphertext_hi.0;
76+
#[cfg(target_arch = "wasm32")]
77+
let grouped_ciphertext_hi = GroupedElGamalCiphertext3Handles::encryption_with_u64(
78+
destination_elgamal_pubkey,
79+
supply_elgamal_keypair.pubkey(),
80+
auditor_elgamal_pubkey,
81+
mint_amount_hi,
82+
&mint_amount_opening_hi,
83+
);
6284

6385
// compute the new supply ciphertext
6486
let mint_amount_ciphertext_supply_lo = mint_amount_grouped_ciphertext_lo
@@ -101,8 +123,8 @@ pub fn mint_split_proof_data(
101123
destination_elgamal_pubkey,
102124
supply_elgamal_keypair.pubkey(),
103125
auditor_elgamal_pubkey,
104-
&mint_amount_grouped_ciphertext_lo.0,
105-
&mint_amount_grouped_ciphertext_hi.0,
126+
&grouped_ciphertext_lo,
127+
&grouped_ciphertext_hi,
106128
mint_amount_lo,
107129
mint_amount_hi,
108130
&mint_amount_opening_lo,

confidential-transfer/proof-generation/src/transfer.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(target_arch = "wasm32")]
2+
use solana_zk_sdk::encryption::grouped_elgamal::GroupedElGamalCiphertext3Handles;
13
use {
24
crate::{
35
encryption::TransferAmountCiphertext, errors::TokenProofGenerationError,
@@ -55,6 +57,16 @@ pub fn transfer_split_proof_data(
5557
destination_elgamal_pubkey,
5658
auditor_elgamal_pubkey,
5759
);
60+
#[cfg(not(target_arch = "wasm32"))]
61+
let grouped_ciphertext_lo = transfer_amount_grouped_ciphertext_lo.0;
62+
#[cfg(target_arch = "wasm32")]
63+
let grouped_ciphertext_lo = GroupedElGamalCiphertext3Handles::encryption_with_u64(
64+
source_elgamal_keypair.pubkey(),
65+
destination_elgamal_pubkey,
66+
auditor_elgamal_pubkey,
67+
transfer_amount_lo,
68+
&transfer_amount_opening_lo,
69+
);
5870

5971
let (transfer_amount_grouped_ciphertext_hi, transfer_amount_opening_hi) =
6072
TransferAmountCiphertext::new(
@@ -63,6 +75,16 @@ pub fn transfer_split_proof_data(
6375
destination_elgamal_pubkey,
6476
auditor_elgamal_pubkey,
6577
);
78+
#[cfg(not(target_arch = "wasm32"))]
79+
let grouped_ciphertext_hi = transfer_amount_grouped_ciphertext_hi.0;
80+
#[cfg(target_arch = "wasm32")]
81+
let grouped_ciphertext_hi = GroupedElGamalCiphertext3Handles::encryption_with_u64(
82+
source_elgamal_keypair.pubkey(),
83+
destination_elgamal_pubkey,
84+
auditor_elgamal_pubkey,
85+
transfer_amount_hi,
86+
&transfer_amount_opening_hi,
87+
);
6688

6789
// Decrypt the current available balance at the source
6890
let current_decrypted_available_balance = current_decryptable_available_balance
@@ -112,8 +134,8 @@ pub fn transfer_split_proof_data(
112134
source_elgamal_keypair.pubkey(),
113135
destination_elgamal_pubkey,
114136
auditor_elgamal_pubkey,
115-
&transfer_amount_grouped_ciphertext_lo.0,
116-
&transfer_amount_grouped_ciphertext_hi.0,
137+
&grouped_ciphertext_lo,
138+
&grouped_ciphertext_hi,
117139
transfer_amount_lo,
118140
transfer_amount_hi,
119141
&transfer_amount_opening_lo,

confidential-transfer/proof-generation/src/transfer_with_fee.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#[cfg(not(target_arch = "wasm32"))]
2+
use solana_zk_sdk::encryption::grouped_elgamal::GroupedElGamal;
3+
#[cfg(target_arch = "wasm32")]
4+
use solana_zk_sdk::encryption::grouped_elgamal::{
5+
GroupedElGamalCiphertext2Handles, GroupedElGamalCiphertext3Handles,
6+
};
17
use {
28
crate::{
39
encryption::{FeeCiphertext, TransferAmountCiphertext},
@@ -11,7 +17,6 @@ use {
1117
encryption::{
1218
auth_encryption::{AeCiphertext, AeKey},
1319
elgamal::{ElGamalCiphertext, ElGamalKeypair, ElGamalPubkey},
14-
grouped_elgamal::GroupedElGamal,
1520
pedersen::{Pedersen, PedersenCommitment, PedersenOpening},
1621
},
1722
zk_elgamal_proof_program::proof_data::{
@@ -71,6 +76,16 @@ pub fn transfer_with_fee_split_proof_data(
7176
destination_elgamal_pubkey,
7277
auditor_elgamal_pubkey,
7378
);
79+
#[cfg(not(target_arch = "wasm32"))]
80+
let grouped_ciphertext_lo = transfer_amount_grouped_ciphertext_lo.0;
81+
#[cfg(target_arch = "wasm32")]
82+
let grouped_ciphertext_lo = GroupedElGamalCiphertext3Handles::encryption_with_u64(
83+
source_elgamal_keypair.pubkey(),
84+
destination_elgamal_pubkey,
85+
auditor_elgamal_pubkey,
86+
transfer_amount_lo,
87+
&transfer_amount_opening_lo,
88+
);
7489

7590
let (transfer_amount_grouped_ciphertext_hi, transfer_amount_opening_hi) =
7691
TransferAmountCiphertext::new(
@@ -79,6 +94,16 @@ pub fn transfer_with_fee_split_proof_data(
7994
destination_elgamal_pubkey,
8095
auditor_elgamal_pubkey,
8196
);
97+
#[cfg(not(target_arch = "wasm32"))]
98+
let grouped_ciphertext_hi = transfer_amount_grouped_ciphertext_hi.0;
99+
#[cfg(target_arch = "wasm32")]
100+
let grouped_ciphertext_hi = GroupedElGamalCiphertext3Handles::encryption_with_u64(
101+
source_elgamal_keypair.pubkey(),
102+
destination_elgamal_pubkey,
103+
auditor_elgamal_pubkey,
104+
transfer_amount_hi,
105+
&transfer_amount_opening_hi,
106+
);
82107

83108
// Decrypt the current available balance at the source
84109
let current_decrypted_available_balance = current_decryptable_available_balance
@@ -130,8 +155,8 @@ pub fn transfer_with_fee_split_proof_data(
130155
source_elgamal_keypair.pubkey(),
131156
destination_elgamal_pubkey,
132157
auditor_elgamal_pubkey,
133-
&transfer_amount_grouped_ciphertext_lo.0,
134-
&transfer_amount_grouped_ciphertext_hi.0,
158+
&grouped_ciphertext_lo,
159+
&grouped_ciphertext_hi,
135160
transfer_amount_lo,
136161
transfer_amount_hi,
137162
&transfer_amount_opening_lo,
@@ -233,6 +258,7 @@ pub fn transfer_with_fee_split_proof_data(
233258

234259
// encrypt the fee amount under the destination and withdraw withheld authority
235260
// ElGamal public key
261+
#[cfg(not(target_arch = "wasm32"))]
236262
let fee_destination_withdraw_withheld_authority_ciphertext_lo = GroupedElGamal::encrypt_with(
237263
[
238264
destination_elgamal_pubkey,
@@ -241,6 +267,16 @@ pub fn transfer_with_fee_split_proof_data(
241267
fee_amount_lo,
242268
&fee_opening_lo,
243269
);
270+
#[cfg(target_arch = "wasm32")]
271+
let fee_destination_withdraw_withheld_authority_ciphertext_lo =
272+
GroupedElGamalCiphertext2Handles::encryption_with_u64(
273+
destination_elgamal_pubkey,
274+
withdraw_withheld_authority_elgamal_pubkey,
275+
fee_amount_lo,
276+
&fee_opening_lo,
277+
);
278+
279+
#[cfg(not(target_arch = "wasm32"))]
244280
let fee_destination_withdraw_withheld_authority_ciphertext_hi = GroupedElGamal::encrypt_with(
245281
[
246282
destination_elgamal_pubkey,
@@ -249,6 +285,14 @@ pub fn transfer_with_fee_split_proof_data(
249285
fee_amount_hi,
250286
&fee_opening_hi,
251287
);
288+
#[cfg(target_arch = "wasm32")]
289+
let fee_destination_withdraw_withheld_authority_ciphertext_hi =
290+
GroupedElGamalCiphertext2Handles::encryption_with_u64(
291+
destination_elgamal_pubkey,
292+
withdraw_withheld_authority_elgamal_pubkey,
293+
fee_amount_hi,
294+
&fee_opening_hi,
295+
);
252296

253297
// generate fee ciphertext validity data
254298
let fee_ciphertext_validity_proof_data =

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"private": true,
33
"scripts": {
44
"programs:build": "zx ./scripts/rust/build-sbf.mjs program",
5+
"programs:build-wasm": "zx ./scripts/rust/build-wasm.mjs program",
56
"programs:test": "zx ./scripts/rust/test-sbf.mjs program",
67
"programs:format": "zx ./scripts/rust/format.mjs program",
78
"programs:lint": "zx ./scripts/rust/lint.mjs program",

program/src/extension/confidential_transfer_fee/processor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use {
2929
processor::Processor,
3030
solana_zk_sdk::encryption::pod::elgamal::PodElGamalPubkey,
3131
},
32-
bytemuck::Zeroable,
3332
solana_account_info::{next_account_info, AccountInfo},
3433
solana_msg::msg,
3534
solana_program_error::{ProgramError, ProgramResult},
@@ -38,6 +37,9 @@ use {
3837
spl_token_confidential_transfer_proof_extraction::instruction::verify_and_extract_context,
3938
};
4039

40+
#[cfg(not(target_arch = "wasm32"))]
41+
use bytemuck::Zeroable;
42+
4143
/// Processes an [`InitializeConfidentialTransferFeeConfig`] instruction.
4244
fn process_initialize_confidential_transfer_fee_config(
4345
accounts: &[AccountInfo],

rust-toolchain.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[toolchain]
22
channel = "1.84.1"
3+
targets = ["wasm32-unknown-unknown"]

scripts/rust/build-wasm.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env zx
2+
import 'zx/globals';
3+
import {
4+
cliArguments,
5+
workingDirectory,
6+
} from '../utils.mjs';
7+
8+
const [folder, ...args] = cliArguments();
9+
const manifestPath = path.join(workingDirectory, folder, 'Cargo.toml');
10+
await $`cargo build --target wasm32-unknown-unknown --manifest-path ${manifestPath} ${args}`;

0 commit comments

Comments
 (0)