add typed state values parsing

This commit is contained in:
collin 2020-08-12 12:30:30 -07:00
parent 36b0d6a508
commit 83ea7c18b9
11 changed files with 113 additions and 39 deletions

View File

@ -0,0 +1,13 @@
use std::num::ParseIntError;
#[derive(Debug, Error)]
pub enum InputValueError {
#[error("expected parameter array of u8 bytes, found `{}`", _0)]
ExpectedBytes(String),
#[error("expected integer parameter, found `{}`", _0)]
ExpectedInteger(String),
#[error("{}", _0)]
ParseIntError(#[from] ParseIntError),
}

View File

@ -1,5 +1,11 @@
pub mod local_data_verification_error;
pub use self::local_data_verification_error::*;
pub mod input_value;
pub use self::input_value::*;
pub mod record_verification_error;
pub use self::record_verification_error::*;
pub mod state_values;
pub use self::state_values::*;
pub mod verify_local_data_commitment;
pub use self::verify_local_data_commitment::*;
pub mod verify_record_commitment;
pub use self::verify_record_commitment::*;

View File

@ -0,0 +1,5 @@
#[derive(Debug, Error)]
pub enum StateValuesError {
#[error("state parameter `{}` not found in state file", _0)]
MissingParameter(String),
}

View File

@ -1,3 +1,5 @@
use crate::InputValueError;
use snarkos_errors::{algorithms::CommitmentError, objects::account::AccountError};
use std::{io::Error as IOError, num::ParseIntError, str::ParseBoolError};
@ -13,11 +15,8 @@ pub enum RecordVerificationError {
#[error("{}", _0)]
CommitmentError(#[from] CommitmentError),
#[error("expected parameter array of u8 bytes, found `{}`", _0)]
ExpectedBytes(String),
#[error("expected integer parameter, found `{}`", _0)]
ExpectedInteger(String),
#[error("{}", _0)]
InputValueError(#[from] InputValueError),
#[error("{}", _0)]
IOError(#[from] IOError),

View File

@ -9,3 +9,6 @@ pub use self::local_data_commitment::*;
pub mod record_commitment;
pub use self::record_commitment::*;
pub mod utilities;
pub use self::utilities::*;

View File

@ -1,4 +1,46 @@
use crate::{input_to_integer_string, input_to_u8_vec, StateValuesError};
use leo_typed::{InputValue, State as TypedState};
use std::convert::TryFrom;
static LEAF_INDEX_PARAMETER_STRING: &str = "leaf_index";
static ROOT_PARAMETER_STRING: &str = "root";
pub struct StateValues {
pub leaf_index: u32,
pub root: Vec<u8>,
}
impl TryFrom<&TypedState> for StateValues {
type Error = StateValuesError;
fn try_from(state: &TypedState) -> Result<Self, Self::Error> {
// Lookup leaf index
let leaf_index_value = get_parameter_value(LEAF_INDEX_PARAMETER_STRING.to_owned(), state).unwrap();
let leaf_index = input_to_integer_string(leaf_index_value)
.unwrap()
.parse::<u32>()
.unwrap();
// Lookup root
let root_value = get_parameter_value(ROOT_PARAMETER_STRING.to_owned(), state).unwrap();
let root = input_to_u8_vec(root_value).unwrap();
Ok(Self { leaf_index, root })
}
}
fn get_parameter_value(name: String, state: &TypedState) -> Result<InputValue, StateValuesError> {
let parameters = state.values();
let matched_parameter = parameters
.iter()
.find(|(parameter, _value)| parameter.variable.name == name);
match matched_parameter {
Some((_parameter, value_option)) => match value_option {
Some(value) => Ok(value.clone()),
None => Err(StateValuesError::MissingParameter(name)),
},
None => Err(StateValuesError::MissingParameter(name)),
}
}

View File

@ -1,4 +1,4 @@
use crate::{verify_record_commitment::verify_record_commitment, LocalDataVerificationError};
use crate::{record_commitment::verify_record_commitment, LocalDataVerificationError, StateValues};
use leo_typed::Input as TypedInput;
use snarkos_algorithms::commitment_tree::CommitmentMerklePath;
@ -9,6 +9,7 @@ use snarkos_models::{
dpc::DPCComponents,
};
use snarkos_utilities::{bytes::ToBytes, to_bytes, FromBytes};
use std::convert::TryFrom;
pub fn verify_local_data_commitment(
typed_input: &TypedInput,
@ -19,13 +20,14 @@ pub fn verify_local_data_commitment(
// verify record commitment
let typed_record = typed_input.get_record();
let dpc_record_values = verify_record_commitment(typed_record, record_commitment_params).unwrap();
let record_commitment: Vec<u8> = dpc_record_values.commitment;
let record_serial_number: Vec<u8> = dpc_record_values.serial_number;
// parse typed state values
let leaf_index: u32 = 0;
let root: Vec<u8> = vec![];
let typed_state = typed_input.get_state();
let state_values = StateValues::try_from(typed_state).unwrap();
let leaf_index: u32 = state_values.leaf_index;
let root: Vec<u8> = state_values.root;
// parse typed state leaf values
let _path: Vec<Vec<u8>> = vec![];

View File

@ -1,4 +1,4 @@
use crate::RecordVerificationError;
use crate::{utilities::*, RecordVerificationError};
use leo_typed::{InputValue, Record as TypedRecord};
use snarkos_dpc::base_dpc::instantiated::Components;
@ -103,28 +103,3 @@ fn get_parameter_value(name: String, record: &TypedRecord) -> Result<InputValue,
None => Err(RecordVerificationError::MissingParameter(name)),
}
}
fn input_to_integer_string(input: InputValue) -> Result<String, RecordVerificationError> {
match input {
InputValue::Integer(_type, string) => Ok(string),
value => Err(RecordVerificationError::ExpectedInteger(value.to_string())),
}
}
fn input_to_u8_vec(input: InputValue) -> Result<Vec<u8>, RecordVerificationError> {
let input_array = match input {
InputValue::Array(values) => values,
value => return Err(RecordVerificationError::ExpectedBytes(value.to_string())),
};
let mut result_vec = vec![];
for input in input_array {
let integer_string = input_to_integer_string(input)?;
let byte = integer_string.parse::<u8>()?;
result_vec.push(byte);
}
Ok(result_vec)
}

View File

@ -0,0 +1,27 @@
use crate::InputValueError;
use leo_typed::InputValue;
pub fn input_to_integer_string(input: InputValue) -> Result<String, InputValueError> {
match input {
InputValue::Integer(_type, string) => Ok(string),
value => Err(InputValueError::ExpectedInteger(value.to_string())),
}
}
pub fn input_to_u8_vec(input: InputValue) -> Result<Vec<u8>, InputValueError> {
let input_array = match input {
InputValue::Array(values) => values,
value => return Err(InputValueError::ExpectedBytes(value.to_string())),
};
let mut result_vec = vec![];
for input in input_array {
let integer_string = input_to_integer_string(input)?;
let byte = integer_string.parse::<u8>()?;
result_vec.push(byte);
}
Ok(result_vec)
}

View File

@ -0,0 +1,2 @@
pub mod input_value;
pub use self::input_value::*;