Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed memmap from verifier and contributor commands #436

Open
wants to merge 1 commit into
base: phase2
Choose a base branch
from
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
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 21 additions & 11 deletions phase1-cli/src/bin/phase1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,59 @@ fn execute_cmd<E: Engine>(opts: Phase1Opts) {
let now = Instant::now();
match command {
Command::New(opt) => {
new_challenge(CHALLENGE_IS_COMPRESSED, &opt.challenge_fname, &parameters);
let challenge = new_challenge(CHALLENGE_IS_COMPRESSED, &parameters);
std::fs::write(&opt.challenge_fname, challenge).expect("Unable to create a challenge file");
}
Command::Contribute(opt) => {
let challenge = std::fs::read(opt.challenge_fname).expect("unable read challenge file");
// contribute to the randomness
let seed = hex::decode(&read_to_string(&opts.seed).expect("should have read seed").trim())
.expect("seed should be a hex string");
let rng = derive_rng_from_seed(&seed);
contribute(
let contribution = contribute(
CHALLENGE_IS_COMPRESSED,
&opt.challenge_fname,
&challenge,
CONTRIBUTION_IS_COMPRESSED,
&opt.response_fname,
CHECK_CONTRIBUTION_INPUT_FOR_CORRECTNESS,
&parameters,
rng,
);
std::fs::write(opt.response_fname, contribution).expect("unable to create response file");
}
Command::Beacon(opt) => {
let challenge = std::fs::read(opt.challenge_fname).expect("unable read challenge file");
// use the beacon's randomness
// Place block hash here (block number #564321)
let beacon_hash = hex::decode(&opt.beacon_hash).expect("could not hex decode beacon hash");
let rng = derive_rng_from_seed(&beacon_randomness(from_slice(&beacon_hash)));
contribute(
let contribution = contribute(
CHALLENGE_IS_COMPRESSED,
&opt.challenge_fname,
&challenge,
CONTRIBUTION_IS_COMPRESSED,
&opt.response_fname,
CHECK_CONTRIBUTION_INPUT_FOR_CORRECTNESS,
&parameters,
rng,
);
std::fs::write(opt.response_fname, contribution).expect("unable to create response file");
}
Command::VerifyAndTransformPokAndCorrectness(opt) => {
// we receive a previous participation, verify it, and generate a new challenge from it
transform_pok_and_correctness(
let challenge =
std::fs::read(opt.challenge_fname).expect("Unable to read the challenge file in this directory");
let response =
std::fs::read(opt.response_fname).expect("Unable to read the response file in this directory");

let new_challenge = transform_pok_and_correctness(
CHALLENGE_IS_COMPRESSED,
&opt.challenge_fname,
&challenge,
CONTRIBUTION_IS_COMPRESSED,
&opt.response_fname,
&response,
CHALLENGE_IS_COMPRESSED,
&opt.new_challenge_fname,
&parameters,
);

std::fs::write(opt.new_challenge_fname, new_challenge)
.expect("Unable to create new challenge file in this directory");
}
Command::VerifyAndTransformRatios(opt) => {
// we receive a previous participation, verify it, and generate a new challenge from it
Expand Down
101 changes: 29 additions & 72 deletions phase1-cli/src/contribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,56 @@ use setup_utils::{calculate_hash, CheckForCorrectness, UseCompression};

use snarkvm_curves::PairingEngine as Engine;

use memmap::*;
use rand::{CryptoRng, Rng};
use std::{
fs::OpenOptions,
io::{Read, Write},
};
use std::io::{Read, Write};

pub fn contribute<T: Engine + Sync>(
compressed_input: UseCompression,
challenge_filename: &str,
challenge: &[u8],
compressed_output: UseCompression,
response_filename: &str,
check_input_correctness: CheckForCorrectness,
parameters: &Phase1Parameters<T>,
mut rng: impl Rng + CryptoRng,
) {
// Try to load challenge file from disk.
let reader = OpenOptions::new()
.read(true)
.open(challenge_filename)
.expect("unable open challenge file");
{
let metadata = reader
.metadata()
.expect("unable to get filesystem metadata for challenge file");
let expected_challenge_length = match compressed_input {
UseCompression::Yes => parameters.contribution_size - parameters.public_key_size,
UseCompression::No => parameters.accumulator_size,
};

if metadata.len() != (expected_challenge_length as u64) {
panic!(
"The size of challenge file should be {}, but it's {}, so something isn't right.",
expected_challenge_length,
metadata.len()
);
}
}

let readable_map = unsafe {
MmapOptions::new()
.map(&reader)
.expect("unable to create a memory map for input")
) -> Vec<u8> {
let expected_challenge_length = match compressed_input {
UseCompression::Yes => parameters.contribution_size - parameters.public_key_size,
UseCompression::No => parameters.accumulator_size,
};

// 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");
if challenge.len() != expected_challenge_length {
panic!(
"The size of challenge file should be {}, but it's {}, so something isn't right.",
expected_challenge_length,
challenge.len()
);
}

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 response = vec![0; required_output_length];

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

assert!(
UseCompression::No == compressed_input,
assert_eq!(
UseCompression::No,
compressed_input,
"Hashing the compressed file in not yet defined"
);
let current_accumulator_hash = calculate_hash(&readable_map);

{
tracing::info!("`challenge` file contains decompressed points and has a hash:");
log_hash(&current_accumulator_hash);
let current_accumulator_hash = calculate_hash(&challenge);

(&mut writable_map[0..])
.write_all(&current_accumulator_hash)
.expect("unable to write a challenge hash to mmap");
tracing::info!("`challenge` file contains decompressed points and has a hash:");
log_hash(&current_accumulator_hash);

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

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

Expand All @@ -112,8 +71,8 @@ pub fn contribute<T: Engine + Sync>(

// this computes a transformation and writes it
Phase1::computation(
&readable_map,
&mut writable_map,
&challenge,
&mut response,
compressed_input,
compressed_output,
check_input_correctness,
Expand All @@ -126,14 +85,11 @@ pub fn contribute<T: Engine + Sync>(

// Write the public key
public_key
.write(&mut writable_map, compressed_output, &parameters)
.write(&mut response, compressed_output, &parameters)
.expect("unable to write public key");

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

// 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 contribution_hash = calculate_hash(&response);

tracing::info!(
"Done!\n\n\
Expand All @@ -142,6 +98,7 @@ pub fn contribute<T: Engine + Sync>(
);
log_hash(&contribution_hash);
tracing::info!("Thank you for your participation, much appreciated! :)");
response
}

fn log_hash(hash: &[u8]) {
Expand Down
35 changes: 8 additions & 27 deletions phase1-cli/src/new_challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,43 @@ use setup_utils::{blank_hash, calculate_hash, print_hash, UseCompression};

use snarkvm_curves::PairingEngine as Engine;

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

pub fn new_challenge<T: Engine + Sync>(
compress_new_challenge: UseCompression,
challenge_filename: &str,
parameters: &Phase1Parameters<T>,
) {
) -> Vec<u8> {
println!(
"Will generate an empty accumulator for 2^{} powers of tau",
parameters.total_size_in_log2
);
println!("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")
};
let mut challenge = vec![0; expected_challenge_length];

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

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

Phase1::initialization(&mut writable_map, compress_new_challenge, &parameters)
Phase1::initialization(&mut challenge, 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 contribution_hash = calculate_hash(&challenge);

println!("Empty contribution is formed with a hash:");
print_hash(&contribution_hash);
println!("Wrote a fresh accumulator to challenge file");

challenge
}
Loading