use system parameters struct

This commit is contained in:
collin 2020-08-12 19:15:55 -07:00
parent 4f3722033a
commit 9b5d14970d
4 changed files with 42 additions and 40 deletions

View File

@ -2,7 +2,10 @@ use crate::{verify_record_commitment, LocalDataVerificationError, StateLeafValue
use leo_typed::Input as TypedInput;
use snarkos_algorithms::commitment_tree::CommitmentMerklePath;
use snarkos_dpc::base_dpc::instantiated::{Components, LocalDataCRH, LocalDataCommitment, RecordCommitment};
use snarkos_dpc::base_dpc::{
instantiated::{Components, LocalDataCRH, LocalDataCommitment},
parameters::SystemParameters,
};
use snarkos_models::{
algorithms::{CommitmentScheme, CRH},
dpc::DPCComponents,
@ -12,14 +15,12 @@ use snarkos_utilities::{bytes::ToBytes, to_bytes, FromBytes};
use std::convert::TryFrom;
pub fn verify_local_data_commitment(
system_parameters: &SystemParameters<Components>,
typed_input: &TypedInput,
record_commitment_params: RecordCommitment,
local_data_commitment_params: LocalDataCommitment,
local_data_crh_params: LocalDataCRH,
) -> Result<bool, LocalDataVerificationError> {
// verify record commitment
let typed_record = typed_input.get_record();
let dpc_record_values = verify_record_commitment(typed_record, record_commitment_params)?;
let dpc_record_values = verify_record_commitment(system_parameters, typed_record)?;
let record_commitment: Vec<u8> = dpc_record_values.commitment;
let record_serial_number: Vec<u8> = dpc_record_values.serial_number;
@ -47,12 +48,15 @@ pub fn verify_local_data_commitment(
// Construct local data commitment leaf
let local_data_leaf_randomness = <LocalDataCommitment as CommitmentScheme>::Randomness::read(&leaf_randomness[..])?;
let local_data_commitment_leaf =
LocalDataCommitment::commit(&local_data_commitment_params, &input_bytes, &local_data_leaf_randomness)?;
let local_data_commitment_leaf = LocalDataCommitment::commit(
&system_parameters.local_data_commitment,
&input_bytes,
&local_data_leaf_randomness,
)?;
// Construct record commitment merkle path
let mut local_data_merkle_path = CommitmentMerklePath::<LocalDataCommitment, LocalDataCRH>::read(&path[..])?;
local_data_merkle_path.parameters = Some(local_data_crh_params);
local_data_merkle_path.parameters = Some(system_parameters.local_data_crh.clone());
// Check record commitment merkle path is valid for the given local data commitment root
let local_data_commitment_root = <LocalDataCRH as CRH>::Output::read(&root[..])?;

View File

@ -1,15 +1,18 @@
use crate::{DPCRecordValues, RecordVerificationError};
use leo_typed::Record as TypedRecord;
use snarkos_dpc::base_dpc::instantiated::RecordCommitment;
use snarkos_dpc::base_dpc::{
instantiated::{Components, RecordCommitment},
parameters::SystemParameters,
};
use snarkos_models::algorithms::CommitmentScheme;
use snarkos_utilities::{bytes::ToBytes, to_bytes, FromBytes};
use std::convert::TryFrom;
pub fn verify_record_commitment(
system_parameters: &SystemParameters<Components>,
typed_record: &TypedRecord,
record_commitment_params: RecordCommitment,
) -> Result<DPCRecordValues, RecordVerificationError> {
// generate a dpc record from the typed record
let record = DPCRecordValues::try_from(typed_record)?;
@ -30,7 +33,7 @@ pub fn verify_record_commitment(
<RecordCommitment as CommitmentScheme>::Randomness::read(&record.commitment_randomness[..])?;
let record_commitment = RecordCommitment::commit(
&record_commitment_params,
&system_parameters.record_commitment,
&record_commitment_input,
&commitment_randomness,
)?;

View File

@ -16,7 +16,29 @@ use rand_xorshift::XorShiftRng;
use snarkos_models::dpc::DPCScheme;
#[test]
fn test_integrate_with_dpc() {
fn test_verify_local_data_commitment_from_file() {
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
// Generate parameters for the record commitment scheme
let system_parameters = InstantiatedDPC::generate_system_parameters(&mut rng).unwrap();
// Load test record state file from `inputs/test.state`
let file_bytes = include_bytes!("inputs/test_state.state");
let file_string = String::from_utf8_lossy(file_bytes);
let file = LeoInputParser::parse_file(&file_string).unwrap();
let mut program_input = Input::new();
program_input.parse_state(file).unwrap();
// check record state is correct by verifying commitment
let result = verify_local_data_commitment(&system_parameters, &program_input).unwrap();
assert!(result);
}
#[test]
#[ignore]
fn test_generate_values_from_dpc() {
use snarkos_testing::storage::*;
type L = Ledger<Tx, CommitmentMerkleParameters>;
@ -175,30 +197,3 @@ fn test_integrate_with_dpc() {
println!();
println!("////////////////////////////////////////////////////");
}
#[test]
fn test_verify_local_data_commitment_from_file() {
let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
// Generate parameters for the record commitment scheme
let system_parameters = InstantiatedDPC::generate_system_parameters(&mut rng).unwrap();
// Load test record state file from `inputs/test.state`
let file_bytes = include_bytes!("inputs/test_state.state");
let file_string = String::from_utf8_lossy(file_bytes);
let file = LeoInputParser::parse_file(&file_string).unwrap();
let mut program_input = Input::new();
program_input.parse_state(file).unwrap();
// check record state is correct by verifying commitment
let result = verify_local_data_commitment(
&program_input,
system_parameters.record_commitment,
system_parameters.local_data_commitment,
system_parameters.local_data_crh,
)
.unwrap();
assert!(result);
}

View File

@ -25,5 +25,5 @@ fn test_verify_record_from_file() {
let typed_record = program_input.get_record();
// check record state is correct by verifying commitment
let _values = verify_record_commitment(typed_record, system_parameters.record_commitment).unwrap();
let _values = verify_record_commitment(&system_parameters, typed_record).unwrap();
}