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

     1  //go:build !js && !wasm
     2  // +build !js,!wasm
     3  
     4  package zcncrypto
     5  
     6  import (
     7  	"encoding/hex"
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"github.com/0chain/errors"
    12  )
    13  
    14  // NewSignatureScheme creates an instance for using signature functions
    15  //   - sigScheme signature scheme to be used
    16  func NewSignatureScheme(sigScheme string) SignatureScheme {
    17  	switch sigScheme {
    18  	case "ed25519":
    19  		return NewED255190chainScheme()
    20  	case "bls0chain":
    21  		return NewHerumiScheme()
    22  	default:
    23  		panic(fmt.Sprintf("unknown signature scheme: %v", sigScheme))
    24  	}
    25  }
    26  
    27  // UnmarshalThresholdSignatureSchemes unmarshal SignatureScheme from json string
    28  func UnmarshalSignatureSchemes(sigScheme string, obj interface{}) ([]SignatureScheme, error) {
    29  	switch sigScheme {
    30  
    31  	case "bls0chain":
    32  
    33  		if obj == nil {
    34  			return nil, nil
    35  		}
    36  
    37  		buf, err := json.Marshal(obj)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  
    42  		var list []*HerumiScheme
    43  
    44  		if err := json.Unmarshal(buf, &list); err != nil {
    45  			return nil, err
    46  		}
    47  
    48  		ss := make([]SignatureScheme, len(list))
    49  
    50  		for i, v := range list {
    51  			// bls.ID from json
    52  			err = v.SetID(v.Ids)
    53  			if err != nil {
    54  				return nil, err
    55  			}
    56  			ss[i] = v
    57  		}
    58  
    59  		return ss, nil
    60  
    61  	default:
    62  		panic(fmt.Sprintf("unknown signature scheme: %v", sigScheme))
    63  	}
    64  }
    65  
    66  // GenerateThresholdKeyShares given a signature scheme will generate threshold sig keys
    67  func GenerateThresholdKeyShares(t, n int, originalKey SignatureScheme) ([]SignatureScheme, error) {
    68  	b0ss, ok := originalKey.(*HerumiScheme)
    69  	if !ok {
    70  		return nil, errors.New("bls0_generate_threshold_key_shares", "Invalid encryption scheme")
    71  	}
    72  
    73  	b0original := BlsSignerInstance.NewSecretKey()
    74  	b0PrivateKeyBytes, err := b0ss.GetPrivateKeyAsByteArray()
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	err = b0original.SetLittleEndian(b0PrivateKeyBytes)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	polynomial, err := b0original.GetMasterSecretKey(t)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	var shares []SignatureScheme
    90  	for i := 1; i <= n; i++ {
    91  		id := BlsSignerInstance.NewID()
    92  		err = id.SetDecString(fmt.Sprint(i))
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  
    97  		sk := BlsSignerInstance.NewSecretKey()
    98  		err = sk.Set(polynomial, id)
    99  		if err != nil {
   100  			return nil, err
   101  		}
   102  
   103  		share := &HerumiScheme{}
   104  		share.PrivateKey = hex.EncodeToString(sk.GetLittleEndian())
   105  		share.PublicKey = sk.GetPublicKey().SerializeToHexStr()
   106  
   107  		share.id = id
   108  		share.Ids = id.GetHexString()
   109  
   110  		shares = append(shares, share)
   111  	}
   112  
   113  	return shares, nil
   114  }