From a525e9e7237701051ea520a50230ede11aa38422 Mon Sep 17 00:00:00 2001 From: Nik <njunger@uwaterloo.ca> Date: Thu, 29 Jan 2015 17:18:40 -0500 Subject: [PATCH] Fixed leaked C export. Preparation for godoc --- doc.go | 1 + element.go | 3 --- element_checked.go | 13 +------------ element_fmt.go | 6 ------ element_unchecked.go | 3 --- errors.go | 16 ++++++++++++++++ generation.go | 12 ++++++------ pairing.go | 2 +- params.go | 25 +++++++++++++------------ 9 files changed, 38 insertions(+), 43 deletions(-) create mode 100644 doc.go create mode 100644 errors.go diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..ef6fe62 --- /dev/null +++ b/doc.go @@ -0,0 +1 @@ +package pbc \ No newline at end of file diff --git a/element.go b/element.go index b00ea72..a2a84f3 100644 --- a/element.go +++ b/element.go @@ -6,14 +6,11 @@ package pbc import "C" import ( - "errors" "fmt" "math/big" "runtime" ) -var ErrUnknownField = errors.New("unchecked element initialized in unknown field") - type Element interface { NewFieldElement() Element diff --git a/element_checked.go b/element_checked.go index cb8f223..e35ba7e 100644 --- a/element_checked.go +++ b/element_checked.go @@ -5,18 +5,7 @@ package pbc */ import "C" -import ( - "errors" - "math/big" -) - -var ( - ErrIllegalOp = errors.New("operation is illegal for elements of this type") - ErrUncheckedOp = errors.New("unchecked element passed to checked operation") - ErrIncompatible = errors.New("elements are from incompatible fields or pairings") - ErrOutOfRange = errors.New("index out of range") - ErrInternal = errors.New("a severe internal error has lead to possible memory corruption") -) +import "math/big" func (el *checkedElement) impl() *elementImpl { return &el.unchecked } diff --git a/element_fmt.go b/element_fmt.go index 8587e2e..3ed7830 100644 --- a/element_fmt.go +++ b/element_fmt.go @@ -15,17 +15,11 @@ import "C" import ( "bytes" - "errors" "fmt" "io" "unsafe" ) -var ( - ErrBadInput = errors.New("invalid element format during scan") - ErrBadVerb = errors.New("invalid verb specified for scan") -) - func (el *elementImpl) errorFormat(f fmt.State, c rune, err string) { fmt.Fprintf(f, "%%!%c(%s pbc.Element)", c, err) } diff --git a/element_unchecked.go b/element_unchecked.go index 3c8c88c..b887b6e 100644 --- a/element_unchecked.go +++ b/element_unchecked.go @@ -6,13 +6,10 @@ package pbc import "C" import ( - "errors" "math/big" "unsafe" ) -var ErrBadPairList = errors.New("pairing product list is in an invalid format") - func (el *elementImpl) impl() *elementImpl { return el } func (el *elementImpl) NewFieldElement() Element { diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..bc5006f --- /dev/null +++ b/errors.go @@ -0,0 +1,16 @@ +package pbc + +import "errors" + +var ( + ErrInvalidParamString = errors.New("invalid pairing parameters") + ErrUnknownField = errors.New("unchecked element initialized in unknown field") + ErrIllegalOp = errors.New("operation is illegal for elements of this type") + ErrUncheckedOp = errors.New("unchecked element passed to checked operation") + ErrIncompatible = errors.New("elements are from incompatible fields or pairings") + ErrBadPairList = errors.New("pairing product list is in an invalid format") + ErrBadInput = errors.New("invalid element format during scan") + ErrBadVerb = errors.New("invalid verb specified for scan") + ErrOutOfRange = errors.New("index out of range") + ErrInternal = errors.New("a severe internal error has lead to possible memory corruption") +) diff --git a/generation.go b/generation.go index a818935..b3b33a6 100644 --- a/generation.go +++ b/generation.go @@ -22,36 +22,36 @@ import ( func GenerateA(rbits uint32, qbits uint32) Params { params := makeParams() - C.pbc_param_init_a_gen(params, C.int(rbits), C.int(qbits)) + C.pbc_param_init_a_gen(params.data, C.int(rbits), C.int(qbits)) return params } func GenerateA1(n *big.Int) Params { params := makeParams() - C.pbc_param_init_a1_gen(params, &big2mpz(n)[0]) + C.pbc_param_init_a1_gen(params.data, &big2mpz(n)[0]) return params } func GenerateD(d uint32, bitlimit uint32) Params { params := makeParams() - C.pbc_cm_search_d((*[0]byte)(C.acceptPairingD), unsafe.Pointer(params), C.uint(d), C.uint(bitlimit)) + C.pbc_cm_search_d((*[0]byte)(C.acceptPairingD), unsafe.Pointer(params.data), C.uint(d), C.uint(bitlimit)) return params } func GenerateE(rbits uint32, qbits uint32) Params { params := makeParams() - C.pbc_param_init_e_gen(params, C.int(rbits), C.int(qbits)) + C.pbc_param_init_e_gen(params.data, C.int(rbits), C.int(qbits)) return params } func GenerateF(bits uint32) Params { params := makeParams() - C.pbc_param_init_f_gen(params, C.int(bits)) + C.pbc_param_init_f_gen(params.data, C.int(bits)) return params } func GenerateG(d uint32, bitlimit uint32) Params { params := makeParams() - C.pbc_cm_search_d((*[0]byte)(C.acceptPairingG), unsafe.Pointer(params), C.uint(d), C.uint(bitlimit)) + C.pbc_cm_search_d((*[0]byte)(C.acceptPairingG), unsafe.Pointer(params.data), C.uint(d), C.uint(bitlimit)) return params } diff --git a/pairing.go b/pairing.go index 00455e6..0687a1c 100644 --- a/pairing.go +++ b/pairing.go @@ -59,7 +59,7 @@ func NewPairingFromString(params string) (Pairing, error) { func NewPairingFromParams(params Params) Pairing { pairing := makePairing() - C.pairing_init_pbc_param(pairing.data, params.(*C.struct_pbc_param_s)) + C.pairing_init_pbc_param(pairing.data, params.(*paramsImpl).data) return pairing } diff --git a/params.go b/params.go index 6f6b6e5..5813f81 100644 --- a/params.go +++ b/params.go @@ -14,44 +14,45 @@ int param_out_str_wrapper(char** bufp, size_t* sizep, pbc_param_t p) { import "C" import ( - "errors" "io" "runtime" "unsafe" ) -var ErrInvalidParamString = errors.New("invalid pairing parameters") - type Params interface { NewPairing() Pairing WriteTo(w io.Writer) (n int64, err error) String() string } +type paramsImpl struct { + data *C.struct_pbc_param_s +} + func NewParamsFromString(s string) (Params, error) { cstr := C.CString(s) defer C.free(unsafe.Pointer(cstr)) params := makeParams() - if ok := C.pbc_param_init_set_str(params, cstr); ok != 0 { + if ok := C.pbc_param_init_set_str(params.data, cstr); ok != 0 { return nil, ErrInvalidParamString } return params, nil } -func (params *C.struct_pbc_param_s) NewPairing() Pairing { +func (params *paramsImpl) NewPairing() Pairing { return NewPairingFromParams(params) } -func (params *C.struct_pbc_param_s) WriteTo(w io.Writer) (n int64, err error) { +func (params *paramsImpl) WriteTo(w io.Writer) (n int64, err error) { count, err := io.WriteString(w, params.String()) return int64(count), err } -func (params *C.struct_pbc_param_s) String() string { +func (params *paramsImpl) String() string { var buf *C.char var bufLen C.size_t - if C.param_out_str_wrapper(&buf, &bufLen, params) == 0 { + if C.param_out_str_wrapper(&buf, &bufLen, params.data) == 0 { return "" } str := C.GoStringN(buf, C.int(bufLen)) @@ -59,13 +60,13 @@ func (params *C.struct_pbc_param_s) String() string { return str } -func clearParams(params *C.struct_pbc_param_s) { +func clearParams(params *paramsImpl) { println("clearparams") - C.pbc_param_clear(params) + C.pbc_param_clear(params.data) } -func makeParams() *C.struct_pbc_param_s { - params := &C.struct_pbc_param_s{} +func makeParams() *paramsImpl { + params := ¶msImpl{data: &C.struct_pbc_param_s{}} runtime.SetFinalizer(params, clearParams) return params } -- GitLab