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

[BUG] [risc0/tinygo] Compiling and Running fib_go.go with risc0/tinygo Result Error #1222

Open
jax-cn opened this issue Dec 13, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@jax-cn
Copy link

jax-cn commented Dec 13, 2023

Apology for Off-Topic Issue Post

I apologize for raising this issue in the risc0 project tracker. I've noticed that the tinygo project does not have issues enabled, which leaves me with no direct avenue to raise this concern there. I hope you understand the necessity of reaching out through this channel and appreciate any guidance you can provide.

Bug Report

I successfully compiled an ELF binary using risc0/tinygo with the command tinygo build -target zkvm fib_go.go. However, when attempting to run the resulting binary with r0vm --elf fib_go, I encountered the following runtime error:

thread 'main' panicked at risc0/r0vm/src/lib.rs:129:20:
called Result::unwrap() on an Err value: address 0x00000001 is an invalid guest address

This suggests that the ELF binary does not conform to the expected ELF specification required by zkvm for guest programs.

Steps to Reproduce

  1. use github workflow build risc0/tinygo through LLVM
  2. download artifacts
  3. install tinygo
  4. run tinygo build -target zkvm fib_go.go
  5. run r0vm --elf fib_go

Expected behavior

use r0vm running fib_go and get proof

Your Environment

  • risc0-zkvm version: 0.19.1
  • tinygo verson: tinygo version 0.26.0 linux/amd64 (using go version go1.18.10 and LLVM version 14.0.0)
  • Rust version: cargo 1.74.1
  • Platform/OS: Ubuntu 22.04

Additional context

I've reviewed the previously closed issue and noted that there was mention of partial success with tinygo. However, it seems that there have been no recent updates to the tinygo project, which leads me to suspect that there might have been changes to zkvm that affect the compatibility of the binaries produced by tinygo.

Would it be possible for you to share the results from your experimentation with tinygo? Any insights or progress made could be incredibly helpful for others encountering similar issues.

Additionally, I wanted to inquire if there have been any recent changes to the zkvm that might require updates to the guest program specifications or the way tinygo compiles binaries for zkvm.

fib_go.go

package main

import (
	"fmt"
	"math/big"
)

func main() {
	var a0, a1 *big.Int
	a0 = big.NewInt(0)
	a1 = big.NewInt(1)
	for i := 2; i <= 10000; i++ {
		a0, a1 = a1, a0.Add(a0, a1)
	}
	modResult := a1.Mod(a1, big.NewInt(1<<32))
	fmt.Printf("%d", modResult)
}
@jax-cn jax-cn added the bug Something isn't working label Dec 13, 2023
@SchmErik
Copy link
Contributor

I was able to get it working however, this has bit-rotted a little bit. risc0/tinygo was done using a much older version of the zkvm and we've changed several small things since then and we need to "wire up" everything again. Some of the things that might need re-examining:

  • system calls - we're thinking about how to do this right now and we're trying to go with the approach of generating a .a file that exposes the symbols so that you can link to them during compilation so we don't have to duplicate the assembly code.
  • Memory mapping: not sure if this changed substantially since I last worked on it but here is a description of the memory layout. The generated code needs to conform to this. We like for stack to be lower than the code and heap to be above code. We're able to pass a parameter to rustc to get the layout we want so I assume this would be similar.
  • we added changes in env::init which does some important stuff. we'd need to do something similar for tinygo. I think we could do this with some sort of runtime library as a part of the .a file. this isn't necessary for the code to run but it contains important security-related initialization.

So the issue that you're facing is the second point: there's something that's trying to read from address 0x0-0x3ff we disabled this so the memory for the generated code must range between 0x400 - 0xC000000. You might be able to make some modifications to the build commands within tinygo to get this to work?

@jax-cn
Copy link
Author

jax-cn commented Dec 19, 2023

I apologize for the delayed response as I was away on vacation and missed your message.

I deeply appreciate your insights regarding the memory requirements for risc0 zkvm compatibility. It seems that my issue stems from memory addressing that does not conform to the zkvm's current specifications. I will attempt to adjust the compile-time parameters for tinygo to align with the memory range of 0x400 to 0xC000000 as you've outlined. If I manage to make it work, I'll be sure to share my solution here for others who might encounter the same challenge.

Furthermore, I'm particularly intrigued by the third point you mentioned regarding env::init. If you could share more details on the potential implementation strategies, I would be eager to follow your guidance and explore how to integrate such a runtime library as part of the .a file. Understanding the security-related initialization that you mentioned is crucial, and I'm keen on getting it right.

Thanks again for your support, and I'm looking forward to making progress with the help of your valuable input.

@mwittie
Copy link

mwittie commented May 16, 2024

@jax-cn have you had any luck getting this to work?

@nuke-web3
Copy link
Member

In case you missed it, this issue is referencing https://github.com/risc0/tinygo .

I do not think that there is presently work towards updating that repo and support for go at this time, but it is something that is increasingly asked about. Come join our discord to chat with others about that: Join here & support forum post most relevant at this time

@austinabell
Copy link
Contributor

In case you missed it, this issue is referencing risc0/tinygo .

I do not think that there is presently work towards updating that repo and support for go at this time, but it is something that is increasingly asked about. Come join our discord to chat with others about that: Join here & support forum post most relevant at this time

There is some work being done intermittently for tinygo support, but more in an experimental capacity. The current status is that it can prove basic go programs (with support for reading and committing data to the journal), but there are some unresolved issues when running some more complex programs (currently memory alignment issues). The work being done is on this branch https://github.com/risc0/tinygo/tree/go21 but it's not yet setup such that I would recommend it.

I will do an update to it to simplify the code needed and possibly create a template (I have a stale one locally) and when I do, I can update this thread!

But just to reiterate, very experimental, and I would generally not recommend this, but if anyone is interested in contributing to this, I can expedite putting the code in a state conducive to that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants