diff options
author | Jimmy Hu <jim2212001@gmail.com> | 2018-09-21 14:33:53 +0800 |
---|---|---|
committer | Sonic <sonic@dexon.org> | 2019-04-08 15:19:37 +0800 |
commit | 8d80cd44a584e18a3b2832c8c87c36d0cb1e3730 (patch) | |
tree | b31d8e817ffdb0b9cb937aa43c0802036d631259 /ffi/go | |
parent | f0d19299b7e9e8d05447e9e24ec0df557ed48bc6 (diff) | |
download | dexon-bls-8d80cd44a584e18a3b2832c8c87c36d0cb1e3730.tar dexon-bls-8d80cd44a584e18a3b2832c8c87c36d0cb1e3730.tar.gz dexon-bls-8d80cd44a584e18a3b2832c8c87c36d0cb1e3730.tar.bz2 dexon-bls-8d80cd44a584e18a3b2832c8c87c36d0cb1e3730.tar.lz dexon-bls-8d80cd44a584e18a3b2832c8c87c36d0cb1e3730.tar.xz dexon-bls-8d80cd44a584e18a3b2832c8c87c36d0cb1e3730.tar.zst dexon-bls-8d80cd44a584e18a3b2832c8c87c36d0cb1e3730.zip |
Add Marshal/UnmarshalJSON to all bls types (#1)
Diffstat (limited to 'ffi/go')
-rw-r--r-- | ffi/go/bls/bls.go | 93 |
1 files changed, 93 insertions, 0 deletions
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()) |