test_go runs on Linux/OSX

dev
MITSUNARI Shigeo 7 years ago
parent f4e167ed1e
commit 77257b3f91
  1. 1
      .travis.yml
  2. 23
      Makefile
  3. 150
      ffi/go/mcl/mcl.go
  4. 117
      ffi/go/mcl/mcl_test.go

@ -12,4 +12,5 @@ script:
- git clone https://github.com/herumi/mcl.git
- cd mcl
- make test DEBUG=1
- make test_go

@ -26,11 +26,14 @@ endif
SHARE_BASENAME_SUF?=_dy
##################################################################
MCL_LIB=$(LIB_DIR)/libmcl.a
MCL_SLIB=$(LIB_DIR)/libmcl$(SHARE_BASENAME_SUF).$(LIB_SUF)
MCL_SNAME=mcl$(SHARE_BASENAME_SUF)
BN256_SNAME=mclbn256$(SHARE_BASENAME_SUF)
BN384_SNAME=mclbn384$(SHARE_BASENAME_SUF)
MCL_SLIB=$(LIB_DIR)/lib$(MCL_SNAME).$(LIB_SUF)
BN256_LIB=$(LIB_DIR)/libmclbn256.a
BN256_SLIB=$(LIB_DIR)/libmclbn256$(SHARE_BASENAME_SUF).$(LIB_SUF)
BN256_SLIB=$(LIB_DIR)/lib$(BN256_SNAME).$(LIB_SUF)
BN384_LIB=$(LIB_DIR)/libmclbn384.a
BN384_SLIB=$(LIB_DIR)/libmclbn384$(SHARE_BASENAME_SUF).$(LIB_SUF)
BN384_SLIB=$(LIB_DIR)/lib$(BN384_SNAME).$(LIB_SUF)
all: $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB)
#LLVM_VER=-3.8
@ -93,14 +96,17 @@ $(MCL_SLIB): $(LIB_OBJ)
$(BN256_LIB): $(BN256_OBJ)
$(AR) $@ $(BN256_OBJ)
ifeq ($(OS),mac)
MAC_LDFLAGS+=-l$(MCL_SNAME) -L./lib
endif
$(BN256_SLIB): $(BN256_OBJ) $(MCL_SLIB)
$(PRE)$(CXX) -o $@ $(BN256_OBJ) -shared $(LDFLAGS) $(MCL_SLIB)
$(PRE)$(CXX) -o $@ $(BN256_OBJ) -shared $(LDFLAGS) $(MAC_LDFLAGS)
$(BN384_LIB): $(BN384_OBJ)
$(AR) $@ $(BN384_OBJ)
$(BN384_SLIB): $(BN384_OBJ) $(MCL_SLIB)
$(PRE)$(CXX) -o $@ $(BN384_OBJ) -shared $(LDFLAGS) $(MCL_SLIB)
$(PRE)$(CXX) -o $@ $(BN384_OBJ) -shared $(LDFLAGS) $(MAC_LDFLAGS)
$(ASM_OBJ): $(ASM_SRC)
$(PRE)$(CXX) -c $< -o $@ $(CFLAGS)
@ -137,11 +143,14 @@ asm: $(LLVM_SRC)
$(LOW_ASM_OBJ): $(LOW_ASM_SRC)
$(ASM) $<
ifeq ($(OS),mac)
MAC_GO_LDFLAGS="-ldflags=-s"
endif
test_go256: $(MCL_SLIB) $(BN256_SLIB)
cd ffi/go/mcl && env CGO_CFLAGS="-I../../../include -DMCLBN_FP_UNIT_SIZE=4" CGO_LDFLAGS="-L../../../lib" LD_LIBRARY_PATH=../../../lib go test -tags bn256 .
cd ffi/go/mcl && env LD_RUN_PATH="../../../lib" CGO_CFLAGS="-I../../../include" CGO_LDFLAGS="-L../../../lib -l$(BN256_SNAME) -l$(MCL_SNAME) -lgmpxx -lgmp -lcrypto -lstdc++" go test $(MAC_GO_LDFLAGS) -tags bn256 .
test_go384: $(MCL_SLIB) $(BN384_SLIB)
cd ffi/go/mcl && env CGO_CFLAGS="-I../../../include -DMCLBN_FP_UNIT_SIZE=6" CGO_LDFLAGS="-L../../../lib" LD_LIBRARY_PATH=../../../lib go test -tags bn384 .
cd ffi/go/mcl && env LD_RUN_PATH="../../../lib" CGO_CFLAGS="-I../../../include" CGO_LDFLAGS="-L../../../lib -l$(BN384_SNAME) -l$(MCL_SNAME) -lgmpxx -lgmp -lcrypto -lstdc++" go test $(MAC_GO_LDFLAGS) .
test_go:
$(MAKE) test_go256

@ -1,8 +1,8 @@
package mcl
/*
#cgo bn256 LDFLAGS:-lmclbn256_dy -lmcl_dy -lgmpxx -lstdc++ -lgmp -lcrypto
#cgo bn384 LDFLAGS:-lmclbn384_dy -lmcl_dy -lgmpxx -lstdc++ -lgmp -lcrypto
#cgo CFLAGS:-DMCLBN_FP_UNIT_SIZE=6
#cgo bn256 CFLAGS:-UMCLBN_FP_UNIT_SIZE -DMCLBN_FP_UNIT_SIZE=4
#include <mcl/bn.h>
*/
import "C"
@ -29,7 +29,41 @@ func Init(curve int) error {
return nil
}
////////////////////////////////////////////////
// GetMaxOpUnitSize --
func GetMaxOpUnitSize() int {
return int(C.MCLBN_FP_UNIT_SIZE)
}
// GetOpUnitSize --
// the length of Fr is GetOpUnitSize() * 8 bytes
func GetOpUnitSize() int {
return int(C.mclBn_getOpUnitSize())
}
// GetCurveOrder --
// return the order of G1
func GetCurveOrder() string {
buf := make([]byte, 1024)
// #nosec
n := C.mclBn_getCurveOrder((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
if n == 0 {
panic("implementation err. size of buf is small")
}
return string(buf[:n])
}
// GetFieldOrder --
// return the characteristic of the field where a curve is defined
func GetFieldOrder() string {
buf := make([]byte, 1024)
// #nosec
n := C.mclBn_getFieldOrder((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
if n == 0 {
panic("implementation err. size of buf is small")
}
return string(buf[:n])
}
// Fr --
type Fr struct {
v C.mclBnFr
@ -90,26 +124,26 @@ func (x *Fr) IsEqual(rhs *Fr) bool {
}
// IsZero --
func (x *Fr) IsZero(rhs *Fr) bool {
func (x *Fr) IsZero() bool {
return C.mclBnFr_isZero(x.getPointer()) == 1
}
// IsOne --
func (x *Fr) IsOne(rhs *Fr) bool {
func (x *Fr) IsOne() bool {
return C.mclBnFr_isOne(x.getPointer()) == 1
}
// SetByCSPRNG --
func (x *Fr) SetByCSPRNG() error {
func (x *Fr) SetByCSPRNG() {
err := C.mclBnFr_setByCSPRNG(x.getPointer())
if err != 0 {
return fmt.Errorf("err mclBnFr_setByCSPRNG")
panic("err mclBnFr_setByCSPRNG")
}
return nil
}
// SetHashOf --
func (x *Fr) SetHashOf(buf []byte) bool {
// #nosec
return C.mclBnFr_setHashOf(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) == 1
}
@ -165,7 +199,6 @@ func FrDiv(out *Fr, x *Fr, y *Fr) {
C.mclBnFr_div(out.getPointer(), x.getPointer(), y.getPointer())
}
////////////////////////////////////////////
// G1 --
type G1 struct {
v C.mclBnG1
@ -209,8 +242,8 @@ func (x *G1) IsEqual(rhs *G1) bool {
return C.mclBnG1_isEqual(x.getPointer(), rhs.getPointer()) == 1
}
// IsEqual --
func (x *G1) IsZero(rhs *G1) bool {
// IsZero --
func (x *G1) IsZero() bool {
return C.mclBnG1_isZero(x.getPointer()) == 1
}
@ -271,7 +304,11 @@ func G1Mul(out *G1, x *G1, y *Fr) {
C.mclBnG1_mul(out.getPointer(), x.getPointer(), y.getPointer())
}
////////////////////////////////////////////
// G1MulCT -- constant time (depending on bit lengh of y)
func G1MulCT(out *G1, x *G1, y *Fr) {
C.mclBnG1_mulCT(out.getPointer(), x.getPointer(), y.getPointer())
}
// G2 --
type G2 struct {
v C.mclBnG2
@ -315,8 +352,8 @@ func (x *G2) IsEqual(rhs *G2) bool {
return C.mclBnG2_isEqual(x.getPointer(), rhs.getPointer()) == 1
}
// IsEqual --
func (x *G2) IsZero(rhs *G2) bool {
// IsZero --
func (x *G2) IsZero() bool {
return C.mclBnG2_isZero(x.getPointer()) == 1
}
@ -362,7 +399,7 @@ func G2Dbl(out *G2, x *G2) {
C.mclBnG2_dbl(out.getPointer(), x.getPointer())
}
// G1Add --
// G2Add --
func G2Add(out *G2, x *G2, y *G2) {
C.mclBnG2_add(out.getPointer(), x.getPointer(), y.getPointer())
}
@ -377,7 +414,6 @@ func G2Mul(out *G2, x *G2, y *Fr) {
C.mclBnG2_mul(out.getPointer(), x.getPointer(), y.getPointer())
}
///////////////////////////////////////////////////////
// GT --
type GT struct {
v C.mclBnGT
@ -395,6 +431,12 @@ func (x *GT) Clear() {
C.mclBnGT_clear(x.getPointer())
}
// SetInt64 --
func (x *GT) SetInt64(v int64) {
// #nosec
C.mclBnGT_setInt(x.getPointer(), C.int64_t(v))
}
// SetString --
func (x *GT) SetString(s string, base int) error {
buf := []byte(s)
@ -422,12 +464,12 @@ func (x *GT) IsEqual(rhs *GT) bool {
}
// IsZero --
func (x *GT) IsZero(rhs *GT) bool {
func (x *GT) IsZero() bool {
return C.mclBnGT_isZero(x.getPointer()) == 1
}
// IsOne --
func (x *GT) IsOne(rhs *GT) bool {
func (x *GT) IsOne() bool {
return C.mclBnGT_isOne(x.getPointer()) == 1
}
@ -488,7 +530,6 @@ 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())
@ -511,16 +552,87 @@ func GetUint64NumToPrecompute() int {
// PrecomputeG2 --
func PrecomputeG2(Qbuf []uint64, Q *G2) {
// #nosec
C.mclBn_precomputeG2((*C.uint64_t)(unsafe.Pointer(&Qbuf[0])), Q.getPointer())
}
// PrecomputedMillerLoop --
func PrecomputedMillerLoop(out *GT, P *G1, Qbuf []uint64) {
// #nosec
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) {
// #nosec
C.mclBn_precomputedMillerLoop2(out.getPointer(), P1.getPointer(), (*C.uint64_t)(unsafe.Pointer(&Q1buf[0])), P1.getPointer(), (*C.uint64_t)(unsafe.Pointer(&Q1buf[0])))
}
// FrEvaluatePolynomial -- y = c[0] + c[1] * x + c[2] * x^2 + ...
func FrEvaluatePolynomial(y *Fr, c []Fr, x *Fr) error {
// #nosec
err := C.mclBn_FrEvaluatePolynomial(y.getPointer(), (*C.mclBnFr)(unsafe.Pointer(&c[0])), (C.size_t)(len(c)), x.getPointer())
if err != 0 {
return fmt.Errorf("err mclBn_FrEvaluatePolynomial")
}
return nil
}
// G1EvaluatePolynomial -- y = c[0] + c[1] * x + c[2] * x^2 + ...
func G1EvaluatePolynomial(y *G1, c []G1, x *Fr) error {
// #nosec
err := C.mclBn_G1EvaluatePolynomial(y.getPointer(), (*C.mclBnG1)(unsafe.Pointer(&c[0])), (C.size_t)(len(c)), x.getPointer())
if err != 0 {
return fmt.Errorf("err mclBn_G1EvaluatePolynomial")
}
return nil
}
// G2EvaluatePolynomial -- y = c[0] + c[1] * x + c[2] * x^2 + ...
func G2EvaluatePolynomial(y *G2, c []G2, x *Fr) error {
// #nosec
err := C.mclBn_G2EvaluatePolynomial(y.getPointer(), (*C.mclBnG2)(unsafe.Pointer(&c[0])), (C.size_t)(len(c)), x.getPointer())
if err != 0 {
return fmt.Errorf("err mclBn_G2EvaluatePolynomial")
}
return nil
}
// FrLagrangeInterpolation --
func FrLagrangeInterpolation(out *Fr, xVec []Fr, yVec []Fr) error {
if len(xVec) != len(yVec) {
return fmt.Errorf("err FrLagrangeInterpolation:bad size")
}
// #nosec
err := C.mclBn_FrLagrangeInterpolation(out.getPointer(), (*C.mclBnFr)(unsafe.Pointer(&xVec[0])), (*C.mclBnFr)(unsafe.Pointer(&yVec[0])), (C.size_t)(len(xVec)))
if err != 0 {
return fmt.Errorf("err FrLagrangeInterpolation")
}
return nil
}
// G1LagrangeInterpolation --
func G1LagrangeInterpolation(out *G1, xVec []Fr, yVec []G1) error {
if len(xVec) != len(yVec) {
return fmt.Errorf("err G1LagrangeInterpolation:bad size")
}
// #nosec
err := C.mclBn_G1LagrangeInterpolation(out.getPointer(), (*C.mclBnFr)(unsafe.Pointer(&xVec[0])), (*C.mclBnG1)(unsafe.Pointer(&yVec[0])), (C.size_t)(len(xVec)))
if err != 0 {
return fmt.Errorf("err G1LagrangeInterpolation")
}
return nil
}
// G2LagrangeInterpolation --
func G2LagrangeInterpolation(out *G2, xVec []Fr, yVec []G2) error {
if len(xVec) != len(yVec) {
return fmt.Errorf("err G2LagrangeInterpolation:bad size")
}
// #nosec
err := C.mclBn_G2LagrangeInterpolation(out.getPointer(), (*C.mclBnFr)(unsafe.Pointer(&xVec[0])), (*C.mclBnG2)(unsafe.Pointer(&yVec[0])), (C.size_t)(len(xVec)))
if err != 0 {
return fmt.Errorf("err G2LagrangeInterpolation")
}
return nil
}

@ -1,27 +1,130 @@
package mcl
import "testing"
import "fmt"
func TestPairing(t *testing.T) {
err := Init(CurveFp254BNb)
func testBadPointOfG2(t *testing.T) {
var Q G2
// this value is not in G2 so should return an error
err := Q.SetString("1 18d3d8c085a5a5e7553c3a4eb628e88b8465bf4de2612e35a0a4eb018fb0c82e9698896031e62fd7633ffd824a859474 1dc6edfcf33e29575d4791faed8e7203832217423bf7f7fbf1f6b36625b12e7132c15fbc15562ce93362a322fb83dd0d 65836963b1f7b6959030ddfa15ab38ce056097e91dedffd996c1808624fa7e2644a77be606290aa555cda8481cfb3cb 1b77b708d3d4f65aeedf54b58393463a42f0dc5856baadb5ce608036baeca398c5d9e6b169473a8838098fd72fd28b50", 16)
if err == nil {
t.Error(err)
}
}
func testGT(t *testing.T) {
var x GT
x.Clear()
if !x.IsZero() {
t.Errorf("not zero")
}
x.SetInt64(1)
if !x.IsOne() {
t.Errorf("not one")
}
}
func testNegAdd(t *testing.T) {
var x Fr
var P1, P2, P3 G1
var Q1, Q2, Q3 G2
err := P1.HashAndMapTo([]byte("this"))
if err != nil {
t.Error(err)
}
err = Q1.HashAndMapTo([]byte("this"))
if err != nil {
t.Error(err)
}
fmt.Printf("P1=%s\n", P1.GetString(16))
fmt.Printf("Q1=%s\n", Q1.GetString(16))
G1Neg(&P2, &P1)
G2Neg(&Q2, &Q1)
fmt.Printf("P2=%s\n", P2.GetString(16))
fmt.Printf("Q2=%s\n", Q2.GetString(16))
x.SetInt64(-1)
G1Mul(&P3, &P1, &x)
G2Mul(&Q3, &Q1, &x)
if !P2.IsEqual(&P3) {
t.Errorf("P2 != P3 %s\n", P3.GetString(16))
}
if !Q2.IsEqual(&Q3) {
t.Errorf("Q2 != Q3 %s\n", Q3.GetString(16))
}
G1Add(&P2, &P2, &P1)
G2Add(&Q2, &Q2, &Q1)
if !P2.IsZero() {
t.Errorf("P2 is not zero %s\n", P2.GetString(16))
}
if !Q2.IsZero() {
t.Errorf("Q2 is not zero %s\n", Q2.GetString(16))
}
}
func testPairing(t *testing.T) {
var a, b, ab Fr
a.SetString("123", 10)
b.SetString("456", 10)
err := a.SetString("123", 10)
if err != nil {
t.Error(err)
return
}
err = b.SetString("456", 10)
if err != nil {
t.Error(err)
return
}
FrMul(&ab, &a, &b)
var P, aP G1
var Q, bQ G2
P.HashAndMapTo([]byte("this"))
err = P.HashAndMapTo([]byte("this"))
if err != nil {
t.Error(err)
return
}
fmt.Printf("P=%s\n", P.GetString(16))
G1Mul(&aP, &P, &a)
Q.HashAndMapTo([]byte("that"))
fmt.Printf("aP=%s\n", aP.GetString(16))
err = Q.HashAndMapTo([]byte("that"))
if err != nil {
t.Error(err)
return
}
fmt.Printf("Q=%s\n", Q.GetString(16))
G2Mul(&bQ, &Q, &b)
fmt.Printf("bQ=%s\n", bQ.GetString(16))
var e1, e2 GT
Pairing(&e1, &P, &Q)
fmt.Printf("e1=%s\n", e1.GetString(16))
Pairing(&e2, &aP, &bQ)
fmt.Printf("e2=%s\n", e1.GetString(16))
GTPow(&e1, &e1, &ab)
fmt.Printf("e1=%s\n", e1.GetString(16))
if !e1.IsEqual(&e2) {
t.Error("not equal pairing")
t.Errorf("not equal pairing\n%s\n%s", e1.GetString(16), e2.GetString(16))
}
}
func testMcl(t *testing.T, c int) {
err := Init(c)
if err != nil {
t.Fatal(err)
}
testNegAdd(t)
testPairing(t)
testGT(t)
testBadPointOfG2(t)
}
func TestMclMain(t *testing.T) {
t.Logf("GetMaxOpUnitSize() = %d\n", GetMaxOpUnitSize())
t.Log("CurveFp254BNb")
testMcl(t, CurveFp254BNb)
if GetMaxOpUnitSize() == 6 {
t.Log("CurveFp382_1")
testMcl(t, CurveFp382_1)
t.Log("CurveFp382_2")
testMcl(t, CurveFp382_2)
}
}

Loading…
Cancel
Save