diff --git a/Cargo.lock b/Cargo.lock index 0192e3c510..3ffb496734 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,6 +92,30 @@ dependencies = [ "serde", ] +[[package]] +name = "bindgen" +version = "0.53.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c72a978d268b1d70b0e963217e60fdabd9523a941457a6c42a7315d15c7e89e5" +dependencies = [ + "bitflags", + "cexpr", + "cfg-if", + "clang-sys", + "clap", + "env_logger", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "proc-macro2 1.0.19", + "quote 1.0.7", + "regex", + "rustc-hash", + "shlex", + "which", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -211,6 +235,18 @@ name = "cc" version = "1.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9a06fb2e53271d7c279ec1efea6ab691c35a2ae67ec0d91d7acec0caf13b518" +dependencies = [ + "jobserver", +] + +[[package]] +name = "cexpr" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27" +dependencies = [ + "nom", +] [[package]] name = "cfg-if" @@ -239,6 +275,17 @@ dependencies = [ "envmnt", ] +[[package]] +name = "clang-sys" +version = "0.29.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe6837df1d5cba2397b835c8530f51723267e16abbf83892e9e5af4f0e5dd10a" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "clap" version = "2.33.3" @@ -254,6 +301,15 @@ dependencies = [ "vec_map", ] +[[package]] +name = "cloudabi" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4344512281c643ae7638bbabc3af17a11307803ec8f0fcad9fae512a8bf36467" +dependencies = [ + "bitflags", +] + [[package]] name = "colored" version = "2.0.0" @@ -412,6 +468,36 @@ dependencies = [ "memchr", ] +[[package]] +name = "curl" +version = "0.4.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9447ad28eee2a5cfb031c329d46bef77487244fff6a724b378885b8691a35f78" +dependencies = [ + "curl-sys", + "libc", + "openssl-probe", + "openssl-sys", + "schannel", + "socket2", + "winapi 0.3.9", +] + +[[package]] +name = "curl-sys" +version = "0.4.34+curl-7.71.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad4eff0be6985b7e709f64b5a541f700e9ad1407190a29f4884319eb663ed1d6" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", + "winapi 0.3.9", +] + [[package]] name = "derivative" version = "2.1.1" @@ -711,6 +797,12 @@ version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" + [[package]] name = "h2" version = "0.2.6" @@ -874,6 +966,12 @@ dependencies = [ "libc", ] +[[package]] +name = "instant" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485" + [[package]] name = "iovec" version = "0.1.4" @@ -913,6 +1011,15 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +[[package]] +name = "jobserver" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c71313ebb9439f74b00d9d2dcec36440beaf57a6aa0623068441dd7cd81a7f2" +dependencies = [ + "libc", +] + [[package]] name = "js-sys" version = "0.3.44" @@ -970,6 +1077,7 @@ dependencies = [ "leo-gadgets", "leo-input", "leo-package", + "leo-state", "log", "notify", "num-bigint", @@ -1014,6 +1122,7 @@ dependencies = [ "leo-ast", "leo-gadgets", "leo-input", + "leo-state", "leo-typed", "log", "num-bigint", @@ -1077,6 +1186,25 @@ dependencies = [ "zip", ] +[[package]] +name = "leo-state" +version = "0.1.0" +dependencies = [ + "leo-input", + "leo-typed", + "rand", + "rand_xorshift", + "snarkos-algorithms", + "snarkos-curves", + "snarkos-dpc", + "snarkos-errors", + "snarkos-models", + "snarkos-objects", + "snarkos-storage", + "snarkos-utilities", + "thiserror", +] + [[package]] name = "leo-typed" version = "0.1.0" @@ -1097,6 +1225,49 @@ version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2f02823cf78b754822df5f7f268fb59822e7296276d3e069d8e8cb26a14bd10" +[[package]] +name = "libloading" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753" +dependencies = [ + "cc", + "winapi 0.3.9", +] + +[[package]] +name = "librocksdb-sys" +version = "6.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "883213ae3d09bfc3d104aefe94b25ebb183b6f4d3a515b23b14817e1f4854005" +dependencies = [ + "bindgen", + "cc", + "glob", + "libc", +] + +[[package]] +name = "libz-sys" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ca8894883d250240341478bf987467332fbdd5da5c42426c69a8f93dbc302f2" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "lock_api" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28247cc5a5be2f05fbcd76dd0cf2c7d3b5400cb978a28042abcd4fa0b3f8261c" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.11" @@ -1242,6 +1413,16 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab250442c86f1850815b5d268639dff018c0627022bc1940eb2d642ca1ce12f0" +[[package]] +name = "nom" +version = "5.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" +dependencies = [ + "memchr", + "version_check", +] + [[package]] name = "notify" version = "4.0.15" @@ -1363,6 +1544,38 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "parking_lot" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4893845fa2ca272e647da5d0e46660a314ead9c2fdd9a883aabc32e481a8733" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b" +dependencies = [ + "cfg-if", + "cloudabi", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi 0.3.9", +] + +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + [[package]] name = "percent-encoding" version = "2.1.0" @@ -1682,12 +1895,28 @@ dependencies = [ "winreg", ] +[[package]] +name = "rocksdb" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12069b106981c6103d3eab7dd1c86751482d0779a520b7c14954c8b586c1e643" +dependencies = [ + "libc", + "librocksdb-sys", +] + [[package]] name = "rustc-demangle" version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustc_version" version = "0.2.3" @@ -1856,6 +2085,12 @@ dependencies = [ "opaque-debug 0.3.0", ] +[[package]] +name = "shlex" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" + [[package]] name = "single" version = "1.0.0" @@ -1880,7 +2115,7 @@ checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" [[package]] name = "snarkos-algorithms" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "blake2", "derivative", @@ -1900,7 +2135,7 @@ dependencies = [ [[package]] name = "snarkos-curves" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "derivative", "rand", @@ -1915,7 +2150,7 @@ dependencies = [ [[package]] name = "snarkos-derives" version = "0.1.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "proc-macro2 1.0.19", "quote 1.0.7", @@ -1925,7 +2160,7 @@ dependencies = [ [[package]] name = "snarkos-dpc" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "blake2", "derivative", @@ -1946,13 +2181,15 @@ dependencies = [ [[package]] name = "snarkos-errors" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "base58", "bech32", "bincode", + "curl", "hex", "jsonrpc-core", + "rocksdb", "thiserror", "toml", ] @@ -1960,7 +2197,7 @@ dependencies = [ [[package]] name = "snarkos-gadgets" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "derivative", "digest 0.8.1", @@ -1975,7 +2212,7 @@ dependencies = [ [[package]] name = "snarkos-models" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "bincode", "derivative", @@ -1991,7 +2228,7 @@ dependencies = [ [[package]] name = "snarkos-objects" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "base58", "bech32", @@ -2012,8 +2249,9 @@ dependencies = [ [[package]] name = "snarkos-parameters" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ + "curl", "hex", "snarkos-algorithms", "snarkos-errors", @@ -2024,12 +2262,31 @@ dependencies = [ [[package]] name = "snarkos-profiler" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" + +[[package]] +name = "snarkos-storage" +version = "0.8.0" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" +dependencies = [ + "bincode", + "hex", + "parking_lot", + "rand", + "rocksdb", + "serde", + "snarkos-algorithms", + "snarkos-errors", + "snarkos-models", + "snarkos-objects", + "snarkos-parameters", + "snarkos-utilities", +] [[package]] name = "snarkos-utilities" version = "0.8.0" -source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#4d2465933876f6cd2a55ed122292b33dbabe9228" +source = "git+ssh://git@github.com/AleoHQ/snarkOS.git#57fef6a27035419e0b22fee5b72ca6639e15e1ac" dependencies = [ "bincode", "rand", @@ -2462,6 +2719,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "which" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" +dependencies = [ + "libc", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index 6019755eb8..0b3546764f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,13 +13,14 @@ name = "leo" path = "leo/main.rs" [workspace] -members = [ "ast", "compiler", "gadgets", "input", "linter", "package", "typed" ] +members = [ "ast", "compiler", "gadgets", "input", "linter", "package", "typed", "state"] [dependencies] leo-compiler = { path = "compiler", version = "0.1.0" } leo-gadgets = { path = "gadgets", version = "0.1.0" } leo-input = { path = "input", version = "0.1.0" } leo-package = { path = "package", version = "0.1.0" } +leo-state = { path = "state", version = "0.1.0" } snarkos-algorithms = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-algorithms", default-features = false } snarkos-curves = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-curves", default-features = false } diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index 35bf31e318..c4d27487d9 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -9,6 +9,7 @@ leo-ast = { path = "../ast", version = "0.1.0" } leo-gadgets = { path = "../gadgets", version = "0.1.0" } leo-input = { path = "../input", version = "0.1.0" } leo-typed = { path = "../typed", version = "0.1.0" } +leo-state = { path = "../state", version = "0.1.0" } snarkos-curves = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-curves", default-features = false } snarkos-dpc = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-dpc", default-features = false } diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index bcde2061c4..949a46e5f9 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -10,8 +10,10 @@ use crate::{ }; use leo_ast::LeoAst; use leo_input::LeoInputParser; +use leo_state::verify_local_data_commitment; use leo_typed::{Input, LeoTypedAst, MainInput, Program}; +use snarkos_dpc::{base_dpc::instantiated::Components, SystemParameters}; use snarkos_errors::gadgets::SynthesisError; use snarkos_models::{ curves::{Field, PrimeField}, @@ -124,6 +126,16 @@ impl> Compiler { self.program_input.set_main_input(input); } + /// Verifies the input to the program + pub fn verify_local_data_commitment( + &self, + system_parameters: &SystemParameters, + ) -> Result { + let result = verify_local_data_commitment(system_parameters, &self.program_input)?; + + Ok(result) + } + pub fn checksum(&self) -> Result { // Read in the main file as string let unparsed_file = fs::read_to_string(&self.main_file_path) diff --git a/compiler/src/errors/compiler.rs b/compiler/src/errors/compiler.rs index ff76268520..42293a676c 100644 --- a/compiler/src/errors/compiler.rs +++ b/compiler/src/errors/compiler.rs @@ -1,6 +1,7 @@ use crate::errors::{FunctionError, ImportError, OutputBytesError, OutputFileError}; use leo_ast::ParserError; use leo_input::InputParserError; +use leo_state::LocalDataVerificationError; use bincode::Error as SerdeError; use std::path::PathBuf; @@ -19,6 +20,9 @@ pub enum CompilerError { #[error("Cannot read from the provided file path - {:?}", _0)] FileReadError(PathBuf), + #[error("{}", _0)] + LocalDataVerificationError(#[from] LocalDataVerificationError), + #[error("`main` function not found")] NoMain, diff --git a/state/Cargo.toml b/state/Cargo.toml new file mode 100644 index 0000000000..b7619ef19a --- /dev/null +++ b/state/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "leo-state" +version = "0.1.0" +authors = ["The Aleo Team "] +edition = "2018" + +[dependencies] +leo-input = { path = "../input", version = "0.1.0" } +leo-typed = { path = "../typed", version = "0.1.0" } + +snarkos-algorithms = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-algorithms", default-features = false } +snarkos-curves = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-curves", default-features = false } +snarkos-dpc = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-dpc", default-features = false } +snarkos-errors = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-errors", default-features = false } +snarkos-models = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-models", default-features = false } +snarkos-objects = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-objects" } +snarkos-utilities = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-utilities" } + + +rand = { version = "0.7" } +rand_xorshift = { version = "0.2" } +thiserror = { version = "1.0" } + + +[dev-dependencies] +snarkos-storage = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", package = "snarkos-storage" } + diff --git a/state/src/errors/dpc_record_values.rs b/state/src/errors/dpc_record_values.rs new file mode 100644 index 0000000000..f659b134b0 --- /dev/null +++ b/state/src/errors/dpc_record_values.rs @@ -0,0 +1,20 @@ +use crate::InputValueError; + +use snarkos_errors::objects::account::AccountError; + +use std::{num::ParseIntError, str::ParseBoolError}; + +#[derive(Debug, Error)] +pub enum DPCRecordValuesError { + #[error("{}", _0)] + AccountError(#[from] AccountError), + + #[error("{}", _0)] + InputValueError(#[from] InputValueError), + + #[error("{}", _0)] + ParseBoolError(#[from] ParseBoolError), + + #[error("{}", _0)] + ParseIntError(#[from] ParseIntError), +} diff --git a/state/src/errors/input_value.rs b/state/src/errors/input_value.rs new file mode 100644 index 0000000000..1504d0ebef --- /dev/null +++ b/state/src/errors/input_value.rs @@ -0,0 +1,16 @@ +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("input parameter `{}` not found in state file", _0)] + MissingParameter(String), + + #[error("{}", _0)] + ParseIntError(#[from] ParseIntError), +} diff --git a/state/src/errors/local_data_commitment.rs b/state/src/errors/local_data_commitment.rs new file mode 100644 index 0000000000..ecdc2b8c18 --- /dev/null +++ b/state/src/errors/local_data_commitment.rs @@ -0,0 +1,26 @@ +use crate::{RecordVerificationError, StateLeafValuesError, StateValuesError}; + +use snarkos_errors::algorithms::{CommitmentError, MerkleError}; + +use std::io::Error as IOError; + +#[derive(Debug, Error)] +pub enum LocalDataVerificationError { + #[error("{}", _0)] + CommitmentError(#[from] CommitmentError), + + #[error("{}", _0)] + MerkleError(#[from] MerkleError), + + #[error("{}", _0)] + IOError(#[from] IOError), + + #[error("{}", _0)] + RecordVerificationError(#[from] RecordVerificationError), + + #[error("{}", _0)] + StateLeafValuesError(#[from] StateLeafValuesError), + + #[error("{}", _0)] + StateValuesError(#[from] StateValuesError), +} diff --git a/state/src/errors/mod.rs b/state/src/errors/mod.rs new file mode 100644 index 0000000000..fd5b2ade5a --- /dev/null +++ b/state/src/errors/mod.rs @@ -0,0 +1,17 @@ +pub mod dpc_record_values; +pub use self::dpc_record_values::*; + +pub mod input_value; +pub use self::input_value::*; + +pub mod state_leaf_values; +pub use self::state_leaf_values::*; + +pub mod state_values; +pub use self::state_values::*; + +pub mod local_data_commitment; +pub use self::local_data_commitment::*; + +pub mod record_commitment; +pub use self::record_commitment::*; diff --git a/state/src/errors/record_commitment.rs b/state/src/errors/record_commitment.rs new file mode 100644 index 0000000000..a00bb8eead --- /dev/null +++ b/state/src/errors/record_commitment.rs @@ -0,0 +1,20 @@ +use crate::DPCRecordValuesError; + +use snarkos_errors::algorithms::CommitmentError; + +use std::io::Error as IOError; + +#[derive(Debug, Error)] +pub enum RecordVerificationError { + #[error("record commitment does not match record data")] + CommitmentsDoNotMatch, + + #[error("{}", _0)] + CommitmentError(#[from] CommitmentError), + + #[error("{}", _0)] + DPCRecordValuesError(#[from] DPCRecordValuesError), + + #[error("{}", _0)] + IOError(#[from] IOError), +} diff --git a/state/src/errors/state_leaf_values.rs b/state/src/errors/state_leaf_values.rs new file mode 100644 index 0000000000..b14620eb89 --- /dev/null +++ b/state/src/errors/state_leaf_values.rs @@ -0,0 +1,15 @@ +use crate::InputValueError; + +use std::{num::ParseIntError, str::ParseBoolError}; + +#[derive(Debug, Error)] +pub enum StateLeafValuesError { + #[error("{}", _0)] + InputValueError(#[from] InputValueError), + + #[error("{}", _0)] + ParseBoolError(#[from] ParseBoolError), + + #[error("{}", _0)] + ParseIntError(#[from] ParseIntError), +} diff --git a/state/src/errors/state_values.rs b/state/src/errors/state_values.rs new file mode 100644 index 0000000000..72b47c51bc --- /dev/null +++ b/state/src/errors/state_values.rs @@ -0,0 +1,15 @@ +use crate::InputValueError; + +use std::{num::ParseIntError, str::ParseBoolError}; + +#[derive(Debug, Error)] +pub enum StateValuesError { + #[error("{}", _0)] + InputValueError(#[from] InputValueError), + + #[error("{}", _0)] + ParseBoolError(#[from] ParseBoolError), + + #[error("{}", _0)] + ParseIntError(#[from] ParseIntError), +} diff --git a/state/src/lib.rs b/state/src/lib.rs new file mode 100644 index 0000000000..40cfd206e8 --- /dev/null +++ b/state/src/lib.rs @@ -0,0 +1,14 @@ +#[macro_use] +extern crate thiserror; + +pub mod errors; +pub use self::errors::*; + +pub mod local_data_commitment; +pub use self::local_data_commitment::*; + +pub mod record_commitment; +pub use self::record_commitment::*; + +pub mod utilities; +pub use self::utilities::*; diff --git a/state/src/local_data_commitment/local_data_commitment.rs b/state/src/local_data_commitment/local_data_commitment.rs new file mode 100644 index 0000000000..53a84b8149 --- /dev/null +++ b/state/src/local_data_commitment/local_data_commitment.rs @@ -0,0 +1,69 @@ +use crate::{verify_record_commitment, LocalDataVerificationError, StateLeafValues, StateValues}; +use leo_typed::Input as TypedInput; + +use snarkos_algorithms::commitment_tree::CommitmentMerklePath; +use snarkos_dpc::base_dpc::{ + instantiated::{Components, LocalDataCRH, LocalDataCommitment}, + parameters::SystemParameters, +}; +use snarkos_models::{ + algorithms::{CommitmentScheme, CRH}, + dpc::DPCComponents, +}; +use snarkos_utilities::{bytes::ToBytes, to_bytes, FromBytes}; + +use std::convert::TryFrom; + +pub fn verify_local_data_commitment( + system_parameters: &SystemParameters, + typed_input: &TypedInput, +) -> Result { + // verify record commitment + let typed_record = typed_input.get_record(); + let dpc_record_values = verify_record_commitment(system_parameters, typed_record)?; + let record_commitment: Vec = dpc_record_values.commitment; + let record_serial_number: Vec = dpc_record_values.serial_number; + + // parse typed state values + let typed_state = typed_input.get_state(); + let state_values = StateValues::try_from(typed_state)?; + let leaf_index: u32 = state_values.leaf_index; + let root: Vec = state_values.root; + + // parse typed state leaf values + let typed_state_leaf = typed_input.get_state_leaf(); + let state_leaf_values = StateLeafValues::try_from(typed_state_leaf)?; + let path: Vec = state_leaf_values.path; + let memo: Vec = state_leaf_values.memo; + let network_id: u8 = state_leaf_values.network_id; + let leaf_randomness: Vec = state_leaf_values.leaf_randomness; + + // Select local data commitment input bytes + let is_death = leaf_index < (Components::NUM_INPUT_RECORDS as u32); + let input_bytes = if is_death { + to_bytes![record_serial_number, record_commitment, memo, network_id]? + } else { + to_bytes![record_commitment, memo, network_id]? + }; + + // Construct local data commitment leaf + let local_data_leaf_randomness = ::Randomness::read(&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 local_data_merkle_path = CommitmentMerklePath::::read(&path[..])?; + + // Check record commitment merkle path is valid for the given local data commitment root + let local_data_commitment_root = ::Output::read(&root[..])?; + let result = local_data_merkle_path.verify( + &system_parameters.local_data_crh, + &local_data_commitment_root, + &local_data_commitment_leaf, + )?; + + Ok(result) +} diff --git a/state/src/local_data_commitment/mod.rs b/state/src/local_data_commitment/mod.rs new file mode 100644 index 0000000000..77a5949d51 --- /dev/null +++ b/state/src/local_data_commitment/mod.rs @@ -0,0 +1,8 @@ +pub mod state_values; +pub use self::state_values::*; + +pub mod state_leaf_values; +pub use self::state_leaf_values::*; + +pub mod local_data_commitment; +pub use self::local_data_commitment::*; diff --git a/state/src/local_data_commitment/state_leaf_values.rs b/state/src/local_data_commitment/state_leaf_values.rs new file mode 100644 index 0000000000..b52badd1da --- /dev/null +++ b/state/src/local_data_commitment/state_leaf_values.rs @@ -0,0 +1,47 @@ +use crate::{find_input, input_to_integer_string, input_to_u8_vec, StateLeafValuesError}; +use leo_typed::StateLeaf as TypedStateLeaf; + +use std::convert::TryFrom; + +static PATH_PARAMETER_STRING: &str = "path"; +static MEMO_PARAMETER_STRING: &str = "memo"; +static NETWORK_ID_PARAMETER_STRING: &str = "network_id"; +static LEAF_RANDOMNESS_PARAMETER_STRING: &str = "leaf_randomness"; + +pub struct StateLeafValues { + pub path: Vec, + pub memo: Vec, + pub network_id: u8, + pub leaf_randomness: Vec, +} + +impl TryFrom<&TypedStateLeaf> for StateLeafValues { + type Error = StateLeafValuesError; + + fn try_from(state_leaf: &TypedStateLeaf) -> Result { + let parameters = state_leaf.values(); + + // Lookup path + let path_value = find_input(PATH_PARAMETER_STRING.to_owned(), ¶meters)?; + let path = input_to_u8_vec(path_value)?; + + // Lookup memo + let memo_value = find_input(MEMO_PARAMETER_STRING.to_owned(), ¶meters)?; + let memo = input_to_u8_vec(memo_value)?; + + // Lookup network id + let network_id_value = find_input(NETWORK_ID_PARAMETER_STRING.to_owned(), ¶meters)?; + let network_id = input_to_integer_string(network_id_value)?.parse::()?; + + // Lookup leaf randomness + let leaf_randomness_value = find_input(LEAF_RANDOMNESS_PARAMETER_STRING.to_owned(), ¶meters)?; + let leaf_randomness = input_to_u8_vec(leaf_randomness_value)?; + + Ok(Self { + path, + memo, + network_id, + leaf_randomness, + }) + } +} diff --git a/state/src/local_data_commitment/state_values.rs b/state/src/local_data_commitment/state_values.rs new file mode 100644 index 0000000000..5a71e19395 --- /dev/null +++ b/state/src/local_data_commitment/state_values.rs @@ -0,0 +1,30 @@ +use crate::{find_input, input_to_integer_string, input_to_u8_vec, StateValuesError}; +use leo_typed::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, +} + +impl TryFrom<&TypedState> for StateValues { + type Error = StateValuesError; + + fn try_from(state: &TypedState) -> Result { + let parameters = state.values(); + + // Lookup leaf index + let leaf_index_value = find_input(LEAF_INDEX_PARAMETER_STRING.to_owned(), ¶meters)?; + let leaf_index = input_to_integer_string(leaf_index_value)?.parse::()?; + + // Lookup root + let root_value = find_input(ROOT_PARAMETER_STRING.to_owned(), ¶meters)?; + let root = input_to_u8_vec(root_value)?; + + Ok(Self { leaf_index, root }) + } +} diff --git a/state/src/record_commitment/dpc_record_values.rs b/state/src/record_commitment/dpc_record_values.rs new file mode 100644 index 0000000000..986eaee90d --- /dev/null +++ b/state/src/record_commitment/dpc_record_values.rs @@ -0,0 +1,92 @@ +use crate::{utilities::*, DPCRecordValuesError}; +use leo_typed::Record as TypedRecord; + +use snarkos_dpc::base_dpc::instantiated::Components; +use snarkos_objects::AccountAddress; + +use std::{convert::TryFrom, str::FromStr}; + +static SERIAL_NUMBER_PARAMETER_STRING: &str = "serial_number"; +static OWNER_PARAMETER_STRING: &str = "owner"; +static IS_DUMMY_PARAMETER_STRING: &str = "is_dummy"; +static VALUE_PARAMETER_STRING: &str = "value"; +static PAYLOAD_PARAMETER_STRING: &str = "payload"; +static BIRTH_PROGRAM_ID_PARAMETER_STRING: &str = "birth_program_id"; +static DEATH_PROGRAM_ID_PARAMETER_STRING: &str = "death_program_id"; +static SERIAL_NUMBER_NONCE_PARAMETER_STRING: &str = "serial_number_nonce"; +static COMMITMENT_PARAMETER_STRING: &str = "commitment"; +static COMMITMENT_RANDOMNESS_PARAMETER_STRING: &str = "commitment_randomness"; + +pub struct DPCRecordValues { + pub serial_number: Vec, + pub owner: AccountAddress, + pub is_dummy: bool, + pub value: u64, + pub payload: Vec, + pub birth_program_id: Vec, + pub death_program_id: Vec, + pub serial_number_nonce: Vec, + pub commitment: Vec, + pub commitment_randomness: Vec, +} + +impl TryFrom<&TypedRecord> for DPCRecordValues { + type Error = DPCRecordValuesError; + + fn try_from(record: &TypedRecord) -> Result { + let parameters = record.values(); + + // Lookup serial number + let serial_number_value = find_input(SERIAL_NUMBER_PARAMETER_STRING.to_owned(), ¶meters)?; + let serial_number = input_to_u8_vec(serial_number_value)?; + + // Lookup record owner + let owner_value = find_input(OWNER_PARAMETER_STRING.to_owned(), ¶meters)?; + let owner = AccountAddress::::from_str(&format!("{}", owner_value))?; + + // Lookup record is_dummy + let is_dummy_value = find_input(IS_DUMMY_PARAMETER_STRING.to_owned(), ¶meters)?; + let is_dummy = is_dummy_value.to_string().parse::()?; + + // Lookup record value + let value_value = find_input(VALUE_PARAMETER_STRING.to_owned(), ¶meters)?; + let value = input_to_integer_string(value_value)?.parse::()?; + + // Lookup record payload + let payload_value = find_input(PAYLOAD_PARAMETER_STRING.to_owned(), ¶meters)?; + let payload = input_to_u8_vec(payload_value)?; + + // Lookup record birth program id + let birth_program_id_value = find_input(BIRTH_PROGRAM_ID_PARAMETER_STRING.to_owned(), ¶meters)?; + let birth_program_id = input_to_u8_vec(birth_program_id_value)?; + + // Lookup record death program id + let death_program_id_value = find_input(DEATH_PROGRAM_ID_PARAMETER_STRING.to_owned(), ¶meters)?; + let death_program_id = input_to_u8_vec(death_program_id_value)?; + + // Lookup record serial number nonce + let serial_number_nonce_value = find_input(SERIAL_NUMBER_NONCE_PARAMETER_STRING.to_owned(), ¶meters)?; + let serial_number_nonce = input_to_u8_vec(serial_number_nonce_value)?; + + // Lookup record commitment + let commitment_value = find_input(COMMITMENT_PARAMETER_STRING.to_owned(), ¶meters)?; + let commitment = input_to_u8_vec(commitment_value)?; + + // Lookup record commitment randomness + let commitment_randomness_value = find_input(COMMITMENT_RANDOMNESS_PARAMETER_STRING.to_owned(), ¶meters)?; + let commitment_randomness = input_to_u8_vec(commitment_randomness_value)?; + + Ok(Self { + serial_number, + owner, + is_dummy, + value, + payload, + birth_program_id, + death_program_id, + serial_number_nonce, + commitment, + commitment_randomness, + }) + } +} diff --git a/state/src/record_commitment/mod.rs b/state/src/record_commitment/mod.rs new file mode 100644 index 0000000000..9d2f688f6e --- /dev/null +++ b/state/src/record_commitment/mod.rs @@ -0,0 +1,5 @@ +pub mod dpc_record_values; +pub use self::dpc_record_values::*; + +pub mod record_commitment; +pub use self::record_commitment::*; diff --git a/state/src/record_commitment/record_commitment.rs b/state/src/record_commitment/record_commitment.rs new file mode 100644 index 0000000000..55493e874f --- /dev/null +++ b/state/src/record_commitment/record_commitment.rs @@ -0,0 +1,46 @@ +use crate::{DPCRecordValues, RecordVerificationError}; +use leo_typed::Record as TypedRecord; + +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, + typed_record: &TypedRecord, +) -> Result { + // generate a dpc record from the typed record + let record = DPCRecordValues::try_from(typed_record)?; + + // verify record commitment + let record_commitment_input = to_bytes![ + record.owner, + record.is_dummy, + record.value, + record.payload, + record.birth_program_id, + record.death_program_id, + record.serial_number_nonce + ]?; + + let commitment = ::Output::read(&record.commitment[..])?; + let commitment_randomness = + ::Randomness::read(&record.commitment_randomness[..])?; + + let record_commitment = RecordCommitment::commit( + &system_parameters.record_commitment, + &record_commitment_input, + &commitment_randomness, + )?; + + if record_commitment == commitment { + Ok(record) + } else { + Err(RecordVerificationError::CommitmentsDoNotMatch) + } +} diff --git a/state/src/utilities/input_value.rs b/state/src/utilities/input_value.rs new file mode 100644 index 0000000000..e453093d8f --- /dev/null +++ b/state/src/utilities/input_value.rs @@ -0,0 +1,63 @@ +use crate::InputValueError; +use leo_typed::{InputValue, Parameter}; + +use std::collections::HashMap; + +pub fn find_input( + name: String, + parameters: &HashMap>, +) -> Result { + 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(InputValueError::MissingParameter(name)), + }, + None => Err(InputValueError::MissingParameter(name)), + } +} + +pub fn input_to_integer_string(input: InputValue) -> Result { + match input { + InputValue::Integer(_type, string) => Ok(string), + value => Err(InputValueError::ExpectedInteger(value.to_string())), + } +} + +pub fn input_to_u8_vec(input: InputValue) -> Result, 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::()?; + + result_vec.push(byte); + } + + Ok(result_vec) +} + +pub fn input_to_nested_u8_vec(input: InputValue) -> Result>, InputValueError> { + let inner_arrays = match input { + InputValue::Array(arrays) => arrays, + value => return Err(InputValueError::ExpectedBytes(value.to_string())), + }; + + let mut result_vec = vec![]; + + for input_array in inner_arrays { + let array = input_to_u8_vec(input_array)?; + + result_vec.push(array); + } + + Ok(result_vec) +} diff --git a/state/src/utilities/mod.rs b/state/src/utilities/mod.rs new file mode 100644 index 0000000000..5166631852 --- /dev/null +++ b/state/src/utilities/mod.rs @@ -0,0 +1,2 @@ +pub mod input_value; +pub use self::input_value::*; diff --git a/state/tests/inputs/test_record.state b/state/tests/inputs/test_record.state new file mode 100644 index 0000000000..dcc712d3de --- /dev/null +++ b/state/tests/inputs/test_record.state @@ -0,0 +1,23 @@ +[[public]] +[state] +leaf_index: u32 = 0; +root: u8[32] = [0u8; 32]; + +[[private]] +[record] +serial_number: u8[32] = [0u8; 32]; +commitment: u8[32] = [24, 156, 6, 189, 180, 191, 65, 243, 196, 227, 127, 239, 207, 46, 119, 151, 6, 98, 159, 197, 6, 239, 1, 149, 94, 119, 37, 190, 168, 146, 198, 6]; +owner: address = aleo1daxej63vwrmn2zhl4dymygagh89k5d2vaw6rjauueme7le6k2q8sjn0ng9; +is_dummy: bool = false; +value: u64 = 13895627391323573723; +payload: u8[32] = [235, 120, 28, 41, 42, 46, 237, 32, 50, 70, 185, 70, 180, 174, 198, 169, 149, 118, 227, 124, 192, 36, 43, 2, 239, 2, 207, 166, 142, 240, 246, 39]; +birth_program_id: u8[48] = [89, 70, 116, 103, 190, 86, 194, 133, 37, 77, 113, 166, 160, 156, 98, 252, 16, 219, 94, 12, 214, 12, 151, 139, 239, 1, 79, 79, 86, 235, 209, 168, 240, 156, 224, 86, 35, 63, 185, 196, 0, 10, 148, 56, 92, 199, 45, 0]; +death_program_id: u8[48] = [89, 70, 116, 103, 190, 86, 194, 133, 37, 77, 113, 166, 160, 156, 98, 252, 16, 219, 94, 12, 214, 12, 151, 139, 239, 1, 79, 79, 86, 235, 209, 168, 240, 156, 224, 86, 35, 63, 185, 196, 0, 10, 148, 56, 92, 199, 45, 0]; +serial_number_nonce: u8[32] = [13, 163, 44, 146, 30, 254, 39, 255, 41, 20, 154, 181, 69, 34, 205, 82, 37, 173, 21, 57, 100, 180, 40, 7, 57, 209, 107, 89, 142, 91, 122, 16]; +commitment_randomness: u8[32] = [55, 189, 0, 182, 148, 137, 120, 223, 121, 7, 140, 240, 100, 202, 196, 41, 236, 128, 38, 189, 60, 59, 23, 210, 25, 102, 65, 117, 38, 140, 136, 1]; + +[state_leaf] +path: u8[128] = [0u8; 128]; +memo: u8[32] = [0u8; 32]; +network_id: u8 = 0; +leaf_randomness: u8[32] = [0u8; 32]; \ No newline at end of file diff --git a/state/tests/inputs/test_state.state b/state/tests/inputs/test_state.state new file mode 100644 index 0000000000..4336865638 --- /dev/null +++ b/state/tests/inputs/test_state.state @@ -0,0 +1,23 @@ +[[public]] +[state] +leaf_index: u32 = 0; +root: u8[32] = [70, 27, 231, 101, 102, 20, 37, 118, 77, 38, 56, 106, 49, 17, 135, 81, 134, 61, 255, 147, 230, 94, 218, 157, 98, 31, 132, 10, 116, 201, 78, 15]; + +[[private]] +[record] +serial_number: u8[64] = [98, 228, 41, 139, 42, 245, 63, 73, 45, 255, 134, 169, 18, 86, 90, 209, 31, 207, 161, 83, 183, 126, 53, 86, 142, 64, 59, 29, 185, 204, 179, 8, 43, 199, 156, 127, 124, 7, 91, 56, 27, 101, 57, 52, 166, 145, 153, 137, 17, 1, 140, 42, 205, 91, 32, 99, 22, 119, 141, 253, 235, 213, 112, 14]; +commitment: u8[32] = [233, 180, 207, 91, 31, 4, 177, 7, 21, 177, 170, 63, 134, 227, 249, 217, 193, 113, 220, 188, 97, 228, 70, 43, 160, 112, 228, 151, 110, 58, 85, 7]; +owner: address = aleo1daxej63vwrmn2zhl4dymygagh89k5d2vaw6rjauueme7le6k2q8sjn0ng9; +is_dummy: bool = false; +value: u64 = 18186969098991041491; +payload: u8[32] = [192, 118, 4, 191, 56, 79, 165, 142, 20, 92, 140, 207, 81, 125, 226, 247, 184, 40, 101, 235, 205, 174, 175, 180, 18, 104, 251, 132, 117, 163, 219, 125]; +birth_program_id: u8[48] = [89, 70, 116, 103, 190, 86, 194, 133, 37, 77, 113, 166, 160, 156, 98, 252, 16, 219, 94, 12, 214, 12, 151, 139, 239, 1, 79, 79, 86, 235, 209, 168, 240, 156, 224, 86, 35, 63, 185, 196, 0, 10, 148, 56, 92, 199, 45, 0]; +death_program_id: u8[48] = [89, 70, 116, 103, 190, 86, 194, 133, 37, 77, 113, 166, 160, 156, 98, 252, 16, 219, 94, 12, 214, 12, 151, 139, 239, 1, 79, 79, 86, 235, 209, 168, 240, 156, 224, 86, 35, 63, 185, 196, 0, 10, 148, 56, 92, 199, 45, 0]; +serial_number_nonce: u8[32] = [74, 128, 103, 188, 105, 165, 185, 183, 83, 178, 164, 202, 65, 224, 154, 216, 132, 146, 103, 158, 153, 229, 73, 162, 0, 182, 176, 162, 17, 201, 27, 6]; +commitment_randomness: u8[32] = [129, 174, 175, 20, 1, 168, 248, 69, 51, 186, 30, 34, 82, 6, 148, 174, 128, 163, 156, 197, 94, 129, 117, 226, 240, 95, 203, 196, 65, 222, 96, 4]; + +[state_leaf] +path: u8[128] = [144, 36, 140, 16, 110, 109, 215, 172, 251, 234, 246, 145, 192, 60, 79, 255, 58, 199, 52, 107, 224, 235, 152, 27, 232, 42, 96, 225, 170, 62, 118, 12, 8, 205, 94, 96, 200, 133, 229, 122, 179, 198, 124, 104, 197, 86, 67, 1, 52, 61, 168, 92, 201, 240, 61, 116, 221, 76, 172, 83, 174, 194, 118, 5, 221, 106, 153, 186, 50, 200, 155, 245, 255, 253, 169, 40, 236, 88, 58, 147, 46, 160, 55, 132, 157, 0, 134, 15, 40, 223, 53, 175, 220, 13, 222, 15, 143, 179, 79, 184, 75, 238, 87, 199, 102, 168, 167, 60, 232, 62, 64, 107, 12, 182, 200, 155, 107, 138, 224, 193, 233, 221, 54, 96, 206, 191, 83, 9]; +memo: u8[32] = [0u8; 32]; +network_id: u8 = 0; +leaf_randomness: u8[32] = [102, 202, 135, 202, 235, 133, 143, 160, 137, 212, 216, 158, 17, 44, 104, 126, 157, 109, 93, 213, 174, 57, 194, 113, 75, 184, 26, 204, 143, 131, 95, 1]; \ No newline at end of file diff --git a/state/tests/mod.rs b/state/tests/mod.rs new file mode 100644 index 0000000000..3f3fd2339d --- /dev/null +++ b/state/tests/mod.rs @@ -0,0 +1,3 @@ +mod test_verify_local_data_commitment; + +mod test_verify_record_commitment; diff --git a/state/tests/test_verify_local_data_commitment.rs b/state/tests/test_verify_local_data_commitment.rs new file mode 100644 index 0000000000..5a42e77865 --- /dev/null +++ b/state/tests/test_verify_local_data_commitment.rs @@ -0,0 +1,200 @@ +use leo_input::LeoInputParser; +use leo_state::verify_local_data_commitment; +use leo_typed::Input; + +use snarkos_dpc::base_dpc::{instantiated::*, record_payload::RecordPayload, DPC}; +use snarkos_models::{ + algorithms::{CommitmentScheme, CRH}, + dpc::Record, + objects::AccountScheme, +}; +use snarkos_objects::Account; +use snarkos_utilities::{bytes::ToBytes, to_bytes}; + +use rand::{Rng, SeedableRng}; +use rand_xorshift::XorShiftRng; +use snarkos_models::dpc::DPCScheme; +use snarkos_storage::Ledger; + +#[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(&system_parameters, &program_input).unwrap(); + + assert!(result); +} + +#[test] +#[ignore] +fn test_generate_values_from_dpc() { + type L = Ledger; + + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + // Specify network_id + let network_id: u8 = 0; + + // Generate parameters for the ledger, commitment schemes, CRH, and the + // "always-accept" program. + let system_parameters = InstantiatedDPC::generate_system_parameters(&mut rng).unwrap(); + let noop_program_snark_pp = + InstantiatedDPC::generate_noop_program_snark_parameters(&system_parameters, &mut rng).unwrap(); + + let noop_program_id = to_bytes![ + ProgramVerificationKeyHash::hash( + &system_parameters.program_verification_key_hash, + &to_bytes![noop_program_snark_pp.verification_key].unwrap() + ) + .unwrap() + ] + .unwrap(); + + let signature_parameters = &system_parameters.account_signature; + let commitment_parameters = &system_parameters.account_commitment; + let encryption_parameters = &system_parameters.account_encryption; + + // Generate metadata and an account for a dummy initial record. + let dummy_account = Account::new( + signature_parameters, + commitment_parameters, + encryption_parameters, + &mut rng, + ) + .unwrap(); + + let sn_nonce = SerialNumberNonce::hash(&system_parameters.serial_number_nonce, &[0u8; 1]).unwrap(); + let value = rng.gen(); + let payload: [u8; 32] = rng.gen(); + + let old_record = DPC::generate_record( + &system_parameters, + &sn_nonce, + &dummy_account.address, + false, + value, + &RecordPayload::from_bytes(&payload), + &noop_program_id, + &noop_program_id, + &mut rng, + ) + .unwrap(); + + // Set the input records for our transaction to be the initial dummy records. + let old_records = vec![old_record.clone(); NUM_INPUT_RECORDS]; + let old_account_private_keys = vec![dummy_account.private_key.clone(); NUM_INPUT_RECORDS]; + + // Construct new records. + + // Create an account for an actual new record. + + let new_account = Account::new( + signature_parameters, + commitment_parameters, + encryption_parameters, + &mut rng, + ) + .unwrap(); + + // Set the new record's program to be the "always-accept" program. + + let new_record_owners = vec![new_account.address.clone(); NUM_OUTPUT_RECORDS]; + let new_is_dummy_flags = vec![false; NUM_OUTPUT_RECORDS]; + let new_values = vec![10; NUM_OUTPUT_RECORDS]; + let new_payloads = vec![RecordPayload::default(); NUM_OUTPUT_RECORDS]; + let new_birth_program_ids = vec![noop_program_id.clone(); NUM_OUTPUT_RECORDS]; + let new_death_program_ids = vec![noop_program_id.clone(); NUM_OUTPUT_RECORDS]; + let memo = [0u8; 32]; + + let context = >::execute_offline( + &system_parameters, + &old_records, + &old_account_private_keys, + &new_record_owners, + &new_is_dummy_flags, + &new_values, + &new_payloads, + &new_birth_program_ids, + &new_death_program_ids, + &memo, + network_id, + &mut rng, + ) + .unwrap(); + + let local_data = context.into_local_data(); + let leaf_index = 0; + let record = &local_data.old_records[leaf_index]; + + let root = local_data.local_data_merkle_tree.root(); + + let serial_number = local_data.old_serial_numbers[0].clone(); + let serial_number_bytes = to_bytes![serial_number].unwrap(); + + let memorandum = local_data.memorandum; + let network_id = local_data.network_id; + let input_bytes = to_bytes![serial_number, record.commitment(), memorandum, network_id].unwrap(); + let leaf_randomness = local_data.local_data_commitment_randomizers[0].clone(); + + let old_record_leaf = ::commit( + &system_parameters.local_data_commitment, + &input_bytes, + &leaf_randomness, + ) + .unwrap(); + + // generate the path + + let path = local_data + .local_data_merkle_tree + .generate_proof(&old_record_leaf) + .unwrap(); + + println!("////////////////////////////////////////////////////"); + println!(); + println!("[state]"); + println!("leaf index {}", leaf_index); + println!("root {:?}", to_bytes![root].unwrap()); + println!(); + println!("[record]"); + println!( + "serial number {:?} len {}", + serial_number_bytes, + serial_number_bytes.len() + ); + println!("commitment {:?}", to_bytes![record.commitment()].unwrap()); + println!("owner {}", record.owner()); + println!("is_dummy {:?}", record.is_dummy()); + println!("value {:?}", record.value()); + println!("payload {:?}", record.payload()); + println!("birth_program_id {:?}", record.birth_program_id()); + println!("death_program_id {:?}", record.death_program_id()); + println!( + "serial number nonce {:?}", + to_bytes![record.serial_number_nonce()].unwrap() + ); + println!( + "commitment randomness {:?}", + to_bytes![record.commitment_randomness()].unwrap() + ); + println!(); + println!("[state_leaf]"); + println!("path {:?}", to_bytes![path].unwrap()); + println!("memo {:?}", memorandum); + println!("network id {:?}", network_id); + println!("leaf randomness {:?}", to_bytes![leaf_randomness].unwrap()); + println!(); + println!("////////////////////////////////////////////////////"); +} diff --git a/state/tests/test_verify_record_commitment.rs b/state/tests/test_verify_record_commitment.rs new file mode 100644 index 0000000000..e7bde411eb --- /dev/null +++ b/state/tests/test_verify_record_commitment.rs @@ -0,0 +1,29 @@ +use leo_input::LeoInputParser; +use leo_state::verify_record_commitment; +use leo_typed::Input; + +use snarkos_dpc::base_dpc::instantiated::*; + +use rand::SeedableRng; +use rand_xorshift::XorShiftRng; + +#[test] +fn test_verify_record_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_record.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(); + + let typed_record = program_input.get_record(); + + // check record state is correct by verifying commitment + let _values = verify_record_commitment(&system_parameters, typed_record).unwrap(); +}