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

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