|
1 | 1 | import 'dart:typed_data'; |
2 | | - |
| 2 | +import 'package:blockchain_utils/crypto/crypto/cdsa/secp256k1/secp256k1.dart'; |
3 | 3 | import 'package:blockchain_utils/utils/utils.dart'; |
4 | 4 | import 'package:blockchain_utils/bip/bip/bip32/base/ibip32_key_derivator.dart'; |
5 | 5 | import 'package:blockchain_utils/bip/bip/bip32/bip32_key_data.dart'; |
@@ -57,16 +57,45 @@ class Bip32Slip10EcdsaDerivator implements IBip32KeyDerivator { |
57 | 57 |
|
58 | 58 | final ilBytes = hmacHalves.item1; |
59 | 59 | final irBytes = hmacHalves.item2; |
60 | | - final ilInt = BigintUtils.fromBytes(ilBytes); |
61 | | - final privKeyInt = BigintUtils.fromBytes(privKeyBytes); |
62 | | - final generator = EllipticCurveGetter.generatorFromType(type); |
63 | | - final scalar = (ilInt + privKeyInt) % generator.order!; |
| 60 | + final scalar = |
| 61 | + _addScalar(privKeyBytes: privKeyBytes, newScalar: ilBytes, type: type); |
64 | 62 | final newPrivKeyBytes = BigintUtils.toBytes(scalar, |
65 | 63 | order: Endian.big, length: privKey.privKey.length); |
66 | 64 |
|
67 | 65 | return Tuple(newPrivKeyBytes, irBytes); |
68 | 66 | } |
69 | 67 |
|
| 68 | + BigInt _addScalar( |
| 69 | + {required List<int> privKeyBytes, |
| 70 | + required List<int> newScalar, |
| 71 | + required EllipticCurveTypes type}) { |
| 72 | + switch (type) { |
| 73 | + case EllipticCurveTypes.secp256k1: |
| 74 | + Secp256k1Scalar privKeyScalar = Secp256k1Scalar(); |
| 75 | + Secp256k1.secp256k1ScalarSetB32(privKeyScalar, privKeyBytes); |
| 76 | + Secp256k1Scalar newSc = Secp256k1Scalar(); |
| 77 | + Secp256k1.secp256k1ScalarSetB32(newSc, newScalar); |
| 78 | + Secp256k1Scalar result = Secp256k1Scalar(); |
| 79 | + Secp256k1.secp256k1ScalarAdd(result, privKeyScalar, newSc); |
| 80 | + final scBytes = List<int>.filled(32, 0); |
| 81 | + Secp256k1.secp256k1ScalarGetB32(scBytes, result); |
| 82 | + final nd = BigintUtils.fromBytes(scBytes); |
| 83 | + |
| 84 | + final ilInt = BigintUtils.fromBytes(newScalar); |
| 85 | + final privKeyInt = BigintUtils.fromBytes(privKeyBytes); |
| 86 | + final generator = EllipticCurveGetter.generatorFromType(type); |
| 87 | + final newScalarBig = (ilInt + privKeyInt) % generator.order!; |
| 88 | + assert(newScalarBig == nd); |
| 89 | + return nd; |
| 90 | + |
| 91 | + default: |
| 92 | + final ilInt = BigintUtils.fromBytes(newScalar); |
| 93 | + final privKeyInt = BigintUtils.fromBytes(privKeyBytes); |
| 94 | + final generator = EllipticCurveGetter.generatorFromType(type); |
| 95 | + return (ilInt + privKeyInt) % generator.order!; |
| 96 | + } |
| 97 | + } |
| 98 | + |
70 | 99 | /// Derive a child public key from the given parent public key using the provided |
71 | 100 | /// index and elliptic curve type. |
72 | 101 | /// |
|
0 commit comments