GuidoDipietro / solana-ed25519-secp256k1-sig-verification

On-chain Ed25519 and Secp256k1 signature verification using instruction introspection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Solana Ed25519 and Secp256k1 signature verification

On-chain Ed25519 and Secp256k1 signature verification using instruction introspection.

Built for checking Solana and Ethereum signatures, with examples (see tests).

Why and how

Solana does not have a way to implement Ed25519 or Secp256k1 sig verification on-chain on custom programs. That's why the native Ed25519Program and native Secp256k1Program exist, which have a set of instructions that can, amongst other things, verify signatures for those curves.

Therefore, the way to build custom instructions that "do" sig verification is by actually sending a transaction made of (at least) two instructions, and checking that the native program instruction was sent.

In doing so, these are the possible outcomes:

  • ❌ Native program instruction fails -> Custom instruction is never executed.
  • ❌ Native program instruction not supplied or supplied with wrong values -> Custom instruction fails to check that the Native program instruction was sent with the proper data, therefore gets rejected.
  • ✅ Native program instruction succeeds -> Custom instruction gets executed -> Custom instruction checks that the Native program instruction was sent with the proper data -> If that succeeds, we can say that Custom instruction indirectly verified the signature.

Instruction introspection

solana_program provides us with the load_instruction_at_checked function on the Instructions Sysvar, that allows us to recover the raw fields of an instruction at a given index (fields are program_id, accounts, data). In order for us to check that that instruction was constructed properly, we need to inspect the data byte array manually.

Building and testing

Install Anchor first.

There are two test files with the same concepts: one, signing using a Solana keypair (Ed25519 signatures); the other one, using an Ethereum Wallet (Secp256k1 signatures).

yarn install
anchor test

About

On-chain Ed25519 and Secp256k1 signature verification using instruction introspection


Languages

Language:TypeScript 71.4%Language:Rust 28.6%