aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-06-06 04:59:29 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-06-06 04:59:29 +0800
commitb93d19879787b0e197f7ca3351a6becf549fe8fd (patch)
tree6d6e803eeb3d8d807490bf594580e17951169e9b
parentf64a67ab06aded2609e51078e8b8db6db87b7b7a (diff)
downloaddexon-bls-b93d19879787b0e197f7ca3351a6becf549fe8fd.tar
dexon-bls-b93d19879787b0e197f7ca3351a6becf549fe8fd.tar.gz
dexon-bls-b93d19879787b0e197f7ca3351a6becf549fe8fd.tar.bz2
dexon-bls-b93d19879787b0e197f7ca3351a6becf549fe8fd.tar.lz
dexon-bls-b93d19879787b0e197f7ca3351a6becf549fe8fd.tar.xz
dexon-bls-b93d19879787b0e197f7ca3351a6becf549fe8fd.tar.zst
dexon-bls-b93d19879787b0e197f7ca3351a6becf549fe8fd.zip
impl bls by mclBn* api
-rw-r--r--Makefile2
-rw-r--r--go/bls/bls.go13
-rw-r--r--go/bls/bls_test.go25
-rw-r--r--go/bls/mcl.go525
-rw-r--r--src/bls_c.cpp135
5 files changed, 582 insertions, 118 deletions
diff --git a/Makefile b/Makefile
index 74e5593..019602c 100644
--- a/Makefile
+++ b/Makefile
@@ -48,7 +48,7 @@ $(BLS384_LIB): $(LIB_OBJ) $(OBJ_DIR)/bls_c384.o
$(BLS384_SLIB): $(BLS384_LIB) $(BN384_LIB)
# $(PRE)$(CXX) -shared -o $@ -Wl,--whole-archive $(BLS384_LIB) $(BN384_LIB) $(MCL_LIB) -Wl,--no-whole-archive
- $(PRE)$(CXX) -shared -o $@ -Wl,--whole-archive $(BLS384_LIB) $(BN384_LIB) -Wl,--no-whole-archive
+ $(PRE)$(CXX) -shared -o $@ -Wl,--whole-archive $(BLS384_LIB) -Wl,--no-whole-archive
VPATH=test sample src
diff --git a/go/bls/bls.go b/go/bls/bls.go
index d516bb1..61b2587 100644
--- a/go/bls/bls.go
+++ b/go/bls/bls.go
@@ -2,22 +2,13 @@ package bls
/*
#cgo CFLAGS:-DMCLBN_FP_UNIT_SIZE=6
-#cgo LDFLAGS:-lbls384 -lmclbn384 -lmcl -lgmpxx -lstdc++ -lgmp -lcrypto
+#cgo LDFLAGS:-lbls384 -lmcl -lgmpxx -lstdc++ -lgmp -lcrypto
#include <bls/bls.h>
*/
import "C"
import "fmt"
import "unsafe"
-// CurveFp254BNb -- 254 bit curve
-const CurveFp254BNb = C.mclBn_CurveFp254BNb
-
-// CurveFp382_1 -- 382 bit curve 1
-const CurveFp382_1 = C.mclBn_CurveFp382_1
-
-// CurveFp382_2 -- 382 bit curve 2
-const CurveFp382_2 = C.mclBn_CurveFp382_2
-
// Init --
// call this function before calling all the other operations
// this function is not thread safe
@@ -26,7 +17,7 @@ func Init(curve int) error {
if err != 0 {
return fmt.Errorf("ERR Init curve=%d", curve)
}
- return nil
+ return mclInit(curve) // QQQ
}
// GetMaxOpUnitSize --
diff --git a/go/bls/bls_test.go b/go/bls/bls_test.go
index 2dd5597..84e5344 100644
--- a/go/bls/bls_test.go
+++ b/go/bls/bls_test.go
@@ -6,6 +6,30 @@ import "strconv"
var unitN = 0
// Tests (for Benchmarks see below)
+func testPairing(t *testing.T) {
+ return
+// err := Init(CurveFp254BNb)
+// if err != nil {
+// t.Error(err)
+// }
+ var a, b, ab Fr
+ a.SetString("12345678901", 10)
+ b.SetString("abcdef0abcd", 16)
+ FrMul(&ab, &a, &b)
+ var P, aP G1
+ var Q, bQ G2
+ P.HashAndMapTo([]byte("this"))
+ G1Mul(&aP, &P, &a)
+ Q.HashAndMapTo([]byte("that"))
+ G2Mul(&bQ, &Q, &b)
+ var e1, e2 GT
+ Pairing(&e1, &P, &Q)
+ Pairing(&e2, &aP, &bQ)
+ GTPow(&e2, &e2, &ab)
+ if !e1.IsEqual(&e2) {
+ t.Errorf("not equal pairing\n%s\n%s", e1.GetString(16), e2.GetString(16))
+ }
+}
func testPre(t *testing.T) {
t.Log("init")
@@ -312,6 +336,7 @@ func test(t *testing.T, c int) {
}
unitN = GetOpUnitSize()
t.Logf("unitN=%d\n", unitN)
+ testPairing(t)
testPre(t)
testRecoverSecretKey(t)
testAdd(t)
diff --git a/go/bls/mcl.go b/go/bls/mcl.go
new file mode 100644
index 0000000..c7a5ffd
--- /dev/null
+++ b/go/bls/mcl.go
@@ -0,0 +1,525 @@
+package bls
+
+/*
+#cgo CFLAGS:-D"MCLBN_FP_UNIT_SIZE=6"
+#include <mcl/bn.h>
+*/
+import "C"
+import "fmt"
+import "unsafe"
+
+// CurveFp254BNb -- 254 bit curve
+const CurveFp254BNb = C.mclBn_CurveFp254BNb
+
+// CurveFp382_1 -- 382 bit curve 1
+const CurveFp382_1 = C.mclBn_CurveFp382_1
+
+// CurveFp382_2 -- 382 bit curve 2
+const CurveFp382_2 = C.mclBn_CurveFp382_2
+
+// Init --
+// call this function before calling all the other operations
+// this function is not thread safe
+func mclInit(curve int) error {
+ err := C.mclBn_init(C.int(curve), C.MCLBN_FP_UNIT_SIZE)
+ if err != 0 {
+ return fmt.Errorf("ERR mclBn_init curve=%d", curve)
+ }
+ return nil
+}
+
+////////////////////////////////////////////////
+// Fr --
+type Fr struct {
+ v C.mclBnFr
+}
+
+// getPointer --
+func (x *Fr) getPointer() (p *C.mclBnFr) {
+ // #nosec
+ return (*C.mclBnFr)(unsafe.Pointer(x))
+}
+
+// Clear --
+func (x *Fr) Clear() {
+ // #nosec
+ C.mclBnFr_clear(x.getPointer())
+}
+
+// SetInt --
+func (x *Fr) SetInt(v int) {
+ // #nosec
+ C.mclBnFr_setInt(x.getPointer(), C.int(v))
+}
+
+// SetString --
+func (x *Fr) SetString(s string, base int) error {
+ buf := []byte(s)
+ // #nosec
+ err := C.mclBnFr_setStr(x.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(base))
+ if err != 0 {
+ return fmt.Errorf("err mclBnFr_setStr %x", err)
+ }
+ return nil
+}
+
+// Deserialize --
+func (x *Fr) Deserialize(buf []byte) error {
+ // #nosec
+ err := C.mclBnFr_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err mclBnFr_deserialize %x", buf)
+ }
+ return nil
+}
+
+// SetLittleEndian --
+func (x *Fr) SetLittleEndian(buf []byte) error {
+ // #nosec
+ err := C.mclBnFr_setLittleEndian(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err mclBnFr_setLittleEndian %x", err)
+ }
+ return nil
+}
+
+// IsEqual --
+func (x *Fr) IsEqual(rhs *Fr) bool {
+ return C.mclBnFr_isEqual(x.getPointer(), rhs.getPointer()) == 1
+}
+
+// IsZero --
+func (x *Fr) IsZero(rhs *Fr) bool {
+ return C.mclBnFr_isZero(x.getPointer()) == 1
+}
+
+// IsOne --
+func (x *Fr) IsOne(rhs *Fr) bool {
+ return C.mclBnFr_isOne(x.getPointer()) == 1
+}
+
+// SetByCSPRNG --
+func (x *Fr) SetByCSPRNG() error {
+ err := C.mclBnFr_setByCSPRNG(x.getPointer())
+ if err != 0 {
+ return fmt.Errorf("err mclBnFr_setByCSPRNG")
+ }
+ return nil
+}
+
+// SetHashOf --
+func (x *Fr) SetHashOf(buf []byte) bool {
+ return C.mclBnFr_setHashOf(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) == 1
+}
+
+// GetString --
+func (x *Fr) GetString(base int) string {
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.mclBnFr_getStr((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), x.getPointer(), C.int(base))
+ if n == 0 {
+ panic("err mclBnFr_getStr")
+ }
+ return string(buf[:n])
+}
+
+// Serialize --
+func (x *Fr) Serialize() []byte {
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.mclBnFr_serialize(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), x.getPointer())
+ if n == 0 {
+ panic("err mclBnFr_serialize")
+ }
+ return buf[:n]
+}
+
+// FrNeg --
+func FrNeg(out *Fr, x *Fr) {
+ C.mclBnFr_neg(out.getPointer(), x.getPointer())
+}
+
+// FrInv --
+func FrInv(out *Fr, x *Fr) {
+ C.mclBnFr_inv(out.getPointer(), x.getPointer())
+}
+
+// FrAdd --
+func FrAdd(out *Fr, x *Fr, y *Fr) {
+ C.mclBnFr_add(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// FrSub --
+func FrSub(out *Fr, x *Fr, y *Fr) {
+ C.mclBnFr_sub(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// FrMul --
+func FrMul(out *Fr, x *Fr, y *Fr) {
+ C.mclBnFr_mul(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// FrDiv --
+func FrDiv(out *Fr, x *Fr, y *Fr) {
+ C.mclBnFr_div(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+////////////////////////////////////////////
+// G1 --
+type G1 struct {
+ v C.mclBnG1
+}
+
+// getPointer --
+func (x *G1) getPointer() (p *C.mclBnG1) {
+ // #nosec
+ return (*C.mclBnG1)(unsafe.Pointer(x))
+}
+
+// Clear --
+func (x *G1) Clear() {
+ // #nosec
+ C.mclBnG1_clear(x.getPointer())
+}
+
+// SetString --
+func (x *G1) SetString(s string, base int) error {
+ buf := []byte(s)
+ // #nosec
+ err := C.mclBnG1_setStr(x.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(base))
+ if err != 0 {
+ return fmt.Errorf("err mclBnG1_setStr %x", err)
+ }
+ return nil
+}
+
+// Deserialize --
+func (x *G1) Deserialize(buf []byte) error {
+ // #nosec
+ err := C.mclBnG1_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err mclBnG1_deserialize %x", buf)
+ }
+ return nil
+}
+
+// IsEqual --
+func (x *G1) IsEqual(rhs *G1) bool {
+ return C.mclBnG1_isEqual(x.getPointer(), rhs.getPointer()) == 1
+}
+
+// IsEqual --
+func (x *G1) IsZero(rhs *G1) bool {
+ return C.mclBnG1_isZero(x.getPointer()) == 1
+}
+
+// HashAndMapTo --
+func (x *G1) HashAndMapTo(buf []byte) error {
+ // #nosec
+ err := C.mclBnG1_hashAndMapTo(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err mclBnG1_hashAndMapTo %x", err)
+ }
+ return nil
+}
+
+// GetString --
+func (x *G1) GetString(base int) string {
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.mclBnG1_getStr((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), x.getPointer(), C.int(base))
+ if n == 0 {
+ panic("err mclBnG1_getStr")
+ }
+ return string(buf[:n])
+}
+
+// Serialize --
+func (x *G1) Serialize() []byte {
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.mclBnG1_serialize(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), x.getPointer())
+ if n == 0 {
+ panic("err mclBnG1_serialize")
+ }
+ return buf[:n]
+}
+
+// G1Neg --
+func G1Neg(out *G1, x *G1) {
+ C.mclBnG1_neg(out.getPointer(), x.getPointer())
+}
+
+// G1Dbl --
+func G1Dbl(out *G1, x *G1) {
+ C.mclBnG1_dbl(out.getPointer(), x.getPointer())
+}
+
+// G1Add --
+func G1Add(out *G1, x *G1, y *G1) {
+ C.mclBnG1_add(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// G1Sub --
+func G1Sub(out *G1, x *G1, y *G1) {
+ C.mclBnG1_sub(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// G1Mul --
+func G1Mul(out *G1, x *G1, y *Fr) {
+ C.mclBnG1_mul(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+////////////////////////////////////////////
+// G2 --
+type G2 struct {
+ v C.mclBnG1
+}
+
+// getPointer --
+func (x *G2) getPointer() (p *C.mclBnG2) {
+ // #nosec
+ return (*C.mclBnG2)(unsafe.Pointer(x))
+}
+
+// Clear --
+func (x *G2) Clear() {
+ // #nosec
+ C.mclBnG2_clear(x.getPointer())
+}
+
+// SetString --
+func (x *G2) SetString(s string, base int) error {
+ buf := []byte(s)
+ // #nosec
+ err := C.mclBnG2_setStr(x.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(base))
+ if err != 0 {
+ return fmt.Errorf("err mclBnG2_setStr %x", err)
+ }
+ return nil
+}
+
+// Deserialize --
+func (x *G2) Deserialize(buf []byte) error {
+ // #nosec
+ err := C.mclBnG2_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err mclBnG2_deserialize %x", buf)
+ }
+ return nil
+}
+
+// IsEqual --
+func (x *G2) IsEqual(rhs *G2) bool {
+ return C.mclBnG2_isEqual(x.getPointer(), rhs.getPointer()) == 1
+}
+
+// IsEqual --
+func (x *G2) IsZero(rhs *G2) bool {
+ return C.mclBnG2_isZero(x.getPointer()) == 1
+}
+
+// HashAndMapTo --
+func (x *G2) HashAndMapTo(buf []byte) error {
+ // #nosec
+ err := C.mclBnG2_hashAndMapTo(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err mclBnG2_hashAndMapTo %x", err)
+ }
+ return nil
+}
+
+// GetString --
+func (x *G2) GetString(base int) string {
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.mclBnG2_getStr((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), x.getPointer(), C.int(base))
+ if n == 0 {
+ panic("err mclBnG2_getStr")
+ }
+ return string(buf[:n])
+}
+
+// Serialize --
+func (x *G2) Serialize() []byte {
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.mclBnG2_serialize(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), x.getPointer())
+ if n == 0 {
+ panic("err mclBnG2_serialize")
+ }
+ return buf[:n]
+}
+
+// G2Neg --
+func G2Neg(out *G2, x *G2) {
+ C.mclBnG2_neg(out.getPointer(), x.getPointer())
+}
+
+// G2Dbl --
+func G2Dbl(out *G2, x *G2) {
+ C.mclBnG2_dbl(out.getPointer(), x.getPointer())
+}
+
+// G1Add --
+func G2Add(out *G2, x *G2, y *G2) {
+ C.mclBnG2_add(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// G2Sub --
+func G2Sub(out *G2, x *G2, y *G2) {
+ C.mclBnG2_sub(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// G2Mul --
+func G2Mul(out *G2, x *G2, y *Fr) {
+ C.mclBnG2_mul(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+///////////////////////////////////////////////////////
+// GT --
+type GT struct {
+ v C.mclBnGT
+}
+
+// getPointer --
+func (x *GT) getPointer() (p *C.mclBnGT) {
+ // #nosec
+ return (*C.mclBnGT)(unsafe.Pointer(x))
+}
+
+// Clear --
+func (x *GT) Clear() {
+ // #nosec
+ C.mclBnGT_clear(x.getPointer())
+}
+
+// SetString --
+func (x *GT) SetString(s string, base int) error {
+ buf := []byte(s)
+ // #nosec
+ err := C.mclBnGT_setStr(x.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(base))
+ if err != 0 {
+ return fmt.Errorf("err mclBnGT_setStr %x", err)
+ }
+ return nil
+}
+
+// Deserialize --
+func (x *GT) Deserialize(buf []byte) error {
+ // #nosec
+ err := C.mclBnGT_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err mclBnGT_deserialize %x", buf)
+ }
+ return nil
+}
+
+// IsEqual --
+func (x *GT) IsEqual(rhs *GT) bool {
+ return C.mclBnGT_isEqual(x.getPointer(), rhs.getPointer()) == 1
+}
+
+// IsZero --
+func (x *GT) IsZero(rhs *GT) bool {
+ return C.mclBnGT_isZero(x.getPointer()) == 1
+}
+
+// IsOne --
+func (x *GT) IsOne(rhs *GT) bool {
+ return C.mclBnGT_isOne(x.getPointer()) == 1
+}
+
+// GetString --
+func (x *GT) GetString(base int) string {
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.mclBnGT_getStr((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), x.getPointer(), C.int(base))
+ if n == 0 {
+ panic("err mclBnGT_getStr")
+ }
+ return string(buf[:n])
+}
+
+// Serialize --
+func (x *GT) Serialize() []byte {
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.mclBnGT_serialize(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), x.getPointer())
+ if n == 0 {
+ panic("err mclBnGT_serialize")
+ }
+ return buf[:n]
+}
+
+// GTNeg --
+func GTNeg(out *GT, x *GT) {
+ C.mclBnGT_neg(out.getPointer(), x.getPointer())
+}
+
+// GTInv --
+func GTInv(out *GT, x *GT) {
+ C.mclBnGT_inv(out.getPointer(), x.getPointer())
+}
+
+// GTAdd --
+func GTAdd(out *GT, x *GT, y *GT) {
+ C.mclBnGT_add(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// GTSub --
+func GTSub(out *GT, x *GT, y *GT) {
+ C.mclBnGT_sub(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// GTMul --
+func GTMul(out *GT, x *GT, y *GT) {
+ C.mclBnGT_mul(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// GTDiv --
+func GTDiv(out *GT, x *GT, y *GT) {
+ C.mclBnGT_div(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// GTPow --
+func GTPow(out *GT, x *GT, y *Fr) {
+ C.mclBnGT_pow(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+//////////////////////
+// Pairing --
+func Pairing(out *GT, x *G1, y *G2) {
+ C.mclBn_pairing(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// FinalExp --
+func FinalExp(out *GT, x *G1, y *G2) {
+ C.mclBn_pairing(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// MillerLoop --
+func MillerLoop(out *GT, x *G1, y *G2) {
+ C.mclBn_pairing(out.getPointer(), x.getPointer(), y.getPointer())
+}
+
+// GetUint64NumToPrecompute --
+func GetUint64NumToPrecompute() int {
+ return int(C.mclBn_getUint64NumToPrecompute())
+}
+
+// PrecomputeG2 --
+func PrecomputeG2(Qbuf []uint64, Q *G2) {
+ C.mclBn_precomputeG2((*C.uint64_t)(unsafe.Pointer(&Qbuf[0])), Q.getPointer())
+}
+
+// PrecomputedMillerLoop --
+func PrecomputedMillerLoop(out *GT, P *G1, Qbuf []uint64) {
+ C.mclBn_precomputedMillerLoop(out.getPointer(), P.getPointer(), (*C.uint64_t)(unsafe.Pointer(&Qbuf[0])))
+}
+
+// PrecomputedMillerLoop2 --
+func PrecomputedMillerLoop2(out *GT, P1 *G1, Q1buf []uint64, P2 *G1, Q2buf []uint64) {
+ C.mclBn_precomputedMillerLoop2(out.getPointer(), P1.getPointer(), (*C.uint64_t)(unsafe.Pointer(&Q1buf[0])), P1.getPointer(), (*C.uint64_t)(unsafe.Pointer(&Q1buf[0])))
+}
+
diff --git a/src/bls_c.cpp b/src/bls_c.cpp
index 959d788..13f3a63 100644
--- a/src/bls_c.cpp
+++ b/src/bls_c.cpp
@@ -1,21 +1,10 @@
+#include "../mcl/src/bn_c_impl.hpp"
#include "bls/bls.hpp"
#define BLS_DLL_EXPORT
#include "bls/bls.h"
#include <iostream>
#include <sstream>
#include <memory.h>
-#include <mcl/fp.hpp>
-
-template<class Inner, class Outer>
-int setStrT(Outer *p, const char *buf, size_t bufSize, int ioMode)
- try
-{
- ((Inner*)p)->setStr(std::string(buf, bufSize), ioMode);
- return 0;
-} catch (std::exception& e) {
- fprintf(stderr, "err setStrT %s\n", e.what());
- return -1;
-}
size_t checkAndCopy(char *buf, size_t maxBufSize, const std::string& s)
{
@@ -26,32 +15,12 @@ size_t checkAndCopy(char *buf, size_t maxBufSize, const std::string& s)
buf[s.size()] = '\0';
return s.size();
}
-template<class Inner, class Outer>
-size_t getStrT(const Outer *p, char *buf, size_t maxBufSize, int ioMode)
- try
-{
- std::string s;
- ((const Inner*)p)->getStr(s, ioMode);
- size_t terminate = 0;
- if (ioMode == 0 || ioMode == bls::IoBin || ioMode == bls::IoDec || ioMode == bls::IoHex) {
- terminate = 1; // for '\0'
- }
- if (s.size() > maxBufSize + terminate) {
- return 0;
- }
- memcpy(buf, s.c_str(), s.size());
- if (terminate) {
- buf[s.size()] = '\0';
- }
- return s.size();
-} catch (std::exception&) {
- return 0;
-}
int blsInit(int curve, int maxUnitSize)
try
{
- bls::init(curve, maxUnitSize);
+ mclBn_init(curve, maxUnitSize);
+ bls::init(curve, maxUnitSize); // QQQ
return 0;
} catch (std::exception&) {
return -1;
@@ -83,81 +52,69 @@ int blsGetFieldOrder(char *buf, size_t maxBufSize)
int blsIdIsEqual(const blsId *lhs, const blsId *rhs)
{
- return *(const bls::Id*)lhs == *(const bls::Id*)rhs ? 1 : 0;
+ return mclBnFr_isEqual(&lhs->v, &rhs->v);
}
int blsIdSetLittleEndian(blsId *id, const void *buf, size_t bufSize)
{
- ((bls::Id*)id)->setLittleEndian(buf, bufSize);
- return 0;
+ return mclBnFr_setLittleEndian(&id->v, buf, bufSize);
}
int blsIdSetDecStr(blsId *id, const char *buf, size_t bufSize)
{
- return setStrT<bls::Id, blsId>(id, buf, bufSize, 10);
+ return mclBnFr_setStr(&id->v, buf, bufSize, 10);
}
int blsIdSetHexStr(blsId *id, const char *buf, size_t bufSize)
{
- return setStrT<bls::Id, blsId>(id, buf, bufSize, 16);
+ return mclBnFr_setStr(&id->v, buf, bufSize, 16);
}
size_t blsIdGetLittleEndian(void *buf, size_t maxBufSize, const blsId *id)
{
- return getStrT<bls::Id, blsId>(id, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
+ return mclBnFr_serialize(buf, maxBufSize, &id->v);
}
size_t blsIdGetDecStr(char *buf, size_t maxBufSize, const blsId *id)
{
- return getStrT<bls::Id, blsId>(id, buf, maxBufSize, 10);
+ return mclBnFr_getStr(buf, maxBufSize, &id->v, 10);
}
size_t blsIdGetHexStr(char *buf, size_t maxBufSize, const blsId *id)
{
- return getStrT<bls::Id, blsId>(id, buf, maxBufSize, 16);
+ return mclBnFr_getStr(buf, maxBufSize, &id->v, 16);
}
int blsSecretKeyIsEqual(const blsSecretKey *lhs, const blsSecretKey *rhs)
{
- return *(const bls::SecretKey*)lhs == *(const bls::SecretKey*)rhs ? 1 : 0;
+ return mclBnFr_isEqual(&lhs->v, &rhs->v);
}
int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, size_t bufSize)
{
- ((bls::SecretKey*)sec)->setLittleEndian(buf, bufSize);
- return 0;
+ return mclBnFr_setLittleEndian(&sec->v, buf, bufSize);
}
int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, size_t bufSize)
{
- return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, 10);
+ return mclBnFr_setStr(&sec->v, buf, bufSize, 10);
}
int blsSecretKeySetHexStr(blsSecretKey *sec, const char *buf, size_t bufSize)
{
- return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, 16);
+ return mclBnFr_setStr(&sec->v, buf, bufSize, 16);
}
size_t blsSecretKeyGetLittleEndian(void *buf, size_t maxBufSize, const blsSecretKey *sec)
{
- return getStrT<bls::SecretKey, blsSecretKey>(sec, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
+ return mclBnFr_serialize(buf, maxBufSize, &sec->v);
}
size_t blsSecretKeyGetDecStr(char *buf, size_t maxBufSize, const blsSecretKey *sec)
{
- return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, 10);
+ return mclBnFr_getStr(buf, maxBufSize, &sec->v, 10);
}
size_t blsSecretKeyGetHexStr(char *buf, size_t maxBufSize, const blsSecretKey *sec)
{
- return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, 16);
+ return mclBnFr_getStr(buf, maxBufSize, &sec->v, 16);
}
int blsHashToSecretKey(blsSecretKey *sec, const void *buf, size_t bufSize)
- try
{
- ((bls::SecretKey*)sec)->setHashOf(buf, bufSize);
- return 0;
-} catch (std::exception& e) {
- fprintf(stderr, "err blsHashToSecretKey %s\n", e.what());
- return -1;
+ return mclBnFr_setHashOf(&sec->v, buf, bufSize);
}
int blsSecretKeySetByCSPRNG(blsSecretKey *sec)
- try
{
- ((bls::SecretKey*)sec)->init();
- return 0;
-} catch (std::exception& e) {
- fprintf(stderr, "err blsSecretKeySetByCSPRNG %s\n", e.what());
- return -1;
+ return mclBnFr_setByCSPRNG(&sec->v);
}
void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs)
{
@@ -199,40 +156,23 @@ void blsGetPop(blsSignature *sig, const blsSecretKey *sec)
int blsPublicKeyIsEqual(const blsPublicKey *lhs, const blsPublicKey *rhs)
{
- return *(const bls::PublicKey*)lhs == *(const bls::PublicKey*)rhs ? 1 : 0;
+ return mclBnG2_isEqual(&lhs->v, &rhs->v);
}
int blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, size_t bufSize)
{
- return setStrT<bls::PublicKey, blsPublicKey>(pub, (const char*)buf, bufSize, bls::IoFixedByteSeq);
+ return mclBnG2_deserialize(&pub->v, buf, bufSize);
}
size_t blsPublicKeySerialize(void *buf, size_t maxBufSize, const blsPublicKey *pub)
{
- return getStrT<bls::PublicKey, blsPublicKey>(pub, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
+ return mclBnG2_serialize(buf, maxBufSize, &pub->v);
}
int blsPublicKeySetHexStr(blsPublicKey *pub, const char *buf, size_t bufSize)
- try
{
- std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize);
- return blsPublicKeyDeserialize(pub, s.c_str(), s.size());
-} catch (std::exception& e) {
- fprintf(stderr, "err blsPublicKeySetHexStr %s\n", e.what());
- return -1;
+ return mclBnG2_setStr(&pub->v, buf, bufSize, 16);
}
size_t blsPublicKeyGetHexStr(char *buf, size_t maxBufSize, const blsPublicKey *pub)
{
- std::string s;
- s.resize(1024);
- size_t len = blsPublicKeySerialize(&s[0], s.size(), pub);
- if (len > 0) {
- s.resize(len);
- s = mcl::fp::littleEndianToHexStr(s.c_str(), s.size());
- if (s.size() < maxBufSize) {
- memcpy(buf, s.c_str(), s.size());
- buf[s.size()] = '\0';
- return s.size();
- }
- }
- return 0;
+ return mclBnG2_getStr(buf, maxBufSize, &pub->v, 16);
}
void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs)
{
@@ -259,40 +199,23 @@ int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const bls
int blsSignatureIsEqual(const blsSignature *lhs, const blsSignature *rhs)
{
- return *(const bls::Signature*)lhs == *(const bls::Signature*)rhs ? 1 : 0;
+ return mclBnG1_isEqual(&lhs->v, &rhs->v);
}
int blsSignatureDeserialize(blsSignature *sig, const void *buf, size_t bufSize)
{
- return setStrT<bls::Signature, blsSignature>(sig, (const char *)buf, bufSize, bls::IoFixedByteSeq);
+ return mclBnG1_deserialize(&sig->v, buf, bufSize);
}
int blsSignatureSetHexStr(blsSignature *sig, const char *buf, size_t bufSize)
- try
{
- std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize);
- return blsSignatureDeserialize(sig, s.c_str(), s.size());
-} catch (std::exception& e) {
- fprintf(stderr, "err blsSignatureSetHexStr %s\n", e.what());
- return -1;
+ return mclBnG1_setStr(&sig->v, buf, bufSize, 16);
}
size_t blsSignatureGetHexStr(char *buf, size_t maxBufSize, const blsSignature *sig)
{
- std::string s;
- s.resize(1024);
- size_t len = blsSignatureSerialize(&s[0], s.size(), sig);
- if (len > 0) {
- s.resize(len);
- s = mcl::fp::littleEndianToHexStr(s.c_str(), s.size());
- if (s.size() < maxBufSize) {
- memcpy(buf, s.c_str(), s.size());
- buf[s.size()] = '\0';
- return s.size();
- }
- }
- return 0;
+ return mclBnG1_getStr(buf, maxBufSize, &sig->v, 16);
}
size_t blsSignatureSerialize(void *buf, size_t maxBufSize, const blsSignature *sig)
{
- return getStrT<bls::Signature, blsSignature>(sig, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
+ return mclBnG1_serialize(buf, maxBufSize, &sig->v);
}
void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs)
{