Added key caching

This commit is contained in:
Sean Bowe 2015-12-19 23:17:51 -07:00
parent 1e30a9e17c
commit 76b5f57dbe
6 changed files with 201 additions and 5 deletions

2
.gitignore vendored
View File

@ -1,6 +1,8 @@
test
*.o
*.d
*.pk
*.vk
depinst
depsrc
target

84
Cargo.lock generated
View File

@ -3,7 +3,10 @@ name = "pay-to-sudoku"
version = "0.0.1"
dependencies = [
"bincode 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bzip2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 1.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
"fixedbitset 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
@ -20,6 +23,11 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ansi_term"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bincode"
version = "0.4.0"
@ -31,26 +39,92 @@ dependencies = [
"serde 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byteorder"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bzip2"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bzip2-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bzip2-sys"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "clap"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fixedbitset"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "flate2"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gcc"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hex"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "miniz-sys"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num"
version = "0.1.28"
@ -83,6 +157,16 @@ dependencies = [
"num 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "strsim"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "whiteread"
version = "0.2.0"

View File

@ -13,9 +13,12 @@ path = "src/client.rs"
[dependencies]
bincode = "0.4.0"
bzip2 = "0.2.3"
clap = "1.5.4"
fixedbitset = "0.1.0"
flate2 = "0.2.11"
hex = "0.1.0"
libc = "0.2.2"
rand = "0.3.12"
serde = "0.6.6"
whiteread = "0.2.0"
hex = "0.1.0"

View File

@ -60,16 +60,21 @@ extern "C" fn handle_keypair_callback(cb: *mut c_void, pk_s: *const c_char, pk_l
}
}
pub fn generate_keypair<F: for<'a> FnMut(&'a [i8], &'a [i8])>(num: usize, mut f: F) {
let mut cb: &mut for<'a> FnMut(&'a [i8], &'a [i8]) = &mut f;
pub fn generate_keypair<F: for<'a> FnMut(&'a [u8], &'a [u8])>(num: usize, mut f: F) {
let mut cb: &mut for<'a> FnMut(&'a [u8], &'a [u8]) = &mut f;
unsafe {
gen_keypair(num as u32, (&mut cb) as *mut _ as *mut c_void, handle_keypair_callback);
}
}
pub fn get_context(pk: &[i8], vk: &[i8], n: usize) -> Context {
pub fn get_context(pk: &[u8], vk: &[u8], n: usize) -> Context {
let keypair = unsafe {
use std::mem::transmute;
let pk: &[i8] = transmute(pk);
let vk: &[i8] = transmute(vk);
load_keypair(&pk[0], pk.len() as i32, &vk[0], vk.len() as i32)
};

View File

@ -29,6 +29,8 @@ extern crate libc;
extern crate bincode;
extern crate hex;
extern crate serde;
extern crate clap;
extern crate flate2;
use std::net::{TcpListener,TcpStream};
use std::io::{self, Read, Write};
@ -40,10 +42,76 @@ use bincode::SizeLimit::Infinite;
use serde::bytes::Bytes;
use std::borrow::Cow;
use hex::{ToHex, FromHex};
use clap::{App, Arg, SubCommand};
mod ffi;
mod util;
fn main() {
initialize();
let matches = App::new("pay-to-sudoku")
.subcommand(SubCommand::with_name("gen")
.about("Generates a proving/verifying zkSNARK keypair")
.arg(Arg::with_name("n")
.required(true)
.validator(|val| {
let n = val.parse::<usize>();
match n {
Err(_) => Err("`n` must be a number".into()),
Ok(n) => {
if n == 0 || n > 9 {
Err("0 < n < n".into())
} else {
Ok(())
}
}
}
})
))
.subcommand(SubCommand::with_name("test"))
.get_matches();
if let Some(ref matches) = matches.subcommand_matches("gen") {
let n: usize = matches.value_of("n").unwrap().parse().unwrap();
generate_keypair(n, |pk, vk| {
println!("Serialized proving key size in bytes: {}", pk.len());
println!("Serialized verifying key size in bytes: {}", vk.len());
println!("Storing...");
write_compressed(&format!("{}.pk", n), &pk);
write_compressed(&format!("{}.vk", n), &vk);
});
}
if let Some(ref matches) = matches.subcommand_matches("test") {
let n = 3;
let ctx = {
let pk = decompress(&format!("{}.pk", n));
let vk = decompress(&format!("{}.vk", n));
get_context(&pk, &vk, n)
};
println!("Enter puzzle:");
let puzzle = get_sudoku_from_stdin(n*n);
println!("Enter solution:");
let solution = get_sudoku_from_stdin(n*n);
let key = vec![206, 64, 25, 10, 245, 205, 246, 107, 191, 157, 114, 181, 63, 40, 95, 134, 6, 178, 210, 43, 243, 10, 217, 251, 246, 248, 0, 21, 86, 194, 100, 94];
let h_of_key = vec![253, 199, 66, 55, 24, 155, 80, 121, 138, 60, 36, 201, 186, 221, 164, 65, 194, 53, 192, 159, 252, 7, 194, 24, 200, 217, 57, 55, 45, 204, 71, 9];
assert!(prove(ctx, &puzzle, &solution, &key, &h_of_key,
|encrypted_solution, proof| {}));
}
}
/*
fn main() {
initialize();
@ -119,4 +187,5 @@ fn handle_client(stream: &mut TcpStream, ctx: Context, pk: &[i8], vk: &[i8]) {
println!("The remote end provided a proof that wasn't valid!");
}
}
}
}
*/

View File

@ -1,5 +1,8 @@
use std::io::{self, Read, Write};
use whiteread::{self,parse_line};
use std::fs;
use flate2::write::{ZlibEncoder, ZlibDecoder};
use flate2::Compression;
pub fn print_sudoku(dim: usize, grid: &[u8]) {
for y in 0..dim {
@ -30,4 +33,34 @@ pub fn get_sudoku_from_stdin(dimension: usize) -> Vec<u8> {
}
acc
}
pub fn write_compressed(path: &str, data: &[u8]) {
let handle = fs::File::create(path).unwrap();
let mut encoder = ZlibEncoder::new(handle, Compression::Best);
encoder.write_all(data).unwrap();
encoder.finish().unwrap();
}
pub fn decompress(path: &str) -> Vec<u8> {
let mut result = Vec::new();
{
let mut decoder = ZlibDecoder::new(&mut result);
let mut handle = fs::File::open(path).unwrap();
loop {
let mut buf = [0; 1024];
let read = handle.read(&mut buf).unwrap();
if read == 0 {
decoder.finish();
break;
}
decoder.write_all(&buf[0..read]).unwrap();
}
}
result
}