Build Bitcoin (and ZCash) in Rust

Consensus-Ready Bitcoin in Rust

Project Details: Entropy (working title)

Github: GitHub - onicslabs/entropy: Bitcoin wallet implemented entirely in Rust

Goals

  • Roadmap
  • After talking with Andrew Poelstra (rust-bitcoin), a fresh Bitcoin implementation in Rust is the best route
  • Most of Andrew’s efforts are focused on MimbleWimble, but he wishes us luck
  • New Github project is up

After Bitcoin Full-node is Complete

Discussion

Bitcoin is one of the more complex projects in recent engineering history. Not only using sophisticated cryptographic design, but complex data structures built with nested templated types. All of these factors make implementing Bitcoin in other languages difficult.

However, more diversity and common packages have been written in Rust that will make implementing Bitcoin and other cryptocurrencies much easier.

I’m reaching out to the community for advice and engineering help to successfully build a production-ready Rust implementation of Bitcoin. There are significant hurdles to overcome, but the payoff is fairly large for both developer communities and currency user economies.

Developer Benefits:

  • Rust devs will be able to join forces with the other Bitcoin devs
  • Bitcoin developers will be able to use memory safe backends for their applications
    • Many of the classes of memory corruption vulnerabilities in C++ are entirely avoided using Rust
  • Many of the cryptographic and networking primitives in Bitcoin are low-level, and have very disparate use-cases
    • Communities entirely outside Bitcoin could benefit from a well-executed implementation

User Benefits:

  • Safer client implementations mean wallet compromise is less likely
  • May gain performance boost
  • Less money lost / spent on security due to less exploitation
  • More money/time saved on developers
    • Redesigned code may be easier to maintain / extend

Project Cons:

  • Enormous amount of developer time required
  • There are truly hard problems that require innovative problem solving
  • Reading thousands of lines of C++ may actually make your eyes bleed
  • This is a community project, so likely no money for contributors :frowning:
  • Banks will have a harder time affording attacks on Bitcoin software… wait…

Building rusty bitcoins isn’t a project that will survive on the fingertips of one person.
This endeavor requires group effort to succeed.

I believe a full re-implementation of Bitcoin in Rust is preferable to trying to use Rust’s FFI for calling into C++ code. There may be better ways for integrating Rust components gradually (middleware using protobufs API?).

Because of the complexities of templated types in the C++ codebase, there are virtually no way to directly implement those classes in Rust without rebuilding a C++ compiler inside the Rust compiler. May be wrong about this, but seems to be the case from discussions I’ve read online.

Please let me know in the replies or private messages if you are interested.

Any critique, advice, information is very welcome.

6 Likes

Oh wow do I ever hear you loud and clear about the complexities and difficulty of recreating something which uses C++ templates as extensively as Zcash and libsnark in another language! The sheer amount of “functions” that produce side effects (often nothing but!) makes me recoil in horror and want to rewrite the whole mess in Haskell, myself :slight_smile:

3 Likes

When I was in college the 2nd time, over Winter Break of the 2005-2006 school year I did SHA-2 in Haskell: Shaskell - An SHA2 library in Haskell
Which was the first thing I ever wrote in it. Of course that’s WAY smaller than libsnark or zcash!

2 Likes

@GPU_Mining I’m a recent convert myself. Been trying to learn as much as possible, and writing most of my projects in Rust now.

@anon47418038 Haskell will always have a warm spot in my heart. I like your library, I don’t understand it yet, but just your pure implementation freeing us from the horrors of openssl is commendable.

From both of your experiences in development, do you have any words of caution taking on a project this size? Tips for pulling it off?

1 Like

@onics My sha2 in haskell has comments citing the standards document All Over it, and a copy is on my site for download…if you read along with it when looking at the code, it’s a LOT clearer. Which was the point of the project, to help me understand how the heck sha-2 worked, as all the implementations I’d see, regardless of language, didn’t seem to bear much resemblance on cursory inspection to the rather clear algorithm outlined in the standard. Of course that’s largely due to optimization and similar concerns, but that was not my goal :slight_smile:

2 Likes

I second that emotion, the bit about losing your sanity :slight_smile:

2 Likes

Thanks for the “encouragement”… :slight_smile:

Looking forward to losing something I’m not sure I ever had

2 Likes

Apologies on the large gap between updates. Things got a bit hectic with work.

I added a github project and am putting together the last bits of the project’s roadmap for the next six months.

Will continue to update this thread as the project progresses.

2 Likes

what’s the github repo?

1 Like

Its truly a skeleton project. I just have the basic outline at the moment, and the minimal files required for a cargo project.

Tonight I will be working on adding code for Bitcoin addresses. i basically understand the dependencies surrounding that class, so figured it looks like a good first target.

4 Likes

Also going to work on some documentation about the process of compiling Rust into an importable shared object.

Luckily Rust allows for code examples in documentation that actually compile, so that should make things interesting :slight_smile:

2 Likes

Looks interesting, starred :slight_smile: !

1 Like

I’d love to contribute. My programming skills are quite limited to Solidity and basic Python/Javascript, but hopefully I can contribute where I can.

2 Likes

Would love to have you on the project!

There is a ton of work to be done, so any programming experience is good.

When I get the project to a more structured place, we can talk about what you would like to work on.

What’s fun for you to program? What kind of problems are exciting for you?

This looks interesting @ebfull :wink:

I found this bitcoin-rust git repo from a year ago. We can perhaps start with porting that stuff over.

3 Likes

This project idea is awesome. I wish I had more time to contribute but Zcash and my 1 year old take up all my time. :wink:

I think it’s admirable to replace Bitcoin Core in a piecemeal fashion by implementing rust components. In fact, we’re thinking of doing sometihng similar for Zcashd! I think it will be a long grueling process that will never be complete.

If I had plenty of time on my hands, I’d be tempted to start a new Zcash implementation from scratch with no need to be compatible with zcashd / bitcoin-core code architecture.

2 Likes

I periodically have insane thoughts of redoing zcash from the ground up in Haskell, and then look in the mirror and my bank account and wake up from that fantasy/fever dream :slight_smile:

If I won the lottery or something I’d be able to afford to spend the time on indulging on dedicating myself to such an undertaking

1 Like

hi @lightswarm, that looks suspiciously similar to the rust-bitcoin library from apoelstra

I’m definitely down to take help wherever we can tho. The stuff I have so far is cannibalized from apoelstra. Because of the large differences in planned architecture, we’ll have to be selective about what we take in from other projects. Just don’t want the project to turn into Frankenstein’s monster.

Thanks for the encouragement @nathan-at-least and @anon47418038. I know what you mean about time consumption. I haven’t made nearly the progress on this project that i have wanted to.

Luckily, I have almost zero obligations outside work and starting my own consulting firm. No kids or family. Even with that, I find myself scrounging for time and battling procrastination. Seeing @ebfull’s latest project also gives me a ton of motivation to push forward

Hi! I’m also very interested into rust+Bitcoin (or any node), and I wanted to share with you some work I’ve been doing on a graduation project. We surely didn’t do nearly as much we wanted (we knew very little about nodes technically), but we managed to handshake with a couple of peers, ask for and receive headers list, initial stuff like that!

We were learning a lot of new structures from Rust, and, on my part, I still want to learn more before continuing the node itself. It’s exploratory rather than work.

The code is here GitHub - swfsql/rustbtc