Skip to content

Commit 61f2971

Browse files
author
simonjiao
committed
restructure export data structures and functions
1 parent b2a7185 commit 61f2971

File tree

3 files changed

+141
-64
lines changed

3 files changed

+141
-64
lines changed

src/components/zkcards_wasm/src/wasm.rs

+84
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,88 @@
1212
#![deny(missing_docs)]
1313
#![allow(clippy::needless_borrow)]
1414

15+
//todo: remove `unwrap`
16+
//todo: more comments
17+
1518
mod zkcards;
19+
20+
use crate::zkcards::{
21+
AggregatePublicKey, Card, CardParameters, MaskedCard, Player, ProofShuffle,
22+
RevealedToken, Surrogate,
23+
};
24+
use rand::thread_rng;
25+
use std::collections::HashMap;
26+
use wasm_bindgen::prelude::*;
27+
28+
#[wasm_bindgen]
29+
/// create a new player with `name` and `card parameters` received from contract
30+
pub fn new_player(pp: CardParameters, name: Vec<u8>) -> Player {
31+
let rng = &mut thread_rng();
32+
Player::new(rng, &pp, &name).unwrap()
33+
}
34+
35+
#[wasm_bindgen]
36+
/// generate a `surrogate` with `ProofKeyOwnerShip` as this player's behave
37+
pub fn new_surrogate(player: &Player, pp: &CardParameters) -> Surrogate {
38+
player.new_surrogate(pp)
39+
}
40+
41+
#[wasm_bindgen]
42+
/// verify a player
43+
pub fn verify_proof_pk(player: Surrogate, pp: &CardParameters) -> bool {
44+
player.verify(&pp)
45+
}
46+
47+
#[wasm_bindgen]
48+
/// Perform a shuffle operation
49+
pub fn shuffle(
50+
player: &Player,
51+
pp: &CardParameters,
52+
deck: &Vec<MaskedCard>,
53+
joint_pk: &AggregatePublicKey,
54+
nums_of_cards: usize,
55+
) -> (Vec<MaskedCard>, ProofShuffle) {
56+
player.shuffle(pp, deck, joint_pk, nums_of_cards).unwrap()
57+
}
58+
59+
#[wasm_bindgen]
60+
/// verify shuffled deck from another player
61+
pub fn verify_shuffle(
62+
parameters: &CardParameters,
63+
joint_pk: &AggregatePublicKey,
64+
original_deck: &Vec<MaskedCard>,
65+
shuffled_deck: &Vec<MaskedCard>,
66+
proof_shuffle: &ProofShuffle,
67+
) -> bool {
68+
Player::verify_shuffle(
69+
parameters,
70+
joint_pk,
71+
original_deck,
72+
shuffled_deck,
73+
proof_shuffle,
74+
)
75+
.is_ok()
76+
}
77+
78+
#[wasm_bindgen]
79+
/// reveal a card
80+
pub fn compute_reveal_token(
81+
player: &Player,
82+
card: &MaskedCard,
83+
pp: &CardParameters,
84+
) -> RevealedToken {
85+
let rng = &mut thread_rng();
86+
player.compute_reveal_token(rng, pp, card).unwrap()
87+
}
88+
89+
#[wasm_bindgen]
90+
/// open a card
91+
pub fn open_card(
92+
parameters: &CardParameters,
93+
reveal_tokens: &Vec<RevealedToken>,
94+
card_mappings: &HashMap<Card, Vec<u8>>,
95+
card: &MaskedCard,
96+
cards: &Vec<MaskedCard>,
97+
) -> Vec<u8> {
98+
Player::peek_at_card(parameters, reveal_tokens, card_mappings, card, cards).unwrap()
99+
}

src/components/zkcards_wasm/src/zkcards/mod.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ mod error;
22
mod player;
33
mod user_card;
44

5+
pub use player::*;
6+
pub use user_card::*;
7+
58
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
69
use barnett::discrete_log_cards;
710
use proof_essentials::{
@@ -51,9 +54,20 @@ impl<'a> From<&'a CardParameters> for &'a discrete_log_cards::Parameters<Curve>
5154
}
5255
}
5356

54-
pub type PlayerPublicKey = discrete_log_cards::PublicKey<Curve>;
55-
pub type PlayerSecretKey = discrete_log_cards::PlayerSecretKey<Curve>;
56-
pub type AggregatePublicKey = discrete_log_cards::PublicKey<Curve>;
57+
type PlayerSecretKey = discrete_log_cards::PlayerSecretKey<Curve>;
58+
59+
#[wasm_bindgen]
60+
#[derive(Clone, Copy, Serialize, Deserialize)]
61+
pub struct PlayerPublicKey(
62+
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
63+
pub(crate) discrete_log_cards::PublicKey<Curve>,
64+
);
65+
#[wasm_bindgen]
66+
#[derive(Clone, Serialize, Deserialize)]
67+
pub struct AggregatePublicKey(
68+
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
69+
pub(crate) discrete_log_cards::PublicKey<Curve>,
70+
);
5771

5872
#[wasm_bindgen]
5973
#[derive(Clone, Copy, Eq, Hash, PartialEq, Debug, Serialize, Deserialize)]
@@ -72,6 +86,11 @@ impl From<Card> for discrete_log_cards::Card<Curve> {
7286
}
7387
}
7488

89+
#[wasm_bindgen]
90+
pub struct MaskedCards {
91+
inner: Vec<MaskedCard>,
92+
}
93+
7594
#[wasm_bindgen]
7695
#[derive(Clone, Copy, Eq, Hash, PartialEq, Debug, Serialize, Deserialize)]
7796
pub struct MaskedCard(
@@ -167,7 +186,6 @@ impl<'a> From<&'a ProofShuffle> for &'a shuffle::proof::Proof<Scalar, Enc, Comm>
167186
}
168187
}
169188

170-
#[wasm_bindgen]
171189
//pub struct ProofMasking(chaum_pedersen_dl_equality::proof::Proof<Curve>);
172190
#[derive(Clone, Copy, Eq, Hash, PartialEq, Debug, Deserialize, Serialize)]
173191
pub struct ProofRemasking(
@@ -185,13 +203,12 @@ impl From<ProofRemasking> for chaum_pedersen_dl_equality::proof::Proof<Curve> {
185203
}
186204
}
187205

188-
//#[wasm_bindgen]
206+
#[wasm_bindgen]
189207
#[derive(Serialize, Deserialize)]
190208
pub struct RevealedToken {
191-
pub token: RevealToken,
192-
pub proof: ProofReveal,
193-
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
194-
pub player: PlayerPublicKey,
209+
pub(crate) token: RevealToken,
210+
pub(crate) proof: ProofReveal,
211+
pub(crate) player: PlayerPublicKey,
195212
}
196213

197214
fn ark_se<S, A: CanonicalSerialize>(a: &A, s: S) -> Result<S::Ok, S::Error>

src/components/zkcards_wasm/src/zkcards/player.rs

+31-55
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,34 @@ use super::{
77
RevealedToken, Scalar,
88
};
99
use ark_std::rand::Rng;
10-
use barnett::BarnettSmartProtocol;
10+
use barnett::{BarnettSmartProtocol, Mask};
1111
use proof_essentials::utils::{permutation::Permutation, rand::sample_vector};
1212
use rand::thread_rng;
1313
use serde::{Deserialize, Serialize};
1414
use std::collections::HashMap;
15+
use wasm_bindgen::prelude::*;
1516

17+
#[wasm_bindgen]
1618
#[derive(Clone)]
1719
pub struct Player {
1820
name: Vec<u8>,
1921
sk: PlayerSecretKey,
2022
pk: PlayerPublicKey,
21-
proof_key: ProofKeyOwnership,
22-
cards: Vec<MaskedCard>,
23-
opened_cards: Vec<Option<ClassicPlayingCard>>,
2423
}
2524

25+
#[wasm_bindgen]
2626
#[derive(Serialize, Deserialize)]
2727
pub struct Surrogate {
28-
pub name: Vec<u8>,
29-
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
30-
pub pk: PlayerPublicKey,
31-
pub proof_key: ProofKeyOwnership,
28+
pub(crate) name: Vec<u8>,
29+
pub(crate) pk: PlayerPublicKey,
30+
pub(crate) proof_key: ProofKeyOwnership,
3231
}
3332

3433
impl Surrogate {
3534
pub fn verify(&self, pp: &CardParameters) -> bool {
3635
CardProtocol::verify_key_ownership(
3736
pp.into(),
38-
&self.pk,
37+
&self.pk.0,
3938
&self.name,
4039
&self.proof_key.into(),
4140
)
@@ -50,15 +49,10 @@ impl Player {
5049
name: &Vec<u8>,
5150
) -> Result<Self> {
5251
let (pk, sk) = CardProtocol::player_keygen(rng, pp.into())?;
53-
let proof_key =
54-
CardProtocol::prove_key_ownership(rng, pp.into(), &pk, &sk, name)?;
5552
Ok(Self {
5653
name: name.clone(),
5754
sk,
58-
pk,
59-
proof_key: proof_key.into(),
60-
cards: vec![],
61-
opened_cards: vec![],
55+
pk: PlayerPublicKey(pk),
6256
})
6357
}
6458

@@ -67,7 +61,7 @@ impl Player {
6761
let proof_key = CardProtocol::prove_key_ownership(
6862
rng,
6963
pp.into(),
70-
&self.pk,
64+
&self.pk.0,
7165
&self.sk,
7266
&self.name,
7367
)
@@ -80,14 +74,6 @@ impl Player {
8074
}
8175
}
8276

83-
pub fn surrogate(&self) -> Surrogate {
84-
Surrogate {
85-
name: self.name.clone(),
86-
pk: self.pk,
87-
proof_key: self.proof_key,
88-
}
89-
}
90-
9177
pub fn shuffle(
9278
&self,
9379
parameters: &CardParameters,
@@ -103,7 +89,7 @@ impl Player {
10389
let (shuffled_deck, shuffle_proof) = CardProtocol::shuffle_and_remask(
10490
&mut rng,
10591
parameters.into(),
106-
joint_pk,
92+
&joint_pk.0,
10793
&deck,
10894
&masking_factors,
10995
&permutation,
@@ -118,7 +104,6 @@ impl Player {
118104
}
119105

120106
pub fn verify_shuffle(
121-
&self,
122107
parameters: &CardParameters,
123108
joint_pk: &AggregatePublicKey,
124109
original_deck: &Vec<MaskedCard>,
@@ -135,67 +120,58 @@ impl Player {
135120
.collect::<Vec<_>>();
136121
CardProtocol::verify_shuffle(
137122
parameters.into(),
138-
joint_pk,
123+
&joint_pk.0,
139124
&original_deck,
140125
&shuffled_deck,
141126
proof_shuffle.into(),
142127
)
143128
.map_err(|e| GameErrors::CryptoError(e))
144129
}
145130

146-
pub fn receive_card(&mut self, card: MaskedCard) {
147-
self.cards.push(card);
148-
self.opened_cards.push(None);
149-
}
150-
151131
pub fn peek_at_card(
152-
&mut self,
153132
parameters: &CardParameters,
154-
reveal_tokens: &mut Vec<RevealedToken>,
133+
reveal_tokens: &Vec<RevealedToken>,
155134
card_mappings: &HashMap<Card, Vec<u8>>,
156135
card: &MaskedCard,
157-
) -> Result<()> {
158-
let i = self.cards.iter().position(|&x| x == *card);
159-
160-
let i = i.ok_or(GameErrors::CardNotFound)?;
161-
162-
//TODO add function to create that without the proof
163-
let rng = &mut thread_rng();
164-
let own_reveal_token = self.compute_reveal_token(rng, parameters, card)?;
165-
reveal_tokens.push(RevealedToken {
166-
token: own_reveal_token.0,
167-
proof: own_reveal_token.1,
168-
player: own_reveal_token.2,
169-
});
136+
cards: &Vec<MaskedCard>,
137+
) -> Result<Vec<u8>> {
138+
let _ = cards
139+
.iter()
140+
.position(|&x| x == *card)
141+
.ok_or(GameErrors::CardNotFound)?;
170142

171143
let raw_reveal_tokens = reveal_tokens
172144
.iter()
173-
.map(|t| (t.token.into(), t.proof.into(), t.player))
145+
.map(|t| (t.token.into(), t.proof.into(), t.player.0))
174146
.collect::<Vec<_>>();
175147

176148
let unmasked_card =
177149
CardProtocol::unmask(parameters.into(), &raw_reveal_tokens, card.into())?;
178-
let opened_card = card_mappings.get(&unmasked_card.into());
179-
let opened_card = opened_card.ok_or(GameErrors::InvalidCard)?;
150+
let opened_card = card_mappings
151+
.get(&unmasked_card.into())
152+
.ok_or(GameErrors::InvalidCard)?;
180153

181-
self.opened_cards[i] = Some(serde_json::from_slice(opened_card).unwrap());
182-
Ok(())
154+
Ok(opened_card.to_owned())
183155
}
184156

185157
pub fn compute_reveal_token<R: Rng>(
186158
&self,
187159
rng: &mut R,
188160
pp: &CardParameters,
189161
card: &MaskedCard,
190-
) -> Result<(RevealToken, ProofReveal, PlayerPublicKey)> {
162+
) -> Result<RevealedToken> {
191163
let (reveal_token, reveal_proof) = CardProtocol::compute_reveal_token(
192164
rng,
193165
pp.into(),
194166
&self.sk,
195-
&self.pk,
167+
&self.pk.0,
196168
card.into(),
197169
)?;
198170

199-
Ok((reveal_token.into(), reveal_proof.into(), self.pk))
171+
Ok(RevealedToken {
172+
token: reveal_token.into(),
173+
proof: reveal_proof.into(),
174+
player: self.pk,
175+
})
200176
}
201177
}

0 commit comments

Comments
 (0)