diff --git a/ffi/go/mcl/mcl.go b/ffi/go/mcl/mcl.go index 97f0c7d..1dd9b84 100644 --- a/ffi/go/mcl/mcl.go +++ b/ffi/go/mcl/mcl.go @@ -31,6 +31,9 @@ const IO_EC_AFFINE = C.MCLBN_IO_EC_AFFINE // IO_EC_PROJ -- const IO_EC_PROJ = C.MCLBN_IO_EC_PROJ +// IRTF -- for SetMapToMode +const IRTF = 5 /* MCL_MAP_TO_MODE_HASH_TO_CURVE_07 */ + // GetFrUnitSize -- func GetFrUnitSize() int { return int(C.MCLBN_FR_UNIT_SIZE) @@ -170,8 +173,8 @@ func (x *Fr) SetString(s string, base int) error { // 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 { + n := C.mclBnFr_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) + if n == 0 || int(n) != len(buf) { return fmt.Errorf("err mclBnFr_deserialize %x", buf) } return nil @@ -197,6 +200,16 @@ func (x *Fr) SetLittleEndianMod(buf []byte) error { return nil } +// SetBigEndianMod -- +func (x *Fr) SetBigEndianMod(buf []byte) error { + // #nosec + err := C.mclBnFr_setBigEndianMod(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) + if err != 0 { + return fmt.Errorf("err mclBnFr_setBigEndianMod %x", err) + } + return nil +} + // IsEqual -- func (x *Fr) IsEqual(rhs *Fr) bool { return C.mclBnFr_isEqual(x.getPointer(), rhs.getPointer()) == 1 @@ -340,8 +353,8 @@ func (x *Fp) SetString(s string, base int) error { // Deserialize -- func (x *Fp) Deserialize(buf []byte) error { // #nosec - err := C.mclBnFp_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) - if err == 0 { + n := C.mclBnFp_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) + if n == 0 || int(n) != len(buf) { return fmt.Errorf("err mclBnFp_deserialize %x", buf) } return nil @@ -367,6 +380,16 @@ func (x *Fp) SetLittleEndianMod(buf []byte) error { return nil } +// SetBigEndianMod -- +func (x *Fp) SetBigEndianMod(buf []byte) error { + // #nosec + err := C.mclBnFp_setBigEndianMod(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) + if err != 0 { + return fmt.Errorf("err mclBnFp_setBigEndianMod %x", err) + } + return nil +} + // IsEqual -- func (x *Fp) IsEqual(rhs *Fp) bool { return C.mclBnFp_isEqual(x.getPointer(), rhs.getPointer()) == 1 @@ -493,8 +516,8 @@ func (x *Fp2) Clear() { // Deserialize -- func (x *Fp2) Deserialize(buf []byte) error { // #nosec - err := C.mclBnFp2_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) - if err == 0 { + n := C.mclBnFp2_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) + if n == 0 || int(n) != len(buf) { return fmt.Errorf("err mclBnFp2_deserialize %x", buf) } return nil @@ -599,8 +622,8 @@ func (x *G1) SetString(s string, base int) error { // 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 { + n := C.mclBnG1_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) + if n == 0 || int(n) != len(buf) { return fmt.Errorf("err mclBnG1_deserialize %x", buf) } return nil @@ -796,8 +819,8 @@ func (x *G2) SetString(s string, base int) error { // 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 { + n := C.mclBnG2_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) + if n == 0 || int(n) != len(buf) { return fmt.Errorf("err mclBnG2_deserialize %x", buf) } return nil @@ -976,8 +999,8 @@ func (x *GT) SetString(s string, base int) error { // 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 { + n := C.mclBnGT_deserialize(x.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) + if n == 0 || int(n) != len(buf) { return fmt.Errorf("err mclBnGT_deserialize %x", buf) } return nil diff --git a/ffi/go/mcl/mcl_test.go b/ffi/go/mcl/mcl_test.go index e128648..a182aca 100644 --- a/ffi/go/mcl/mcl_test.go +++ b/ffi/go/mcl/mcl_test.go @@ -2,6 +2,7 @@ package mcl import "testing" import "fmt" +import "encoding/hex" func testBadPointOfG2(t *testing.T) { var Q G2 @@ -261,6 +262,29 @@ func testETHserialize(t *testing.T) { fmt.Printf("AAA x=%s\n", x.GetString(16)) } +func testMapToG1(t *testing.T) { + SetMapToMode(IRTF) + fpHex := "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350" + g1Hex := "ad7721bcdb7ce1047557776eb2659a444166dc6dd55c7ca6e240e21ae9aa18f529f04ac31d861b54faf3307692545db7" + fpBin, _ := hex.DecodeString(fpHex) + var x Fp + x.SetBigEndianMod(fpBin) + var P1 G1 + err := MapToG1(&P1, &x) + if err != nil { + t.Fatal("MapToG1") + } + g1Str, _ := hex.DecodeString(g1Hex) + var P2 G1 + err = P2.Deserialize(g1Str) + if err != nil { + t.Fatal("G1.Deserialize") + } + if !P1.IsEqual(&P2) { + t.Fatal("bad MapToG1") + } +} + func TestMclMain(t *testing.T) { t.Logf("GetMaxOpUnitSize() = %d\n", GetMaxOpUnitSize()) t.Log("CurveFp254BNb") @@ -273,5 +297,6 @@ func TestMclMain(t *testing.T) { t.Log("BLS12_381") testMcl(t, BLS12_381) testETHserialize(t) + testMapToG1(t) } }