Skip to content

Commit 1a9a82b

Browse files
authored
v1.0.14 Do not use cpp_sign() (#64)
* Add C++ library instructions * Do not use cpp_sign() * v1.0.14
1 parent 2df5cda commit 1a9a82b

3 files changed

Lines changed: 45 additions & 18 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,16 @@ web3_client_with_keys = Client(
119119
web3_client_with_keys.onboarding.create_user()
120120
web3_client_with_keys.api_keys.create_api_key()
121121
```
122+
123+
### Using the C++ Library for STARK Signing
124+
125+
By default, STARK curve operations such as signing and verification will use the Python native implementation. These operations occur whenever placing an order or requesting a withdrawal. To use the C++ implementation, initialize the client object with `crypto_c_exports_path`:
126+
127+
```python
128+
client = Client(
129+
crypto_c_exports_path='./libcrypto_c_exports.so',
130+
...
131+
)
132+
```
133+
134+
The path should point to a C++ shared library file, built from Starkware's `crypto-cpp` library ([CMake target](https://github.com/starkware-libs/crypto-cpp/blob/601de408bee9f897315b8a5cb0c88e2450a91282/src/starkware/crypto/ffi/CMakeLists.txt#L3)) for the particular platform (e.g. Linux, etc.) that you are running your trading program on.
Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
from dydx3.starkex.starkex_resources.cpp_signature import (
2-
check_cpp_lib_path, cpp_hash, cpp_sign, cpp_verify
3-
)
4-
from dydx3.starkex.starkex_resources.python_signature import (
5-
py_sign, ECSignature, ECPoint, py_verify, py_pedersen_hash
6-
)
71
from typing import Optional, Union
82

9-
def sign(msg_hash: int, priv_key: int, seed: Optional[int] = None) -> ECSignature:
10-
if check_cpp_lib_path():
11-
return cpp_sign(msg_hash=msg_hash, priv_key=priv_key, seed=seed)
3+
from dydx3.starkex.starkex_resources.cpp_signature import check_cpp_lib_path
4+
from dydx3.starkex.starkex_resources.cpp_signature import cpp_hash
5+
from dydx3.starkex.starkex_resources.cpp_signature import cpp_verify
6+
from dydx3.starkex.starkex_resources.python_signature import ECPoint
7+
from dydx3.starkex.starkex_resources.python_signature import ECSignature
8+
from dydx3.starkex.starkex_resources.python_signature import py_pedersen_hash
9+
from dydx3.starkex.starkex_resources.python_signature import py_sign
10+
from dydx3.starkex.starkex_resources.python_signature import py_verify
1211

13-
return py_sign(msg_hash=msg_hash, priv_key=priv_key, seed=seed)
1412

15-
def verify(msg_hash: int, r: int, s: int, public_key: Union[int, ECPoint]) -> bool:
16-
if check_cpp_lib_path():
17-
return cpp_verify(msg_hash=msg_hash, r=r, s=s, stark_key=public_key)
13+
def sign(
14+
msg_hash: int,
15+
priv_key: int,
16+
seed: Optional[int] = None,
17+
) -> ECSignature:
18+
# Note: cpp_sign() is not optimized and is currently slower than py_sign().
19+
# So always use py_sign() for now.
20+
return py_sign(msg_hash=msg_hash, priv_key=priv_key, seed=seed)
21+
22+
23+
def verify(
24+
msg_hash: int,
25+
r: int,
26+
s: int,
27+
public_key: Union[int, ECPoint],
28+
) -> bool:
29+
if check_cpp_lib_path():
30+
return cpp_verify(msg_hash=msg_hash, r=r, s=s, stark_key=public_key)
31+
32+
return py_verify(msg_hash=msg_hash, r=r, s=s, public_key=public_key)
1833

19-
return py_verify(msg_hash=msg_hash, r=r, s=s, public_key=public_key)
2034

2135
def get_hash(*elements: int) -> int:
22-
if check_cpp_lib_path():
23-
return cpp_hash(*elements)
36+
if check_cpp_lib_path():
37+
return cpp_hash(*elements)
2438

25-
return py_pedersen_hash(*elements)
39+
return py_pedersen_hash(*elements)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
setup(
2121
name='dydx-v3-python',
22-
version='1.0.13',
22+
version='1.0.14',
2323
packages=find_packages(),
2424
package_data={
2525
'dydx3': [

0 commit comments

Comments
 (0)