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