github.com/0chain/gosdk@v1.17.11/core/zcncrypto/bls_herumi.go (about)

     1  //go:build !js && !wasm
     2  // +build !js,!wasm
     3  
     4  package zcncrypto
     5  
     6  import (
     7  	"errors"
     8  	"io"
     9  
    10  	"github.com/herumi/bls-go-binary/bls"
    11  )
    12  
    13  func init() {
    14  	err := bls.Init(bls.CurveFp254BNb)
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  	BlsSignerInstance = &herumiBls{}
    19  }
    20  
    21  type herumiBls struct {
    22  }
    23  
    24  func (b *herumiBls) NewFr() Fr {
    25  	return &herumiFr{}
    26  }
    27  func (b *herumiBls) NewSecretKey() SecretKey {
    28  	return &herumiSecretKey{}
    29  }
    30  
    31  func (b *herumiBls) NewPublicKey() PublicKey {
    32  	return &herumiPublicKey{
    33  		PublicKey: &bls.PublicKey{},
    34  	}
    35  }
    36  
    37  func (b *herumiBls) NewSignature() Signature {
    38  	sg := &herumiSignature{
    39  		Sign: &bls.Sign{},
    40  	}
    41  
    42  	return sg
    43  }
    44  
    45  func (b *herumiBls) NewID() ID {
    46  	id := &herumiID{}
    47  
    48  	return id
    49  }
    50  
    51  func (b *herumiBls) SetRandFunc(randReader io.Reader) {
    52  	bls.SetRandFunc(randReader)
    53  }
    54  
    55  func (b *herumiBls) FrSub(out Fr, x Fr, y Fr) {
    56  	o1, _ := out.(*herumiFr)
    57  	x1, _ := x.(*herumiFr)
    58  	y1, _ := y.(*herumiFr)
    59  
    60  	bls.FrSub(&o1.Fr, &x1.Fr, &y1.Fr)
    61  }
    62  
    63  type herumiFr struct {
    64  	bls.Fr
    65  }
    66  
    67  func (fr *herumiFr) Serialize() []byte {
    68  	return fr.Fr.Serialize()
    69  }
    70  
    71  func (fr *herumiFr) SetLittleEndian(buf []byte) error {
    72  	return fr.Fr.SetLittleEndian(buf)
    73  }
    74  
    75  type herumiSecretKey struct {
    76  	bls.SecretKey
    77  }
    78  
    79  func (sk *herumiSecretKey) SerializeToHexStr() string {
    80  	return sk.SecretKey.SerializeToHexStr()
    81  }
    82  func (sk *herumiSecretKey) DeserializeHexStr(s string) error {
    83  	return sk.SecretKey.DeserializeHexStr(s)
    84  }
    85  
    86  func (sk *herumiSecretKey) Serialize() []byte {
    87  	return sk.SecretKey.Serialize()
    88  }
    89  
    90  func (sk *herumiSecretKey) GetLittleEndian() []byte {
    91  	return sk.SecretKey.GetLittleEndian()
    92  }
    93  func (sk *herumiSecretKey) SetLittleEndian(buf []byte) error {
    94  	return sk.SecretKey.SetLittleEndian(buf)
    95  }
    96  
    97  func (sk *herumiSecretKey) SetByCSPRNG() {
    98  	sk.SecretKey.SetByCSPRNG()
    99  }
   100  
   101  func (sk *herumiSecretKey) GetPublicKey() PublicKey {
   102  	pk := sk.SecretKey.GetPublicKey()
   103  	return &herumiPublicKey{
   104  		PublicKey: pk,
   105  	}
   106  }
   107  
   108  func (sk *herumiSecretKey) Add(rhs SecretKey) {
   109  	i, _ := rhs.(*herumiSecretKey)
   110  	sk.SecretKey.Add(&i.SecretKey)
   111  }
   112  
   113  func (sk *herumiSecretKey) Sign(m string) Signature {
   114  	sig := sk.SecretKey.Sign(m)
   115  
   116  	return &herumiSignature{
   117  		Sign: sig,
   118  	}
   119  }
   120  
   121  func (sk *herumiSecretKey) GetMasterSecretKey(k int) ([]SecretKey, error) {
   122  	if k < 1 {
   123  		return nil, errors.New("cannot get master secret key for threshold less than 1")
   124  	}
   125  
   126  	list := sk.SecretKey.GetMasterSecretKey(k)
   127  
   128  	msk := make([]SecretKey, len(list))
   129  
   130  	for i, it := range list {
   131  		msk[i] = &herumiSecretKey{SecretKey: it}
   132  
   133  	}
   134  
   135  	return msk, nil
   136  }
   137  
   138  func (sk *herumiSecretKey) Set(msk []SecretKey, id ID) error {
   139  
   140  	blsMsk := make([]bls.SecretKey, len(msk))
   141  
   142  	for i, it := range msk {
   143  		k, ok := it.(*herumiSecretKey)
   144  		if !ok {
   145  			return errors.New("invalid herumi secret key")
   146  		}
   147  
   148  		blsMsk[i] = k.SecretKey
   149  	}
   150  
   151  	blsID, _ := id.(*herumiID)
   152  
   153  	return sk.SecretKey.Set(blsMsk, &blsID.ID)
   154  }
   155  
   156  type herumiPublicKey struct {
   157  	*bls.PublicKey
   158  }
   159  
   160  func (pk *herumiPublicKey) SerializeToHexStr() string {
   161  	return pk.PublicKey.SerializeToHexStr()
   162  }
   163  
   164  func (pk *herumiPublicKey) DeserializeHexStr(s string) error {
   165  	return pk.PublicKey.DeserializeHexStr(s)
   166  }
   167  
   168  func (pk *herumiPublicKey) Serialize() []byte {
   169  	return pk.PublicKey.Serialize()
   170  }
   171  
   172  type herumiSignature struct {
   173  	*bls.Sign
   174  }
   175  
   176  // SerializeToHexStr --
   177  func (sg *herumiSignature) SerializeToHexStr() string {
   178  	return sg.Sign.SerializeToHexStr()
   179  }
   180  
   181  func (sg *herumiSignature) DeserializeHexStr(s string) error {
   182  	return sg.Sign.DeserializeHexStr(s)
   183  }
   184  
   185  func (sg *herumiSignature) Add(rhs Signature) {
   186  	sg2, _ := rhs.(*herumiSignature)
   187  
   188  	sg.Sign.Add(sg2.Sign)
   189  }
   190  
   191  func (sg *herumiSignature) Verify(pk PublicKey, m string) bool {
   192  	pub, _ := pk.(*herumiPublicKey)
   193  
   194  	return sg.Sign.Verify(pub.PublicKey, m)
   195  }
   196  
   197  type herumiID struct {
   198  	bls.ID
   199  }
   200  
   201  func (id *herumiID) SetHexString(s string) error {
   202  	return id.ID.SetHexString(s)
   203  }
   204  func (id *herumiID) GetHexString() string {
   205  	return id.ID.GetHexString()
   206  }
   207  
   208  func (id *herumiID) SetDecString(s string) error {
   209  	return id.ID.SetDecString(s)
   210  }