-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f01065
commit 8b11743
Showing
6 changed files
with
234 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
500 40 80 80 0. 0. 0. 1. 1. 1. 1 10000 | ||
# The first line of this file contains the configuration data. Each number must be followed by a space. | ||
# In order, they represent: | ||
# the time between steps in µs | ||
# the pixel size | ||
# the grid dimensions (height then depth) | ||
# the ‘dead’ color (r, g, b) | ||
# the ‘alive’ color (r, g, b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
1 0 0 0 0 0 | ||
0 1 1 0 0 0 | ||
1 1 0 0 0 0 | ||
0 0 0 0 0 0 | ||
0 0 0 0 0 0 | ||
0 0 0 0 0 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
pub use ggez::{ GameResult, Context, graphics, conf, event }; | ||
use std::time::Duration; | ||
use std::thread::sleep; | ||
use std::sync::Mutex; | ||
use super::*; | ||
|
||
pub struct MainState { | ||
board: Board, | ||
time_step: Duration, | ||
first_frame: bool, | ||
pixel_size: usize, | ||
col1: (f32,f32,f32), | ||
col2: (f32,f32,f32), | ||
server_keys: Vec<Mutex<ServerKey>>, | ||
zeros: (Ciphertext, Ciphertext, Ciphertext), | ||
client_key: ClientKey | ||
} | ||
|
||
impl MainState { | ||
pub fn new(board: Board, config: &Config, | ||
server_keys: Vec<Mutex<ServerKey>>, zeros: (Ciphertext, Ciphertext, Ciphertext), | ||
client_key: ClientKey) | ||
-> Result<MainState, Box<dyn std::error::Error>> | ||
{ | ||
Ok(MainState { board, | ||
time_step: Duration::from_micros(config.wait_time_micros), | ||
first_frame: true, | ||
pixel_size: config.pixel_size, | ||
col1: config.col1, | ||
col2: config.col2, | ||
server_keys, | ||
zeros, | ||
client_key }) | ||
} | ||
} | ||
|
||
impl event::EventHandler<ggez::GameError> for MainState { | ||
|
||
fn update(&mut self, _ctx: &mut Context) -> GameResult { | ||
if self.first_frame { | ||
self.first_frame = false; | ||
} else { | ||
sleep(self.time_step); | ||
self.board.update(&self.server_keys, &self.zeros); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn draw(&mut self, mut ctx: &mut Context) -> GameResult { | ||
|
||
// clear the window | ||
graphics::clear(ctx, [self.col1.0, self.col1.1, self.col1.2, 1.].into()); | ||
|
||
for i in 0..self.board.dimensions.0 { | ||
for j in 0..self.board.dimensions.1 { | ||
if self.client_key.decrypt(&self.board.states[i*self.board.dimensions.1 + j]) { | ||
let pixel = graphics::MeshBuilder::new().rectangle( | ||
graphics::DrawMode::Fill(graphics::FillOptions::DEFAULT), | ||
graphics::Rect::new(0., 0., self.pixel_size as f32, self.pixel_size as f32), | ||
graphics::Color::new(self.col2.0, self.col2.1, self.col2.2, 1.) | ||
) | ||
.unwrap() | ||
.build(&mut ctx).unwrap(); | ||
graphics::draw(ctx, &pixel, graphics::DrawParam::new() | ||
.offset([-((j*self.pixel_size) as f32), -((i*self.pixel_size) as f32)]))?; | ||
} | ||
} | ||
} | ||
|
||
graphics::present(ctx)?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,45 @@ | ||
use homomorphic_game_of_life::*; | ||
use std::sync::Mutex; | ||
|
||
fn main() { | ||
|
||
// numbers of rows and columns in the board | ||
let (n_rows, n_cols): (usize, usize) = (6,6); | ||
|
||
|
||
// number of times the server key is duplicated | ||
let n_sk = 16; | ||
|
||
// read the dimensions and initial state | ||
let (dimensions, initial_state): ((usize, usize), Vec<bool>) | ||
= read_csv("initial_state.csv").unwrap(); | ||
|
||
// read the cofig file | ||
let config = Config::read("Config.csv").unwrap(); | ||
|
||
// set-up the graphical interface | ||
let cb = ggez::ContextBuilder::new("Game of Life", "FM") | ||
.window_setup(conf::WindowSetup::default().title("Conway's Game of Life")) | ||
.window_mode(conf::WindowMode::default().dimensions( | ||
(config.pixel_size*dimensions.1) as f32, | ||
(config.pixel_size*dimensions.0) as f32)); | ||
let (ctx, event_loop) = cb.build().unwrap(); | ||
|
||
// generate the keys | ||
let (client_key, server_key) = gen_keys(); | ||
|
||
// encrypt false three times | ||
let zeros = (client_key.encrypt(false), client_key.encrypt(false), client_key.encrypt(false)); | ||
|
||
// initial configuration | ||
let states = vec![true, false, false, false, false, false, | ||
false, true, true, false, false, false, | ||
true, true, false, false, false, false, | ||
false, false, false, false, false, false, | ||
false, false, false, false, false, false, | ||
false, false, false, false, false, false]; | ||
|
||
// encrypt the initial configuration | ||
let states: Vec<Ciphertext> = states.into_iter().map(|x| client_key.encrypt(x)).collect(); | ||
let initial_state: Vec<Ciphertext> = initial_state.into_iter().map(|x| client_key.encrypt(x)).collect(); | ||
|
||
// build the board | ||
let mut board = Board::new(n_cols, states); | ||
|
||
loop { | ||
|
||
// show the board | ||
for i in 0..n_rows { | ||
println!(""); | ||
for j in 0..n_rows { | ||
if client_key.decrypt(&board.states[i*n_cols+j]) { | ||
print!("█"); | ||
} else { | ||
print!("░"); | ||
} | ||
} | ||
} | ||
println!(""); | ||
|
||
// increase the time step | ||
board.evolve(&server_key, &zeros); | ||
let board = Board::new(dimensions.1, initial_state); | ||
|
||
// create a vector of server keys for multithreading | ||
let mut server_keys = Vec::<Mutex<ServerKey>>::new(); | ||
for _ in 0..n_sk { | ||
server_keys.push(Mutex::new(server_key.clone())); | ||
} | ||
|
||
// run the simulation | ||
let state = MainState::new(board, &config, server_keys, zeros, client_key).unwrap(); | ||
event::run(ctx, event_loop, state) | ||
} |