From 8db6ca2d99789cc1a00ef1e065f33762adde6b8f Mon Sep 17 00:00:00 2001 From: Bobbin Threadbare Date: Wed, 6 Dec 2023 23:48:20 -0800 Subject: [PATCH] refactor: update NoteScript serialization --- Cargo.toml | 20 +++++++----------- objects/src/notes/mod.rs | 24 +++++++++++++++++++++ objects/src/notes/script.rs | 42 +++++++++++-------------------------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5a85fd986..f2612243b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,16 +16,10 @@ codegen-units = 1 lto = true [workspace.dependencies] -assembly = { package = "miden-assembly", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "hacka-codeblock-programast-serialize-deserialize", default-features = false } -miden-prover = { package = "miden-prover", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "hacka-codeblock-programast-serialize-deserialize", default-features = false } -miden-stdlib = { package = "miden-stdlib", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "hacka-codeblock-programast-serialize-deserialize", default-features = false } -miden-test-utils = { package = "miden-test-utils", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "hacka-codeblock-programast-serialize-deserialize", default-features = false } -miden-verifier = { package = "miden-verifier", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "hacka-codeblock-programast-serialize-deserialize", default-features = false } -vm-core = { package = "miden-core", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "hacka-codeblock-programast-serialize-deserialize", default-features = false } -vm-processor = { package = "miden-processor", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "hacka-codeblock-programast-serialize-deserialize", default-features = false } - -[patch.crates-io] -winter-utils = { package = "winter-utils", git = "https://github.com/hackaugusto/winterfell.git", branch = "hacka-default-deserializers" } - -[patch."https://github.com/0xPolygonMiden/crypto"] -miden-crypto = { package = "miden-crypto", git = "https://github.com/hackaugusto/crypto.git", branch = "hacka-serde-merklepath" } +assembly = { package = "miden-assembly", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "next", default-features = false } +miden-prover = { package = "miden-prover", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "next", default-features = false } +miden-stdlib = { package = "miden-stdlib", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "next", default-features = false } +miden-test-utils = { package = "miden-test-utils", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "next", default-features = false } +miden-verifier = { package = "miden-verifier", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "next", default-features = false } +vm-core = { package = "miden-core", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "next", default-features = false } +vm-processor = { package = "miden-processor", git = "https://github.com/0xPolygonMiden/miden-vm.git", branch = "next", default-features = false } diff --git a/objects/src/notes/mod.rs b/objects/src/notes/mod.rs index 0c65eb31e..31e927949 100644 --- a/objects/src/notes/mod.rs +++ b/objects/src/notes/mod.rs @@ -61,6 +61,7 @@ pub const NOTE_LEAF_DEPTH: u8 = NOTE_TREE_DEPTH + 1; #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Note { + #[cfg_attr(feature = "serde", serde(with = "serialization"))] script: NoteScript, inputs: NoteInputs, vault: NoteVault, @@ -279,3 +280,26 @@ impl Deserializable for RecordedNote { Ok(Self { note, proof }) } } + +#[cfg(feature = "serde")] +mod serialization { + use super::NoteScript; + use crate::utils::serde::{Deserializable, Serializable}; + + pub fn serialize(code: &NoteScript, serializer: S) -> Result + where + S: serde::Serializer, + { + let bytes = code.to_bytes(); + serializer.serialize_bytes(&bytes) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let bytes: Vec = as serde::Deserialize>::deserialize(deserializer)?; + + NoteScript::read_from_bytes(&bytes).map_err(serde::de::Error::custom) + } +} diff --git a/objects/src/notes/script.rs b/objects/src/notes/script.rs index d1d2daf13..76e56e75b 100644 --- a/objects/src/notes/script.rs +++ b/objects/src/notes/script.rs @@ -1,41 +1,23 @@ -use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable}; +use super::{Assembler, AssemblyContext, CodeBlock, Digest, NoteError, ProgramAst}; +use crate::utils::serde::{ByteReader, ByteWriter, Deserializable, Serializable}; +use assembly::ast::AstSerdeOptions; use vm_processor::DeserializationError; -use super::{Assembler, AssemblyContext, CodeBlock, Digest, NoteError, ProgramAst}; +// CONSTANTS +// ================================================================================================ + +/// Default serialization options for script code AST. +const CODE_SERDE_OPTIONS: AstSerdeOptions = AstSerdeOptions::new(true); + +// NOTE SCRIPT +// ================================================================================================ #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct NoteScript { hash: Digest, - #[cfg_attr(feature = "serde", serde(with = "serialization"))] code: ProgramAst, } -#[cfg(feature = "serde")] -mod serialization { - use assembly::ast::AstSerdeOptions; - - pub fn serialize(module: &super::ProgramAst, serializer: S) -> Result - where - S: serde::Serializer, - { - let bytes = module.to_bytes(AstSerdeOptions { - serialize_imports: true, - }); - - serializer.serialize_bytes(&bytes) - } - - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let bytes: Vec = as serde::Deserialize>::deserialize(deserializer)?; - - super::ProgramAst::from_bytes(&bytes).map_err(serde::de::Error::custom) - } -} - impl NoteScript { pub fn new(code: ProgramAst, assembler: &Assembler) -> Result<(Self, CodeBlock), NoteError> { let code_block = assembler @@ -65,7 +47,7 @@ impl NoteScript { impl Serializable for NoteScript { fn write_into(&self, target: &mut W) { self.hash.write_into(target); - self.code.write_into(target); + self.code.write_into(target, CODE_SERDE_OPTIONS); } }