Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 18 additions & 44 deletions phase1-cli/src/contribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use algebra::PairingEngine as Engine;
use memmap::*;
use rand::Rng;
use std::{
fs::OpenOptions,
io::{Read, Write},
fs::{OpenOptions},
io::{Read, Write, BufWriter},

};
use std::fs::File;
use tracing::info;

const COMPRESSED_INPUT: UseCompression = UseCompression::No;
Expand All @@ -29,7 +31,7 @@ pub fn contribute<T: Engine + Sync>(
.read(true)
.open(challenge_filename)
.expect("unable open challenge file");
{

let metadata = reader
.metadata()
.expect("unable to get filesystem metadata for challenge file");
Expand All @@ -45,36 +47,20 @@ pub fn contribute<T: Engine + Sync>(
metadata.len()
);
}
}


let readable_map = unsafe {
MmapOptions::new()
.map(&reader)
.expect("unable to create a memory map for input")
};

// Create response file in this directory
let writer = OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(response_filename)
.expect("unable to create response file");

let required_output_length = match COMPRESSED_OUTPUT {
UseCompression::Yes => parameters.contribution_size,
UseCompression::No => parameters.accumulator_size + parameters.public_key_size,
};

writer
.set_len(required_output_length as u64)
.expect("must make output file large enough");

let mut writable_map = unsafe {
MmapOptions::new()
.map_mut(&writer)
.expect("unable to create a memory map for output")
};
let mut writable_map = vec![0; required_output_length];

info!("Calculating previous contribution hash...");

Expand All @@ -84,33 +70,17 @@ pub fn contribute<T: Engine + Sync>(
);
let current_accumulator_hash = calculate_hash(&readable_map);

{
info!("`challenge` file contains decompressed points and has a hash:");

info!("`challenge` file contains decompressed points and has a hash:");
print_hash(&current_accumulator_hash);
std::fs::File::create(challenge_hash_filename)
.expect("unable to open current accumulator hash file")
.write_all(current_accumulator_hash.as_slice())
.expect("unable to write current accumulator hash");

(&mut writable_map[0..])
.write_all(current_accumulator_hash.as_slice())
.expect("unable to write a challenge hash to mmap");

writable_map.flush().expect("unable to write hash to response file");
}

{
let mut challenge_hash = [0; 64];
let mut memory_slice = readable_map.get(0..64).expect("must read point data from file");
memory_slice
.read_exact(&mut challenge_hash)
.expect("couldn't read hash of challenge file from response file");
writable_map[..current_accumulator_hash.len()]
.copy_from_slice(current_accumulator_hash.as_slice());

info!(
"`challenge` file claims (!!! Must not be blindly trusted) that it was based on the original contribution with a hash:"
);
print_hash(&challenge_hash);
}

// Construct our keypair using the RNG we created above
let (public_key, private_key) =
Expand Down Expand Up @@ -139,11 +109,15 @@ pub fn contribute<T: Engine + Sync>(
.write(&mut writable_map, COMPRESSED_OUTPUT, &parameters)
.expect("unable to write public key");

writable_map.flush().expect("must flush a memory map");
let mut file = BufWriter::new(File::create(response_filename).expect("unable to create challenge file"));
file.write_all(&writable_map).expect("unable to write buffer to challenge file");
file.flush().expect("unable to flush buffer to challenge file");

// Get the hash of the contribution, so the user can compare later
let output_readonly = writable_map.make_read_only().expect("must make a map readonly");
let contribution_hash = calculate_hash(&output_readonly);
let mut file = File::open(response_filename).expect("unable to open challenge file for hashing");
let mut file_contents = Vec::new();
file.read_to_end(&mut file_contents).expect("unable to read challenge file");
let contribution_hash = calculate_hash(&file_contents);

info!(
"Done!\n\n\
Expand Down
54 changes: 17 additions & 37 deletions phase1-cli/src/new_challenge.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use phase1::{Phase1, Phase1Parameters};
use setup_utils::{blank_hash, calculate_hash, print_hash, UseCompression};
use setup_utils::{calculate_hash, print_hash, UseCompression};

use algebra::PairingEngine as Engine;

use memmap::*;
use std::{fs::OpenOptions, io::Write};
use std::{io::Write};
use tracing::info;

use std::fs::File;
use std::io::{Read, BufWriter};

const COMPRESS_NEW_CHALLENGE: UseCompression = UseCompression::No;

pub fn new_challenge<T: Engine + Sync>(
Expand All @@ -20,46 +22,24 @@ pub fn new_challenge<T: Engine + Sync>(
);
info!("In total will generate up to {} powers", parameters.powers_g1_length);

let file = OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(challenge_filename)
.expect("unable to create challenge file");

let expected_challenge_length = match COMPRESS_NEW_CHALLENGE {
UseCompression::Yes => parameters.contribution_size - parameters.public_key_size,
UseCompression::No => parameters.accumulator_size,
};

file.set_len(expected_challenge_length as u64)
.expect("unable to allocate large enough file");

let mut writable_map = unsafe {
MmapOptions::new()
.map_mut(&file)
.expect("unable to create a memory map")
};

// Write a blank BLAKE2b hash:
let hash = blank_hash();
(&mut writable_map[0..])
.write_all(hash.as_slice())
.expect("unable to write a default hash to mmap");
writable_map
.flush()
.expect("unable to write blank hash to challenge file");

info!("Blank hash for an empty challenge:");
print_hash(&hash);

Phase1::initialization(&mut writable_map, COMPRESS_NEW_CHALLENGE, &parameters)
let mut buffer = vec![0; expected_challenge_length];

Phase1::initialization(&mut buffer, COMPRESS_NEW_CHALLENGE, &parameters)
.expect("generation of initial accumulator is successful");
writable_map.flush().expect("unable to flush memmap to disk");

// Get the hash of the contribution, so the user can compare later
let output_readonly = writable_map.make_read_only().expect("must make a map readonly");
let contribution_hash = calculate_hash(&output_readonly);

let mut file = BufWriter::new(File::create(challenge_filename).expect("unable to create challenge file"));
file.write_all(&buffer).expect("unable to write buffer to challenge file");
file.flush().expect("unable to flush buffer to challenge file");

let mut file = File::open(challenge_filename).expect("unable to open challenge file for hashing");
let mut file_contents = Vec::new();
file.read_to_end(&mut file_contents).expect("unable to read challenge file");
let contribution_hash = calculate_hash(&file_contents);

std::fs::File::create(challenge_hash_filename)
.expect("unable to open new challenge hash file")
Expand Down
55 changes: 23 additions & 32 deletions phase1-cli/src/transform_pok_and_correctness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use setup_utils::{calculate_hash, print_hash, CheckForCorrectness, SubgroupCheck

use memmap::*;
use std::{
fs::OpenOptions,
io::{Read, Write},
fs::{File, OpenOptions},
io::{BufWriter, Read, Write},
};
use tracing::info;

Expand Down Expand Up @@ -137,34 +137,13 @@ pub fn transform_pok_and_correctness<T: Engine + Sync>(

info!("Verifying a contribution to contain proper powers and correspond to the public key...");

// Create new challenge file in this directory
let writer = OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(new_challenge_filename)
.expect("unable to create new challenge file in this directory");

// Recomputation strips the public key and uses hashing to link with the previous contribution after decompression
writer
.set_len(parameters.accumulator_size as u64)
.expect("must make output file large enough");

let mut writable_map = unsafe {
MmapOptions::new()
.map_mut(&writer)
.expect("unable to create a memory map for output")
};
let mut new_challenge_writer = BufWriter::new(
File::create(new_challenge_filename).expect("unable to create new challenge file in this directory"),
);

{
(&mut writable_map[0..])
.write_all(response_hash.as_slice())
.expect("unable to write a default hash to mmap");
let mut writable_map = vec![0; parameters.accumulator_size];

writable_map
.flush()
.expect("unable to write hash to new challenge file");
}
writable_map[..response_hash.len()].copy_from_slice(response_hash.as_slice());

let res = Phase1::verification(
&challenge_readable_map,
Expand All @@ -186,14 +165,26 @@ pub fn transform_pok_and_correctness<T: Engine + Sync>(

writable_map.flush().expect("must flush the memory map");

let new_challenge_readable_map = writable_map.make_read_only().expect("must make a map readonly");
// Write the contents of writable_map to the new challenge file
new_challenge_writer
.write_all(&writable_map)
.expect("unable to write to new challenge file");
new_challenge_writer
.flush()
.expect("unable to flush new challenge file");

let recompressed_hash = calculate_hash(&new_challenge_readable_map);
let recompressed_hash = calculate_hash(&writable_map);

std::fs::File::create(new_challenge_hash_filename)
.expect("unable to open new challenge hash file")
let mut new_challenge_hash_file = BufWriter::new(
File::create(new_challenge_hash_filename)
.expect("unable to open new challenge hash file"),
);
new_challenge_hash_file
.write_all(recompressed_hash.as_slice())
.expect("unable to write new challenge hash");
new_challenge_hash_file
.flush()
.expect("unable to flush new challenge hash file");

info!("Here's the BLAKE2b hash of the decompressed participant's response as new_challenge file:");
print_hash(&recompressed_hash);
Expand Down