-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ethereum/carver/optional-backends
Add pysha3 backend, better auto backend selection
- Loading branch information
Showing
21 changed files
with
327 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
eth\_hash\.backends package | ||
=========================== | ||
|
||
Submodules | ||
---------- | ||
|
||
eth\_hash\.backends\.pycryptodome module | ||
---------------------------------------- | ||
|
||
.. automodule:: eth_hash.backends.pycryptodome | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
eth\_hash\.backends\.pysha3 module | ||
---------------------------------- | ||
|
||
.. automodule:: eth_hash.backends.pysha3 | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
|
||
Module contents | ||
--------------- | ||
|
||
.. automodule:: eth_hash.backends | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
eth\_hash package | ||
================= | ||
|
||
eth\_hash\.auto module | ||
---------------------- | ||
|
||
.. automodule:: eth_hash.auto | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
eth\_hash\.main module | ||
---------------------- | ||
|
||
.. automodule:: eth_hash.main | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
Quickstart | ||
============ | ||
|
||
Choose a hashing backend | ||
--------------------------- | ||
|
||
If you're not sure, choose "pycryptodome" because it supports pypy3. | ||
|
||
You can find a full list of each currently supported backend in :mod:`eth_hash.backends`. | ||
|
||
Install | ||
---------- | ||
|
||
Put the backend you would like to use in brackets during install, like: | ||
|
||
.. code-block:: shell | ||
pip install eth-hash[pycryptodome] | ||
Compute a Keccak256 Hash | ||
----------------------------- | ||
|
||
.. doctest:: | ||
|
||
>>> from eth_hash.auto import keccak | ||
>>> keccak(b'') | ||
b"\xc5\xd2F\x01\x86\xf7#<\x92~}\xb2\xdc\xc7\x03\xc0\xe5\x00\xb6S\xca\x82';{\xfa\xd8\x04]\x85\xa4p" | ||
|
||
Select one of many installed backends | ||
--------------------------------------- | ||
|
||
If you have several backends installed, you may want to | ||
explicitly specify which one to load. You can specify | ||
in an environment variable, or at runtime. | ||
|
||
Specify backend by environment variable | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. code-block:: shell | ||
$ ETH_HASH_BACKEND="pysha3" python | ||
>>> from eth_hash.auto import keccak | ||
# This runs with the pysha3 backend | ||
>>> keccak(b'') | ||
b"\xc5\xd2F\x01\x86\xf7#<\x92~}\xb2\xdc\xc7\x03\xc0\xe5\x00\xb6S\xca\x82';{\xfa\xd8\x04]\x85\xa4p" | ||
Specify backend at runtime | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. code-block:: python | ||
>>> from eth_hash.backends import pysha3 | ||
>>> from eth_hash import Keccak256 | ||
>>> keccak = Keccak256(pysha3) | ||
>>> keccak(b'') | ||
b"\xc5\xd2F\x01\x86\xf7#<\x92~}\xb2\xdc\xc7\x03\xc0\xe5\x00\xb6S\xca\x82';{\xfa\xd8\x04]\x85\xa4p" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
from .backends import ( | ||
pycryptodome, | ||
) | ||
from .main import ( | ||
from eth_hash.main import ( | ||
Keccak256, | ||
) | ||
from eth_hash.utils import ( | ||
auto_choose_backend, | ||
) | ||
|
||
keccak = Keccak256(pycryptodome) | ||
keccak = Keccak256(auto_choose_backend()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
''' | ||
A collection of optional backends. You must manually select and | ||
install the backend you want. If the backend is not installed, | ||
then trying to import the module for that backend will cause | ||
an :class:`ImportError`. | ||
See :ref:`Choose a hashing backend` for more. | ||
''' | ||
|
||
SUPPORTED_BACKENDS = [ | ||
'pycryptodome', # prefer this over pysha3, for pypy3 support | ||
'pysha3', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from sha3 import ( | ||
keccak_256, | ||
) | ||
|
||
|
||
def keccak256(prehash: bytes) -> bytes: | ||
return keccak_256(prehash).digest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import importlib | ||
import os | ||
|
||
from eth_hash.backends import ( | ||
SUPPORTED_BACKENDS, | ||
) | ||
|
||
|
||
def auto_choose_backend(): | ||
env_backend = get_backend_in_environment() | ||
|
||
if env_backend: | ||
return load_environment_backend(env_backend) | ||
else: | ||
return choose_available_backend() | ||
|
||
|
||
def get_backend_in_environment(): | ||
return os.environ.get('ETH_HASH_BACKEND', None) | ||
|
||
|
||
def load_backend(backend_name): | ||
return importlib.import_module('eth_hash.backends.%s' % backend_name) | ||
|
||
|
||
def load_environment_backend(env_backend): | ||
if env_backend in SUPPORTED_BACKENDS: | ||
try: | ||
return load_backend(env_backend) | ||
except ImportError as e: | ||
raise ImportError( | ||
"The backend specified in ETH_HASH_BACKEND, '{0}', is not installed. " | ||
"Install with `pip install eth-hash[{0}]`.".format(env_backend) | ||
) | ||
else: | ||
raise ValueError( | ||
"The backend specified in ETH_HASH_BACKEND, %r, is not supported. " | ||
"Choose one of: %r" % (env_backend, SUPPORTED_BACKENDS) | ||
) | ||
|
||
|
||
def choose_available_backend(): | ||
for backend in SUPPORTED_BACKENDS: | ||
try: | ||
return load_backend(backend) | ||
except ImportError: | ||
pass | ||
raise ImportError("None of these backends are available: %r" % SUPPORTED_BACKENDS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture(params=['auto', 'explicit', 'env']) | ||
def keccak(monkeypatch, request, keccak_auto): | ||
if request.param == 'auto': | ||
return keccak_auto | ||
elif request.param == 'explicit': | ||
from eth_hash.backends import pycryptodome | ||
from eth_hash import Keccak256 | ||
return Keccak256(pycryptodome) | ||
elif request.param == 'env': | ||
monkeypatch.setenv('ETH_HASH_BACKEND', 'pycryptodome') | ||
from eth_hash.auto import keccak | ||
return keccak | ||
else: | ||
raise AssertionError("Unrecognized approach to import keccak: %s" % request.param) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../test_results.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture(params=['auto', 'explicit', 'env']) | ||
def keccak(monkeypatch, request, keccak_auto): | ||
if request.param == 'auto': | ||
return keccak_auto | ||
elif request.param == 'explicit': | ||
from eth_hash.backends import pysha3 | ||
from eth_hash import Keccak256 | ||
return Keccak256(pysha3) | ||
elif request.param == 'env': | ||
monkeypatch.setenv('ETH_HASH_BACKEND', 'pysha3') | ||
from eth_hash.auto import keccak | ||
return keccak | ||
else: | ||
raise AssertionError("Unrecognized approach to import keccak: %s" % request.param) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../test_results.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
|
||
@pytest.fixture | ||
def keccak(): | ||
def keccak_auto(): | ||
from eth_hash.auto import keccak | ||
return keccak | ||
|
||
|
Oops, something went wrong.