@@ -7,35 +7,34 @@ use super::{
7
7
RevealedToken , Scalar ,
8
8
} ;
9
9
use ark_std:: rand:: Rng ;
10
- use barnett:: BarnettSmartProtocol ;
10
+ use barnett:: { BarnettSmartProtocol , Mask } ;
11
11
use proof_essentials:: utils:: { permutation:: Permutation , rand:: sample_vector} ;
12
12
use rand:: thread_rng;
13
13
use serde:: { Deserialize , Serialize } ;
14
14
use std:: collections:: HashMap ;
15
+ use wasm_bindgen:: prelude:: * ;
15
16
17
+ #[ wasm_bindgen]
16
18
#[ derive( Clone ) ]
17
19
pub struct Player {
18
20
name : Vec < u8 > ,
19
21
sk : PlayerSecretKey ,
20
22
pk : PlayerPublicKey ,
21
- proof_key : ProofKeyOwnership ,
22
- cards : Vec < MaskedCard > ,
23
- opened_cards : Vec < Option < ClassicPlayingCard > > ,
24
23
}
25
24
25
+ #[ wasm_bindgen]
26
26
#[ derive( Serialize , Deserialize ) ]
27
27
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 ,
32
31
}
33
32
34
33
impl Surrogate {
35
34
pub fn verify ( & self , pp : & CardParameters ) -> bool {
36
35
CardProtocol :: verify_key_ownership (
37
36
pp. into ( ) ,
38
- & self . pk ,
37
+ & self . pk . 0 ,
39
38
& self . name ,
40
39
& self . proof_key . into ( ) ,
41
40
)
@@ -50,15 +49,10 @@ impl Player {
50
49
name : & Vec < u8 > ,
51
50
) -> Result < Self > {
52
51
let ( pk, sk) = CardProtocol :: player_keygen ( rng, pp. into ( ) ) ?;
53
- let proof_key =
54
- CardProtocol :: prove_key_ownership ( rng, pp. into ( ) , & pk, & sk, name) ?;
55
52
Ok ( Self {
56
53
name : name. clone ( ) ,
57
54
sk,
58
- pk,
59
- proof_key : proof_key. into ( ) ,
60
- cards : vec ! [ ] ,
61
- opened_cards : vec ! [ ] ,
55
+ pk : PlayerPublicKey ( pk) ,
62
56
} )
63
57
}
64
58
@@ -67,7 +61,7 @@ impl Player {
67
61
let proof_key = CardProtocol :: prove_key_ownership (
68
62
rng,
69
63
pp. into ( ) ,
70
- & self . pk ,
64
+ & self . pk . 0 ,
71
65
& self . sk ,
72
66
& self . name ,
73
67
)
@@ -80,14 +74,6 @@ impl Player {
80
74
}
81
75
}
82
76
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
-
91
77
pub fn shuffle (
92
78
& self ,
93
79
parameters : & CardParameters ,
@@ -103,7 +89,7 @@ impl Player {
103
89
let ( shuffled_deck, shuffle_proof) = CardProtocol :: shuffle_and_remask (
104
90
& mut rng,
105
91
parameters. into ( ) ,
106
- joint_pk,
92
+ & joint_pk. 0 ,
107
93
& deck,
108
94
& masking_factors,
109
95
& permutation,
@@ -118,7 +104,6 @@ impl Player {
118
104
}
119
105
120
106
pub fn verify_shuffle (
121
- & self ,
122
107
parameters : & CardParameters ,
123
108
joint_pk : & AggregatePublicKey ,
124
109
original_deck : & Vec < MaskedCard > ,
@@ -135,67 +120,58 @@ impl Player {
135
120
. collect :: < Vec < _ > > ( ) ;
136
121
CardProtocol :: verify_shuffle (
137
122
parameters. into ( ) ,
138
- joint_pk,
123
+ & joint_pk. 0 ,
139
124
& original_deck,
140
125
& shuffled_deck,
141
126
proof_shuffle. into ( ) ,
142
127
)
143
128
. map_err ( |e| GameErrors :: CryptoError ( e) )
144
129
}
145
130
146
- pub fn receive_card ( & mut self , card : MaskedCard ) {
147
- self . cards . push ( card) ;
148
- self . opened_cards . push ( None ) ;
149
- }
150
-
151
131
pub fn peek_at_card (
152
- & mut self ,
153
132
parameters : & CardParameters ,
154
- reveal_tokens : & mut Vec < RevealedToken > ,
133
+ reveal_tokens : & Vec < RevealedToken > ,
155
134
card_mappings : & HashMap < Card , Vec < u8 > > ,
156
135
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 ) ?;
170
142
171
143
let raw_reveal_tokens = reveal_tokens
172
144
. iter ( )
173
- . map ( |t| ( t. token . into ( ) , t. proof . into ( ) , t. player ) )
145
+ . map ( |t| ( t. token . into ( ) , t. proof . into ( ) , t. player . 0 ) )
174
146
. collect :: < Vec < _ > > ( ) ;
175
147
176
148
let unmasked_card =
177
149
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 ) ?;
180
153
181
- self . opened_cards [ i] = Some ( serde_json:: from_slice ( opened_card) . unwrap ( ) ) ;
182
- Ok ( ( ) )
154
+ Ok ( opened_card. to_owned ( ) )
183
155
}
184
156
185
157
pub fn compute_reveal_token < R : Rng > (
186
158
& self ,
187
159
rng : & mut R ,
188
160
pp : & CardParameters ,
189
161
card : & MaskedCard ,
190
- ) -> Result < ( RevealToken , ProofReveal , PlayerPublicKey ) > {
162
+ ) -> Result < RevealedToken > {
191
163
let ( reveal_token, reveal_proof) = CardProtocol :: compute_reveal_token (
192
164
rng,
193
165
pp. into ( ) ,
194
166
& self . sk ,
195
- & self . pk ,
167
+ & self . pk . 0 ,
196
168
card. into ( ) ,
197
169
) ?;
198
170
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
+ } )
200
176
}
201
177
}
0 commit comments