Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add miden-package crate with Package type to represent a compiled Miden program/library. #1544

Open
wants to merge 29 commits into
base: next
Choose a base branch
from

Conversation

greenhat
Copy link
Contributor

@greenhat greenhat commented Oct 23, 2024

This PR adds miden-package crate with Package type ported from the compiler's ad-hoc implementation to represent a compiled Miden program/library.

Please consider reviewing this PR commit-by-commit. I crafted the commits to be focused on a single change and show the evolution of the implementation.

The reasons for the new crate are explained in 0xPolygonMiden/compiler#376

The compiler's PR that uses miden-package from this PR is 0xPolygonMiden/compiler#349

TODO:

  • branch out a new branch for the compiler to ship on the pre-EAM(element addressable memory) VM
  • rebase;
  • address review notes;

@greenhat greenhat changed the title Ass miden-package crate with Package type to represent a compiled Miden program/library. Add miden-package crate with Package type to represent a compiled Miden program/library. Oct 23, 2024
@greenhat greenhat force-pushed the greenhat/miden-package branch 4 times, most recently from 76a4b7e to 9ff342e Compare October 30, 2024 09:44
@greenhat greenhat force-pushed the greenhat/miden-package branch from 9ff342e to 7c9b95b Compare October 30, 2024 10:14
@greenhat greenhat changed the base branch from next to greenhat/library-deser-uncheck-exports October 30, 2024 10:15
@greenhat greenhat force-pushed the greenhat/library-deser-uncheck-exports branch from 1ca8581 to 3581727 Compare November 4, 2024 17:37
Base automatically changed from greenhat/library-deser-uncheck-exports to next November 4, 2024 21:37
@greenhat greenhat force-pushed the greenhat/miden-package branch 7 times, most recently from 41b5f5d to c5ff6f0 Compare November 12, 2024 14:07
@greenhat greenhat force-pushed the greenhat/miden-package branch 6 times, most recently from 211f34c to 84ea102 Compare November 19, 2024 08:49
@greenhat greenhat changed the base branch from next to greenhat/i1547-mastforest-add-advicemap November 19, 2024 08:49
@greenhat greenhat changed the title Add miden-package crate with Package type to represent a compiled Miden program/library. [2/2] Add miden-package crate with Package type to represent a compiled Miden program/library. Nov 19, 2024
@greenhat greenhat force-pushed the greenhat/i1547-mastforest-add-advicemap branch from eda4a01 to 006e03a Compare November 19, 2024 14:40
@greenhat greenhat force-pushed the greenhat/miden-package branch 2 times, most recently from 5ec902e to d6419ca Compare November 19, 2024 15:00
@greenhat greenhat force-pushed the greenhat/i1547-mastforest-add-advicemap branch from 006e03a to 92f3309 Compare November 20, 2024 07:05
greenhat and others added 8 commits January 16, 2025 12:53
Co-authored-by: Philippe Laferrière <plafer@protonmail.com>
Co-authored-by: Philippe Laferrière <plafer@protonmail.com>
Co-authored-by: Philippe Laferrière <plafer@protonmail.com>
Co-authored-by: Philippe Laferrière <plafer@protonmail.com>
Co-authored-by: Philippe Laferrière <plafer@protonmail.com>
Co-authored-by: Philippe Laferrière <plafer@protonmail.com>
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left some comments inline. The main ones are about:

  • Potentially adding some validation to library path deserialization.
  • Using Serializable/Deserializable instead of serde.
  • Not including functionality that assumes downstream crates (i.e., miden-base).

package/Cargo.toml Outdated Show resolved Hide resolved
package/Cargo.toml Outdated Show resolved Hide resolved
package/README.md Show resolved Hide resolved
@@ -0,0 +1,31 @@
[package]
name = "miden-package"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should make it a bit more specific. Maybe miden-vm-package or miden-code-package (though, not sure if I like these better than just miden-package).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got so much used to miden-package so I'm biased here. :) In general, if we plan to have another "packages" (account?) giving it a more specific name makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about miden-mast-package here? I would also maybe expand the description a little bit to make it more specific.

package/src/dep/mod.rs Outdated Show resolved Hide resolved
package/src/dep/mod.rs Outdated Show resolved Hide resolved
@greenhat
Copy link
Contributor Author

@bobbinth @plafer Thank you for the review! I've addressed all notes, implemented easy ones, and I am converting this PR to draft to work on:

  • Switch PackageExport::name to have QualifiedProcedureName type and propagate Arbitrary implementation for proptest;
  • Remove bitcode and serde based serialization in favor of Serializable/Deserializable;
  • New validation for ids (LibraryPath, ProcedureName) on deserialization;

@greenhat greenhat marked this pull request as draft January 17, 2025 11:17
Implement `Serializable/Deserializable` and `Arbitrary`(proptest)
for `QualifiedProcedureName`. Add serialization roundtrip tests for
`LibraryPath` and `ProcedureName`.
@greenhat greenhat force-pushed the greenhat/miden-package branch from 915c093 to a93f3a2 Compare January 20, 2025 16:07
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left some more comments/questions inline.

Besides these, I think the main thing remaining here is potentially adding some validation to library path deserialization - right?

package/src/lib.rs Show resolved Hide resolved
package/src/package.rs Outdated Show resolved Hide resolved
package/src/package.rs Outdated Show resolved Hide resolved
package/src/tests.rs Outdated Show resolved Hide resolved
package/src/package.rs Outdated Show resolved Hide resolved
Comment on lines 102 to 110
pub struct Package {
/// Name of the package
pub name: String,
/// The MAST artifact ([Program] or [Library]) of the package
pub mast: MastArtifact,
/// The package manifest, containing the set of exported procedures and their signatures,
/// if known.
pub manifest: PackageManifest,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not for this PR, but where would we put custom metadata? Here, I'm thinking about the storage layout metadata we'd need to instantiate account components.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking, either directly in Package or in PackageManifest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create an issue for this.

@greenhat greenhat force-pushed the greenhat/miden-package branch from 223c95f to 37fe048 Compare January 22, 2025 05:17
@greenhat
Copy link
Contributor Author

@bobbinth @plafer I implemented everything and addressed all your notes. Please do another round.

Copy link
Contributor

@plafer plafer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good! I left some more comments inline - most are pretty minor.

Library::read_from(source).map(Arc::new).map(MastArtifact::Library)
} else {
Err(DeserializationError::InvalidValue(format!(
"Invalid MAST artifact tag: {:?}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we usually don't capitalize error the first letter of error messages (i.e., "Invalid" -> "invalid").

Comment on lines +74 to +75
/// A procedure exported by a package, along with its digest and
/// signature(will be added after MASM type attributes are implemented).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there should be a space after "signature". Also, we usually do newlines after about 100 chars. So, this comment should look like:

/// A procedure exported by a package, along with its digest and signature (will be added after
/// MASM type attributes are implemented).

#[cfg(test)]
extern crate std;

pub use vm_core::{chiplets::hasher::Digest, mast::MastForest, Program};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also re-export Library, QualifiedProcedureName, LibraryPath, and maybe ProcedureName as they are also used in the public interface (these would come from the assembly crate though).

Comment on lines +336 to +338
Some((_, c))
if c.is_ascii_lowercase() || c == '_' || c == '-' || c == '$' || c == '.' =>
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but the code in this function is a bit difficult to follow. Let's create an issue (could be a "good first issue") to make the code more readable.

@@ -505,7 +505,23 @@ impl Deserializable for LibraryPath {
let path = source.read_slice(len)?;
let path =
str::from_utf8(path).map_err(|e| DeserializationError::InvalidValue(e.to_string()))?;
Self::new(path).map_err(|e| DeserializationError::InvalidValue(e.to_string()))
LibraryPath::new(path).or_else(|_| {
// Try to parse at least the namespace
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we expand the comment a bit here to explain what the code below is intending to cover? My understanding is:

If path is not a valid LibraryPath, we try to take the first "leg" of the path and use it as a namespace and the rest of the string as the module name. And if there is no first "leg", then we use Anon namespace and try to use the whole path as the module name.

If the above is correct, it is not clear to me what exactly we are trying to enable here. It seems like maybe we want to make sure that a path with no namespaces get Anon namespace? If so, could we simplify the code as:

LibraryPath::new(path).or_else(|_| {
    let module_id = Ident::new(path).map_err(|e| {
        DeserializationError::InvalidValue(format!("Invalid module id: {}", e))
    })?;
    Ok(LibraryPath::new_from_components(LibraryNamespace::Anon, [module_id]))
}

Comment on lines +3 to +5
version = "0.11.0"
description = "Miden VM package"
documentation = "https://docs.rs/miden-package/0.11.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's increment the versions to v0.12.0.

@@ -0,0 +1,31 @@
[package]
name = "miden-package"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about miden-mast-package here? I would also maybe expand the description a little bit to make it more specific.

@bobbinth
Copy link
Contributor

Let's also resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants