Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secp256k1_surjectionproof_initialize cannot be used from python's ctypes ffi #36

Closed
dgpv opened this issue Dec 24, 2018 · 3 comments
Closed

Comments

@dgpv
Copy link
Contributor

dgpv commented Dec 24, 2018

I'm porting BlindTransaction() function from Elements Core (https://github.com/ElementsProject/elements/blob/5467be3b16f014b371ecf6bced73be0254755cfa/src/blind.cpp#L193) to python, using ctypes FFI.
(I want https://github.com/Simplexum/python-bitcointx to be able to blind/unblind Elements sidechain txs on its own).

working with secp256k1_* functions was easy until now, but I hit a problem with secp256k1_surjectionproof_initialize. It requires a reference to secp256k1_surjectionproof struct, but the size of this structure may change for different platforms or versions. This means that the only way to reliably use surjectionproof functions is to call them from C/C++ code that can include the header file for a particular version of the library. And then you probably want to ship this library with your code, or link with it statically, or else there might be size mismatch if you use the code compiled with old-version secp256k1_surjectionproof.h but have new-version dynamic library installed.

The calling code that do not have access to relevant header file might try to just alloc a big buffer and hope that the structure will not ever be bigger than that. But that is unclean and unreliable.

the library could export the size of the structure as a global symbol
const int SECP256K1_SURJECTIONPROOF_RAW_SIZE = sizeof(secp256k1_surjectionproof);

or export a function that will return this size, or somehow expose this size in another way (but of course the caller itself should allocate the buffer of the specified size, not the library)

I understand that secp256k1-zkp is work in progress, and to be used on one's own risk. But it would be excellent to be able to use it (on my own risk) from python :-)

@dgpv dgpv changed the title unspecified size of secp256k1_surjectionproof prevents using surjectionproof functions from python's ctypes ffi unspecified size of secp256k1_surjectionproof prevents using it from python's ctypes ffi Dec 24, 2018
@dgpv
Copy link
Contributor Author

dgpv commented Dec 24, 2018

Note that other opaque structures in the library usually have their maximum size specified in the comments, with text like "not guaranteed to be portable, but guaranteed to be 64 bytes in size", so it was easy to work with that - just allocate a buffer of needed size.

@dgpv dgpv changed the title unspecified size of secp256k1_surjectionproof prevents using it from python's ctypes ffi secp256k1_surjectionproof_initialize cannot be used from python's ffi Dec 24, 2018
@dgpv dgpv changed the title secp256k1_surjectionproof_initialize cannot be used from python's ffi secp256k1_surjectionproof_initialize cannot be used from python's ctypes ffi May 16, 2019
@dgpv
Copy link
Contributor Author

dgpv commented May 16, 2019

After conversation on #secp256k1 irc channel, it was determined that better way is to add two functions: one that would allocate+initialize the structure, and another one destroy it, so that client would not need to know the size.

I prepared PR #55 that implements those functions (currently it is not final, because there are questions regarding naming for the allocate+initialize function, and the need for guard values to prevent accidental misuse of destroy function on stack-allocated struct)

@apoelstra
Copy link
Contributor

Closed by #55

tomtau pushed a commit to crypto-com/secp256k1-zkp that referenced this issue Jul 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants