From a8eddbc9c76d50546e10fcbd9cee81c1f6c7c1de Mon Sep 17 00:00:00 2001 From: sheagrief <3a.mad1earth4@gmail.com> Date: Thu, 17 Oct 2024 13:55:13 +0900 Subject: [PATCH 1/8] Revise gitignore to rename werewolf data dir --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index df29a0e8..d7f23fb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ target/ /outputs inputs/inputs.json -werewolf/ \ No newline at end of file +werewolf_game/ \ No newline at end of file From d09e3ea93cdcb34dcb583babc54479cb2ee7a08c Mon Sep 17 00:00:00 2001 From: sheagrief <3a.mad1earth4@gmail.com> Date: Thu, 17 Oct 2024 13:56:17 +0900 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=8E=A8=20Refactor=20bin=5Fwerewolf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/bin_werewolf.rs | 260 +++------------------------------------ src/lib.rs | 2 + src/werewolf.rs | 2 + src/werewolf/types.rs | 136 ++++++++++++++++++++ src/werewolf/utils.rs | 130 ++++++++++++++++++++ 5 files changed, 284 insertions(+), 246 deletions(-) create mode 100644 src/werewolf.rs create mode 100644 src/werewolf/types.rs create mode 100644 src/werewolf/utils.rs diff --git a/examples/bin_werewolf.rs b/examples/bin_werewolf.rs index bf8a737d..0f683d65 100644 --- a/examples/bin_werewolf.rs +++ b/examples/bin_werewolf.rs @@ -23,11 +23,8 @@ use mpc_algebra::FromLocal; use mpc_algebra::LessThan; use mpc_algebra::Reveal; use mpc_net::{MpcMultiNet as Net, MpcNet}; -use rand::seq::SliceRandom; use rand::Rng; use serde::Deserialize; -use std::collections::BTreeMap; -use std::collections::HashMap; use std::{fs::File, path::PathBuf}; use structopt::StructOpt; use zk_mpc::circuits::LocalOrMPC; @@ -46,6 +43,10 @@ use zk_mpc::marlin::{prove_and_verify, setup_and_index}; use zk_mpc::preprocessing; use zk_mpc::serialize::{write_r, write_to_file}; use zk_mpc::she; +use zk_mpc::werewolf::{ + types::{GroupingParameter, Role, RoleData}, + utils::{calc_shuffle_matrix, generate_individual_shuffle_matrix}, +}; use nalgebra as na; @@ -330,9 +331,9 @@ fn role_assignment(opt: &Opt) -> Result<(), std::io::Error> { let grouping_parameter = GroupingParameter::new( vec![ - (Roles::Villager, (3, false)), - (Roles::FortuneTeller, (1, false)), - (Roles::Werewolf, (2, true)), + (Role::Villager, (3, false)), + (Role::FortuneTeller, (1, false)), + (Role::Werewolf, (2, true)), ] .into_iter() .collect(), @@ -446,227 +447,6 @@ fn role_assignment(opt: &Opt) -> Result<(), std::io::Error> { Ok(()) } -// Compute shuffle matrix and return role, raw role id, and player id in the same group. -fn calc_shuffle_matrix( - grouping_parameter: &GroupingParameter, - shuffle_matrix: &[na::DMatrix], - id: usize, -) -> Result<(Roles, usize, Option>), std::io::Error> { - // parameters - let n = grouping_parameter.get_num_players(); - let m = grouping_parameter.get_num_groups(); - - // generate tau matrix - let tau_matrix = grouping_parameter.generate_tau_matrix(); - - // compute rho matrix - let m_matrix = shuffle_matrix - .iter() - .fold(na::DMatrix::::identity(n + m, n + m), |acc, x| acc * x); - let rho_matrix = m_matrix.transpose() * tau_matrix * m_matrix; - - // iterate. get rho^1, rho^2, ..., rho^num_players - let mut rho_sequence = Vec::with_capacity(n); - let mut current_rho = rho_matrix.clone(); - for _ in 0..n { - rho_sequence.push(current_rho.clone()); - current_rho *= rho_matrix.clone(); // rho^(i+1) = rho^i * rho - } - - let mut unit_vec = na::DVector::::zeros(n + m); - unit_vec[id] = MFr::one(); - - // player i: for each j in {1..n}, get rho^j(i) - let result = rho_sequence - .iter() - .map(|rho| rho * unit_vec.clone()) - .map(|x| { - let index = x.column(0).into_iter().enumerate().find_map(|(j, value)| { - if *value != MFr::zero() { - Some(j) - } else { - None - } - }); - index.unwrap_or_else(|| panic!("Error: No index found")) - }) // search for the index of the one element - .collect::>(); - - println!("player {:?} result is {:?}", id, result); - - // get role value. get val which is max value in result. - let role_val = result.iter().max().expect("Failed to get max value"); - - // get role - let role = grouping_parameter.get_corresponding_role(*role_val); - - let mut fellow = result - .iter() - .filter(|x| **x != id && **x < n) - .copied() - .collect::>(); - - if fellow.is_empty() { - Ok((role, *role_val, None)) - } else { - fellow.sort(); - fellow.dedup(); - Ok((role, *role_val, Some(fellow))) - } -} - -fn generate_individual_shuffle_matrix(n: usize, m: usize, rng: &mut R) -> na::DMatrix { - let mut shuffle_matrix = na::DMatrix::::zeros(n + m, n + m); - - // generate permutation - let mut permutation: Vec = (0..n).collect(); - permutation.shuffle(rng); - - // shuffle_matrix - for i in 0..n { - shuffle_matrix[(i, permutation[i])] = MFr::one(); - } - - for i in n..n + m { - shuffle_matrix[(i, i)] = MFr::one(); - } - - shuffle_matrix -} - -#[test] -fn test_shuffle_matrix() { - let grouping_parameter = GroupingParameter::new( - vec![ - (Roles::Villager, (4, false)), - (Roles::FortuneTeller, (1, false)), - (Roles::Werewolf, (2, true)), - ] - .into_iter() - .collect(), - ); - - let shuffle_matrix = vec![generate_individual_shuffle_matrix( - grouping_parameter.get_num_players(), - grouping_parameter.get_num_groups(), - &mut test_rng(), - )]; - - for id in 0..grouping_parameter.get_num_players() { - let (role, _, player_ids) = - calc_shuffle_matrix(&grouping_parameter, &shuffle_matrix, id).unwrap(); - println!("role is {:?}", role); - println!("fellow is {:?}", player_ids); - } -} - -struct GroupingParameter(BTreeMap); - -impl GroupingParameter { - fn new(input: BTreeMap) -> Self { - Self(input) - } - - fn generate_tau_matrix(&self) -> na::DMatrix { - let num_players = self.get_num_players(); - let num_groups = self.get_num_groups(); - - let mut tau = na::DMatrix::::zeros(num_players + num_groups, num_players + num_groups); - - let mut player_index = 0; - let mut group_index = 0; - - for (_, (count, is_not_alone)) in self.0.iter() { - if *is_not_alone { - assert!( - *count >= 2, - "Error: not alone group count must be greater than 2" - ); - - // group - tau[(player_index, num_players + group_index)] = MFr::one(); - - // player - for _ in 0..*count - 1 { - tau[(player_index + 1, player_index)] = MFr::one(); - player_index += 1; - } - tau[(num_players + group_index, player_index)] = MFr::one(); - player_index += 1; - group_index += 1; - } else { - for _ in 0..*count { - // group - tau[(player_index, num_players + group_index)] = MFr::one(); - // player - tau[(num_players + group_index, player_index)] = MFr::one(); - player_index += 1; - group_index += 1; - } - } - } - - tau - } - - fn get_num_roles(&self) -> usize { - self.0.len() - } - - fn get_num_groups(&self) -> usize { - self.0 - .values() - .map(|(count, is_not_alone)| if *is_not_alone { 1 } else { *count }) - .sum() - } - - fn get_num_players(&self) -> usize { - self.0.values().map(|x| x.0).sum() - } - - fn get_max_group_size(&self) -> usize { - self.0 - .values() - .map(|(count, is_not_alone)| if *is_not_alone { *count } else { 1 }) - .max() - .expect("Error: No max value found") - } - - fn get_corresponding_role(&self, role_id: usize) -> Roles { - let mut count = self.get_num_players(); - for (role, (role_count, is_not_alone)) in self.0.iter() { - count += if *is_not_alone { 1 } else { *role_count }; - if role_id < count { - return role.clone(); - } - } - - panic!("Error: Invalid role id is given"); - } -} - -#[test] -fn test_grouping_parameter() { - let grouping_parameter = GroupingParameter::new( - vec![ - (Roles::Villager, (4, false)), - (Roles::FortuneTeller, (1, false)), - (Roles::Werewolf, (2, true)), - ] - .into_iter() - .collect(), - ); - - // Villager, FortuneTeller, Werewolf - assert_eq!(grouping_parameter.get_num_roles(), 3); - - // Villager: 1, 2, 3, 4, FortuneTeller: 1, Werewolfs: 1 - assert_eq!(grouping_parameter.get_num_groups(), 6); - - // Total 4 + 1 + 2 = 7 - assert_eq!(grouping_parameter.get_num_players(), 7); -} - fn night_werewolf(opt: &Opt) -> Result<(), std::io::Error> { // init Net::init_from_file( @@ -682,24 +462,12 @@ fn night_werewolf(opt: &Opt) -> Result<(), std::io::Error> { Ok(()) } -#[derive(Debug, PartialEq, Eq, Hash, Ord, PartialOrd, Clone)] -enum Roles { - FortuneTeller, - Werewolf, - Villager, -} - -#[derive(Debug, Deserialize)] -struct Role { - role: String, -} - #[derive(Debug, Deserialize)] struct ArgSecretKey { secret_key: String, } -fn get_my_role() -> Roles { +fn get_my_role() -> Role { let id = Net::party_id(); println!("id is {:?}", id); @@ -711,12 +479,12 @@ fn get_my_role() -> Roles { file.read_to_string(&mut output_string) .expect("Failed to read file"); - let data: Role = serde_json::from_str(&output_string).unwrap(); + let data: RoleData = serde_json::from_str(&output_string).unwrap(); - let remove_prefix_string = if let Some(stripped) = data.role.strip_prefix("0x") { + let remove_prefix_string = if let Some(stripped) = data.role().strip_prefix("0x") { stripped.to_string() } else { - data.role.clone() + data.role().clone() }; let reader: &[u8] = &hex::decode(remove_prefix_string).unwrap(); @@ -724,9 +492,9 @@ fn get_my_role() -> Roles { let deserialized_role = ::deserialize(reader).unwrap(); match deserialized_role.as_str() { - "FortuneTeller" => Roles::FortuneTeller, - "Werewolf" => Roles::Werewolf, - "Villager" => Roles::Villager, + "FortuneTeller" => Role::FortuneTeller, + "Werewolf" => Role::Werewolf, + "Villager" => Role::Villager, _ => panic!("Invalid role"), } } diff --git a/src/lib.rs b/src/lib.rs index 615cd206..c895b93d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,5 @@ pub mod marlin; pub mod preprocessing; pub mod serialize; pub mod she; + +pub mod werewolf; diff --git a/src/werewolf.rs b/src/werewolf.rs new file mode 100644 index 00000000..b4ab6a6a --- /dev/null +++ b/src/werewolf.rs @@ -0,0 +1,2 @@ +pub mod types; +pub mod utils; diff --git a/src/werewolf/types.rs b/src/werewolf/types.rs new file mode 100644 index 00000000..0c965830 --- /dev/null +++ b/src/werewolf/types.rs @@ -0,0 +1,136 @@ +use ark_ff::PrimeField; +use nalgebra; +use serde::Deserialize; +use std::collections::BTreeMap; + +#[derive(Debug, PartialEq, Eq, Hash, Ord, PartialOrd, Clone)] +pub enum Role { + FortuneTeller, + Werewolf, + Villager, +} + +#[derive(Debug, Deserialize)] +pub struct RoleData { + role: String, +} + +impl RoleData { + pub fn role(&self) -> String { + self.role.clone() + } +} + +pub struct GroupingParameter(BTreeMap); + +impl GroupingParameter { + pub fn new(input: BTreeMap) -> Self { + Self(input) + } + + // F: Field used in the MPC. + pub fn generate_tau_matrix(&self) -> nalgebra::DMatrix { + let num_players = self.get_num_players(); + let num_groups = self.get_num_groups(); + + let mut tau = + nalgebra::DMatrix::::zeros(num_players + num_groups, num_players + num_groups); + + let mut player_index = 0; + let mut group_index = 0; + + for (_, (count, is_not_alone)) in self.0.iter() { + if *is_not_alone { + assert!( + *count >= 2, + "Error: not alone group count must be greater than 2" + ); + + // group + tau[(player_index, num_players + group_index)] = F::one(); + + // player + for _ in 0..*count - 1 { + tau[(player_index + 1, player_index)] = F::one(); + player_index += 1; + } + tau[(num_players + group_index, player_index)] = F::one(); + player_index += 1; + group_index += 1; + } else { + for _ in 0..*count { + // group + tau[(player_index, num_players + group_index)] = F::one(); + // player + tau[(num_players + group_index, player_index)] = F::one(); + player_index += 1; + group_index += 1; + } + } + } + + tau + } + + pub fn get_num_roles(&self) -> usize { + self.0.len() + } + + pub fn get_num_groups(&self) -> usize { + self.0 + .values() + .map(|(count, is_not_alone)| if *is_not_alone { 1 } else { *count }) + .sum() + } + + pub fn get_num_players(&self) -> usize { + self.0.values().map(|x| x.0).sum() + } + + pub fn get_max_group_size(&self) -> usize { + self.0 + .values() + .map(|(count, is_not_alone)| if *is_not_alone { *count } else { 1 }) + .max() + .expect("Error: No max value found") + } + + pub fn get_corresponding_role(&self, role_id: usize) -> Role { + let mut count = self.get_num_players(); + for (role, (role_count, is_not_alone)) in self.0.iter() { + count += if *is_not_alone { 1 } else { *role_count }; + if role_id < count { + return role.clone(); + } + } + + panic!("Error: Invalid role id is given"); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_grouping_parameter() { + let grouping_parameter = GroupingParameter::new( + vec![ + (Role::Villager, (4, false)), + (Role::FortuneTeller, (1, false)), + (Role::Werewolf, (2, true)), + ] + .into_iter() + .collect(), + ); + + // Villager, FortuneTeller, Werewolf + assert_eq!(grouping_parameter.get_num_roles(), 3); + + // Villager: 1, 2, 3, 4, FortuneTeller: 1, Werewolfs: 1 + assert_eq!(grouping_parameter.get_num_groups(), 6); + + // Total 4 + 1 + 2 = 7 + assert_eq!(grouping_parameter.get_num_players(), 7); + } +} diff --git a/src/werewolf/utils.rs b/src/werewolf/utils.rs new file mode 100644 index 00000000..fb6d7bdf --- /dev/null +++ b/src/werewolf/utils.rs @@ -0,0 +1,130 @@ +use ark_ff::PrimeField; +use nalgebra::{DMatrix, DVector}; +use rand::{seq::SliceRandom, Rng}; + +use super::types::{GroupingParameter, Role}; + +// Compute shuffle matrix and return role, raw role id, and player id in the same group. +pub fn calc_shuffle_matrix( + grouping_parameter: &GroupingParameter, + shuffle_matrix: &[DMatrix], + id: usize, +) -> Result<(Role, usize, Option>), std::io::Error> { + // parameters + let n = grouping_parameter.get_num_players(); + let m = grouping_parameter.get_num_groups(); + + // generate tau matrix + let tau_matrix = grouping_parameter.generate_tau_matrix(); + + // compute rho matrix + let m_matrix = shuffle_matrix + .iter() + .fold(DMatrix::::identity(n + m, n + m), |acc, x| acc * x); + let rho_matrix = m_matrix.transpose() * tau_matrix * m_matrix; + + // iterate. get rho^1, rho^2, ..., rho^num_players + let mut rho_sequence = Vec::with_capacity(n); + let mut current_rho = rho_matrix.clone(); + for _ in 0..n { + rho_sequence.push(current_rho.clone()); + current_rho *= rho_matrix.clone(); // rho^(i+1) = rho^i * rho + } + + let mut unit_vec = DVector::::zeros(n + m); + unit_vec[id] = F::one(); + + // player i: for each j in {1..n}, get rho^j(i) + let result = rho_sequence + .iter() + .map(|rho| rho * unit_vec.clone()) + .map(|x| { + let index = x.column(0).into_iter().enumerate().find_map(|(j, value)| { + if *value != F::zero() { + Some(j) + } else { + None + } + }); + index.unwrap_or_else(|| panic!("Error: No index found")) + }) // search for the index of the one element + .collect::>(); + + println!("player {:?} result is {:?}", id, result); + + // get role value. get val which is max value in result. + let role_val = result.iter().max().expect("Failed to get max value"); + + // get role + let role = grouping_parameter.get_corresponding_role(*role_val); + + let mut fellow = result + .iter() + .filter(|x| **x != id && **x < n) + .copied() + .collect::>(); + + if fellow.is_empty() { + Ok((role, *role_val, None)) + } else { + fellow.sort(); + fellow.dedup(); + Ok((role, *role_val, Some(fellow))) + } +} + +pub fn generate_individual_shuffle_matrix( + n: usize, + m: usize, + rng: &mut R, +) -> DMatrix { + let mut shuffle_matrix = DMatrix::::zeros(n + m, n + m); + + // generate permutation + let mut permutation: Vec = (0..n).collect(); + permutation.shuffle(rng); + + // shuffle_matrix + for i in 0..n { + shuffle_matrix[(i, permutation[i])] = F::one(); + } + + for i in n..n + m { + shuffle_matrix[(i, i)] = F::one(); + } + + shuffle_matrix +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::marlin::MFr; + use ark_std::test_rng; + + #[test] + fn test_shuffle_matrix() { + let grouping_parameter = GroupingParameter::new( + vec![ + (Role::Villager, (4, false)), + (Role::FortuneTeller, (1, false)), + (Role::Werewolf, (2, true)), + ] + .into_iter() + .collect(), + ); + + let shuffle_matrix = vec![generate_individual_shuffle_matrix( + grouping_parameter.get_num_players(), + grouping_parameter.get_num_groups(), + &mut test_rng(), + )]; + + for id in 0..grouping_parameter.get_num_players() { + let (role, _, player_ids) = + calc_shuffle_matrix::(&grouping_parameter, &shuffle_matrix, id).unwrap(); + println!("role is {:?}", role); + println!("fellow is {:?}", player_ids); + } + } +} From d68cd79f57a3a4902836ca71b9d1a5d7a450d33c Mon Sep 17 00:00:00 2001 From: sheagrief <3a.mad1earth4@gmail.com> Date: Thu, 17 Oct 2024 15:04:53 +0900 Subject: [PATCH 3/8] =?UTF-8?q?=E2=9C=A8=20Add=20generate=5Frandom=5Fcommi?= =?UTF-8?q?tment=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/werewolf/utils.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/werewolf/utils.rs b/src/werewolf/utils.rs index fb6d7bdf..3fa4f1f1 100644 --- a/src/werewolf/utils.rs +++ b/src/werewolf/utils.rs @@ -96,6 +96,27 @@ pub fn generate_individual_shuffle_matrix( shuffle_matrix } +pub fn generate_random_commitment, R: Rng>( + rng: &mut R, + pedersen_param: &F::PedersenParam, +) -> F::PedersenCommitment { + let random_value = F::rand(rng); + + let commitment = >::PedersenComScheme::commit( + pedersen_param, + &random_value.convert_input(), + &F::PedersenRandomness::default(), + ) + .unwrap(); + + // record random value + let id = Net::party_id(); + let file_path = format!("./werewolf_game/{}/random.json", id); + write_to_file(vec![("random".to_string(), random_value)], &file_path).unwrap(); + + commitment +} + #[cfg(test)] mod tests { use super::*; From 5626091d7597058a64e926143d713e2c41d94ace Mon Sep 17 00:00:00 2001 From: sheagrief <3a.mad1earth4@gmail.com> Date: Thu, 17 Oct 2024 16:33:33 +0900 Subject: [PATCH 4/8] =?UTF-8?q?=E2=9C=A8=20Add=20load=20rand=5Fcommitment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/werewolf/utils.rs | 66 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/src/werewolf/utils.rs b/src/werewolf/utils.rs index 3fa4f1f1..b4c7ecfb 100644 --- a/src/werewolf/utils.rs +++ b/src/werewolf/utils.rs @@ -1,9 +1,17 @@ +use std::io::Read; + +use super::types::{GroupingParameter, Role}; +use crate::{circuits::LocalOrMPC, serialize::write_to_file}; + +use ark_bls12_377::Fr; use ark_ff::PrimeField; +use ark_ff::UniformRand; +use ark_serialize::CanonicalDeserialize; +use mpc_algebra::{channel::MpcSerNet, CommitmentScheme}; +use mpc_net::{MpcMultiNet as Net, MpcNet}; use nalgebra::{DMatrix, DVector}; use rand::{seq::SliceRandom, Rng}; -use super::types::{GroupingParameter, Role}; - // Compute shuffle matrix and return role, raw role id, and player id in the same group. pub fn calc_shuffle_matrix( grouping_parameter: &GroupingParameter, @@ -96,16 +104,16 @@ pub fn generate_individual_shuffle_matrix( shuffle_matrix } -pub fn generate_random_commitment, R: Rng>( +pub fn generate_random_commitment( rng: &mut R, - pedersen_param: &F::PedersenParam, -) -> F::PedersenCommitment { - let random_value = F::rand(rng); + pedersen_param: &>::PedersenParam, +) -> Vec<>::PedersenCommitment> { + let random_value = Fr::rand(rng); - let commitment = >::PedersenComScheme::commit( + let commitment = >::PedersenComScheme::commit( pedersen_param, &random_value.convert_input(), - &F::PedersenRandomness::default(), + &>::PedersenRandomness::default(), ) .unwrap(); @@ -114,7 +122,47 @@ pub fn generate_random_commitment, R: Rng>( let file_path = format!("./werewolf_game/{}/random.json", id); write_to_file(vec![("random".to_string(), random_value)], &file_path).unwrap(); - commitment + let commitment_vec = Net::broadcast(&commitment); + let commitment_vec_data = commitment_vec + .clone() + .into_iter() + .enumerate() + .map(|(i, c)| (i.to_string(), c)) + .collect::>(); + let file_path = "./werewolf_game/rand_commitmnet.json".to_string(); + write_to_file(commitment_vec_data, &file_path).unwrap(); + + commitment_vec +} + +pub fn load_random_commitment( +) -> Result>::PedersenCommitment>, std::io::Error> { + let file_path = "./werewolf_game/rand_commitmnet.json".to_string(); + let mut file = std::fs::File::open(file_path)?; + let mut output_string = String::new(); + file.read_to_string(&mut output_string) + .expect("Failed to read file"); + + let data: serde_json::Value = serde_json::from_str(&output_string)?; + + let data = data + .as_object() + .ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to parse JSON data") + })? + .iter() + .collect::>(); + + let commitment_vec = data + .iter() + .map(|v| { + let reader: &[u8] = + &hex::decode(v.1.as_str().unwrap().strip_prefix("0x").unwrap()).unwrap(); + >::PedersenCommitment::deserialize(reader).unwrap() + }) + .collect::>(); + + Ok(commitment_vec) } #[cfg(test)] From fa723b3394d86908784f1065feba6e44beafb55d Mon Sep 17 00:00:00 2001 From: sheagrief <3a.mad1earth4@gmail.com> Date: Thu, 17 Oct 2024 17:44:21 +0900 Subject: [PATCH 5/8] =?UTF-8?q?=F0=9F=9A=A7=20Update=20werewolf=20circuits?= =?UTF-8?q?=20and=20binary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/bin_werewolf.rs | 86 +++++++++++++++++++++++++++++++------- src/circuits/werewolf.rs | 89 +++++++++++++++++++++++++++++++++++++++- src/werewolf/utils.rs | 32 +++++++++++++++ 3 files changed, 191 insertions(+), 16 deletions(-) diff --git a/examples/bin_werewolf.rs b/examples/bin_werewolf.rs index 0f683d65..818d0d8e 100644 --- a/examples/bin_werewolf.rs +++ b/examples/bin_werewolf.rs @@ -43,6 +43,8 @@ use zk_mpc::marlin::{prove_and_verify, setup_and_index}; use zk_mpc::preprocessing; use zk_mpc::serialize::{write_r, write_to_file}; use zk_mpc::she; +use zk_mpc::werewolf::utils::load_random_commitment; +use zk_mpc::werewolf::utils::load_random_value; use zk_mpc::werewolf::{ types::{GroupingParameter, Role, RoleData}, utils::{calc_shuffle_matrix, generate_individual_shuffle_matrix}, @@ -137,7 +139,7 @@ fn main() -> Result<(), Box> { fn initialize_game(opt: &Opt) -> Result<(), std::io::Error> { // public.json - let file_path = "./werewolf/public.json"; + let file_path = "./werewolf_game/public.json"; File::create(file_path)?; let datas = vec![ @@ -161,7 +163,7 @@ fn initialize_game(opt: &Opt) -> Result<(), std::io::Error> { // TODO: randomize let role = ["FortuneTeller", "Werewolf", "Villager"]; for i in 0..opt.num_players.unwrap() { - let file_path = format!("./werewolf/{}/role.json", i); + let file_path = format!("./werewolf_game/{}/role.json", i); File::create(&file_path)?; let datas = vec![("role".to_string(), role[i].to_string())]; @@ -212,7 +214,13 @@ fn preprocessing_mpc(opt: &Opt) -> Result<(), std::io::Error> { // save to file // , [r] for input share - write_r(opt.num_players.unwrap(), "werewolf", r_angle, r_bracket).unwrap(); + write_r( + opt.num_players.unwrap(), + "werewolf_game", + r_angle, + r_bracket, + ) + .unwrap(); Ok(()) } @@ -304,19 +312,19 @@ fn preprocessing_werewolf(opt: &Opt) -> Result<(), std::io::Error> { if Net::party_id() == 0 { let datas = vec![("public_key".to_string(), pk)]; - write_to_file(datas, "./werewolf/fortune_teller_key.json").unwrap(); + write_to_file(datas, "./werewolf_game/fortune_teller_key.json").unwrap(); let secret_data = vec![("secret_key".to_string(), sk.0)]; write_to_file( secret_data, - format!("./werewolf/{}/secret_key.json", Net::party_id()).as_str(), + format!("./werewolf_game/{}/secret_key.json", Net::party_id()).as_str(), ) .unwrap(); let elgamal_parameter_data = vec![("elgamal_param".to_string(), elgamal_params.generator)]; - write_to_file(elgamal_parameter_data, "./werewolf/elgamal_param.json").unwrap(); + write_to_file(elgamal_parameter_data, "./werewolf_game/elgamal_param.json").unwrap(); } Ok(()) @@ -346,6 +354,9 @@ fn role_assignment(opt: &Opt) -> Result<(), std::io::Error> { let pedersen_param = >::PedersenComScheme::setup(rng).unwrap(); + let player_randomness = load_random_value()?; + let player_commitment = load_random_commitment()?; + // calc let shuffle_matrix = vec![ generate_individual_shuffle_matrix( @@ -392,6 +403,8 @@ fn role_assignment(opt: &Opt) -> Result<(), std::io::Error> { shuffle_matrices: vec![na::DMatrix::::zeros(n + m, n + m); 2], role_commitment: commitment, randomness, + player_randomness: player_randomness.clone(), + player_commitment: player_commitment.clone(), }; let srs = LocalMarlin::universal_setup(1000000, 50000, 100000, rng).unwrap(); @@ -428,6 +441,14 @@ fn role_assignment(opt: &Opt) -> Result<(), std::io::Error> { shuffle_matrices: shuffle_matrix, randomness: mpc_randomness, role_commitment: role_commitment.clone(), + player_randomness: player_randomness + .iter() + .map(|x| MFr::from_public(*x)) + .collect::>(), + player_commitment: player_commitment + .iter() + .map(|x| >::PedersenCommitment::from_public(*x)) + .collect::>(), }; let mut inputs = Vec::new(); @@ -473,7 +494,7 @@ fn get_my_role() -> Role { println!("id is {:?}", id); // read role.json - let file_path = format!("./werewolf/{}/role.json", id); + let file_path = format!("./werewolf_game/{}/role.json", id); let mut file = File::open(file_path).unwrap(); let mut output_string = String::new(); file.read_to_string(&mut output_string) @@ -608,7 +629,7 @@ fn multi_divination(_opt: &Opt) -> Result<(), std::io::Error> { assert!(is_valid); // save divination reesult - let file_path = format!("./werewolf/{}/secret_key.json", 0); + let file_path = format!("./werewolf_game/{}/secret_key.json", 0); let mut file = File::open(file_path).unwrap(); let mut output_string = String::new(); file.read_to_string(&mut output_string) @@ -660,7 +681,7 @@ fn multi_divination(_opt: &Opt) -> Result<(), std::io::Error> { write_to_file( datas, - format!("./werewolf/{}/divination_result.json", Net::party_id()).as_str(), + format!("./werewolf_game/{}/divination_result.json", Net::party_id()).as_str(), ) .unwrap(); } @@ -674,6 +695,14 @@ fn voting(opt: &Opt) -> Result<(), std::io::Error> { opt.input.clone().unwrap().to_str().unwrap(), opt.id.unwrap(), ); + + let rng = &mut test_rng(); + + let pedersen_param = >::PedersenComScheme::setup(rng).unwrap(); + + let player_randomness = load_random_value()?; + let player_commitment = load_random_commitment()?; + // calc let most_voted_id = Fr::from(1); let invalid_most_voted_id = Fr::from(0); @@ -686,11 +715,15 @@ fn voting(opt: &Opt) -> Result<(), std::io::Error> { vec![Fr::from(0), Fr::from(0), Fr::from(1)], ], is_most_voted_id: Fr::from(1), + pedersen_param: pedersen_param.clone(), + player_randomness: player_randomness.clone(), + player_commitment: player_commitment.clone(), }; let (mpc_index_pk, index_vk) = setup_and_index(local_voting_circuit); let rng = &mut test_rng(); + let mpc_pedersen_param = >::PedersenParam::from_local(&pedersen_param); let mpc_voting_circuit = AnonymousVotingCircuit { is_target_id: vec![ @@ -711,6 +744,15 @@ fn voting(opt: &Opt) -> Result<(), std::io::Error> { ], ], is_most_voted_id: MFr::king_share(Fr::from(1), rng), + pedersen_param: mpc_pedersen_param, + player_randomness: player_randomness + .iter() + .map(|x| MFr::from_public(*x)) + .collect::>(), + player_commitment: player_commitment + .iter() + .map(|x| >::PedersenCommitment::from_public(*x)) + .collect::>(), }; let inputs = vec![most_voted_id]; @@ -776,6 +818,9 @@ fn winning_judgment(opt: &Opt) -> Result<(), std::io::Error> { .map(|x| x.generate_input(&mpc_pedersen_param, &mpc_common_randomness)) .collect::>(); + let player_randomness = load_random_value()?; + let player_commitment = load_random_commitment()?; + // calc // TODO: correctly. @@ -797,6 +842,9 @@ fn winning_judgment(opt: &Opt) -> Result<(), std::io::Error> { pedersen_param: pedersen_param.clone(), am_werewolf: am_werewolf_vec.clone(), game_state: Fr::default(), + + player_randomness: player_randomness.clone(), + player_commitment: player_commitment.clone(), }; let (mpc_index_pk, index_vk) = setup_and_index(local_judgment_circuit); @@ -808,6 +856,14 @@ fn winning_judgment(opt: &Opt) -> Result<(), std::io::Error> { pedersen_param: mpc_pedersen_param, am_werewolf: mpc_am_werewolf_vec.clone(), game_state, + player_randomness: player_randomness + .iter() + .map(|x| MFr::from_public(*x)) + .collect::>(), + player_commitment: player_commitment + .iter() + .map(|x| >::PedersenCommitment::from_public(*x)) + .collect::>(), }; let mut inputs = vec![num_alive, game_state.reveal()]; @@ -867,7 +923,7 @@ fn get_elgamal_param_pubkey() -> ( >::ElGamalPubKey, ) { // loading public key - let file_path = format!("./werewolf/fortune_teller_key.json"); + let file_path = format!("./werewolf_game/fortune_teller_key.json"); let mut file = File::open(file_path).unwrap(); let mut output_string = String::new(); file.read_to_string(&mut output_string) @@ -890,7 +946,7 @@ fn get_elgamal_param_pubkey() -> ( // loading secret key // let file_path = format!("./werewolf/{}/secret_key.json", opt.target.unwrap()); - let file_path = format!("./werewolf/{}/secret_key.json", 0); + let file_path = format!("./werewolf_game/{}/secret_key.json", 0); let mut file = File::open(file_path).unwrap(); let mut output_string = String::new(); @@ -918,7 +974,7 @@ fn get_elgamal_param_pubkey() -> ( ); // loading elgamal param - let file_path = format!("./werewolf/elgamal_param.json"); + let file_path = format!("./werewolf_game/elgamal_param.json"); let mut file = File::open(file_path).unwrap(); let mut output_string = String::new(); file.read_to_string(&mut output_string) @@ -949,7 +1005,7 @@ fn get_elgamal_param_pubkey() -> ( #[ignore] fn test_encryption_decryption() -> Result<(), std::io::Error> { // loading public key - let file_path = format!("./werewolf/fortune_teller_key.json"); + let file_path = format!("./werewolf_game/fortune_teller_key.json"); let mut file = File::open(file_path).unwrap(); let mut output_string = String::new(); file.read_to_string(&mut output_string) @@ -972,7 +1028,7 @@ fn test_encryption_decryption() -> Result<(), std::io::Error> { // loading secret key // let file_path = format!("./werewolf/{}/secret_key.json", opt.target.unwrap()); - let file_path = format!("./werewolf/{}/secret_key.json", 0); + let file_path = format!("./werewolf_game/{}/secret_key.json", 0); let mut file = File::open(file_path).unwrap(); let mut output_string = String::new(); @@ -1000,7 +1056,7 @@ fn test_encryption_decryption() -> Result<(), std::io::Error> { ); // loading elgamal param - let file_path = format!("./werewolf/elgamal_param.json"); + let file_path = format!("./werewolf_game/elgamal_param.json"); let mut file = File::open(file_path).unwrap(); let mut output_string = String::new(); file.read_to_string(&mut output_string) diff --git a/src/circuits/werewolf.rs b/src/circuits/werewolf.rs index d080600d..8b3475b4 100644 --- a/src/circuits/werewolf.rs +++ b/src/circuits/werewolf.rs @@ -32,7 +32,7 @@ use mpc_algebra::encryption::elgamal::{ constraints::ElGamalEncGadget as MpcElGamalEncGadget, elgamal::ElGamal as MpcElGamal, }; -use super::{LocalOrMPC, PedersenComCircuit}; +use super::{circuit, LocalOrMPC, PedersenComCircuit}; use crate::input::{InputWithCommit, WerewolfKeyInput, WerewolfMpcInput}; #[derive(Clone)] @@ -577,6 +577,10 @@ impl ConstraintSynthesizer> for DivinationCircuit> { pub is_target_id: Vec>, pub is_most_voted_id: F, + + pub pedersen_param: F::PedersenParam, + pub player_randomness: Vec, + pub player_commitment: Vec, } impl ConstraintSynthesizer for AnonymousVotingCircuit { @@ -584,6 +588,17 @@ impl ConstraintSynthesizer for AnonymousVotingCircuit { // initialize let player_num = self.is_target_id.len(); + // check player commitment + for i in 0..player_num { + let pedersen_circuit = PedersenComCircuit { + param: Some(self.pedersen_param.clone()), + input: self.player_randomness[i], + open: >::PedersenRandomness::default(), + commit: self.player_commitment[i], + }; + pedersen_circuit.generate_constraints(cs.clone())?; + } + let is_target_id_var = self .is_target_id .iter() @@ -652,6 +667,19 @@ impl ConstraintSynthesizer> for AnonymousVotingCircuit as LocalOrMPC>>::PedersenRandomness::default( + ), + commit: self.player_commitment[i], + }; + pedersen_circuit.generate_constraints(cs.clone())?; + } + let is_target_id_var = self .is_target_id .iter() @@ -735,10 +763,25 @@ pub struct WinningJudgeCircuit> { pub pedersen_param: F::PedersenParam, pub am_werewolf: Vec>, pub game_state: F, + + pub player_randomness: Vec, + pub player_commitment: Vec, } impl ConstraintSynthesizer for WinningJudgeCircuit { fn generate_constraints(self, cs: ConstraintSystemRef) -> ark_relations::r1cs::Result<()> { + // check player commitment + let player_num = self.player_randomness.len(); + for i in 0..player_num { + let pedersen_circuit = PedersenComCircuit { + param: Some(self.pedersen_param.clone()), + input: self.player_randomness[i], + open: >::PedersenRandomness::default(), + commit: self.player_commitment[i], + }; + pedersen_circuit.generate_constraints(cs.clone())?; + } + // initialize let num_alive_var = FpVar::new_input(cs.clone(), || Ok(self.num_alive))?; @@ -798,6 +841,20 @@ impl ConstraintSynthesizer> for WinningJudgeCircuit>, ) -> ark_relations::r1cs::Result<()> { + // check player commitment + let player_num = self.player_randomness.len(); + for i in 0..player_num { + let pedersen_circuit = PedersenComCircuit { + param: Some(self.pedersen_param.clone()), + input: self.player_randomness[i], + open: + as LocalOrMPC>>::PedersenRandomness::default( + ), + commit: self.player_commitment[i], + }; + pedersen_circuit.generate_constraints(cs.clone())?; + } + //initialize let num_alive_var = MpcFpVar::new_input(cs.clone(), || Ok(self.num_alive))?; @@ -861,14 +918,29 @@ pub struct RoleAssignmentCircuit> { // instance pub tau_matrix: na::DMatrix, pub role_commitment: Vec, + pub player_commitment: Vec, // witness pub shuffle_matrices: Vec>, pub randomness: Vec, + pub player_randomness: Vec, } impl ConstraintSynthesizer for RoleAssignmentCircuit { fn generate_constraints(self, cs: ConstraintSystemRef) -> ark_relations::r1cs::Result<()> { + // check player commitment + assert_eq!(self.num_players, self.player_randomness.len()); + assert_eq!(self.num_players, self.player_commitment.len()); + for i in 0..self.num_players { + let pedersen_circuit = PedersenComCircuit { + param: Some(self.pedersen_param.clone()), + input: self.player_randomness[i], + open: >::PedersenRandomness::default(), + commit: self.player_commitment[i], + }; + pedersen_circuit.generate_constraints(cs.clone())?; + } + // initialize let tau_matrix_var = na::DMatrix::from_iterator( self.tau_matrix.nrows(), @@ -985,6 +1057,21 @@ impl ConstraintSynthesizer> for RoleAssignmentCircuit>, ) -> ark_relations::r1cs::Result<()> { + // check player commitment + assert_eq!(self.num_players, self.player_randomness.len()); + assert_eq!(self.num_players, self.player_commitment.len()); + for i in 0..self.num_players { + let pedersen_circuit = PedersenComCircuit { + param: Some(self.pedersen_param.clone()), + input: self.player_randomness[i], + open: + as LocalOrMPC>>::PedersenRandomness::default( + ), + commit: self.player_commitment[i], + }; + pedersen_circuit.generate_constraints(cs.clone())?; + } + // initialize let tau_matrix_var = na::DMatrix::from_iterator( self.tau_matrix.nrows(), diff --git a/src/werewolf/utils.rs b/src/werewolf/utils.rs index b4c7ecfb..5518518b 100644 --- a/src/werewolf/utils.rs +++ b/src/werewolf/utils.rs @@ -135,6 +135,38 @@ pub fn generate_random_commitment( commitment_vec } +pub fn load_random_value() -> Result, std::io::Error> { + // let id = Net::party_id(); + // let file_path = format!("./werewolf_game/{}/random.json", id); + // let mut file = std::fs::File::open(file_path)?; + // let mut output_string = String::new(); + // file.read_to_string(&mut output_string) + // .expect("Failed to read file"); + + // let data: serde_json::Value = serde_json::from_str(&output_string)?; + + // let data = data + // .as_object() + // .ok_or_else(|| { + // std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to parse JSON data") + // })? + // .iter() + // .collect::>(); + + // let random_value = data + // .iter() + // .map(|v| { + // let reader: &[u8] = + // &hex::decode(v.1.as_str().unwrap().strip_prefix("0x").unwrap()).unwrap(); + // Fr::deserialize(reader).unwrap() + // }) + // .collect::>()[0]; + + // Ok(random_value) + + todo!() +} + pub fn load_random_commitment( ) -> Result>::PedersenCommitment>, std::io::Error> { let file_path = "./werewolf_game/rand_commitmnet.json".to_string(); From 22b51f24cc0a4c37f65980d69f61ab4202a4256f Mon Sep 17 00:00:00 2001 From: sheagrief <3a.mad1earth4@gmail.com> Date: Thu, 17 Oct 2024 18:20:24 +0900 Subject: [PATCH 6/8] =?UTF-8?q?=E2=9C=85=20Update=20all=20bin=5Fwerewolf?= =?UTF-8?q?=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/bin_werewolf.rs | 23 ++++++++++++---- src/werewolf/utils.rs | 59 ++++++++++++++++++++-------------------- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/examples/bin_werewolf.rs b/examples/bin_werewolf.rs index 818d0d8e..216433c4 100644 --- a/examples/bin_werewolf.rs +++ b/examples/bin_werewolf.rs @@ -339,9 +339,9 @@ fn role_assignment(opt: &Opt) -> Result<(), std::io::Error> { let grouping_parameter = GroupingParameter::new( vec![ - (Role::Villager, (3, false)), + (Role::Villager, (1, false)), (Role::FortuneTeller, (1, false)), - (Role::Werewolf, (2, true)), + (Role::Werewolf, (1, false)), ] .into_iter() .collect(), @@ -451,7 +451,10 @@ fn role_assignment(opt: &Opt) -> Result<(), std::io::Error> { .collect::>(), }; - let mut inputs = Vec::new(); + let mut inputs = player_commitment + .iter() + .flat_map(|c| vec![c.x, c.y]) + .collect::>(); role_commitment.iter().for_each(|x| { inputs.push(x.reveal().x); @@ -755,7 +758,12 @@ fn voting(opt: &Opt) -> Result<(), std::io::Error> { .collect::>(), }; - let inputs = vec![most_voted_id]; + let mut inputs = player_commitment + .iter() + .flat_map(|c| vec![c.x, c.y]) + .collect::>(); + + inputs.push(most_voted_id); let invalid_inputs = vec![invalid_most_voted_id]; assert!(prove_and_verify( @@ -866,7 +874,12 @@ fn winning_judgment(opt: &Opt) -> Result<(), std::io::Error> { .collect::>(), }; - let mut inputs = vec![num_alive, game_state.reveal()]; + let mut inputs = player_commitment + .iter() + .flat_map(|c| vec![c.x, c.y]) + .collect::>(); + + inputs.extend_from_slice(&[num_alive, game_state.reveal()]); for iwc in mpc_am_werewolf_vec.iter() { inputs.push(iwc.commitment.reveal().x); diff --git a/src/werewolf/utils.rs b/src/werewolf/utils.rs index 5518518b..7887eead 100644 --- a/src/werewolf/utils.rs +++ b/src/werewolf/utils.rs @@ -135,36 +135,37 @@ pub fn generate_random_commitment( commitment_vec } +// TODO: change to return mpc shared value pub fn load_random_value() -> Result, std::io::Error> { - // let id = Net::party_id(); - // let file_path = format!("./werewolf_game/{}/random.json", id); - // let mut file = std::fs::File::open(file_path)?; - // let mut output_string = String::new(); - // file.read_to_string(&mut output_string) - // .expect("Failed to read file"); - - // let data: serde_json::Value = serde_json::from_str(&output_string)?; - - // let data = data - // .as_object() - // .ok_or_else(|| { - // std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to parse JSON data") - // })? - // .iter() - // .collect::>(); - - // let random_value = data - // .iter() - // .map(|v| { - // let reader: &[u8] = - // &hex::decode(v.1.as_str().unwrap().strip_prefix("0x").unwrap()).unwrap(); - // Fr::deserialize(reader).unwrap() - // }) - // .collect::>()[0]; - - // Ok(random_value) - - todo!() + let id = Net::party_id(); + let file_path = format!("./werewolf_game/{}/random.json", id); + let mut file = std::fs::File::open(file_path)?; + let mut output_string = String::new(); + file.read_to_string(&mut output_string) + .expect("Failed to read file"); + + let data: serde_json::Value = serde_json::from_str(&output_string)?; + + let data = data + .as_object() + .ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to parse JSON data") + })? + .iter() + .collect::>(); + + let random_value = data + .iter() + .map(|v| { + let reader: &[u8] = + &hex::decode(v.1.as_str().unwrap().strip_prefix("0x").unwrap()).unwrap(); + Fr::deserialize(reader).unwrap() + }) + .collect::>()[0]; + + let random_vec = Net::broadcast(&random_value); + + Ok(random_vec) } pub fn load_random_commitment( From 70d14f1bcdfd05c48757613c79cebbd3add3bd27 Mon Sep 17 00:00:00 2001 From: sheagrief <3a.mad1earth4@gmail.com> Date: Thu, 17 Oct 2024 19:27:22 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=F0=9F=90=9B=20Fix=20dir=20name=20in=20run?= =?UTF-8?q?=5Fwerewolf.zsh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run_werewolf.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/run_werewolf.zsh b/run_werewolf.zsh index 57bdb05b..31e10173 100755 --- a/run_werewolf.zsh +++ b/run_werewolf.zsh @@ -13,12 +13,12 @@ players=3 case $mode in init) for i in $(seq 0 $((players - 1))); do - mkdir -p ./werewolf/$i + mkdir -p ./werewolf_game/$i done RUST_BACKTRACE=1 $BIN init --num-players $players wait for i in $(seq 0 $((players - 1))); do - mkdir -p ./werewolf/$i + mkdir -p ./werewolf_game/$i if [ $i == 0 ]; then RUST_BACKTRACE=1 $BIN preprocessing $i ./data/address & pid=$! @@ -35,7 +35,7 @@ init) done ;; night) - if [ ! -f "./werewolf/public.json" ]; then + if [ ! -f "./werewolf_game/public.json" ]; then echo "not found public.json" exit 1 fi From f300a9d4dcb25e4c5609247b4f793bf5dee90632 Mon Sep 17 00:00:00 2001 From: sheagrief <3a.mad1earth4@gmail.com> Date: Thu, 17 Oct 2024 19:46:16 +0900 Subject: [PATCH 8/8] =?UTF-8?q?=F0=9F=90=9B=20Fix=20CI=20test=20(Insert=20?= =?UTF-8?q?record=20rand&commit)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/rust_ci.yml | 3 +++ examples/bin_werewolf.rs | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/.github/workflows/rust_ci.yml b/.github/workflows/rust_ci.yml index 70f8445a..6a662a28 100644 --- a/.github/workflows/rust_ci.yml +++ b/.github/workflows/rust_ci.yml @@ -256,5 +256,8 @@ jobs: - name: Run werewolf vote run: ./run_werewolf.zsh vote + - name: Run werewolf judgment + run: ./run_werewolf.zsh judgment + - name: Run werewolf role assignment run: ./run_werewolf.zsh role_assignment \ No newline at end of file diff --git a/examples/bin_werewolf.rs b/examples/bin_werewolf.rs index 216433c4..cb309720 100644 --- a/examples/bin_werewolf.rs +++ b/examples/bin_werewolf.rs @@ -23,6 +23,7 @@ use mpc_algebra::FromLocal; use mpc_algebra::LessThan; use mpc_algebra::Reveal; use mpc_net::{MpcMultiNet as Net, MpcNet}; +use rand::thread_rng; use rand::Rng; use serde::Deserialize; use std::{fs::File, path::PathBuf}; @@ -43,6 +44,7 @@ use zk_mpc::marlin::{prove_and_verify, setup_and_index}; use zk_mpc::preprocessing; use zk_mpc::serialize::{write_r, write_to_file}; use zk_mpc::she; +use zk_mpc::werewolf::utils::generate_random_commitment; use zk_mpc::werewolf::utils::load_random_commitment; use zk_mpc::werewolf::utils::load_random_value; use zk_mpc::werewolf::{ @@ -235,6 +237,9 @@ fn preprocessing_werewolf(opt: &Opt) -> Result<(), std::io::Error> { // TODO: changable let num_players = 3; + let pedersen_param = >::PedersenComScheme::setup(&mut test_rng()).unwrap(); + generate_random_commitment(&mut thread_rng(), &pedersen_param); + // dummmy input let mut pub_key_or_dummy_x = vec![Fr::from(0); num_players]; let mut pub_key_or_dummy_y = vec![Fr::from(0); num_players];