safe-eth-native 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {safe_eth_native-0.1.0 → safe_eth_native-0.2.0}/Cargo.lock +1 -1
- {safe_eth_native-0.1.0 → safe_eth_native-0.2.0}/Cargo.toml +1 -1
- {safe_eth_native-0.1.0 → safe_eth_native-0.2.0}/PKG-INFO +3 -2
- {safe_eth_native-0.1.0 → safe_eth_native-0.2.0}/README.md +2 -1
- {safe_eth_native-0.1.0 → safe_eth_native-0.2.0}/safe_eth_native.pyi +3 -1
- {safe_eth_native-0.1.0 → safe_eth_native-0.2.0}/src/lib.rs +23 -9
- {safe_eth_native-0.1.0 → safe_eth_native-0.2.0}/LICENSE +0 -0
- {safe_eth_native-0.1.0 → safe_eth_native-0.2.0}/pyproject.toml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: safe-eth-native
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Classifier: Intended Audience :: Developers
|
|
5
5
|
Classifier: License :: OSI Approved :: MIT License
|
|
6
6
|
Classifier: Programming Language :: Python :: 3.11
|
|
@@ -37,7 +37,8 @@ Native (Rust) versions of the hot paths of [safe-eth-py](https://github.com/safe
|
|
|
37
37
|
precompiles: `v` must be 27 or 28, `r` and `s` in `[1, n - 1]`, and high `s` values are
|
|
38
38
|
accepted. Values out of those ranges return `None` / `False` instead of raising; only a
|
|
39
39
|
wrong type, or a hash that is not 32 bytes for `ecrecover`, raises. Both release the GIL
|
|
40
|
-
while they run, and so does `keccak256` for inputs of 2048 bytes or more.
|
|
40
|
+
while they run, and so does `keccak256` for inputs of 2048 bytes or more. `keccak256` hashes
|
|
41
|
+
the raw bytes of `bytes` or of any C-contiguous buffer, like `hashlib`.
|
|
41
42
|
|
|
42
43
|
The package is not meant to be used directly. Install it with the `native` extra of
|
|
43
44
|
safe-eth-py, which uses it when it is installed and falls back to the Python code when it is
|
|
@@ -14,7 +14,8 @@ Native (Rust) versions of the hot paths of [safe-eth-py](https://github.com/safe
|
|
|
14
14
|
precompiles: `v` must be 27 or 28, `r` and `s` in `[1, n - 1]`, and high `s` values are
|
|
15
15
|
accepted. Values out of those ranges return `None` / `False` instead of raising; only a
|
|
16
16
|
wrong type, or a hash that is not 32 bytes for `ecrecover`, raises. Both release the GIL
|
|
17
|
-
while they run, and so does `keccak256` for inputs of 2048 bytes or more.
|
|
17
|
+
while they run, and so does `keccak256` for inputs of 2048 bytes or more. `keccak256` hashes
|
|
18
|
+
the raw bytes of `bytes` or of any C-contiguous buffer, like `hashlib`.
|
|
18
19
|
|
|
19
20
|
The package is not meant to be used directly. Install it with the `native` extra of
|
|
20
21
|
safe-eth-py, which uses it when it is installed and falls back to the Python code when it is
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
from typing_extensions import Buffer
|
|
2
|
+
|
|
1
3
|
def ecrecover(message_hash: bytes, v: int, r: int, s: int) -> bytes | None: ...
|
|
2
4
|
def p256_verify(
|
|
3
5
|
message_hash: int, r: int, s: int, public_key_x: int, public_key_y: int
|
|
4
6
|
) -> bool: ...
|
|
5
|
-
def keccak256(data:
|
|
7
|
+
def keccak256(data: Buffer) -> bytes: ...
|
|
6
8
|
def to_checksum_address(value: str | bytes) -> str: ...
|
|
7
9
|
def is_checksum_address(value: object) -> bool: ...
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
use pyo3::buffer::
|
|
2
|
-
use pyo3::exceptions::{PyTypeError, PyValueError};
|
|
1
|
+
use pyo3::buffer::PyUntypedBuffer;
|
|
2
|
+
use pyo3::exceptions::{PyBufferError, PyTypeError, PyValueError};
|
|
3
3
|
use pyo3::prelude::*;
|
|
4
4
|
use pyo3::types::{PyBytes, PyInt, PyString};
|
|
5
5
|
use secp256k1::Message;
|
|
@@ -147,21 +147,35 @@ fn keccak256_digest_releasing_gil(py: Python<'_>, data: &[u8]) -> [u8; 32] {
|
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
/// Keccak-256 digest (Ethereum flavour, not NIST SHA3-256) of
|
|
151
|
-
/// other buffer
|
|
150
|
+
/// Keccak-256 digest (Ethereum flavour, not NIST SHA3-256) of the raw bytes of `bytes` or of
|
|
151
|
+
/// any other C-contiguous buffer (`bytearray`, `memoryview`, `array`...), like `hashlib`.
|
|
152
152
|
///
|
|
153
|
-
/// Raises `TypeError` for
|
|
153
|
+
/// Raises `TypeError` for an object that is not a buffer, and `BufferError` for a buffer
|
|
154
|
+
/// that is not C-contiguous.
|
|
154
155
|
#[pyfunction]
|
|
155
156
|
fn keccak256<'py>(py: Python<'py>, data: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyBytes>> {
|
|
156
157
|
if let Ok(data_bytes) = data.cast::<PyBytes>() {
|
|
157
158
|
let digest = keccak256_digest_releasing_gil(py, data_bytes.as_bytes());
|
|
158
159
|
return Ok(PyBytes::new(py, &digest));
|
|
159
160
|
}
|
|
161
|
+
let buffer = PyUntypedBuffer::get(data)
|
|
162
|
+
.map_err(|_| PyTypeError::new_err("Expected bytes or another object with a buffer"))?;
|
|
163
|
+
if !buffer.is_c_contiguous() {
|
|
164
|
+
return Err(PyBufferError::new_err("Expected a C-contiguous buffer"));
|
|
165
|
+
}
|
|
160
166
|
// Other threads can write to a mutable buffer while it is hashed, so it is copied first
|
|
161
|
-
let
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
167
|
+
let mut data_copy = vec![0u8; buffer.len_bytes()];
|
|
168
|
+
if !data_copy.is_empty() {
|
|
169
|
+
// SAFETY: `buffer` is held until the end of the function, so its `len_bytes`
|
|
170
|
+
// C-contiguous bytes at `buf_ptr` stay allocated
|
|
171
|
+
unsafe {
|
|
172
|
+
std::ptr::copy_nonoverlapping(
|
|
173
|
+
buffer.buf_ptr().cast::<u8>(),
|
|
174
|
+
data_copy.as_mut_ptr(),
|
|
175
|
+
data_copy.len(),
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
165
179
|
let digest = keccak256_digest_releasing_gil(py, &data_copy);
|
|
166
180
|
Ok(PyBytes::new(py, &digest))
|
|
167
181
|
}
|
|
File without changes
|
|
File without changes
|