github.com/ronperry/cryptoedge@v0.0.0-20150815114006-cc363e290743/singhdas/generic.go (about)

     1  package singhdas
     2  
     3  import (
     4  	"github.com/ronperry/cryptoedge/eccutil"
     5  	"github.com/ronperry/cryptoedge/genericblinding"
     6  	"math/big"
     7  )
     8  
     9  // GenericSigner is a generic interface signer instance
    10  type GenericSigner struct {
    11  	Signer
    12  }
    13  
    14  // NewGenericBlindingServer returns a new signer
    15  func NewGenericBlindingServer(privkey []byte, pubkey *eccutil.Point, curve *eccutil.Curve) *GenericSigner {
    16  	s := new(GenericSigner)
    17  	s.privkey = new(big.Int)
    18  	s.privkey = s.privkey.SetBytes(privkey)
    19  	s.pubkey = pubkey
    20  	s.curve = curve
    21  	return s
    22  }
    23  
    24  // GenericSignerClient encapsulates a client to a signer using generic interface
    25  type GenericSignerClient struct {
    26  	SignerClient
    27  }
    28  
    29  // NewGenericBlindingClient returns a new client to a signer over curve with publickey
    30  func NewGenericBlindingClient(pubkey *eccutil.Point, curve *eccutil.Curve) *GenericSignerClient {
    31  	sc := new(GenericSignerClient)
    32  	sc.pubkey = pubkey
    33  	sc.curve = curve
    34  	return sc
    35  }
    36  
    37  // Blind a ClearMessage with server-supplied BlindingParamClient
    38  func (client GenericSignerClient) Blind(bpci genericblinding.BlindingParamClient, cmi genericblinding.ClearMessage) (genericblinding.BlindingFactors, genericblinding.BlindMessage, error) {
    39  	_, err := genericblinding.MatchMessage(bpci, SchemeName, genericblinding.TypeBlindingParamClient, client.pubkey)
    40  	if err != nil {
    41  		return nil, nil, err
    42  	}
    43  	bpc, ok := bpci.(BlindingParamClient)
    44  	if !ok {
    45  		return nil, nil, genericblinding.ErrBadType
    46  	}
    47  	_, err = genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.pubkey)
    48  	if err != nil {
    49  		return nil, nil, err
    50  	}
    51  	cm, ok := cmi.(ClearMessage)
    52  	if !ok {
    53  		return nil, nil, genericblinding.ErrBadType
    54  	}
    55  
    56  	bc := new(SignerClient)
    57  	bc.pubkey = client.pubkey
    58  	bc.curve = client.curve
    59  	bm, bfac, err := bc.Blind(cm.UniqueID(), &bpc.Q)
    60  	if err != nil {
    61  		return nil, nil, err
    62  	}
    63  	_, _ = bm, bfac
    64  	blindmessage := NewBlindMessage(client.pubkey)
    65  	blindmessage.Message = bm.Message
    66  	blindmessage.SignerBlind = *bm.SignerBlind
    67  
    68  	blindingfactors := NewBlindingFactors(client.pubkey)
    69  
    70  	blindingfactors.R2 = bfac.r2
    71  	blindingfactors.R1inv = bfac.r1inv
    72  	blindingfactors.R1 = bfac.r1
    73  	blindingfactors.N = bfac.N
    74  	blindingfactors.Hm = bfac.Hm
    75  	blindingfactors.R = *bfac.R
    76  	blindingfactors.SignerBlind = *bfac.SignerBlind
    77  	blindingfactors.IsUsed = false
    78  
    79  	return blindingfactors, blindmessage, nil
    80  
    81  }
    82  
    83  // Unblind a BlindSignature of ClearMessage using BlindingFactors
    84  func (client GenericSignerClient) Unblind(bfaci genericblinding.BlindingFactors, cmi genericblinding.ClearMessage, bsigi genericblinding.BlindSignature) (genericblinding.ClearSignature, genericblinding.ClearMessage, error) {
    85  	_, err := genericblinding.MatchMessage(bfaci, SchemeName, genericblinding.TypeBlindingFactors, client.pubkey)
    86  	if err != nil {
    87  		return nil, nil, err
    88  	}
    89  	bfac, ok := bfaci.(BlindingFactors)
    90  	if !ok {
    91  		return nil, nil, genericblinding.ErrBadType
    92  	}
    93  
    94  	_, err = genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.pubkey)
    95  	if err != nil {
    96  		return nil, nil, err
    97  	}
    98  	cm, ok := cmi.(ClearMessage)
    99  	if !ok {
   100  		return nil, nil, genericblinding.ErrBadType
   101  	}
   102  
   103  	_, err = genericblinding.MatchMessage(bsigi, SchemeName, genericblinding.TypeBlindSignature, client.pubkey)
   104  	if err != nil {
   105  		return nil, nil, err
   106  	}
   107  	bsig, ok := bsigi.(BlindSignature)
   108  	if !ok {
   109  		return nil, nil, genericblinding.ErrBadType
   110  	}
   111  
   112  	bc := new(SignerClient)
   113  	bc.pubkey = client.pubkey
   114  	bc.curve = client.curve
   115  	blindSignature := new(BlindSignatureInt)
   116  	blindSignature.S = bsig.S
   117  	blindSignature.SignerBlind = &bsig.SignerBlind
   118  
   119  	blindingFactors := new(BlindingFactorsInt)
   120  	blindingFactors.r2 = bfac.R2
   121  	blindingFactors.r1inv = bfac.R1inv
   122  	blindingFactors.r1 = bfac.R1
   123  	blindingFactors.N = bfac.N
   124  	blindingFactors.Hm = bfac.Hm
   125  	blindingFactors.R = &bfac.R
   126  	blindingFactors.SignerBlind = &bfac.SignerBlind
   127  	blindingFactors.used = bfac.IsUsed
   128  
   129  	signature, err := bc.UnBlind(blindSignature, blindingFactors)
   130  	if err != nil {
   131  		return nil, nil, err
   132  	}
   133  	csig := NewClearSignature(client.pubkey)
   134  	csig.S = signature.S
   135  	csig.R2 = signature.r2
   136  	csig.R = *signature.R
   137  	csig.Hm = signature.Hm
   138  
   139  	return csig, cm, nil
   140  
   141  }
   142  
   143  // Verify that ClearSignature is a signature of ClearMessage
   144  func (client GenericSignerClient) Verify(sigi genericblinding.ClearSignature, cmi genericblinding.ClearMessage) (bool, error) {
   145  	_, err := genericblinding.MatchMessage(sigi, SchemeName, genericblinding.TypeClearSignature, client.pubkey)
   146  	if err != nil {
   147  		return false, err
   148  	}
   149  	sig, ok := sigi.(ClearSignature)
   150  	if !ok {
   151  		return false, genericblinding.ErrBadType
   152  	}
   153  
   154  	_, err = genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.pubkey)
   155  	if err != nil {
   156  		return false, err
   157  	}
   158  	cm, ok := cmi.(ClearMessage)
   159  	if !ok {
   160  		return false, genericblinding.ErrBadType
   161  	}
   162  
   163  	bc := new(SignerClient)
   164  	bc.pubkey = client.pubkey
   165  	bc.curve = client.curve
   166  	signature := new(SignatureInt)
   167  	signature.S = sig.S
   168  	signature.r2 = sig.R2
   169  	signature.R = &sig.R
   170  	signature.Hm = sig.Hm
   171  	return bc.Verify(cm.UniqueID(), signature)
   172  
   173  }
   174  
   175  // GetParams generates one-time BlindingParam
   176  func (server GenericSigner) GetParams() (genericblinding.BlindingParamClient, genericblinding.BlindingParamServer, error) {
   177  	bs := new(Signer)
   178  	bs.curve = server.curve
   179  	bs.pubkey = server.pubkey
   180  	bs.privkey = server.privkey
   181  	signparams, err := bs.NewRequest()
   182  	if err != nil {
   183  		return nil, nil, err
   184  	}
   185  	clientparams := NewBlindingParamClient(server.pubkey)
   186  	clientparams.Q = *signparams.Q
   187  	serverparams := NewBlindingParamServer(server.pubkey)
   188  	serverparams.K = signparams.k
   189  	serverparams.Q = *signparams.Q
   190  	serverparams.R = signparams.r
   191  	serverparams.IsUsed = false
   192  
   193  	//return clientparams.(genericblinding.BlindingParamClient), serverparams.(genericblinding.BlindingParamServer), nil
   194  	return clientparams, serverparams, nil
   195  }
   196  
   197  // Sign a BlindMessage usign BlindingParam
   198  func (server GenericSigner) Sign(bpsi genericblinding.BlindingParamServer, bmi genericblinding.BlindMessage) (genericblinding.BlindSignature, error) {
   199  	_, err := genericblinding.MatchMessage(bpsi, SchemeName, genericblinding.TypeBlindingParamServer, server.pubkey)
   200  	if err != nil {
   201  		return nil, err
   202  	}
   203  	bps, ok := bpsi.(BlindingParamServer)
   204  	if !ok {
   205  		return nil, genericblinding.ErrBadType
   206  	}
   207  
   208  	_, err = genericblinding.MatchMessage(bmi, SchemeName, genericblinding.TypeBlindMessage, server.pubkey)
   209  	if err != nil {
   210  		return nil, err
   211  	}
   212  	bm, ok := bmi.(BlindMessage)
   213  	if !ok {
   214  		return nil, genericblinding.ErrBadType
   215  	}
   216  
   217  	bs := new(Signer)
   218  	bs.curve = server.curve
   219  	bs.pubkey = server.pubkey
   220  	bs.privkey = server.privkey
   221  	blindMessage := new(BlindMessageInt)
   222  	blindMessage.Message = bm.Message
   223  	blindMessage.SignerBlind = &bm.SignerBlind
   224  	signParams := new(SignParamsInt)
   225  	signParams.Q = &bps.Q
   226  	signParams.k = bps.K
   227  	signParams.r = bps.R
   228  	signParams.used = bps.IsUsed
   229  	S, err := bs.Sign(blindMessage, signParams)
   230  	if err != nil {
   231  		return nil, err
   232  	}
   233  	signature := NewBlindSignature(server.pubkey)
   234  	signature.SignerBlind = *S.SignerBlind
   235  	signature.S = S.S
   236  
   237  	return signature, nil
   238  
   239  }