def _privkey_secret_exponent(cls, privkey): '''Return the private key as a secret exponent if it is a valid private key.''' if not isinstance(privkey, (bytes, bytearray)): raise TypeError('privkey must be raw bytes') if len(privkey) != 32: raise ValueError('privkey must be 32 bytes') exponent = bytes_to_int(privkey) if not 1 <= exponent < cls.CURVE.order: raise ValueError('privkey represents an invalid exponent') return exponent
def child(self, n): '''Return the derived child extended privkey at index N.''' if not 0 <= n < (1 << 32): raise ValueError('invalid BIP32 private key child number') if n >= self.HARDENED: serkey = b'\0' + self.privkey_bytes else: serkey = self.public_key.pubkey_bytes msg = serkey + struct.pack('>I', n) L, R = self._hmac_sha512(msg) curve = self.CURVE L = bytes_to_int(L) exponent = (L + bytes_to_int(self.privkey_bytes)) % curve.order if exponent == 0 or L >= curve.order: raise DerivationError privkey = _exponent_to_bytes(exponent) return MasterPrivKey(privkey, R, n, self.depth + 1, self.fingerprint())
def encode(be_bytes): """Converts a big-endian bytearray into a base58 string.""" value = bytes_to_int(be_bytes) txt = '' while value: value, mod = divmod(value, 58) txt += Base58.chars[mod] for byte in be_bytes: if byte != 0: break txt += '1' return txt[::-1]
def child_verkey_R(self, n): if not 0 <= n < (1 << 31): raise ValueError('invalid BIP32 public key child number') msg = self.pubkey_bytes + struct.pack('>I', n) L, R = self._hmac_sha512(msg) curve = self.CURVE L = bytes_to_int(L) if L >= curve.order: raise DerivationError point = curve.generator * L + self.ec_point() if point == EC.INFINITY: raise DerivationError return ecdsa.VerifyingKey.from_public_point(point, curve=curve), R
def child(self, n): '''Return the derived child extended pubkey at index N.''' if not 0 <= n < (1 << 31): raise ValueError('invalid BIP32 public key child number') msg = self.pubkey_bytes + struct.pack('>I', n) L, R = self._hmac_sha512(msg) curve = self.CURVE L = bytes_to_int(L) if L >= curve.order: raise DerivationError point = curve.generator * L + self.ec_point() if point == EC.INFINITY: raise DerivationError verkey = ecdsa.VerifyingKey.from_public_point(point, curve=curve) return PubKey(verkey, R, n, self.depth + 1, self)
def _verifying_key_from_pubkey(cls, pubkey): '''Converts a 33-byte compressed pubkey into an ecdsa.VerifyingKey object''' if not isinstance(pubkey, (bytes, bytearray)): raise TypeError('pubkey must be raw bytes') if len(pubkey) != 33: raise ValueError('pubkey must be 33 bytes') if pubkey[0] not in (2, 3): raise ValueError('invalid pubkey prefix byte') curve = cls.CURVE.curve is_odd = pubkey[0] == 3 x = bytes_to_int(pubkey[1:]) # p is the finite field order a, b, p = curve.a(), curve.b(), curve.p() y2 = pow(x, 3, p) + b assert a == 0 # Otherwise y2 += a * pow(x, 2, p) y = NT.square_root_mod_prime(y2 % p, p) if bool(y & 1) != is_odd: y = p - y point = EC.Point(curve, x, y) return ecdsa.VerifyingKey.from_public_point(point, curve=cls.CURVE)
def test_bytes_to_int(): assert util.bytes_to_int(b'\x07[\xcd\x15') == 123456789