-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsecp256k1.go
114 lines (95 loc) · 3.03 KB
/
secp256k1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package secp256k1
/*
#define SECP256K1_API __attribute__ ((visibility ("hidden")))
#define USE_BASIC_CONFIG 1
#include "./secp256k1-zkp/src/basic-config.h"
#define ENABLE_MODULE_ECDH 1
#define ENABLE_MODULE_GENERATOR 1
#define ENABLE_MODULE_RANGEPROOF 1
#define ENABLE_MODULE_SURJECTIONPROOF 1
#include "secp256k1-zkp/src/secp256k1.c"
#cgo CFLAGS: -I${SRCDIR}/secp256k1-zkp -I${SRCDIR}/secp256k1-zkp/src
*/
import "C"
import (
"fmt"
"unsafe"
)
const (
// ContextNone wraps the SECP256K1_CONTEXT_NONE constant
ContextNone = uint(C.SECP256K1_CONTEXT_NONE)
// ContextSign wraps the SECP256K1_CONTEXT_SIGN constant
ContextSign = uint(C.SECP256K1_CONTEXT_SIGN)
// ContextVerify wraps the SECP256K1_CONTEXT_VERIFY constant
ContextVerify = uint(C.SECP256K1_CONTEXT_VERIFY)
// ContextBoth includes all context types
ContextBoth = ContextSign | ContextVerify
)
var ctxmap map[uint]*Context
// Context wraps a *secp256k1_context, required to use all functions.
// It can be initialized for signing, verification, or both.
type Context struct {
ctx *C.secp256k1_context
}
func init() {
ctxmap = make(map[uint]*Context)
}
func newContext() *Context {
var ctx *C.secp256k1_context
return &Context{ctx}
}
func cBuf(goSlice []byte) *C.uchar {
if goSlice == nil {
return nil
}
return (*C.uchar)(unsafe.Pointer(&goSlice[0]))
}
func u64Arr(a []uint64) *C.uint64_t {
if a == nil {
return nil
}
return (*C.uint64_t)(unsafe.Pointer(&a[0]))
}
func goBytes(cSlice []C.uchar, size C.int) []byte {
return C.GoBytes(unsafe.Pointer(&cSlice[0]), size)
}
// ContextCreate produces a new *Context, initialized with a bitmask of flags
// depending on it's intended usage. The supported flags are currently
// ContextSign and ContextVerify. Although expressed in the return type
// signature, the function does not currently return an error.
func ContextCreate(flags uint) (*Context, error) {
context := newContext()
context.ctx = C.secp256k1_context_create(C.uint(flags))
return context, nil
}
// ContextClone makes a copy of the provided *Context. The provided context
// must not be NULL.
func ContextClone(ctx *Context) (*Context, error) {
other := newContext()
other.ctx = C.secp256k1_context_clone(ctx.ctx)
return other, nil
}
// ContextDestroy destroys the context. The provided context must not be NULL.
func ContextDestroy(ctx *Context) {
C.secp256k1_context_destroy(ctx.ctx)
}
// ContextRandomize accepts a [32]byte seed in order to update the context
// randomization. NULL may be passed to reset to initial state. The context
// pointer must not be null.
func ContextRandomize(ctx *Context, seed32 [32]byte) int {
return int(C.secp256k1_context_randomize(ctx.ctx, cBuf(seed32[:])))
}
// SharedContext returns a managed context
func SharedContext(flags uint) (context *Context) {
flags = flags & ContextBoth
context, exists := ctxmap[flags]
if !exists {
var err error
context, err = ContextCreate(flags)
if err != nil {
panic(fmt.Sprintf("error creating default context object (flags: %d, error: %s)", flags, err))
}
ctxmap[flags] = context
}
return
}