pq-crystals / kyber

Other
839 stars 201 forks source link

Load shared library by using module ctypes of python #51

Open knotgrass opened 1 year ago

knotgrass commented 1 year ago

I build shared library by using command cd ref && make shared. The outputs file is

ref/libpqcrystals_aes256ctr_ref.so
ref/libpqcrystals_fips202_ref.so
ref/libpqcrystals_sha2_ref.so
ref/libpqcrystals_kyber512-90s_ref.so
ref/libpqcrystals_kyber512_ref.so
ref/libpqcrystals_kyber768-90s_ref.so
ref/libpqcrystals_kyber768_ref.so
ref/libpqcrystals_kyber1024-90s_ref.so
ref/libpqcrystals_kyber1024_ref.so

After that, I using python try to open these .so file

import os
from ctypes import cdll

shared_lib    = os.path.abspath(os.path.join(__file__, '..', 'ref'))
aes256ctr     = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_aes256ctr_ref.so'    ))
fips202       = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_fips202_ref.so'      ))
sha2          = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_sha2_ref.so'         ))

try: kyber512 = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_kyber512_ref.so'))
except Exception as ex: print(ex)

try: kyber512_90s = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_kyber512-90s_ref.so'))
except Exception as ex: print(ex)

try: kyber768 = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_kyber768_ref.so'))
except Exception as ex: print(ex)

try: kyber768_90s = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_kyber768-90s_ref.so'))
except Exception as ex: print(ex)

try: kyber1024 = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_kyber1024_ref.so'))
except Exception as ex: print(ex)

try: kyber1024_90s = cdll.LoadLibrary(os.path.join(shared_lib, 'libpqcrystals_kyber1024-90s_ref.so'))
except Exception as ex: print(ex)

And i have a problem

Traceback (most recent call last):
/home/tz/Documents/KYBER/kyber_python/ref/libpqcrystals_kyber512_ref.so: undefined symbol: pqcrystals_kyber_fips202_ref_shake256
/home/tz/Documents/KYBER/kyber_python/ref/libpqcrystals_kyber512-90s_ref.so: undefined symbol: pqcrystals_kyber_aes256ctr_ref_init
/home/tz/Documents/KYBER/kyber_python/ref/libpqcrystals_kyber768_ref.so: undefined symbol: pqcrystals_kyber_fips202_ref_shake256
/home/tz/Documents/KYBER/kyber_python/ref/libpqcrystals_kyber768-90s_ref.so: undefined symbol: pqcrystals_kyber_aes256ctr_ref_init
/home/tz/Documents/KYBER/kyber_python/ref/libpqcrystals_kyber1024_ref.so: undefined symbol: pqcrystals_kyber_fips202_ref_shake256
/home/tz/Documents/KYBER/kyber_python/ref/libpqcrystals_kyber1024-90s_ref.so: undefined symbol: pqcrystals_kyber_aes256ctr_ref_init

I am new in C/C++. Please help me fix these thing.

knotgrass commented 1 year ago

I edited the Makefile, and It worked