a16z / jolt

The simplest and most extensible zkVM. Fast and fully open source from a16z crypto and friends. ⚡
https://jolt.a16zcrypto.com
MIT License
587 stars 106 forks source link

Can Guest Code Function Return String? #315

Closed khandar-william closed 2 months ago

khandar-william commented 2 months ago

So from the initial Jolt project (created with jolt new), I tried adding this new function in guest/src/lib.rs

extern crate alloc;
use alloc::string::String;

#[jolt::provable]
fn hello() -> String {
    String::from("Hello, World!")
}

Then calling it from the main.rs

    let (prove_hello, verify_hello) = guest::build_hello();
    let (output_hello, proof_hello) = prove_hello();
    let is_hello_valid = verify_hello(proof_hello);

    print!("output_hello: {:?}", output_hello);
    print!("valid_hello: {:?}", is_hello_valid);

But I got this error

   Compiling guest v0.1.0 (/home/asdf/jolt-hello/guest)
error[E0277]: the trait bound `String: serde::ser::Serialize` is not satisfied
  --> guest/src/lib.rs:23:15
   |
23 | fn hello() -> String {
   |               ^^^^^^ the trait `serde::ser::Serialize` is not implemented for `String`
   |
   = help: the trait `serde::ser::Serialize` is implemented for `str`
note: required by a bound in `to_slice`
  --> /home/asdf/.cargo/registry/src/index.crates.io-6f17d22bba15001f/postcard-1.0.8/src/ser/mod.rs:87:8
   |
85 | pub fn to_slice<'a, 'b, T>(value: &'b T, buf: &'a mut [u8]) -> Result<&'a mut [u8]>
   |        -------- required by a bound in this function
86 | where
87 |     T: Serialize + ?Sized,
   |        ^^^^^^^^^ required by this bound in `to_slice`

For more information about this error, try `rustc --explain E0277`.

Is this expected? Can guest code function return String?

ncitron commented 2 months ago

At the moment guest code with no_std cannot return a string as serde does not support serializing strings in no_std. We do however have experimental std support which will allow this.

To use it, reinstall the jolt tool if you had installed it more than a few days ago and rerun jolt install-toolchain. From there add the guest-std feature to the jolt import in the cargo file of the guest and remove the no_std declaration on the top of your guest's lib.rs.

This should give you full access to the standard library with no need to use alloc along with support for using strings as inputs and output.s