Schnorr signatures - anyone here willing to collaborate or create a lib?

Been tinkering with nostr lately. It’s a new open protocol being built in the wild. It has a possibility to disrupt centralized social networks such as Twitter. It’s been recently funded by Jack Dorsey, which is the Twitter founder himself. He’s actually using it and he said that it’s much like the very early Twittr days, with better potential. Ok, enough about Dorsey…

What’s interesting here, is that it’s possible to create all sorts of things that can communicate on this network without having to ask permission. I’d personally like to create bots. Even so, clients meant for people are very rudimentary and I have a good feeling this thing could be greatly improved with realtime Phoenix LiveView apps, for instance.

Problem is… to send anything on this network, messages have to be signed with Schnorr Signatures and I can’t find any lib that can natively do it either for Elixir or Erlang. By the way, this signature scheme is also enabling the new Bitcoin taproot addresses.

So, this is a call to elixir cryptographists and math wizards… is there anyone here willing to collaborate or create such a lib? I’m willing to do my part and would most probably make good use of it.

5 Likes

There are parts that could be tricky in pure Erlang / Elixir, for instance anything that requires constant-time arithmetic.

One way to avoid that would be to wrap an existing Rust library like k256 with Rustler. I’m neither a cryptographer nor much of a Rust dev, but this example seems like it wouldn’t be too much hassle to hook up.

4 Likes

Just a side question: why do you think LiveView is a good fit? Because of its real-timeness? Or because it’s also websocket based?

Both. The real-timeness is possible thanks to websockets.

Havent tried to embed a rust lib in elixir yet… guess it might be time to learn a thing or two about Rustler.

I was hoping for a full native implementation to limit dependencies, such as a rust compiler. I wonder how much complexity this would add to a lib.

Looks like with the help of rustler_precompiled, it’s possible to embed rust code in a lib and not require all dependent projects to have the whole rust toolkit installed…

I would guess the least complex and most straight forward way would be wrap an elixir front end to a well-maintained rust or c++ library, if you do end up going the rust wrapper route I would love to contribute in some way I have dabbled with ideas around this kind of thing but no real killer ideas, I’m in no way a cryptography expert so you’d probably want to find someone who really knows their way around this stuff however I am learning tonnes and happy to pitch in when/how I can.

3 Likes

Well, actually trying somehow wrap this in a nice elixir library that’s also rustler_precompiled… if successful, that would do it. Goal is to embed rust without requiring its compiler in apps depending on the said lib.

Right now, I’m learning rust basics so I have a slight idea of how rustler works as explained here.

At that point, any help would be greatly appreciated. The endgame is to make elixir usable on nostr.

Here’s a library I’ve found that wraps secp256k1 as a NIF library doing a quick search, perhaps this could help point you in the right direction as Schnorr is an extension on ECDSA. Sadly it hasn’t been maintained for several years, I would suppose some good examples of rust NIFs in the community today are:

There are others but these are just the ones I have personally taken a look under the hood of. The rustler documentation has some detailed examples that can get you pretty far. In general this is a common problem, cryptographic primitives as applied to the blockchain often don’t have good library support in elixir

2 Likes

Guess there’s a void waiting to be filled here…

1 Like

Hey all, not sure how much help I can be, but I’ve dabbled some in Rust and Rustler and think I might be able to help a bit. Looks like folks the Schnorr signatures implementation inside the k256 crate. Curious how you want to go about this, do you want to wrap the entirety of rust-elliptic and only implemented the wrapper for k256 (exposing more as you need later), or do you want to only wrap the k256 portion?

Note that I’m not a cryptography person, just happen to have some Rust and Elixir experience and have played some with Rustler. With that in mind, what can I do that would be helpful for you all?

3 Likes

Only schnorr related stuff would suffice for now… signatures and verification.

Thanks for the support! :pray:

Hey! Did a rough implementation that I think covers your use-case

alias K256.Schnorr

signing_key = Schnorr.generate_random_signing_key()
message = "This is some content to sign"
assert {:ok, signature} = Schnorr.create_signature(signing_key, message)
assert {:ok, verifying_key} = Schnorr.create_verifying_key(signing_key)
assert :ok = Schnorr.validate_signature(message, signature, verifying_key)

There’s a few things I’m not happy about in this first pass

  1. I’m not pre-compiling anything for downstream users, that’ll probably be necessary if this is being published to Hex.
  2. signature and verifying_key are all lists, but I think people would expect binaries or some other opaque blob. Not sure the best way to do this atm.
  3. Missing specs.
  4. READMEs are whatever was generated by default, docs are minimal

Let me know your thoughts and if you’d like I can run through anything you’d like to know more about :slight_smile:

Again, not a cryptographer, don’t know much about nostr, but enjoyed the challenge here.

4 Likes

Thanks, I will try this right away and potentially start a nostr lib today, thanks to you!

I Agree about the 4 improvements, in the same order. Binaries would be the best as input.

4 Likes

There is a PR for Secp256k1 Schnorr support natively in an open PR in our Bitcoinex repo here: GitHub - RiverFinancial/bitcoinex: Bitcoin utilities in Elixir. We’re working on making the signature and verification primitives work for Nostr.

3 Likes

nostr lib working right here GitHub - RooSoft/nostr: Connect to the nostr network with Elixir

Early alpha, but still a great toy to play with…

4 Likes

looks really good so far, love how this collaboratively came together. First time poster on elixir forum (I’ve been read only for a very long time) and it was really lovely to see this play out.

I wonder how much stuff is still needed before being able to go from this library to building an application on top of the protocol?

2 Likes

Will probably develop faster once holidays come to an end :sweat_smile:

1 Like

We just shipped the update to BitcoinEx to support Schnorr signatures: GitHub - RiverFinancial/bitcoinex: Bitcoin utilities in Elixir

2 Likes