Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: yoshimitsu777 on January 05, 2023, 01:45:07 PM



Title: iceland secp256k1 import in python
Post by: yoshimitsu777 on January 05, 2023, 01:45:07 PM
i downloaded
https://github.com/iceland2k14/secp256k1

into directory /home/yin4/secp256k1

Code:
yin4@qq:~ ls secp256k1
CHANGELOG.md  ice_secp256k1.dll  ice_secp256k1.so  LICENSE  __pycache__  README.md  secp256k1.py

when i am inside this directory it works good and i can import with command
Code:
yin4@qq:~/secp256k1$ python3

>>> import secp256k1 as ice

>>> ice.somecommand

but i want to have this working also if i start python3 console from /other/directory

i created sys environment variable
Code:
yin4@qq:/other/directory$ export PYTHONPATH=/home/yin4/secp256k1

Code:
yin4@qq:/other/directory$ python3

>>> import secp256k1 as ice

>>> ice.
then I press tab but no commands available only these:
Code:
>>> ice.__
ice.__annotations__     ice.__format__(         ice.__loader__          ice.__reduce_ex__(
ice.__class__(          ice.__ge__(             ice.__lt__(             ice.__repr__()
ice.__delattr__(        ice.__getattribute__(   ice.__name__            ice.__setattr__(
ice.__dict__            ice.__gt__(             ice.__ne__(             ice.__sizeof__()
ice.__dir__(            ice.__hash__()          ice.__new__(            ice.__spec__
ice.__doc__             ice.__init__(           ice.__package__         ice.__str__()
ice.__eq__(             ice.__init_subclass__(  ice.__path__            ice.__subclasshook__(
ice.__file__            ice.__le__(             ice.__reduce__()       

then i try

Code:
>>> from secp256k1 import secp256k1 as ice
File ice_secp256k1.so not found
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/yin4/secp256k1/secp256k1.py", line 92, in <module>
    ice.scalar_multiplication.argtypes = [ctypes.c_char_p, ctypes.c_char_p]   # pvk,ret
NameError: name 'ice' is not defined

what is wrong how can i fix and use library from all directorys?


Title: Re: iceland secp256k1 import in python
Post by: citb0in on January 05, 2023, 01:57:54 PM
I am not a Python expert, but as far as I understand it correctly, it is because the library is not found. Look at line 27 of secp256k1.py there you can see how iceland tries to load the needed ice_secp256k1.so file. You could try to put the absolute path there, e.g. like this:
Code:
    #dllfile = 'ice_secp256k1.so'
    dllfile = '/home/yin4/secp256k1/ice_secp256k1.so'

this should work but I bet there is a more convenient and best-practice way to achieve this. You will certainly get some better ideas on this from other users...


Title: Re: iceland secp256k1 import in python
Post by: yoshimitsu777 on January 05, 2023, 03:45:22 PM
fantastic now works
thanks.

is there more elegant way to solve this?


Title: Re: iceland secp256k1 import in python
Post by: NotATether on January 05, 2023, 06:13:16 PM
fantastic now works
thanks.

is there more elegant way to solve this?

Python has a "get home folder" and "get current directory" functions in tho os module, you can look into those. Especially if you never change directories and just want your CWD to be the same folder your binary (and libs) are located at.


Title: Re: iceland secp256k1 import in python
Post by: citb0in on January 06, 2023, 01:10:19 PM
fantastic now works
thanks.

is there more elegant way to solve this?

yes there is. I have written a patch that allows you to import iceland's secp256k1 module from any folder
==> PATCH (https://github.com/iceland2k14/secp256k1/pull/39/commits/f2b3aefd54b5bcd9d09d2a1e01630ac3bd30d2a0) <==

https://i.ibb.co/dMgNBgh/2023-01-06-14-03-45.png (https://ibb.co/dMgNBgh)

You can get the patch or clone the patched version from my git repository (https://github.com/citb0in/ice_secp256k1).

Without the patch, it would be necessary to change to the directory where iceland's secp256k1 files are located. Then you would run Python console with "python" or "python3" and import the module with:
Code:
import secp256k1 as ice
this only works because in the current working directory the file "secp256k1.py" exists and also the required shared library "ice_secp256k1.so" is located there. If you would try to start the Python console from another directory and load the module, it would fail with an error as you already noticed.

With the patch you can do it from any working directory, it doesn't matter then which is your working directory. The module will always find its required shared library "ice_secp256k1.so".

Additional note:

Since many users use both the standard Python module secp256k1 (https://pypi.org/project/secp256k1/) as well as iceland2k14/secp256k1 (https://github.com/iceland2k14/secp256k1) I would personally recommend renaming iceland's module to e.g. ice_secp256k1. Because the two modules are different. This will prevent you from shadowing the other module when importing one of the two modules.

I myself have placed all 3rd party python modules in ~/foo/python/modules
Iceland's secp256k1 module is located in ~/foo/python/modules/ice_secp256k1

To allow Python to search for modules in ~/foo/python/modules you can use the PYTHONPATH environment variable:
Code:
export PYTHONPATH=$PYTHONPATH:/home/citb0in/foo/python/modules
As you do not want to do this on each system start or terminal start it is suggested to add the export command mentioned above in your ~/.bashrc so it is executed each time you login or start a new terminal. You can ensure that everything is correct by
Code:
echo $PYTHONPATH

Whenever I need iceland's secp256k1 module, I import it like that:
Code:
python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> from ice_secp256k1 import secp256k1 as ice
>>> ice.privatekey_to_address(0, True, 123)
'141fTonryMQxmkKcba9FstmyQv3tqdBzTY'
>>>

Whenever I need Python's standard module "secp256k1" I just ue
Code:
import secp256k1

This way you can clearly distinguish between the two modules with the same name and you do not run the risk of confusion and problems.



Title: Re: iceland secp256k1 import in python
Post by: COBRAS on January 06, 2023, 05:25:13 PM
 :-XHow to use this lib for scalar to scalar/number's multiplication , addition, substraction ?

what function of modular inversion ?

Br


Title: Re: iceland secp256k1 import in python
Post by: citb0in on January 06, 2023, 05:27:09 PM
RTFM and see what functions are available and how they are used?

https://github.com/iceland2k14/secp256k1/blob/main/README.md


Title: Re: iceland secp256k1 import in python
Post by: COBRAS on January 06, 2023, 05:41:26 PM
RTFM and see what functions are available and how they are used?

https://github.com/iceland2k14/secp256k1/blob/main/README.md

Can't find function for scalar. Only mul pub to priv, no function priv * priv for ex.


Title: Re: iceland secp256k1 import in python
Post by: citb0in on January 06, 2023, 05:51:59 PM
RTFM and see what functions are available and how they are used?

https://github.com/iceland2k14/secp256k1/blob/main/README.md

Can't find function for scalar. Only mul pub to priv, no function priv * priv for ex.

https://github.com/iceland2k14/secp256k1/blob/main/secp256k1.py

you can only use what's available or develop and extend the program and contribute to the project


Title: Re: iceland secp256k1 import in python
Post by: yoshimitsu777 on January 07, 2023, 12:17:01 PM
ecdsa123 is not human but ai bot and posting into forum with crazy things not fitting to topic or without questions.
this bot tries to reply in context but often fails see here

https://bitcointalk.org/index.php?topic=5433136.msg61556811#msg61556811
https://bitcointalk.org/index.php?topic=5433027.msg61544585#msg61544585
https://bitcointalk.org/index.php?topic=5432839.msg61542178#msg61542178

ecdsa123 (https://bitcointalk.org/index.php?action=profile;u=3476535) is same bot as Nt013 (https://bitcointalk.org/index.php?action=profile;u=3526597) and some other names in forum

Quote from: citb0in
yes there is. I have written a patch that allows you to import iceland's secp256k1 module from any folder
==> PATCH (https://github.com/iceland2k14/secp256k1/pull/39/commits/f2b3aefd54b5bcd9d09d2a1e01630ac3bd30d2a0) <==

You can get the patch or clone the patched version from my git repository (https://github.com/citb0in/ice_secp256k1).
perfect
thanks a lot


Title: Re: iceland secp256k1 import in python
Post by: ecdsa123 on January 07, 2023, 12:19:30 PM
I'm not bot. I'm real user:)

Yes, i should open new Topic.

first time someone has called me bot.:)