예제 #1
0
파일: bip32.py 프로젝트: kodxana/torba
 def _private_key_secret_exponent(cls, private_key):
     """ Return the private key as a secret exponent if it is a valid private key. """
     if not isinstance(private_key, (bytes, bytearray)):
         raise TypeError('private key must be raw bytes')
     if len(private_key) != 32:
         raise ValueError('private key must be 32 bytes')
     exponent = bytes_to_int(private_key)
     if not 1 <= exponent < cls.CURVE.order:
         raise ValueError('private key represents an invalid exponent')
     return exponent
예제 #2
0
파일: bip32.py 프로젝트: kodxana/torba
    def child(self, n):
        """ Return the derived child extended private key 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.private_key_bytes
        else:
            serkey = self.public_key.pubkey_bytes

        msg = serkey + struct.pack('>I', n)
        L, R = self._hmac_sha512(msg)  # pylint: disable=invalid-name

        curve = self.CURVE
        L = bytes_to_int(L)  # pylint: disable=invalid-name
        exponent = (L + bytes_to_int(self.private_key_bytes)) % curve.order
        if exponent == 0 or L >= curve.order:
            raise DerivationError

        privkey = _exponent_to_bytes(exponent)

        return PrivateKey(self.ledger, privkey, R, n, self.depth + 1, self)
예제 #3
0
파일: hash.py 프로젝트: hackrush01/torba
    def encode(cls, be_bytes):
        """Converts a big-endian bytearray into a base58 string."""
        value = bytes_to_int(be_bytes)

        txt = u''
        while value:
            value, mod = divmod(value, 58)
            txt += cls.chars[mod]

        for byte in be_bytes:
            if byte != 0:
                break
            txt += u'1'

        return txt[::-1].encode()
예제 #4
0
파일: bip32.py 프로젝트: kodxana/torba
    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)  # pylint: disable=invalid-name

        curve = self.CURVE
        L = bytes_to_int(L)  # pylint: disable=invalid-name
        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(self.ledger, verkey, R, n, self.depth + 1, self)
예제 #5
0
파일: bip32.py 프로젝트: kodxana/torba
    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()  # pylint: disable=invalid-name
        y2 = pow(x, 3, p) + b  # pylint: disable=invalid-name
        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)