From 8d80cd44a584e18a3b2832c8c87c36d0cb1e3730 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Fri, 21 Sep 2018 14:33:53 +0800 Subject: Add Marshal/UnmarshalJSON to all bls types (#1) --- ffi/go/bls/bls.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/ffi/go/bls/bls.go b/ffi/go/bls/bls.go index 891870c..91af1d1 100644 --- a/ffi/go/bls/bls.go +++ b/ffi/go/bls/bls.go @@ -18,6 +18,7 @@ import "C" import "fmt" import "unsafe" import "io" +import "encoding/json" // Init -- // call this function before calling all the other operations @@ -79,6 +80,29 @@ func (id *ID) IsEqual(rhs *ID) bool { return id.v.IsEqual(&rhs.v) } +// MarshalJSON implements json.Marshaller. +func (id *ID) MarshalJSON() ([]byte, error) { + return json.Marshal(&struct { + ID []byte `json:"id"` + }{ + id.GetLittleEndian(), + }) +} + +// UnmarshalJSON implements json.Unmarshaller. +func (id *ID) UnmarshalJSON(data []byte) error { + aux := &struct { + ID []byte `json:"id"` + }{} + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + if err := id.SetLittleEndian(aux.ID); err != nil { + return err + } + return nil +} + // SecretKey -- type SecretKey struct { v Fr @@ -158,6 +182,29 @@ func (sec *SecretKey) GetMasterSecretKey(k int) (msk []SecretKey) { return msk } +// MarshalJSON implements json.Marshaller. +func (sec *SecretKey) MarshalJSON() ([]byte, error) { + return json.Marshal(&struct { + SecretKey []byte `json:"secret_key"` + }{ + sec.GetLittleEndian(), + }) +} + +// UnmarshalJSON implements json.Unmarshaller. +func (sec *SecretKey) UnmarshalJSON(data []byte) error { + aux := &struct { + SecretKey []byte `json:"secret_key"` + }{} + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + if err := sec.SetLittleEndian(aux.SecretKey); err != nil { + return err + } + return nil +} + // GetMasterPublicKey -- func GetMasterPublicKey(msk []SecretKey) (mpk []PublicKey) { n := len(msk) @@ -253,6 +300,29 @@ func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []ID) error { return G2LagrangeInterpolation(&pub.v, *(*[]Fr)(unsafe.Pointer(&idVec)), *(*[]G2)(unsafe.Pointer(&pubVec))) } +// MarshalJSON implements json.Marshaller. +func (pub *PublicKey) MarshalJSON() ([]byte, error) { + return json.Marshal(&struct { + PublicKey []byte `json:"public_key"` + }{ + pub.Serialize(), + }) +} + +// UnmarshalJSON implements json.Unmarshaller. +func (pub *PublicKey) UnmarshalJSON(data []byte) error { + aux := &struct { + PublicKey []byte `json:"public_key"` + }{} + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + if err := pub.Deserialize(aux.PublicKey); err != nil { + return err + } + return nil +} + // Sign -- type Sign struct { v G1 @@ -344,6 +414,29 @@ func (sign *Sign) VerifyPop(pub *PublicKey) bool { return C.blsVerifyPop(sign.getPointer(), pub.getPointer()) == 1 } +// MarshalJSON implements json.Marshaller. +func (sign *Sign) MarshalJSON() ([]byte, error) { + return json.Marshal(&struct { + Sign []byte `json:"sign"` + }{ + sign.Serialize(), + }) +} + +// UnmarshalJSON implements json.Unmarshaller. +func (sign *Sign) UnmarshalJSON(data []byte) error { + aux := &struct { + Sign []byte `json:"sign"` + }{} + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + if err := sign.Deserialize(aux.Sign); err != nil { + return err + } + return nil +} + // DHKeyExchange -- func DHKeyExchange(sec *SecretKey, pub *PublicKey) (out PublicKey) { C.blsDHKeyExchange(out.getPointer(), sec.getPointer(), pub.getPointer()) -- cgit v1.2.3