Skip to content

Commit

Permalink
✅ Update all bin_werewolf tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sheagrief committed Oct 17, 2024
1 parent fa723b3 commit 22b51f2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
23 changes: 18 additions & 5 deletions examples/bin_werewolf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -451,7 +451,10 @@ fn role_assignment(opt: &Opt) -> Result<(), std::io::Error> {
.collect::<Vec<_>>(),
};

let mut inputs = Vec::new();
let mut inputs = player_commitment
.iter()
.flat_map(|c| vec![c.x, c.y])
.collect::<Vec<_>>();

role_commitment.iter().for_each(|x| {
inputs.push(x.reveal().x);
Expand Down Expand Up @@ -755,7 +758,12 @@ fn voting(opt: &Opt) -> Result<(), std::io::Error> {
.collect::<Vec<_>>(),
};

let inputs = vec![most_voted_id];
let mut inputs = player_commitment
.iter()
.flat_map(|c| vec![c.x, c.y])
.collect::<Vec<_>>();

inputs.push(most_voted_id);
let invalid_inputs = vec![invalid_most_voted_id];

assert!(prove_and_verify(
Expand Down Expand Up @@ -866,7 +874,12 @@ fn winning_judgment(opt: &Opt) -> Result<(), std::io::Error> {
.collect::<Vec<_>>(),
};

let mut inputs = vec![num_alive, game_state.reveal()];
let mut inputs = player_commitment
.iter()
.flat_map(|c| vec![c.x, c.y])
.collect::<Vec<_>>();

inputs.extend_from_slice(&[num_alive, game_state.reveal()]);

for iwc in mpc_am_werewolf_vec.iter() {
inputs.push(iwc.commitment.reveal().x);
Expand Down
59 changes: 30 additions & 29 deletions src/werewolf/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,37 @@ pub fn generate_random_commitment<R: Rng>(
commitment_vec
}

// TODO: change to return mpc shared value
pub fn load_random_value() -> Result<Vec<Fr>, 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::<Vec<_>>();

// 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::<Vec<_>>()[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::<Vec<_>>();

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::<Vec<_>>()[0];

let random_vec = Net::broadcast(&random_value);

Ok(random_vec)
}

pub fn load_random_commitment(
Expand Down

0 comments on commit 22b51f2

Please sign in to comment.