|
| 1 | +import { TinySecp256k1Interface } from './bip32'; |
| 2 | + |
| 3 | +const h = (hex: string): Buffer => Buffer.from(hex, 'hex'); |
| 4 | + |
| 5 | +export function testEcc(ecc: TinySecp256k1Interface): void { |
| 6 | + assert( |
| 7 | + ecc.isPoint( |
| 8 | + h('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'), |
| 9 | + ), |
| 10 | + ); |
| 11 | + assert( |
| 12 | + !ecc.isPoint( |
| 13 | + h('030000000000000000000000000000000000000000000000000000000000000005'), |
| 14 | + ), |
| 15 | + ); |
| 16 | + assert( |
| 17 | + ecc.isPrivate( |
| 18 | + h('79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'), |
| 19 | + ), |
| 20 | + ); |
| 21 | + // order - 1 |
| 22 | + assert( |
| 23 | + ecc.isPrivate( |
| 24 | + h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140'), |
| 25 | + ), |
| 26 | + ); |
| 27 | + // 0 |
| 28 | + assert( |
| 29 | + !ecc.isPrivate( |
| 30 | + h('0000000000000000000000000000000000000000000000000000000000000000'), |
| 31 | + ), |
| 32 | + ); |
| 33 | + // order |
| 34 | + assert( |
| 35 | + !ecc.isPrivate( |
| 36 | + h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'), |
| 37 | + ), |
| 38 | + ); |
| 39 | + // order + 1 |
| 40 | + assert( |
| 41 | + !ecc.isPrivate( |
| 42 | + h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364142'), |
| 43 | + ), |
| 44 | + ); |
| 45 | + assert( |
| 46 | + Buffer.from( |
| 47 | + ecc.pointFromScalar( |
| 48 | + h('b1121e4088a66a28f5b6b0f5844943ecd9f610196d7bb83b25214b60452c09af'), |
| 49 | + )!, |
| 50 | + ).equals( |
| 51 | + h('02b07ba9dca9523b7ef4bd97703d43d20399eb698e194704791a25ce77a400df99'), |
| 52 | + ), |
| 53 | + ); |
| 54 | + assert( |
| 55 | + Buffer.from( |
| 56 | + ecc.pointAddScalar( |
| 57 | + h('0379be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'), |
| 58 | + h('0000000000000000000000000000000000000000000000000000000000000003'), |
| 59 | + )!, |
| 60 | + ).equals( |
| 61 | + h('02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5'), |
| 62 | + ), |
| 63 | + ); |
| 64 | + assert( |
| 65 | + Buffer.from( |
| 66 | + ecc.privateAdd( |
| 67 | + h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036413e'), |
| 68 | + h('0000000000000000000000000000000000000000000000000000000000000002'), |
| 69 | + )!, |
| 70 | + ).equals( |
| 71 | + h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140'), |
| 72 | + ), |
| 73 | + ); |
| 74 | + assert( |
| 75 | + Buffer.from( |
| 76 | + ecc.sign( |
| 77 | + h('5e9f0a0d593efdcf78ac923bc3313e4e7d408d574354ee2b3288c0da9fbba6ed'), |
| 78 | + h('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140'), |
| 79 | + )!, |
| 80 | + ).equals( |
| 81 | + h( |
| 82 | + '54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed07082304410efa6b2943111b6a4e0aaa7b7db55a07e9861d1fb3cb1f421044a5', |
| 83 | + ), |
| 84 | + ), |
| 85 | + ); |
| 86 | + assert( |
| 87 | + ecc.verify( |
| 88 | + h('5e9f0a0d593efdcf78ac923bc3313e4e7d408d574354ee2b3288c0da9fbba6ed'), |
| 89 | + h('0379be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'), |
| 90 | + h( |
| 91 | + '54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed07082304410efa6b2943111b6a4e0aaa7b7db55a07e9861d1fb3cb1f421044a5', |
| 92 | + ), |
| 93 | + ), |
| 94 | + ); |
| 95 | + if (ecc.signSchnorr) { |
| 96 | + assert( |
| 97 | + Buffer.from( |
| 98 | + ecc.signSchnorr( |
| 99 | + h('7e2d58d8b3bcdf1abadec7829054f90dda9805aab56c77333024b9d0a508b75c'), |
| 100 | + h('c90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b14e5c9'), |
| 101 | + h('c87aa53824b4d7ae2eb035a2b5bbbccc080e76cdc6d1692c4b0b62d798e6d906'), |
| 102 | + )!, |
| 103 | + ).equals( |
| 104 | + h( |
| 105 | + '5831aaeed7b44bb74e5eab94ba9d4294c49bcf2a60728d8b4c200f50dd313c1bab745879a5ad954a72c45a91c3a51d3c7adea98d82f8481e0e1e03674a6f3fb7', |
| 106 | + ), |
| 107 | + ), |
| 108 | + ); |
| 109 | + } |
| 110 | + if (ecc.verifySchnorr) { |
| 111 | + assert( |
| 112 | + ecc.verifySchnorr( |
| 113 | + h('7e2d58d8b3bcdf1abadec7829054f90dda9805aab56c77333024b9d0a508b75c'), |
| 114 | + h('dd308afec5777e13121fa72b9cc1b7cc0139715309b086c960e18fd969774eb8'), |
| 115 | + h( |
| 116 | + '5831aaeed7b44bb74e5eab94ba9d4294c49bcf2a60728d8b4c200f50dd313c1bab745879a5ad954a72c45a91c3a51d3c7adea98d82f8481e0e1e03674a6f3fb7', |
| 117 | + ), |
| 118 | + ), |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function assert(bool: boolean): void { |
| 124 | + if (!bool) throw new Error('ecc library invalid'); |
| 125 | +} |
0 commit comments