-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: Add libsecp256k1 c library.
Add a c library that has some primitive cryptographic functions needed for working with adaptor signatures.
- Loading branch information
1 parent
5cc1e8b
commit be12d55
Showing
7 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
### Package libsecp256k1 | ||
|
||
Package libsecp256k1 includes some primative cryptographic functions needed for | ||
working with adaptor signatures that are not currently found in golang. This imports | ||
code from https://github.com/tecnovert/secp256k1 and uses that with cgo. Both | ||
that library and this package are in an experimental stage. | ||
|
||
### Usage | ||
|
||
Run the `build.sh` script. Currently untested on mac and will not work on Windows. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 .. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Submodule secp256k1
added at
e3ebcd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters