Add simple deserialization example showing inability to deserialize p… #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…rover keys from wasm
Overview
For the wallet and many other applications, we would like to generate proofs from the WASM. Currently, this is not possible mainly due to the serialization and deserialization of the prover/verify keys.
This PR is a simple test to illustrate the issue.
As I believe it's crucial to enable proof generation from the wasm, I'm open to any feedback. My hope is that we can agree on an approach and I'm happy to implement it. If I'm misunderstanding anything about the issue, please let me know. I hope I'm wrong and the fix is simple.
How to run
Put this snarkVM branch in the same parent directory: Make Marlin key types public snarkVM#2
.../aleo-dev/snarkVM
&.../aleo-dev/aleo
Update this line to the location your transfer prover file
Run the rust test:
cargo test --package aleo-wasm --lib -- program::proving_key::tests::test_deserialize_key --exact --nocapture
It should output the first 1000 bytes of the transfer prover file.
wasm-pack test --node --lib -- program::proving_key::tests::test_deserialize_key_wasm
It should fail with the error:
Background & Explanation
The reader gets:
d3c0 073d 5cd9 c5ec 75f4 ede3 8d59 1c9e c4a1 d48f eaf0 73d6 2a89 e70e 948a 628b d794 dc9e bbd1 6a8f 1fd8 1b9c 9033 6b00 00
But when running from the wasm, the reader gets:
5966 0200 0000 0000 eedf 0100 0000 0000 0c00 0000 0000 0000 d3c0 073d 5cd9 c5ec 75f4 ede3 8d59 1c9e c4a1 d48f eaf0 73d6 00
The wasm is missing an additional 24 byte offset.
The prover key will continue to deserialize successfully for about a couple of KB until it hits a similar error where once again it tries to deserialize a field but is missing an offset:
Should be:
[255, 191, 61, 21, 31, 87, 78, 184, 34, 113, 126, 20, 187, 13, 181, 251, 187, 25, 62, 187, 56, 30, 239, 1, 36, 225, 174, 5, 251, 139, 214, 34, 58, 141, 133, 53, 119, 214, 140, 129, 159, 111, 59, 65, 57, 56, 100, 1, 0]
But is instead:
[0, 0, 0, 0, 255, 191, 61, 21, 31, 87, 78, 184, 34, 113, 126, 20, 187, 13, 181, 251, 187, 25, 62, 187, 56, 30, 239, 1, 36, 225, 174, 5, 251, 139, 214, 34, 58, 141, 133, 53, 119, 214, 140, 129, 159, 111, 59, 65, 0]
Speculative Cause
I believe this is caused by the dependence of the CanonicalSerialization & CanonicalDeserialization on the AST:
https://github.com/AleoHQ/snarkVM/blob/testnet3/utilities/derives/src/canonical_serialize.rs#L74 but I'm not 100% sure.
As the WASM is able to deserialize individual types successfully provided the correct offset, it doesn't seem to be a problem with incompatible types in WebAssembly.
Since the wasm feature flags generate a different AST, deserializing a key created with a different AST breaks.
This explanation would also explain why the offsets are always missing reads, as in the Rust version always adds additional bytes in comparison before trying to deserialize some type.
Potential Fixes
Assuming I'm accurately describing the problem, I'm not sure if there's a simple fix. Here are some ideas:
Create a new serialization format, not dependent on the AST using something like serde that can be used by the wasm. The potential here is that adding a new serialization format could change the AST enough as to require new generation of default keys like the credits program, inclusion keys, and fee keys.
Change the default serialization of keys to not be dependent on the AST. There are a few different ways of doing this but a simple one would be adding an unique ID to each type and using that in the serialization instead of the Ident. This would require regeneration of the keys.