-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a c library that has some primitive cryptographic functions needed for working with adaptor signatures.
- Loading branch information
1 parent
5cc1e8b
commit 6448f91
Showing
5 changed files
with
134 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 @@ | ||
#!/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,53 @@ | ||
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 | ||
) | ||
|
||
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 | ||
} | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} |
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