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

     1  // Package jjm implements the JJM blind signature scheme over ECC
     2  package jjm
     3  
     4  // Implementation of generic blinding interface over JJM
     5  
     6  import (
     7  	"fmt"
     8  	"github.com/ronperry/cryptoedge/eccutil"
     9  	"github.com/ronperry/cryptoedge/genericblinding"
    10  )
    11  
    12  // GenericBlindingServer implements JJM blinding over generic interface
    13  type GenericBlindingServer struct {
    14  	Signer
    15  }
    16  
    17  // GenericBlindingClient a blinding client
    18  type GenericBlindingClient struct {
    19  	BlindingClient
    20  }
    21  
    22  // NewGenericBlindingServer returns blinding server over generic interface (GenericBlindingServer)
    23  func NewGenericBlindingServer(privkey []byte, pubkey *eccutil.Point, curve *eccutil.Curve) *GenericBlindingServer {
    24  	bs := new(GenericBlindingServer)
    25  	bs.curve = curve
    26  	bs.pubkey = pubkey
    27  	bs.privkey = privkey
    28  	return bs
    29  }
    30  
    31  // GetParams returns per-signature blinding parameters
    32  func (server *GenericBlindingServer) GetParams() (genericblinding.BlindingParamClient, genericblinding.BlindingParamServer, error) {
    33  	bs := NewSigner(server.privkey, server.pubkey, server.curve)
    34  	pub, priv, err := bs.NewSignRequest()
    35  	if err != nil {
    36  		return nil, nil, err
    37  	}
    38  	clientparams := NewBlindingParamClient(server.pubkey)
    39  	clientparams.PointRs1, clientparams.PointRs2 = *pub.PointRs1, *pub.PointRs2
    40  	clientparams.ScalarLs1, clientparams.ScalarLs2 = pub.ScalarLs1, pub.ScalarLs2
    41  
    42  	serverparams := NewBlindingParamServer(server.pubkey)
    43  	serverparams.ScalarKs1, serverparams.ScalarKs2 = priv.ScalarKs1, priv.ScalarKs2
    44  	serverparams.PointRs1, serverparams.PointRs2 = *priv.PointRs1, *priv.PointRs2
    45  	serverparams.ScalarLs1, serverparams.ScalarLs2 = priv.ScalarLs1, priv.ScalarLs2
    46  	serverparams.ScalarRs1, serverparams.ScalarRs2 = priv.ScalarRs1, priv.ScalarRs2
    47  	serverparams.IsUsed = false
    48  
    49  	return clientparams, serverparams, nil
    50  }
    51  
    52  // Sign a blind message
    53  func (server *GenericBlindingServer) Sign(bpsi genericblinding.BlindingParamServer, bmi genericblinding.BlindMessage) (genericblinding.BlindSignature, error) {
    54  	_, err := genericblinding.MatchMessage(bpsi, SchemeName, genericblinding.TypeBlindingParamServer, server.pubkey)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	bps, ok := bpsi.(BlindingParamServer)
    59  	if !ok {
    60  		return nil, genericblinding.ErrBadType
    61  	}
    62  
    63  	_, err = genericblinding.MatchMessage(bmi, SchemeName, genericblinding.TypeBlindMessage, server.pubkey)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	bm, ok := bmi.(BlindMessage)
    68  	if !ok {
    69  		fmt.Println("Message")
    70  		return nil, genericblinding.ErrBadType
    71  	}
    72  
    73  	bs := NewSigner(server.privkey, server.pubkey, server.curve)
    74  	blindmessage := new(BlindMessageInt)
    75  	blindmessage.M1, blindmessage.M2 = bm.M1, bm.M2
    76  	privateParams := new(SignRequestPrivateInt)
    77  	privateParams.ScalarKs1, privateParams.ScalarKs2 = bps.ScalarKs1, bps.ScalarKs2
    78  	privateParams.PointRs1, privateParams.PointRs2 = &bps.PointRs1, &bps.PointRs2
    79  	privateParams.ScalarLs1, privateParams.ScalarLs2 = bps.ScalarLs1, bps.ScalarLs2
    80  	privateParams.ScalarRs1, privateParams.ScalarRs2 = bps.ScalarRs1, bps.ScalarRs2
    81  
    82  	signature, err := bs.Sign(blindmessage, privateParams)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	blindsig := NewBlindSignature(server.pubkey)
    87  	blindsig.ScalarS1, blindsig.ScalarS2 = signature.ScalarS1, signature.ScalarS2
    88  	return blindsig, nil
    89  }
    90  
    91  // NewGenericBlindingClient returns a client for generic blinding interface
    92  func NewGenericBlindingClient(pubkey *eccutil.Point, curve *eccutil.Curve) *GenericBlindingClient {
    93  	c := new(GenericBlindingClient)
    94  	c.curve = curve
    95  	c.PubKey = pubkey
    96  	return c
    97  }
    98  
    99  // Blind a message
   100  func (client *GenericBlindingClient) Blind(bpci genericblinding.BlindingParamClient, cmi genericblinding.ClearMessage) (genericblinding.BlindingFactors, genericblinding.BlindMessage, error) {
   101  	_, err := genericblinding.MatchMessage(bpci, SchemeName, genericblinding.TypeBlindingParamClient, client.PubKey)
   102  	if err != nil {
   103  		return nil, nil, err
   104  	}
   105  	bpc, ok := bpci.(BlindingParamClient)
   106  	if !ok {
   107  		return nil, nil, genericblinding.ErrBadType
   108  	}
   109  	_, err = genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.PubKey)
   110  	if err != nil {
   111  		return nil, nil, err
   112  	}
   113  	cm, ok := cmi.(ClearMessage)
   114  	if !ok {
   115  		return nil, nil, genericblinding.ErrBadType
   116  	}
   117  
   118  	bc := NewBlindingClient(client.curve, client.PubKey)
   119  	serverParams := new(SignRequestPublicInt)
   120  	serverParams.PointRs1, serverParams.PointRs2 = &bpc.PointRs1, &bpc.PointRs2
   121  	serverParams.ScalarLs1, serverParams.ScalarLs2 = bpc.ScalarLs1, bpc.ScalarLs2
   122  
   123  	privateParams, err := bc.CalculateBlindingParams(serverParams)
   124  	if err != nil {
   125  		return nil, nil, err
   126  	}
   127  	blindmessage, err := bc.Blind(cm.UniqueID(), serverParams, privateParams)
   128  	if err != nil {
   129  		return nil, nil, err
   130  	}
   131  	bf := NewBlindingFactors(client.PubKey)
   132  	bf.ScalarW, bf.ScalarZ = privateParams.ScalarW, privateParams.ScalarZ
   133  	bf.ScalarE, bf.ScalarD = privateParams.ScalarE, privateParams.ScalarD
   134  	bf.ScalarA, bf.ScalarB = privateParams.ScalarA, privateParams.ScalarB
   135  	bf.PointR1, bf.PointR2 = *privateParams.PointR1, *privateParams.PointR2
   136  	bf.ScalarR1, bf.ScalarR2 = privateParams.ScalarR1, privateParams.ScalarR2
   137  	bf.ScalarRs1, bf.ScalarRs2 = privateParams.ScalarRs1, privateParams.ScalarRs2
   138  	bf.IsUsed = false
   139  
   140  	bm := NewBlindMessage(client.PubKey)
   141  	bm.M1, bm.M2 = blindmessage.M1, blindmessage.M2
   142  
   143  	return bf, bm, nil
   144  }
   145  
   146  // Unblind a signature
   147  func (client *GenericBlindingClient) Unblind(bfi genericblinding.BlindingFactors, cmi genericblinding.ClearMessage, bsi genericblinding.BlindSignature) (genericblinding.ClearSignature, genericblinding.ClearMessage, error) {
   148  	_, err := genericblinding.MatchMessage(bfi, SchemeName, genericblinding.TypeBlindingFactors, client.PubKey)
   149  	if err != nil {
   150  		return nil, nil, err
   151  	}
   152  	bf, ok := bfi.(BlindingFactors)
   153  	if !ok {
   154  		return nil, nil, genericblinding.ErrBadType
   155  	}
   156  
   157  	_, err = genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.PubKey)
   158  	if err != nil {
   159  		return nil, nil, err
   160  	}
   161  	cm, ok := cmi.(ClearMessage)
   162  	if !ok {
   163  		return nil, nil, genericblinding.ErrBadType
   164  	}
   165  
   166  	_, err = genericblinding.MatchMessage(bsi, SchemeName, genericblinding.TypeBlindSignature, client.PubKey)
   167  	if err != nil {
   168  		return nil, nil, err
   169  	}
   170  	bs, ok := bsi.(BlindSignature)
   171  	if !ok {
   172  		return nil, nil, genericblinding.ErrBadType
   173  	}
   174  	bc := NewBlindingClient(client.curve, client.PubKey)
   175  	blindSignature := new(BlindSignatureInt)
   176  	blindSignature.ScalarS1, blindSignature.ScalarS2 = bs.ScalarS1, bs.ScalarS2
   177  
   178  	blindingParams := new(BlindingParamsPrivateInt)
   179  	blindingParams.ScalarW, blindingParams.ScalarZ = bf.ScalarW, bf.ScalarZ
   180  	blindingParams.ScalarE, blindingParams.ScalarD = bf.ScalarE, bf.ScalarD
   181  	blindingParams.ScalarA, blindingParams.ScalarB = bf.ScalarA, bf.ScalarB
   182  
   183  	blindingParams.PointR1, blindingParams.PointR2 = &bf.PointR1, &bf.PointR2
   184  	blindingParams.ScalarR1, blindingParams.ScalarR2 = bf.ScalarR1, bf.ScalarR2
   185  	blindingParams.ScalarRs1, blindingParams.ScalarRs2 = bf.ScalarRs1, bf.ScalarRs2
   186  
   187  	signature, err := bc.Unblind(blindSignature, blindingParams)
   188  	if err != nil {
   189  		return nil, nil, err
   190  	}
   191  	sig := NewClearSignature(client.PubKey)
   192  	sig.PointR = *signature.PointR
   193  	sig.ScalarS = signature.ScalarS
   194  	sig.ScalarR = signature.ScalarR
   195  
   196  	return sig, cm, nil
   197  }
   198  
   199  // Verify a signature
   200  func (client *GenericBlindingClient) Verify(csi genericblinding.ClearSignature, cmi genericblinding.ClearMessage) (bool, error) {
   201  	_, err := genericblinding.MatchMessage(csi, SchemeName, genericblinding.TypeClearSignature, client.PubKey)
   202  	if err != nil {
   203  		return false, err
   204  	}
   205  	cs, ok := csi.(ClearSignature)
   206  	if !ok {
   207  		return false, genericblinding.ErrBadType
   208  	}
   209  
   210  	_, err = genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.PubKey)
   211  	if err != nil {
   212  		return false, err
   213  	}
   214  	cm, ok := cmi.(ClearMessage)
   215  	if !ok {
   216  		return false, genericblinding.ErrBadType
   217  	}
   218  
   219  	bc := NewBlindingClient(client.curve, client.PubKey)
   220  	signature := new(SignatureInt)
   221  	signature.PointR = &cs.PointR
   222  	signature.ScalarR = cs.ScalarR
   223  	signature.ScalarS = cs.ScalarS
   224  	return bc.Verify(cm.UniqueID(), signature), nil
   225  }