Skip to content

Commit

Permalink
dex: Add libsecp256k1 c library.
Browse files Browse the repository at this point in the history
Add a c library that has some primitive cryptographic functions needed
for working with adaptor signatures.
  • Loading branch information
JoeGruffins committed Jun 13, 2024
1 parent 5cc1e8b commit 8e7f104
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ client/cmd/simnet-trade-tests/simnet-trade-tests
client/cmd/mmbot/mmbot
docs/examples/rpcclient/rpcclient
dex/testing/loadbot/loadbot
dex/libsecp256k1/secp256k1
bin/
bin-v*/
client/webserver/site/template-builder/template-builder
Expand Down
10 changes: 10 additions & 0 deletions dex/libsecp256k1/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

rm -fr secp256k1
git clone https://github.com/tecnovert/secp256k1 -b anonswap_v0.2

cd secp256k1
./autogen.sh
./configure --enable-module-dleag --enable-experimental --enable-module-generator --enable-module-ed25519 --enable-module-recovery
make
cd ..
60 changes: 60 additions & 0 deletions dex/libsecp256k1/libsecp256k1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.

package libsecp256k1

/*
#cgo CFLAGS: -g -Wall
#cgo LDFLAGS: -L. -l:secp256k1/.libs/libsecp256k1.a
#include "secp256k1/include/secp256k1_dleag.h"
#include <stdlib.h>
secp256k1_context* _ctx() {
return secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
}
*/
import "C"
import (
"errors"
"unsafe"

"decred.org/dcrdex/dex/encode"
"github.com/decred/dcrd/dcrec/edwards/v2"
)

const (
proofLength = 48893
)

// Ed25519DleagProve creates a proof for checking a discrete logarithm is equal
// across the secp256k1 and ed25519 curves.
func Ed25519DleagProve(privKey *edwards.PrivateKey) (proof [proofLength]byte, err error) {
secpCtx := C._ctx()
defer C.free(unsafe.Pointer(secpCtx))
nonce := [32]byte{}
copy(nonce[:], encode.RandomBytes(32))
key := [32]byte{}
copy(key[:], privKey.Serialize())
n := (*C.uchar)(unsafe.Pointer(&nonce))
k := (*C.uchar)(unsafe.Pointer(&key))
nBits := uint64(252)
nb := (*C.ulong)(unsafe.Pointer(&nBits))
plen := C.ulong(proofLength)
p := (*C.uchar)(unsafe.Pointer(&proof))
res := C.secp256k1_ed25519_dleag_prove(secpCtx, p, &plen, k, *nb, n)
if int(res) != 1 {
return [proofLength]byte{}, errors.New("C.secp256k1_ed25519_dleag_prove exited with error")
}
return proof, nil
}

// Ed25519DleagVerify verifies that a descrete logarithm is equal across the
// secp256k1 and ed25519 curves.
func Ed25519DleagVerify(proof [proofLength]byte) bool {
secpCtx := C._ctx()
defer C.free(unsafe.Pointer(secpCtx))
pl := C.ulong(proofLength)
p := (*C.uchar)(unsafe.Pointer(&proof))
res := C.secp256k1_ed25519_dleag_verify(secpCtx, p, pl)
return res == 1
}
64 changes: 64 additions & 0 deletions dex/libsecp256k1/libsecp256k1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//go:build libsecp256k1

package libsecp256k1

import (
"testing"

"github.com/decred/dcrd/dcrec/edwards/v2"
)

func TestEd25519DleagProve(t *testing.T) {
tests := []struct {
name string
}{{
name: "ok",
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pk, err := edwards.GeneratePrivateKey()
if err != nil {
t.Fatal(err)
}
_, err = Ed25519DleagProve(pk)
if err != nil {
t.Fatal(err)
}
})
}
}

func TestEd25519DleagVerify(t *testing.T) {
pk, err := edwards.GeneratePrivateKey()
if err != nil {
panic(err)
}
proof, err := Ed25519DleagProve(pk)
if err != nil {
panic(err)
}
tests := []struct {
name string
proof [proofLength]byte
ok bool
}{{
name: "ok",
proof: proof,
ok: true,
}, {
name: "bad proof",
proof: func() (p [proofLength]byte) {
copy(p[:], proof[:])
p[0] ^= p[0]
return p
}(),
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ok := Ed25519DleagVerify(test.proof)
if ok != test.ok {
t.Fatalf("want %v but got %v", test.ok, ok)
}
})
}
}
6 changes: 6 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ echo "Go version: $GV"
# Ensure html templates pass localization.
go generate -x ./client/webserver/site # no -write

cd ./dex/libsecp256k1
./build.sh
go test -race -tags libsecp256k1

cd "$dir"

# list of all modules to test
modules=". /dex/testing/loadbot /client/cmd/bisonw-desktop"

Expand Down

0 comments on commit 8e7f104

Please sign in to comment.