Skip to content

Commit

Permalink
restructure export data structures and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjiao committed Apr 22, 2023
1 parent b2a7185 commit 8f6a932
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/components/zkcards_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rand = "0.8"
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
wasm-bindgen = { version = "0.2.84", features = ["serde-serialize"] }
serde-wasm-bindgen = "0.5.0"

# Must enable the "js"-feature,
# OR the compiling will fail.
Expand Down
123 changes: 123 additions & 0 deletions src/components/zkcards_wasm/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,127 @@
#![deny(missing_docs)]
#![allow(clippy::needless_borrow)]

//todo: remove `unwrap`
//todo: more comments

mod zkcards;

use crate::zkcards::{
AggregatePublicKey, Card, CardParameters, MaskedCard, Player, ProofShuffle,
RevealToken, RevealedToken, ShuffleResult, Surrogate,
};
use rand::thread_rng;
use std::collections::HashMap;
use std::hash::Hash;
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<u8>) -> 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: JsValue,
joint_pk: &AggregatePublicKey,
nums_of_cards: usize,
) -> JsValue {
let raw_deck: Vec<Vec<u8>> = serde_wasm_bindgen::from_value(deck).unwrap();
let deck = raw_deck
.into_iter()
.map(|c| serde_json::from_slice(c.as_slice()).unwrap())
.collect::<Vec<MaskedCard>>();
let (tokens, proof) = player.shuffle(pp, &deck, joint_pk, nums_of_cards).unwrap();
let res = ShuffleResult { tokens, proof };

serde_wasm_bindgen::to_value(&res).unwrap()
}

#[wasm_bindgen]
/// verify shuffled deck from another player
pub fn verify_shuffle(
parameters: &CardParameters,
joint_pk: &AggregatePublicKey,
original_deck: JsValue,
shuffled_deck: JsValue,
proof_shuffle: &ProofShuffle,
) -> bool {
let raw_original_deck: Vec<Vec<u8>> =
serde_wasm_bindgen::from_value(original_deck).unwrap();
let original_deck = raw_original_deck
.into_iter()
.map(|c| serde_json::from_slice(c.as_slice()).unwrap())
.collect::<Vec<MaskedCard>>();
let raw_shuffled_deck: Vec<Vec<u8>> =
serde_wasm_bindgen::from_value(shuffled_deck).unwrap();
let shuffled_deck = raw_shuffled_deck
.into_iter()
.map(|c| serde_json::from_slice(c.as_slice()).unwrap())
.collect::<Vec<MaskedCard>>();

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: JsValue,
card_mappings: JsValue,
card: &MaskedCard,
cards: JsValue,
) -> JsValue {
let raw_reveal_tokens: Vec<Vec<u8>> =
serde_wasm_bindgen::from_value(reveal_tokens).unwrap();
let reveal_tokens = raw_reveal_tokens
.into_iter()
.map(|c| serde_json::from_slice(c.as_slice()).unwrap())
.collect::<Vec<RevealedToken>>();
let card_mappings: HashMap<Card, Vec<u8>> =
serde_wasm_bindgen::from_value(card_mappings).unwrap();
let raw_cards: Vec<Vec<u8>> = serde_wasm_bindgen::from_value(cards).unwrap();
let cards = raw_cards
.into_iter()
.map(|c| serde_json::from_slice(c.as_slice()).unwrap())
.collect::<Vec<MaskedCard>>();

let play_card =
Player::peek_at_card(parameters, &reveal_tokens, &card_mappings, card, &cards)
.unwrap();
serde_wasm_bindgen::to_value(&play_card).unwrap()
}
47 changes: 38 additions & 9 deletions src/components/zkcards_wasm/src/zkcards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -51,9 +54,20 @@ impl<'a> From<&'a CardParameters> for &'a discrete_log_cards::Parameters<Curve>
}
}

pub type PlayerPublicKey = discrete_log_cards::PublicKey<Curve>;
pub type PlayerSecretKey = discrete_log_cards::PlayerSecretKey<Curve>;
pub type AggregatePublicKey = discrete_log_cards::PublicKey<Curve>;
type PlayerSecretKey = discrete_log_cards::PlayerSecretKey<Curve>;

#[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<Curve>,
);
#[wasm_bindgen]
#[derive(Clone, Serialize, Deserialize)]
pub struct AggregatePublicKey(
#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
pub(crate) discrete_log_cards::PublicKey<Curve>,
);

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

#[wasm_bindgen]
pub struct MaskedCards {
inner: Vec<MaskedCard>,
}

#[wasm_bindgen]
#[derive(Clone, Copy, Eq, Hash, PartialEq, Debug, Serialize, Deserialize)]
pub struct MaskedCard(
Expand Down Expand Up @@ -167,7 +186,6 @@ impl<'a> From<&'a ProofShuffle> for &'a shuffle::proof::Proof<Scalar, Enc, Comm>
}
}

#[wasm_bindgen]
//pub struct ProofMasking(chaum_pedersen_dl_equality::proof::Proof<Curve>);
#[derive(Clone, Copy, Eq, Hash, PartialEq, Debug, Deserialize, Serialize)]
pub struct ProofRemasking(
Expand All @@ -185,13 +203,24 @@ impl From<ProofRemasking> for chaum_pedersen_dl_equality::proof::Proof<Curve> {
}
}

//#[wasm_bindgen]
#[derive(Serialize, Deserialize)]
pub struct ShuffleResult {
pub tokens: Vec<MaskedCard>,
pub proof: ProofShuffle,
}

impl ShuffleResult {
pub fn new(tokens: Vec<MaskedCard>, proof: ProofShuffle) -> Self {
Self { tokens, proof }
}
}

#[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<S, A: CanonicalSerialize>(a: &A, s: S) -> Result<S::Ok, S::Error>
Expand Down
Loading

0 comments on commit 8f6a932

Please sign in to comment.