github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/lnutil/curvelib_test.go (about) 1 package lnutil 2 3 import ( 4 "testing" 5 6 "github.com/mit-dci/lit/btcutil/chaincfg/chainhash" 7 "github.com/mit-dci/lit/crypto/koblitz" 8 ) 9 10 var ( 11 // invalid pubkey which is not on the curve, Secp256k1 12 invalidPubKeyCmpd = [33]byte{ 13 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 14 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 15 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 16 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 17 0x00} 18 19 // pubkey from bitcoin blockchain tx 20 // adfa661e05b2221a1425190265f2ab397ff5e00380b33c35b57142575defff14 21 // pubKeyCmpd0 = [33]byte{ 22 // 0x03, 0x70, 0xed, 0xd2, 0xa2, 0x47, 0x90, 0x76, 23 // 0x6d, 0xdc, 0xbc, 0x25, 0x98, 0x00, 0xd6, 0x7e, 24 // 0x83, 0x6d, 0x5b, 0xed, 0xd0, 0xa2, 0x74, 0x3c, 25 // 0x5f, 0x8c, 0x67, 0xd2, 0x6c, 0x9d, 0x90, 0xf6, 26 // 0x35} 27 // pubkey from bitcoin blockchain tx 28 // 23df526af42b7546987ebe7c2dd712faa146f78c7ae2162b3515fd861c7b045f 29 // pubKeyCmpd1 = [33]byte{ 30 // 0x03, 0xc1, 0xa3, 0xb5, 0xdc, 0x62, 0x60, 0xff, 31 // 0xdd, 0xd0, 0x6c, 0x6e, 0x52, 0x8c, 0x34, 0x40, 32 // 0x84, 0xe6, 0x96, 0xb2, 0x11, 0x14, 0x3f, 0xac, 33 // 0x15, 0xf6, 0x1f, 0x85, 0xe2, 0x45, 0x89, 0x29, 34 // 0x5a} 35 36 // very simple privkey to test 37 privKeyB0 = []byte{0x00, 0x01} 38 privKeyB1 = []byte{0x00, 0x03} 39 ) 40 41 // AddPubsEZ 42 func TestAddPubsEZ(t *testing.T) { 43 // test a normal situation 44 // input a: pubKeyCmpd0, 33 bytes array 45 // input b: pubKeyCmpd1, 33 bytes array 46 // want: wantB33, 33 bytes array 47 // 48 // inputs a and b are [33]byte and they can not be nil so no test for nil 49 var wantB33 = [33]byte{ 50 0x02, 0x95, 0x82, 0xad, 0x0e, 0xa8, 0xc0, 0xd3, 51 0x20, 0x79, 0x96, 0xee, 0x7f, 0x5a, 0x53, 0x28, 52 0x71, 0x25, 0xfb, 0x88, 0x4c, 0x14, 0xff, 0xbe, 53 0x15, 0x10, 0x13, 0xa7, 0x48, 0x9f, 0x75, 0xd4, 54 0x8e} 55 if AddPubsEZ(pubKeyCmpd0, pubKeyCmpd1) != wantB33 { 56 t.Fatalf("it needs to be equal") 57 } 58 59 // test an anomaly situation 60 // input a: invalidPubKeyCmpd, 33 bytes array 61 // input b: pubKeyCmpd0, 33 bytes array 62 // want: invalidPubKeyCmpd, 33 bytes array 63 // 64 // if HAKD base is invalid pubkey, it just returns invalid HAKD base 65 if AddPubsEZ(invalidPubKeyCmpd, pubKeyCmpd0) != invalidPubKeyCmpd { 66 t.Fatalf("it needs that output equals to invalid pubkey") 67 } 68 69 // test an anomaly situation 70 // input a: pubKeyCmpd0, 33 bytes array 71 // input b: invalidPubKeyCmpd, 33 bytes array 72 // want: not invalidPubKeyCmpd, 33 bytes array 73 // 74 // if elekpoint is invalid pubkey, it does not return invalid HAKD base 75 if AddPubsEZ(pubKeyCmpd0, invalidPubKeyCmpd) == invalidPubKeyCmpd { 76 t.Fatalf("it needs that output does not equal to invalid pubkey") 77 } 78 } 79 80 // CombinePubs 81 func TestCombinePubs(t *testing.T) { 82 // test a normal situation 83 // input a: pubKeyCmpd0, 33 bytes array 84 // input b: pubKeyCmpd1, 33 bytes array 85 // want: wantB33, 33 bytes array 86 // 87 // inputs a and b are [33]byte and they can not be nil so no test for nil 88 var wantB33 = [33]byte{ 89 0x03, 0xee, 0xd5, 0x4a, 0x37, 0x98, 0xe8, 0xa8, 90 0x2b, 0x35, 0xd4, 0x38, 0x3e, 0x24, 0x68, 0xef, 91 0x74, 0x3b, 0xb6, 0x3e, 0x20, 0xb8, 0x81, 0x29, 92 0x90, 0xf4, 0x71, 0xf5, 0xd7, 0x35, 0xfb, 0x61, 93 0xf4} 94 if CombinePubs(pubKeyCmpd0, pubKeyCmpd1) != wantB33 { 95 t.Fatalf("it needs to be equal") 96 } 97 98 // test an anomaly situation 99 // if one of inputs is an invalid pubkey, it returns [33]byte{0x00, 0x00, ..., 0x00} 100 // 101 // input a: invalidPubKeyCmpd, 33 bytes array 102 // input b: pubKeyCmpd0, 33 bytes array 103 // want: 33 bytes array, [33]byte{0x00, 0x00, ..., 0x00} 104 if CombinePubs(invalidPubKeyCmpd, pubKeyCmpd0) != [33]byte{} { 105 t.Fatalf("it needs that output equals to 33 bytes array which has only 0x00") 106 } 107 // input a: pubKeyCmpd0, 33 bytes array 108 // input b: invalidPubKeyCmpd, 33 bytes array 109 // want: 33 bytes array, [33]byte{0x00, 0x00, ..., 0x00} 110 if CombinePubs(pubKeyCmpd0, invalidPubKeyCmpd) != [33]byte{} { 111 t.Fatalf("it needs that output equals to 33 bytes array which has only 0x00") 112 } 113 // input a: invalidPubKeyCmpd, 33 bytes array 114 // input b: invalidPubKeyCmpd, 33 bytes array 115 // want: 33 bytes array, [33]byte{0x00, 0x00, ..., 0x00} 116 if CombinePubs(invalidPubKeyCmpd, invalidPubKeyCmpd) != [33]byte{} { 117 t.Fatalf("it needs that output equals to 33 bytes array which has only 0x00") 118 } 119 } 120 121 // Swap(CombinablePubKeySlice method) 122 // this is obvious but test in case 123 func TestSwap(t *testing.T) { 124 // test for a normal situation 125 // input: CombinablePubKeySlice contains 126 // PubKey: pubKeyCmpd0 127 // PubKey: pubKeyCmpd1 128 // want: first elm and second elm is swapped 129 inPubKey0, _ := koblitz.ParsePubKey(pubKeyCmpd0[:], koblitz.S256()) 130 inPubKey1, _ := koblitz.ParsePubKey(pubKeyCmpd1[:], koblitz.S256()) 131 var c CombinablePubKeySlice = []*koblitz.PublicKey{inPubKey0, inPubKey1} 132 133 c.Swap(0, 1) 134 if !c[0].IsEqual(inPubKey1) { 135 t.Fatalf("it needs to be equal") 136 } 137 if !c[1].IsEqual(inPubKey0) { 138 t.Fatalf("it needs to be equal") 139 } 140 141 // test for an anomaly situation 142 // input: CombinablePubKeySlice contains 143 // PubKey: ivalidPubKeyCmpd 144 // PubKey: pubKeyCmpd1 145 // want: first elm and second elm is swapped even if elms has nil 146 inPubKeyNil, _ := koblitz.ParsePubKey(invalidPubKeyCmpd[:], koblitz.S256()) 147 var c0Nil CombinablePubKeySlice = []*koblitz.PublicKey{inPubKeyNil, inPubKey1} 148 149 c0Nil.Swap(0, 1) 150 if !c0Nil[0].IsEqual(inPubKey1) { 151 t.Fatalf("it needs to be equal") 152 } 153 if c0Nil[1] != inPubKeyNil { // can not use IsEqual because inPubKeyNil is nil 154 t.Fatalf("it needs to be equal") 155 } 156 } 157 158 // Len(CombinablePubKeySlice method) 159 // this is obvious but test in case 160 func TestLen(t *testing.T) { 161 // test a normal situation 162 // input: CombinablePubKeySlice contains 163 // PubKey: pubKeyCmpd0 164 // PubKey: pubKeyCmpd1 165 // want: 2 166 inPubKey0, _ := koblitz.ParsePubKey(pubKeyCmpd0[:], koblitz.S256()) 167 inPubKey1, _ := koblitz.ParsePubKey(pubKeyCmpd1[:], koblitz.S256()) 168 var c CombinablePubKeySlice = []*koblitz.PublicKey{inPubKey0, inPubKey1} 169 170 if c.Len() != 2 { 171 t.Fatalf("it needs to be equal") 172 } 173 174 // test for an anomaly situation 175 // input: CombinablePubKeySlice contains 176 // PubKey: ivalidPubKeyCmpd 177 // PubKey: pubKeyCmpd1 178 // want: 2 even if elms has nil 179 inPubKeyNil, _ := koblitz.ParsePubKey(invalidPubKeyCmpd[:], koblitz.S256()) 180 var c0Nil CombinablePubKeySlice = []*koblitz.PublicKey{inPubKeyNil, inPubKey1} 181 182 if c0Nil.Len() != 2 { 183 t.Fatalf("it needs to be equal") 184 } 185 } 186 187 // Less(CombinablePubKeySlice method) 188 // this is obvious but test in case 189 func TestLess(t *testing.T) { 190 // test a normal situation 191 // input: CombinablePubKeySlice contains 192 // PubKey: pubKeyCmpd0 193 // PubKey: pubKeyCmpd1 194 inPubKey0, _ := koblitz.ParsePubKey(pubKeyCmpd0[:], koblitz.S256()) 195 inPubKey1, _ := koblitz.ParsePubKey(pubKeyCmpd1[:], koblitz.S256()) 196 var c CombinablePubKeySlice = []*koblitz.PublicKey{inPubKey0, inPubKey1} 197 198 if c.Less(0, 1) != true { 199 t.Fatalf("it needs to be equal") 200 } 201 if c.Less(1, 0) != false { 202 t.Fatalf("it needs to be equal") 203 } 204 if c.Less(0, 0) != false { 205 t.Fatalf("it needs to be equal") 206 } 207 if c.Less(1, 1) != false { 208 t.Fatalf("it needs to be equal") 209 } 210 211 // test an anomaly situation 212 // TODO: fix it 213 // panic if one of compared pubkeys is nil 214 215 //inPubKeyNil, _ := koblitz.ParsePubKey(invalidPubKeyCmpd[:], koblitz.S256()) 216 //var c0Nil CombinablePubKeySlice = []*koblitz.PublicKey{inPubKeyNil, inPubKey1} 217 /* 218 if c0Nil.Less(0, 1) != true { 219 t.Fatalf("it needs to be equal") 220 } 221 if c0Nil.Less(1, 0) != false { 222 t.Fatalf("it needs to be equal") 223 } 224 if c0Nil.Less(0, 0) != false { 225 t.Fatalf("it needs to be equal") 226 } 227 if c0Nil.Less(1, 1) != false { 228 t.Fatalf("it needs to be equal") 229 } 230 */ 231 } 232 233 // ComboCommit(CombinablePubKeySlice method) 234 func TestComboCommit(t *testing.T) { 235 // test a normal situation 236 // input: CombinablePubKeySlice contains 237 // PubKey: pubKeyCmpd0 238 // PubKey: pubKeyCmpd1 239 // want: wantH, chainhash.Hash 240 inPubKey0, _ := koblitz.ParsePubKey(pubKeyCmpd0[:], koblitz.S256()) 241 inPubKey1, _ := koblitz.ParsePubKey(pubKeyCmpd1[:], koblitz.S256()) 242 var c CombinablePubKeySlice = []*koblitz.PublicKey{inPubKey0, inPubKey1} 243 244 var wantH chainhash.Hash = [32]byte{ 245 0x4d, 0x3e, 0x71, 0x71, 0xa1, 0x98, 0x2f, 0x96, 246 0x23, 0x5f, 0x7b, 0x6b, 0x39, 0x95, 0x71, 0x5d, 247 0x29, 0x43, 0x35, 0x25, 0x16, 0x27, 0x4e, 0x0d, 248 0xf0, 0xae, 0xb5, 0x43, 0x60, 0x4a, 0x4d, 0x08} 249 250 if c.ComboCommit() != wantH { 251 t.Fatalf("it needs to be equal") 252 } 253 254 // test an anomaly situation 255 // TODO: fix it 256 // panic if one of pubkeys is nil 257 258 //inPubKeyNil, _ := koblitz.ParsePubKey(invalidPubKeyCmpd[:], koblitz.S256()) 259 //want := ... 260 //var c0Nil CombinablePubKeySlice = []*koblitz.PublicKey{inPubKeyNil, inPubKey1} 261 //if c0Nil.ComboCommit() != want { 262 // t.Fatalf("it needs to be equal") 263 //} 264 } 265 266 // CombinePrivKeyAndSubtract 267 func TestCombinePrivKeyAndSubtract(t *testing.T) { 268 // test a normal situation 269 // input1: inPrivKey0, koblitz.PrivateKey 270 // input2: inPrivKey1, koblitz.PrivateKey 271 // want: wantB, 32 bytes array 272 inPrivKey0, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), privKeyB0) 273 inPrivKeyB1 := privKeyB1 274 wantB := [32]byte{ 275 0xfc, 0x92, 0xf7, 0x65, 0x42, 0xd7, 0x96, 0xb8, 276 0x21, 0xba, 0x8d, 0xf6, 0xcd, 0x2a, 0x9e, 0x0a, 277 0xe9, 0x72, 0xb7, 0x0f, 0xaf, 0x10, 0x5c, 0xb1, 278 0x49, 0xfd, 0x41, 0x9a, 0x94, 0x84, 0x11, 0xf4} 279 if CombinePrivKeyAndSubtract(inPrivKey0, inPrivKeyB1) != wantB { 280 t.Fatalf("it needs to be equal") 281 } 282 } 283 284 // CombinePrivKeyWithBytes 285 func TestCombinePrivKeyWithBytes(t *testing.T) { 286 // test a normal situation 287 // input1: inPrivKey0, koblitz.PrivateKey 288 // input2: inPrivKeyB1, byte slice 289 // want: wantPrivKey, koblitz.PrivateKey 290 inPrivKey0, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), privKeyB0) 291 inPrivKeyB1 := privKeyB1 292 wantPrivKey, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), []byte{ 293 0xfc, 0x92, 0xf7, 0x65, 0x42, 0xd7, 0x96, 0xb8, 294 0x21, 0xba, 0x8d, 0xf6, 0xcd, 0x2a, 0x9e, 0x0a, 295 0xe9, 0x72, 0xb7, 0x0f, 0xaf, 0x10, 0x5c, 0xb1, 296 0x49, 0xfd, 0x41, 0x9a, 0x94, 0x84, 0x11, 0xf5}) 297 if CombinePrivKeyWithBytes(inPrivKey0, inPrivKeyB1).D.Cmp(wantPrivKey.D) != 0 { 298 t.Fatalf("it needs to be equal") 299 } 300 } 301 302 // CombinePrivateKeys 303 // this can have some parameters but the parameters are not as slice or array. 304 // it should be already tested as golang itself if too many parameters 305 func TestCombinePrivateKeys(t *testing.T) { 306 // test a normal situation 307 // the number of input: 2 308 // input1: inPrivKey0, koblitz.PrivateKey 309 // input2: inPrivKey1, koblitz.PrivateKey 310 // want: wantPrivKey, koblitz.PrivateKey 311 inPrivKey0, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), privKeyB0) 312 inPrivKey1, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), privKeyB1) 313 wantPrivKey, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), []byte{ 314 0xfc, 0x92, 0xf7, 0x65, 0x42, 0xd7, 0x96, 0xb8, 315 0x21, 0xba, 0x8d, 0xf6, 0xcd, 0x2a, 0x9e, 0x0a, 316 0xe9, 0x72, 0xb7, 0x0f, 0xaf, 0x10, 0x5c, 0xb1, 317 0x49, 0xfd, 0x41, 0x9a, 0x94, 0x84, 0x11, 0xf5}) 318 if CombinePrivateKeys(inPrivKey0, inPrivKey1).D.Cmp(wantPrivKey.D) != 0 { 319 t.Fatalf("it needs to be equal") 320 } 321 322 // test a normal situation 323 // the number of input: 1 324 // input1: inPrivKey0, koblitz.PrivateKey 325 // want: inPrivKey0, koblitz.PrivateKey, 326 // it should return input itself if the number of parameters is one 327 if CombinePrivateKeys(inPrivKey0) != inPrivKey0 { 328 t.Fatalf("it needs to be equal") 329 } 330 331 // test an anomaly situation 332 // the number of input: 1 333 // input1: koblitz.PrivateKey but nil 334 // want: nil 335 var inNil *koblitz.PrivateKey 336 if CombinePrivateKeys(inNil) != nil { 337 t.Fatalf("it needs to be nil") 338 } 339 } 340 341 // ElkScalar 342 func TestElkScalar(t *testing.T) { 343 // test a normal situation 344 // input: inHash, chainhash.Hash 345 // want: wantHash, chainhash.Hash 346 var inHash chainhash.Hash = [32]byte{ 347 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 348 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 349 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 350 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} 351 var wantHash chainhash.Hash = [32]byte{ 352 0x49, 0xb8, 0x31, 0x3d, 0xe7, 0xec, 0x03, 0xb1, 353 0x21, 0xdc, 0x26, 0xf4, 0x9a, 0x51, 0x6d, 0xb7, 354 0x46, 0xde, 0x9b, 0xd5, 0x6e, 0x46, 0x87, 0x4a, 355 0xcf, 0xb1, 0xd2, 0xd0, 0xba, 0x72, 0x37, 0xe9} 356 if ElkScalar(&inHash) != wantHash { 357 t.Fatalf("it needs to be equal") 358 } 359 } 360 361 // ElkPointFromHash 362 func TestElkPointFromHash(t *testing.T) { 363 // test a normal situation 364 // input: inHash, chainhash.Hash 365 // want: wantB33, 33 bytes array 366 var inHash chainhash.Hash = [32]byte{ 367 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 368 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 369 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 370 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} 371 var wantB33 = [33]byte{ 372 0x02, 0x8d, 0x73, 0x16, 0x29, 0xe3, 0x3a, 0xad, 373 0x54, 0xa4, 0xc1, 0x9b, 0xd7, 0xe3, 0x1a, 0xba, 374 0x65, 0xe2, 0xde, 0xba, 0x7b, 0xfc, 0x4b, 0xa2, 375 0xc9, 0xb9, 0xb1, 0x71, 0xff, 0xae, 0x42, 0x71, 376 0xfd} 377 if ElkPointFromHash(&inHash) != wantB33 { 378 t.Fatalf("it needs to be equal") 379 } 380 } 381 382 // PubFromHash 383 func TestPubFromHash(t *testing.T) { 384 // test a normal situation 385 // input: inHash, chainhash.Hash 386 // want: wantB33, 33 bytes array 387 var inHash chainhash.Hash = [32]byte{ 388 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 389 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 390 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 391 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} 392 var wantB33 = [33]byte{ 393 0x02, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 394 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 395 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 396 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 397 0x98} 398 if PubFromHash(inHash) != wantB33 { 399 t.Fatalf("it needs to be equal") 400 } 401 }