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

Add unpredictable chain of values to epoch message #183

Closed
wants to merge 18 commits into from
Closed
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
78 changes: 78 additions & 0 deletions crates/bls-snark-sys/src/snark/epoch_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ const PUBKEY_BYTES: usize = 96;
#[no_mangle]
pub extern "C" fn encode_epoch_block_to_bytes(
in_epoch_index: c_ushort,
in_epoch_entropy: *const u8,
in_parent_entropy: *const u8,
in_maximum_non_signers: c_uint,
in_maximum_validators :c_uint,
in_added_public_keys: *const *const PublicKey,
in_added_public_keys_len: c_int,
in_should_encode_aggregated_pk: bool,
out_bytes: *mut *mut u8,
out_len: *mut c_int,
// Add max validators length
) -> bool {
convert_result_to_bool::<_, EncodingError, _>(|| {
let added_public_keys_ptrs = unsafe {
Expand All @@ -33,14 +37,22 @@ pub extern "C" fn encode_epoch_block_to_bytes(
.map(|pk| unsafe { &*pk }.clone())
.collect::<Vec<PublicKey>>();

let epoch_entropy = unsafe { read_epoch_entropy(in_epoch_entropy) };
let parent_entropy = unsafe { read_epoch_entropy(in_parent_entropy) };
let epoch_block = EpochBlock::new(
in_epoch_index as u16,
epoch_entropy,
parent_entropy,
in_maximum_non_signers as u32,
in_maximum_validators as usize,
added_public_keys,
);
// Add padding within encode to bytes
let mut encoded = if in_should_encode_aggregated_pk {
epoch_block.encode_to_bytes_with_aggregated_pk()?
} else {
// Pad the bits in proving in cli prover with the correct zeroes
// pad the result here with the generator instances to get to the max validators length
epoch_block.encode_to_bytes()?
};
encoded.shrink_to_fit();
Expand All @@ -59,22 +71,33 @@ pub extern "C" fn encode_epoch_block_to_bytes(
pub struct EpochBlockFFI {
/// The epoch's index
pub index: u16,
/// The epoch's entropy value, derived from the epoch block hash.
pub epoch_entropy: *const u8,
/// The parent epoch's entropy value.
pub parent_entropy: *const u8,
/// Pointer to the public keys array
pub pubkeys: *const u8,
/// The number of public keys to be read from the pointer
pub pubkeys_num: usize,
/// Maximum number of non signers for that epoch
pub maximum_non_signers: u32,
/// Maximum number of validators
pub maximum_validators: usize,
}

impl TryFrom<&EpochBlockFFI> for EpochBlock {
type Error = EncodingError;

fn try_from(src: &EpochBlockFFI) -> Result<EpochBlock, Self::Error> {
let pubkeys = unsafe { read_pubkeys(src.pubkeys, src.pubkeys_num as usize)? };
let epoch_entropy = unsafe { read_epoch_entropy(src.epoch_entropy) };
let parent_entropy = unsafe { read_epoch_entropy(src.parent_entropy) };
Ok(EpochBlock {
index: src.index,
epoch_entropy,
parent_entropy,
maximum_non_signers: src.maximum_non_signers,
maximum_validators: src.maximum_validators,
new_public_keys: pubkeys,
})
}
Expand Down Expand Up @@ -130,6 +153,20 @@ unsafe fn read_pubkeys(ptr: *const u8, num: usize) -> Result<Vec<PublicKey>, Enc
Ok(pubkeys)
}

/// Reads `ENTROPY_BYTES` byte epoch entropy value from the given pointer location.
///
/// # Safety
///
/// This WILL read invalid data if the given pointer locates less than `ENTROPY_BYTES`
/// bytes of data. Use with caution.
unsafe fn read_epoch_entropy(ptr: *const u8) -> Option<Vec<u8>> {
if ptr.is_null() {
None
} else {
Some(slice::from_raw_parts(ptr, EpochBlock::ENTROPY_BYTES).to_vec())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -145,16 +182,57 @@ mod tests {
fn ffi_block_conversion() {
let num_keys = 10;
let pubkeys = rand_pubkeys(num_keys);
let epoch_entropy = Some((0..EpochBlock::ENTROPY_BYTES).map(|n| n as u8).collect());
let parent_entropy = Some(
(EpochBlock::ENTROPY_BYTES..2 * EpochBlock::ENTROPY_BYTES)
.map(|n| n as u8)
.collect(),
);
let block = EpochBlock {
index: 1,
epoch_entropy,
parent_entropy,
maximum_non_signers: 19,
maximum_validators: pubkeys.len(),
new_public_keys: pubkeys,
};
let src = block;
let serialized_pubkeys = serialize_pubkeys(&src.new_public_keys).unwrap();
let ffi_block = EpochBlockFFI {
index: src.index,
epoch_entropy: &src.epoch_entropy.as_ref().unwrap()[0],
parent_entropy: &src.parent_entropy.as_ref().unwrap()[0],
maximum_non_signers: src.maximum_non_signers,
maximum_validators: src.new_public_keys.len(),
pubkeys_num: src.new_public_keys.len(),
pubkeys: &serialized_pubkeys[0] as *const u8,
};
let block_from_ffi = EpochBlock::try_from(&ffi_block).unwrap();
assert_eq!(block_from_ffi, src);
}

#[test]
fn ffi_block_conversion_without_entropy() {
let num_keys = 10;
let pubkeys = rand_pubkeys(num_keys);
let epoch_entropy = None;
let parent_entropy = None;
let block = EpochBlock {
index: 1,
epoch_entropy,
parent_entropy,
maximum_non_signers: 19,
maximum_validators: pubkeys.len(),
new_public_keys: pubkeys,
};
let src = block;
let serialized_pubkeys = serialize_pubkeys(&src.new_public_keys).unwrap();
let ffi_block = EpochBlockFFI {
index: src.index,
epoch_entropy: std::ptr::null(),
parent_entropy: std::ptr::null(),
maximum_non_signers: src.maximum_non_signers,
maximum_validators: src.new_public_keys.len(),
pubkeys_num: src.new_public_keys.len(),
pubkeys: &serialized_pubkeys[0] as *const u8,
};
Expand Down
7 changes: 7 additions & 0 deletions crates/bls-snark-sys/src/snark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod tests {
#[test]
// Trimmed down version of the other E2E groth test to ensure
// that the verifier works correctly for a proof which we have verified on our own
// TODO(#185) Include epoch entropy values here when a new proof is available.
fn simple_verifier_groth16() {
let serialized_proof = hex::decode(PROOF).unwrap();
let serialized_vk = hex::decode(VK).unwrap();
Expand All @@ -65,15 +66,21 @@ mod tests {

let first_epoch = EpochBlockFFI {
index: 0,
epoch_entropy: std::ptr::null(),
parent_entropy: std::ptr::null(),
maximum_non_signers: 1,
pubkeys_num: 4,
maximum_validators: 4,
pubkeys: &first_pubkeys[0] as *const u8,
};

let last_epoch = EpochBlockFFI {
index: 2,
epoch_entropy: std::ptr::null(),
parent_entropy: std::ptr::null(),
maximum_non_signers: 1,
pubkeys_num: 4,
maximum_validators: 4,
pubkeys: &last_pubkeys[0] as *const u8,
};

Expand Down
4 changes: 4 additions & 0 deletions crates/epoch-snark/src/api/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ fn generate_hash_helper(
fn to_epoch_data(block: &EpochBlock) -> EpochData<BLSCurve> {
EpochData {
index: Some(block.index),
epoch_entropy: block.epoch_entropy.as_ref().map(|e| e.to_vec()),
parent_entropy: block.parent_entropy.as_ref().map(|e| e.to_vec()),
maximum_non_signers: block.maximum_non_signers,
public_keys: block
.new_public_keys
Expand All @@ -153,6 +155,8 @@ fn to_dummy_update(num_validators: u32) -> SingleUpdate<BLSCurve> {
SingleUpdate {
epoch_data: EpochData {
maximum_non_signers: 0,
epoch_entropy: Some(vec![0u8; EpochBlock::ENTROPY_BYTES]),
parent_entropy: Some(vec![0u8; EpochBlock::ENTROPY_BYTES]),
index: Some(0),
public_keys: (0..num_validators)
.map(|_| Some(BLSCurveG2::prime_subgroup_generator()))
Expand Down
Loading