Skip to content

Commit

Permalink
refactor: update NoteScript serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Dec 7, 2023
1 parent c38f083 commit 8db6ca2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
20 changes: 7 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
24 changes: 24 additions & 0 deletions objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<S>(code: &NoteScript, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let bytes = code.to_bytes();
serializer.serialize_bytes(&bytes)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<NoteScript, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes: Vec<u8> = <Vec<u8> as serde::Deserialize>::deserialize(deserializer)?;

NoteScript::read_from_bytes(&bytes).map_err(serde::de::Error::custom)
}
}
42 changes: 12 additions & 30 deletions objects/src/notes/script.rs
Original file line number Diff line number Diff line change
@@ -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<S>(module: &super::ProgramAst, serializer: S) -> Result<S::Ok, S::Error>
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<super::ProgramAst, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes: Vec<u8> = <Vec<u8> 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
Expand Down Expand Up @@ -65,7 +47,7 @@ impl NoteScript {
impl Serializable for NoteScript {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.hash.write_into(target);
self.code.write_into(target);
self.code.write_into(target, CODE_SERDE_OPTIONS);
}
}

Expand Down

0 comments on commit 8db6ca2

Please sign in to comment.