diff --git a/src/components/zkcards_wasm/src/wasm.rs b/src/components/zkcards_wasm/src/wasm.rs index eda903f4d..519e06e0e 100644 --- a/src/components/zkcards_wasm/src/wasm.rs +++ b/src/components/zkcards_wasm/src/wasm.rs @@ -12,4 +12,88 @@ #![deny(missing_docs)] #![allow(clippy::needless_borrow)] +//todo: remove `unwrap` +//todo: more comments + mod zkcards; + +use crate::zkcards::{ + AggregatePublicKey, Card, CardParameters, MaskedCard, Player, ProofShuffle, + RevealedToken, Surrogate, +}; +use rand::thread_rng; +use std::collections::HashMap; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +/// create a new player with `name` and `card parameters` received from contract +pub fn new_player(pp: CardParameters, name: Vec) -> Player { + let rng = &mut thread_rng(); + Player::new(rng, &pp, &name).unwrap() +} + +#[wasm_bindgen] +/// generate a `surrogate` with `ProofKeyOwnerShip` as this player's behave +pub fn new_surrogate(player: &Player, pp: &CardParameters) -> Surrogate { + player.new_surrogate(pp) +} + +#[wasm_bindgen] +/// verify a player +pub fn verify_proof_pk(player: Surrogate, pp: &CardParameters) -> bool { + player.verify(&pp) +} + +#[wasm_bindgen] +/// Perform a shuffle operation +pub fn shuffle( + player: &Player, + pp: &CardParameters, + deck: &Vec, + joint_pk: &AggregatePublicKey, + nums_of_cards: usize, +) -> (Vec, ProofShuffle) { + player.shuffle(pp, deck, joint_pk, nums_of_cards).unwrap() +} + +#[wasm_bindgen] +/// verify shuffled deck from another player +pub fn verify_shuffle( + parameters: &CardParameters, + joint_pk: &AggregatePublicKey, + original_deck: &Vec, + shuffled_deck: &Vec, + proof_shuffle: &ProofShuffle, +) -> bool { + Player::verify_shuffle( + parameters, + joint_pk, + original_deck, + shuffled_deck, + proof_shuffle, + ) + .is_ok() +} + +#[wasm_bindgen] +/// reveal a card +pub fn compute_reveal_token( + player: &Player, + card: &MaskedCard, + pp: &CardParameters, +) -> RevealedToken { + let rng = &mut thread_rng(); + player.compute_reveal_token(rng, pp, card).unwrap() +} + +#[wasm_bindgen] +/// open a card +pub fn open_card( + parameters: &CardParameters, + reveal_tokens: &Vec, + card_mappings: &HashMap>, + card: &MaskedCard, + cards: &Vec, +) -> Vec { + Player::peek_at_card(parameters, reveal_tokens, card_mappings, card, cards).unwrap() +} diff --git a/src/components/zkcards_wasm/src/zkcards/mod.rs b/src/components/zkcards_wasm/src/zkcards/mod.rs index 38822f8db..37cfce2ec 100644 --- a/src/components/zkcards_wasm/src/zkcards/mod.rs +++ b/src/components/zkcards_wasm/src/zkcards/mod.rs @@ -2,6 +2,9 @@ mod error; mod player; mod user_card; +pub use player::*; +pub use user_card::*; + use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use barnett::discrete_log_cards; use proof_essentials::{ @@ -51,9 +54,20 @@ impl<'a> From<&'a CardParameters> for &'a discrete_log_cards::Parameters } } -pub type PlayerPublicKey = discrete_log_cards::PublicKey; -pub type PlayerSecretKey = discrete_log_cards::PlayerSecretKey; -pub type AggregatePublicKey = discrete_log_cards::PublicKey; +type PlayerSecretKey = discrete_log_cards::PlayerSecretKey; + +#[wasm_bindgen] +#[derive(Clone, Copy, Serialize, Deserialize)] +pub struct PlayerPublicKey( + #[serde(serialize_with = "ark_se", deserialize_with = "ark_de")] + pub(crate) discrete_log_cards::PublicKey, +); +#[wasm_bindgen] +#[derive(Clone, Serialize, Deserialize)] +pub struct AggregatePublicKey( + #[serde(serialize_with = "ark_se", deserialize_with = "ark_de")] + pub(crate) discrete_log_cards::PublicKey, +); #[wasm_bindgen] #[derive(Clone, Copy, Eq, Hash, PartialEq, Debug, Serialize, Deserialize)] @@ -72,6 +86,11 @@ impl From for discrete_log_cards::Card { } } +#[wasm_bindgen] +pub struct MaskedCards { + inner: Vec, +} + #[wasm_bindgen] #[derive(Clone, Copy, Eq, Hash, PartialEq, Debug, Serialize, Deserialize)] pub struct MaskedCard( @@ -167,7 +186,6 @@ impl<'a> From<&'a ProofShuffle> for &'a shuffle::proof::Proof } } -#[wasm_bindgen] //pub struct ProofMasking(chaum_pedersen_dl_equality::proof::Proof); #[derive(Clone, Copy, Eq, Hash, PartialEq, Debug, Deserialize, Serialize)] pub struct ProofRemasking( @@ -185,13 +203,12 @@ impl From for chaum_pedersen_dl_equality::proof::Proof { } } -//#[wasm_bindgen] +#[wasm_bindgen] #[derive(Serialize, Deserialize)] pub struct RevealedToken { - pub token: RevealToken, - pub proof: ProofReveal, - #[serde(serialize_with = "ark_se", deserialize_with = "ark_de")] - pub player: PlayerPublicKey, + pub(crate) token: RevealToken, + pub(crate) proof: ProofReveal, + pub(crate) player: PlayerPublicKey, } fn ark_se(a: &A, s: S) -> Result diff --git a/src/components/zkcards_wasm/src/zkcards/player.rs b/src/components/zkcards_wasm/src/zkcards/player.rs index 7bb04a341..cac39ebea 100644 --- a/src/components/zkcards_wasm/src/zkcards/player.rs +++ b/src/components/zkcards_wasm/src/zkcards/player.rs @@ -7,35 +7,34 @@ use super::{ RevealedToken, Scalar, }; use ark_std::rand::Rng; -use barnett::BarnettSmartProtocol; +use barnett::{BarnettSmartProtocol, Mask}; use proof_essentials::utils::{permutation::Permutation, rand::sample_vector}; use rand::thread_rng; use serde::{Deserialize, Serialize}; use std::collections::HashMap; +use wasm_bindgen::prelude::*; +#[wasm_bindgen] #[derive(Clone)] pub struct Player { name: Vec, sk: PlayerSecretKey, pk: PlayerPublicKey, - proof_key: ProofKeyOwnership, - cards: Vec, - opened_cards: Vec>, } +#[wasm_bindgen] #[derive(Serialize, Deserialize)] pub struct Surrogate { - pub name: Vec, - #[serde(serialize_with = "ark_se", deserialize_with = "ark_de")] - pub pk: PlayerPublicKey, - pub proof_key: ProofKeyOwnership, + pub(crate) name: Vec, + pub(crate) pk: PlayerPublicKey, + pub(crate) proof_key: ProofKeyOwnership, } impl Surrogate { pub fn verify(&self, pp: &CardParameters) -> bool { CardProtocol::verify_key_ownership( pp.into(), - &self.pk, + &self.pk.0, &self.name, &self.proof_key.into(), ) @@ -50,15 +49,10 @@ impl Player { name: &Vec, ) -> Result { let (pk, sk) = CardProtocol::player_keygen(rng, pp.into())?; - let proof_key = - CardProtocol::prove_key_ownership(rng, pp.into(), &pk, &sk, name)?; Ok(Self { name: name.clone(), sk, - pk, - proof_key: proof_key.into(), - cards: vec![], - opened_cards: vec![], + pk: PlayerPublicKey(pk), }) } @@ -67,7 +61,7 @@ impl Player { let proof_key = CardProtocol::prove_key_ownership( rng, pp.into(), - &self.pk, + &self.pk.0, &self.sk, &self.name, ) @@ -80,14 +74,6 @@ impl Player { } } - pub fn surrogate(&self) -> Surrogate { - Surrogate { - name: self.name.clone(), - pk: self.pk, - proof_key: self.proof_key, - } - } - pub fn shuffle( &self, parameters: &CardParameters, @@ -103,7 +89,7 @@ impl Player { let (shuffled_deck, shuffle_proof) = CardProtocol::shuffle_and_remask( &mut rng, parameters.into(), - joint_pk, + &joint_pk.0, &deck, &masking_factors, &permutation, @@ -118,7 +104,6 @@ impl Player { } pub fn verify_shuffle( - &self, parameters: &CardParameters, joint_pk: &AggregatePublicKey, original_deck: &Vec, @@ -135,7 +120,7 @@ impl Player { .collect::>(); CardProtocol::verify_shuffle( parameters.into(), - joint_pk, + &joint_pk.0, &original_deck, &shuffled_deck, proof_shuffle.into(), @@ -143,43 +128,30 @@ impl Player { .map_err(|e| GameErrors::CryptoError(e)) } - pub fn receive_card(&mut self, card: MaskedCard) { - self.cards.push(card); - self.opened_cards.push(None); - } - pub fn peek_at_card( - &mut self, parameters: &CardParameters, - reveal_tokens: &mut Vec, + reveal_tokens: &Vec, card_mappings: &HashMap>, card: &MaskedCard, - ) -> Result<()> { - let i = self.cards.iter().position(|&x| x == *card); - - let i = i.ok_or(GameErrors::CardNotFound)?; - - //TODO add function to create that without the proof - let rng = &mut thread_rng(); - let own_reveal_token = self.compute_reveal_token(rng, parameters, card)?; - reveal_tokens.push(RevealedToken { - token: own_reveal_token.0, - proof: own_reveal_token.1, - player: own_reveal_token.2, - }); + cards: &Vec, + ) -> Result> { + let _ = cards + .iter() + .position(|&x| x == *card) + .ok_or(GameErrors::CardNotFound)?; let raw_reveal_tokens = reveal_tokens .iter() - .map(|t| (t.token.into(), t.proof.into(), t.player)) + .map(|t| (t.token.into(), t.proof.into(), t.player.0)) .collect::>(); let unmasked_card = CardProtocol::unmask(parameters.into(), &raw_reveal_tokens, card.into())?; - let opened_card = card_mappings.get(&unmasked_card.into()); - let opened_card = opened_card.ok_or(GameErrors::InvalidCard)?; + let opened_card = card_mappings + .get(&unmasked_card.into()) + .ok_or(GameErrors::InvalidCard)?; - self.opened_cards[i] = Some(serde_json::from_slice(opened_card).unwrap()); - Ok(()) + Ok(opened_card.to_owned()) } pub fn compute_reveal_token( @@ -187,15 +159,19 @@ impl Player { rng: &mut R, pp: &CardParameters, card: &MaskedCard, - ) -> Result<(RevealToken, ProofReveal, PlayerPublicKey)> { + ) -> Result { let (reveal_token, reveal_proof) = CardProtocol::compute_reveal_token( rng, pp.into(), &self.sk, - &self.pk, + &self.pk.0, card.into(), )?; - Ok((reveal_token.into(), reveal_proof.into(), self.pk)) + Ok(RevealedToken { + token: reveal_token.into(), + proof: reveal_proof.into(), + player: self.pk, + }) } }