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

     1  package singhdas
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"encoding/asn1"
     6  	"github.com/ronperry/cryptoedge/eccutil"
     7  	"github.com/ronperry/cryptoedge/genericblinding"
     8  	"math/big"
     9  )
    10  
    11  // SchemeName is the name of this blinding scheme
    12  const SchemeName = "SNG"
    13  
    14  // BlindingParamClient is not needed in SNG
    15  type BlindingParamClient struct {
    16  	SchemeName string
    17  	DataType   genericblinding.DataType
    18  	PubKey     eccutil.Point
    19  	Q          eccutil.Point // Public, given to requestor
    20  }
    21  
    22  // NewBlindingParamClient returns a new BlindingParamClient
    23  func NewBlindingParamClient(PubKey *eccutil.Point) BlindingParamClient {
    24  	n := new(BlindingParamClient)
    25  	n.SchemeName = SchemeName
    26  	n.DataType = genericblinding.TypeBlindingParamClient
    27  	n.PubKey = *PubKey
    28  	return *n
    29  }
    30  
    31  // SchemeData returns general data for the scheme and BlindingData type
    32  func (blindingParamClient BlindingParamClient) SchemeData() (string, genericblinding.DataType, *eccutil.Point) {
    33  	return blindingParamClient.SchemeName, blindingParamClient.DataType, &blindingParamClient.PubKey
    34  }
    35  
    36  // Marshal a BlindingParamClient
    37  func (blindingParamClient BlindingParamClient) Marshal() ([]byte, error) {
    38  	return asn1.Marshal(blindingParamClient)
    39  }
    40  
    41  // Unmarshal []byte into BlindingParamClient
    42  func (blindingParamClient BlindingParamClient) Unmarshal(b []byte) (genericblinding.BlindingData, error) {
    43  	n := new(BlindingParamClient)
    44  	_, err := asn1.Unmarshal(b, n)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	if n.SchemeName != blindingParamClient.SchemeName {
    49  		return nil, genericblinding.ErrBadScheme
    50  	}
    51  	if n.DataType != blindingParamClient.DataType {
    52  		return nil, genericblinding.ErrBadType
    53  	}
    54  	if !eccutil.PointEqual(&blindingParamClient.PubKey, &n.PubKey) {
    55  		return nil, genericblinding.ErrBadSigner
    56  	}
    57  	return *n, nil
    58  }
    59  
    60  // UniqueID returns a unique ID for this element. Constant in this case (zeros)
    61  func (blindingParamClient BlindingParamClient) UniqueID() []byte {
    62  	x := make([]byte, 64)
    63  	x = append(x, blindingParamClient.Q.X.Bytes()...)
    64  	x = append(x, blindingParamClient.Q.Y.Bytes()...)
    65  	y := sha256.Sum256(x)
    66  	return y[:]
    67  }
    68  
    69  // ------------------------------------------
    70  //
    71  // ------------------------------------------
    72  
    73  // BlindingParamServer is not needed in SNG
    74  type BlindingParamServer struct {
    75  	SchemeName string
    76  	DataType   genericblinding.DataType
    77  	PubKey     eccutil.Point
    78  	K          *big.Int      // Private, never share
    79  	Q          eccutil.Point // Public, given to requestor
    80  	R          *big.Int
    81  	IsUsed     bool // Never use twice
    82  }
    83  
    84  // NewBlindingParamServer returns a new BlindingParamServer
    85  func NewBlindingParamServer(PubKey *eccutil.Point) BlindingParamServer {
    86  	n := new(BlindingParamServer)
    87  	n.SchemeName = SchemeName
    88  	n.DataType = genericblinding.TypeBlindingParamServer
    89  	n.PubKey = *PubKey
    90  	return *n
    91  }
    92  
    93  // SchemeData returns general data for the scheme and BlindingData type
    94  func (blindingParamServer BlindingParamServer) SchemeData() (string, genericblinding.DataType, *eccutil.Point) {
    95  	return blindingParamServer.SchemeName, blindingParamServer.DataType, &blindingParamServer.PubKey
    96  }
    97  
    98  // Marshal a BlindingParamServer
    99  func (blindingParamServer BlindingParamServer) Marshal() ([]byte, error) {
   100  	return asn1.Marshal(blindingParamServer)
   101  }
   102  
   103  // Unmarshal []byte into BlindingParamServer
   104  func (blindingParamServer BlindingParamServer) Unmarshal(b []byte) (genericblinding.BlindingData, error) {
   105  	n := new(BlindingParamServer)
   106  	_, err := asn1.Unmarshal(b, n)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	if n.SchemeName != blindingParamServer.SchemeName {
   111  		return nil, genericblinding.ErrBadScheme
   112  	}
   113  	if n.DataType != blindingParamServer.DataType {
   114  		return nil, genericblinding.ErrBadType
   115  	}
   116  	if !eccutil.PointEqual(&blindingParamServer.PubKey, &n.PubKey) {
   117  		return nil, genericblinding.ErrBadSigner
   118  	}
   119  	return *n, nil
   120  }
   121  
   122  // UniqueID returns a unique ID for this element. Constant in this case (zeros)
   123  func (blindingParamServer BlindingParamServer) UniqueID() []byte {
   124  	x := make([]byte, 128)
   125  	x = append(x, blindingParamServer.Q.X.Bytes()...)
   126  	x = append(x, blindingParamServer.Q.Y.Bytes()...)
   127  	x = append(x, blindingParamServer.K.Bytes()...)
   128  	x = append(x, blindingParamServer.R.Bytes()...)
   129  	y := sha256.Sum256(x)
   130  	return y[:]
   131  }
   132  
   133  // --------------------------------
   134  //
   135  // --------------------------------
   136  
   137  // ClearMessage contains a message
   138  type ClearMessage struct {
   139  	SchemeName string
   140  	DataType   genericblinding.DataType
   141  	Message    []byte
   142  }
   143  
   144  // NewClearMessage returns a new BlindingParamClient
   145  func NewClearMessage(msg []byte) ClearMessage {
   146  	n := new(ClearMessage)
   147  	n.SchemeName = SchemeName
   148  	n.DataType = genericblinding.TypeClearMessage
   149  	n.Message = msg
   150  	return *n
   151  }
   152  
   153  // SchemeData returns general data for the scheme and BlindingData type
   154  func (clearMessage ClearMessage) SchemeData() (string, genericblinding.DataType, *eccutil.Point) {
   155  	return clearMessage.SchemeName, clearMessage.DataType, nil
   156  }
   157  
   158  // Marshal a BlindingParamClient
   159  func (clearMessage ClearMessage) Marshal() ([]byte, error) {
   160  	return asn1.Marshal(clearMessage)
   161  }
   162  
   163  // Unmarshal []byte into BlindingParamClient
   164  func (clearMessage ClearMessage) Unmarshal(b []byte) (genericblinding.BlindingData, error) {
   165  	n := new(ClearMessage)
   166  	_, err := asn1.Unmarshal(b, n)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	if n.SchemeName != clearMessage.SchemeName {
   171  		return nil, genericblinding.ErrBadScheme
   172  	}
   173  	if n.DataType != clearMessage.DataType {
   174  		return nil, genericblinding.ErrBadType
   175  	}
   176  	return *n, nil
   177  }
   178  
   179  // UniqueID returns a unique ID for this element. Constant in this case (zeros)
   180  func (clearMessage ClearMessage) UniqueID() []byte {
   181  	x := sha256.Sum256(clearMessage.Message)
   182  	return x[:]
   183  }
   184  
   185  // --------------------------------
   186  //
   187  // --------------------------------
   188  
   189  // BlindingFactors contains the client-produced blinding factors
   190  type BlindingFactors struct {
   191  	SchemeName string
   192  	DataType   genericblinding.DataType
   193  	PubKey     eccutil.Point
   194  
   195  	R2          *big.Int
   196  	R1inv       *big.Int
   197  	R1          *big.Int
   198  	N           *big.Int
   199  	Hm          *big.Int
   200  	R           eccutil.Point
   201  	SignerBlind eccutil.Point
   202  	IsUsed      bool
   203  }
   204  
   205  // NewBlindingFactors returns a new BlindingParamClient
   206  func NewBlindingFactors(PubKey *eccutil.Point) BlindingFactors {
   207  	n := new(BlindingFactors)
   208  	n.SchemeName = SchemeName
   209  	n.DataType = genericblinding.TypeBlindingFactors
   210  	n.PubKey = *PubKey
   211  	return *n
   212  }
   213  
   214  // SchemeData returns general data for the scheme and BlindingData type
   215  func (blindingFactors BlindingFactors) SchemeData() (string, genericblinding.DataType, *eccutil.Point) {
   216  	return blindingFactors.SchemeName, blindingFactors.DataType, &blindingFactors.PubKey
   217  }
   218  
   219  // Marshal a BlindingParamClient
   220  func (blindingFactors BlindingFactors) Marshal() ([]byte, error) {
   221  	return asn1.Marshal(blindingFactors)
   222  }
   223  
   224  // Unmarshal []byte into BlindingParamClient
   225  func (blindingFactors BlindingFactors) Unmarshal(b []byte) (genericblinding.BlindingData, error) {
   226  	n := new(BlindingFactors)
   227  	_, err := asn1.Unmarshal(b, n)
   228  	if err != nil {
   229  		return nil, err
   230  	}
   231  	if n.SchemeName != blindingFactors.SchemeName {
   232  		return nil, genericblinding.ErrBadScheme
   233  	}
   234  	if n.DataType != blindingFactors.DataType {
   235  		return nil, genericblinding.ErrBadType
   236  	}
   237  	if !eccutil.PointEqual(&blindingFactors.PubKey, &n.PubKey) {
   238  		return nil, genericblinding.ErrBadSigner
   239  	}
   240  	return *n, nil
   241  }
   242  
   243  // UniqueID returns a unique ID for this element. Constant in this case (zeros)
   244  func (blindingFactors BlindingFactors) UniqueID() []byte {
   245  	x := make([]byte, 320)
   246  	x = append(x, blindingFactors.R2.Bytes()...)
   247  	x = append(x, blindingFactors.R1inv.Bytes()...)
   248  	x = append(x, blindingFactors.R1.Bytes()...)
   249  	x = append(x, blindingFactors.N.Bytes()...)
   250  	x = append(x, blindingFactors.Hm.Bytes()...)
   251  	x = append(x, blindingFactors.R.X.Bytes()...)
   252  	x = append(x, blindingFactors.R.Y.Bytes()...)
   253  	x = append(x, blindingFactors.SignerBlind.X.Bytes()...)
   254  	x = append(x, blindingFactors.SignerBlind.Y.Bytes()...)
   255  	y := sha256.Sum256(x)
   256  	return y[:]
   257  }
   258  
   259  // --------------------------------
   260  //
   261  // --------------------------------
   262  
   263  // BlindMessage contains the client-produced blinding factors
   264  type BlindMessage struct {
   265  	SchemeName  string
   266  	DataType    genericblinding.DataType
   267  	PubKey      eccutil.Point
   268  	Message     *big.Int
   269  	SignerBlind eccutil.Point
   270  }
   271  
   272  // NewBlindMessage returns a new BlindingParamClient
   273  func NewBlindMessage(PubKey *eccutil.Point) BlindMessage {
   274  	n := new(BlindMessage)
   275  	n.SchemeName = SchemeName
   276  	n.DataType = genericblinding.TypeBlindMessage
   277  	n.PubKey = *PubKey
   278  	return *n
   279  }
   280  
   281  // SchemeData returns general data for the scheme and BlindingData type
   282  func (blindMessage BlindMessage) SchemeData() (string, genericblinding.DataType, *eccutil.Point) {
   283  	return blindMessage.SchemeName, blindMessage.DataType, &blindMessage.PubKey
   284  }
   285  
   286  // Marshal a BlindingParamClient
   287  func (blindMessage BlindMessage) Marshal() ([]byte, error) {
   288  	return asn1.Marshal(blindMessage)
   289  }
   290  
   291  // Unmarshal []byte into BlindingParamClient
   292  func (blindMessage BlindMessage) Unmarshal(b []byte) (genericblinding.BlindingData, error) {
   293  	n := new(BlindMessage)
   294  	_, err := asn1.Unmarshal(b, n)
   295  	if err != nil {
   296  		return nil, err
   297  	}
   298  	if n.SchemeName != blindMessage.SchemeName {
   299  		return nil, genericblinding.ErrBadScheme
   300  	}
   301  	if n.DataType != blindMessage.DataType {
   302  		return nil, genericblinding.ErrBadType
   303  	}
   304  	if !eccutil.PointEqual(&blindMessage.PubKey, &n.PubKey) {
   305  		return nil, genericblinding.ErrBadSigner
   306  	}
   307  	return *n, nil
   308  }
   309  
   310  // UniqueID returns a unique ID for this element. Constant in this case (zeros)
   311  func (blindMessage BlindMessage) UniqueID() []byte {
   312  	d := make([]byte, 96)
   313  	d = append(d, blindMessage.Message.Bytes()...)
   314  	d = append(d, blindMessage.SignerBlind.X.Bytes()...)
   315  	d = append(d, blindMessage.SignerBlind.Y.Bytes()...)
   316  	x := sha256.Sum256(d)
   317  	return x[:]
   318  }
   319  
   320  // --------------------------------
   321  //
   322  // --------------------------------
   323  
   324  // BlindSignature contains the client-produced blinding factors
   325  type BlindSignature struct {
   326  	SchemeName  string
   327  	DataType    genericblinding.DataType
   328  	PubKey      eccutil.Point
   329  	SignerBlind eccutil.Point
   330  	S           *big.Int
   331  }
   332  
   333  // NewBlindSignature returns a new BlindingParamClient
   334  func NewBlindSignature(PubKey *eccutil.Point) BlindSignature {
   335  	n := new(BlindSignature)
   336  	n.SchemeName = SchemeName
   337  	n.DataType = genericblinding.TypeBlindSignature
   338  	n.PubKey = *PubKey
   339  	return *n
   340  }
   341  
   342  // SchemeData returns general data for the scheme and BlindingData type
   343  func (blindSignature BlindSignature) SchemeData() (string, genericblinding.DataType, *eccutil.Point) {
   344  	return blindSignature.SchemeName, blindSignature.DataType, &blindSignature.PubKey
   345  }
   346  
   347  // Marshal a BlindingParamClient
   348  func (blindSignature BlindSignature) Marshal() ([]byte, error) {
   349  	return asn1.Marshal(blindSignature)
   350  }
   351  
   352  // Unmarshal []byte into BlindingParamClient
   353  func (blindSignature BlindSignature) Unmarshal(b []byte) (genericblinding.BlindingData, error) {
   354  	n := new(BlindSignature)
   355  	_, err := asn1.Unmarshal(b, n)
   356  	if err != nil {
   357  		return nil, err
   358  	}
   359  	if n.SchemeName != blindSignature.SchemeName {
   360  		return nil, genericblinding.ErrBadScheme
   361  	}
   362  	if n.DataType != blindSignature.DataType {
   363  		return nil, genericblinding.ErrBadType
   364  	}
   365  	if !eccutil.PointEqual(&blindSignature.PubKey, &n.PubKey) {
   366  		return nil, genericblinding.ErrBadSigner
   367  	}
   368  	return *n, nil
   369  }
   370  
   371  // UniqueID returns a unique ID for this element. Constant in this case (zeros)
   372  func (blindSignature BlindSignature) UniqueID() []byte {
   373  	d := make([]byte, 96)
   374  	d = append(d, blindSignature.S.Bytes()...)
   375  	d = append(d, blindSignature.SignerBlind.X.Bytes()...)
   376  	d = append(d, blindSignature.SignerBlind.Y.Bytes()...)
   377  	x := sha256.Sum256(d)
   378  	return x[:]
   379  }
   380  
   381  // --------------------------------
   382  //
   383  // --------------------------------
   384  
   385  // ClearSignature contains the client-produced blinding factors
   386  type ClearSignature struct {
   387  	SchemeName string
   388  	DataType   genericblinding.DataType
   389  	PubKey     eccutil.Point
   390  	S          *big.Int
   391  	R2         *big.Int
   392  	R          eccutil.Point
   393  	Hm         *big.Int
   394  }
   395  
   396  // NewClearSignature returns a new BlindingParamClient
   397  func NewClearSignature(PubKey *eccutil.Point) ClearSignature {
   398  	n := new(ClearSignature)
   399  	n.SchemeName = SchemeName
   400  	n.DataType = genericblinding.TypeClearSignature
   401  	n.PubKey = *PubKey
   402  	return *n
   403  }
   404  
   405  // SchemeData returns general data for the scheme and BlindingData type
   406  func (clearSignature ClearSignature) SchemeData() (string, genericblinding.DataType, *eccutil.Point) {
   407  	return clearSignature.SchemeName, clearSignature.DataType, &clearSignature.PubKey
   408  }
   409  
   410  // Marshal a BlindingParamClient
   411  func (clearSignature ClearSignature) Marshal() ([]byte, error) {
   412  	return asn1.Marshal(clearSignature)
   413  }
   414  
   415  // Unmarshal []byte into BlindingParamClient
   416  func (clearSignature ClearSignature) Unmarshal(b []byte) (genericblinding.BlindingData, error) {
   417  	n := new(ClearSignature)
   418  	_, err := asn1.Unmarshal(b, n)
   419  	if err != nil {
   420  		return nil, err
   421  	}
   422  	if n.SchemeName != clearSignature.SchemeName {
   423  		return nil, genericblinding.ErrBadScheme
   424  	}
   425  	if n.DataType != clearSignature.DataType {
   426  		return nil, genericblinding.ErrBadType
   427  	}
   428  	if !eccutil.PointEqual(&clearSignature.PubKey, &n.PubKey) {
   429  		return nil, genericblinding.ErrBadSigner
   430  	}
   431  	return *n, nil
   432  }
   433  
   434  // UniqueID returns a unique ID for this element. Constant in this case (zeros)
   435  func (clearSignature ClearSignature) UniqueID() []byte {
   436  	d := make([]byte, 160)
   437  	d = append(d, clearSignature.R.X.Bytes()...)
   438  	d = append(d, clearSignature.R.Y.Bytes()...)
   439  	d = append(d, clearSignature.S.Bytes()...)
   440  	d = append(d, clearSignature.R2.Bytes()...)
   441  	d = append(d, clearSignature.Hm.Bytes()...)
   442  	x := sha256.Sum256(d)
   443  	return x[:]
   444  }