# Requesting help with writing a CFFI wrapper of our secp256k1-zkp fork

**URL:** <https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024>\
**Category:** Development and Technical Discussion\
**Created:** [September 10, 2022, 8:45am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024 "2022-09-10T08:45:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [September 10, 2022, 8:45am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/1 "2022-09-10T08:45:27Z")

</div>

Dear all,

As some of you may have already heard, I am working on [Mimblewimble implementation in Python](https://github.com/grinventions/mimblewimble-py/). The library already provides some useful functionalities, but it is far from ready.

Current obstacle is access to secp256k1-zkp methods. We have [our own fork](https://github.com/mimblewimble/secp256k1-zkp) of secp256k1-zkp library and the way it is used in Rust is [via a wrapper](https://github.com/mimblewimble/rust-secp256k1-zkp).

I am writing a similar wrapper for Python. I have already wrapped some methods, it builds and those methods are accessible using Python. Unfortunately, I am not fluent with C and my work on it is very inefficient. I have prepared a necessary setup to get started

> **[GitHub - grinventions/secp256k1-zkp-mw: Python cffi wrapper of the...](https://github.com/grinventions/secp256k1-zkp-mw)**
>
> Python cffi wrapper of the secp256k1-zkp fork for the GRIN's Mimblewimble protocol - GitHub - grinventions/secp256k1-zkp-mw: Python cffi wrapper of the secp256k1-zkp fork for the GRIN's Mim...

Basically one needs to help me write more definitions in [deps.c](https://github.com/grinventions/secp256k1-zkp-mw/blob/5e233e9c4cd6e3baf4aa40a9ecd03d259f7078e2/defs.c) in a way to avoid type errors during compilation I keep getting. The repo contains everything you need to get started with, once we have the definitions and everything builds (not just few methods I prepared to get started) I will make a Python module out of it and publish it and integrate it with mimblewimble-py.

I also hope ツ Community Council could offer a bounty for someone who is willing to help.  
Please leave your feedback!  
Cheers!

---

<div class="post-metadata">

**Author:** ![Anynomous](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/anynomous/32/2928_2.png) [@Anynomous](https://forum.grin.mw/u/Anynomous)\
**Post date:** [September 10, 2022, 12:49pm UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/2 "2022-09-10T12:49:23Z")

</div>

> [@renzokuken](#):
>
> I also hope ツ Community Council could offer a bounty for someone who is willing to help.

Sounds reasonable. No idea what a fair amount would be though? Add it to the agenda for the next CC meeting @Cobragrin so we can discuss it.

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [September 13, 2022, 10:21am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/3 "2022-09-13T10:21:00Z")

</div>

> [@Anynomous](#):
>
> No idea what a fair amount would be though?

Really hard to tell, it depends on experience… My suggestion is:

1. Offer a flat rate prize and state this is assuming X hours of work are needed.
2. Make it clear that if in the process the problem ends up more challenging the extra time will also be compensated as long as sufficiently documented and there are traces of work over time (GitHub, questions on the forum / keybase etc).

---

<div class="post-metadata">

**Author:** ![walkbackgod](https://avatars.discourse-cdn.com/v4/letter/w/e0b2c6/32.png) [@walkbackgod](https://forum.grin.mw/u/walkbackgod)\
**Post date:** [September 15, 2022, 3:32am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/4 "2022-09-15T03:32:41Z")

</div>

I’m interested in working on this one, will fork it tonight and have a look to see what is happening. Do you have a branch that has the problem with the compilation errors?

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [September 15, 2022, 8:42am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/5 "2022-09-15T08:42:47Z")

</div>

Hi, so I created a `how-to-start` branch

> **[GitHub - grinventions/secp256k1-zkp-mw at how-to-start](https://github.com/grinventions/secp256k1-zkp-mw/tree/how-to-start)**
>
> how-to-start

You may simply start grabbing definitions from

> <https://github.com/rustyrussell/secp256k1-py/blob/5bad581d959d722bf6c2df5eaa996fd4c24096aa/_cffi_build/secp256k1.h#L58-L63>

and put them in `defs.c`

> <https://github.com/grinventions/secp256k1-zkp-mw/blob/004c1c86fa7da8512dd31f37d14683b4ee7f1fa2/defs.c#L58-L63>

and then fetch the C source

```nohighlight
git submodule init
git submodule update

```

and run the build

```nohighlight
python build.py

```

and you will start getting parsing errors, type errors etc

```auto
Traceback (most recent call last):
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/cffi/cparser.py", line 336, in _parse
    ast = _get_parser().parse(fullcsource)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/pycparser/c_parser.py", line 152, in parse
    debug=debuglevel)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/pycparser/ply/yacc.py", line 331, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/pycparser/ply/yacc.py", line 1199, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/pycparser/ply/yacc.py", line 193, in call_errorfunc
    r = errorfunc(token)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/pycparser/c_parser.py", line 1861, in p_error
    column=self.clex.find_tok_column(p)))
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/pycparser/plyparser.py", line 67, in _parse_error
    raise ParseError("%s: %s" % (coord, msg))
pycparser.plyparser.ParseError: <cdef source string>:57:5: before: secp256k1_pubkey

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "build.py", line 51, in <module>
    ffi.cdef(_source)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/cffi/api.py", line 112, in cdef
    self._cdef(csource, override=override, packed=packed, pack=pack)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/cffi/api.py", line 126, in _cdef
    self._parser.parse(csource, override=override, **options)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/cffi/cparser.py", line 389, in parse
    self._internal_parse(csource)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/cffi/cparser.py", line 394, in _internal_parse
    ast, macros, csource = self._parse(csource)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/cffi/cparser.py", line 338, in _parse
    self.convert_pycparser_error(e, csource)
  File "/Users/marek/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages/cffi/cparser.py", line 367, in convert_pycparser_error
    raise CDefError(msg)
cffi.CDefError: cannot parse "secp256k1_pubkey* pubkey,"
<cdef source string>:57:5: before: secp256k1_pubkey

```

This is just how to get started. The goal is not to copy all the methods from rustyrussell/secp256k1-py/ but to have all those as well as all the MW zkp methods from [GitHub - mimblewimble/secp256k1-zkp: Fork of secp256k1-zkp for the Grin/MimbleWimble project](https://github.com/mimblewimble/secp256k1-zkp)

---

<div class="post-metadata">

**Author:** ![walkbackgod](https://avatars.discourse-cdn.com/v4/letter/w/e0b2c6/32.png) [@walkbackgod](https://forum.grin.mw/u/walkbackgod)\
**Post date:** [September 15, 2022, 10:33pm UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/6 "2022-09-15T22:33:13Z")

</div>

Thanks Renzokuken.  
Started taking a look, it’s quite abit of work. Work that I’m happy to do though 😃

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [September 16, 2022, 8:13am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/7 "2022-09-16T08:13:45Z")

</div>

Glad you are so motivated! Please keep updating us on your progress.

@Anynomous could you please confirm the bounty has been approved?

---

<div class="post-metadata">

**Author:** ![Anynomous](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/anynomous/32/2928_2.png) [@Anynomous](https://forum.grin.mw/u/Anynomous)\
**Post date:** [September 16, 2022, 9:24pm UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/8 "2022-09-16T21:24:08Z")

</div>

@renzokuken,@walkbackgod,@Cobragrin  
The bounty proposed in Tuesdays 13th of September’s CC meeting has officially been approved by the CC members:

**Bounty for a Python cffi secp256k1-zkp wrapper**  
-3000$ bounty for significant progress  
-10.000$ (includes the $3000) for the finished, well documented and well tested wrapper. MIT license open source, or if possible with the wrapped package, Apache license are required.

We now lock the bounty for you to work on this project @walkbackgod. Keep us informed on your estimate timeline and any progress.

---

<div class="post-metadata">

**Author:** ![walkbackgod](https://avatars.discourse-cdn.com/v4/letter/w/e0b2c6/32.png) [@walkbackgod](https://forum.grin.mw/u/walkbackgod)\
**Post date:** [September 19, 2022, 11:01pm UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/9 "2022-09-19T23:01:41Z")

</div>

OK have forked and managed to get through one set of errors.  
I expect to spend some hours on this later today, will let you guys know how going in next few days.

I suspect there will be sections that are harder 😃

Here is the FORK/BRANCH I’m working with.

> **[GitHub - walkbackgod/secp256k1-zkp-mw at wbg-pythonwrapper](https://github.com/walkbackgod/secp256k1-zkp-mw/tree/wbg-pythonwrapper)**
>
> wbg-pythonwrapper

---

<div class="post-metadata">

**Author:** ![walkbackgod](https://avatars.discourse-cdn.com/v4/letter/w/e0b2c6/32.png) [@walkbackgod](https://forum.grin.mw/u/walkbackgod)\
**Post date:** [October 3, 2022, 12:21am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/10 "2022-10-03T00:21:15Z")

</div>

As you can see from the repo, I completed the first bit a while ago which was to get all the methods from the mw fork working and building. I am working on the next bit which is definitely harder. Trying to get the other methods working… Once i get past the first few it may become easier, but right now it’s difficult. @renzokuken not sure if you want to check the current repo to make sure you are happy with what is there right now?

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [October 4, 2022, 9:03am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/11 "2022-10-04T09:03:28Z")

</div>

Thank you! I will do my best to review your code before October 7th, 2022. I am currently travelling for business and it is difficult for me to find a time slot, but I acknowledge I received your request and I added this review to my current TODO list.

---

<div class="post-metadata">

**Author:** ![renzokuken](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.grin.mw/renzokuken/32/3563_2.png) [@renzokuken](https://forum.grin.mw/u/renzokuken)\
**Post date:** [October 13, 2022, 9:01am UTC](https://forum.grin.mw/t/requesting-help-with-writing-a-cffi-wrapper-of-our-secp256k1-zkp-fork/10024/12 "2022-10-13T09:01:24Z")

</div>

Terribly sorry for the delays. @walkbackgod I just added your remote and build from the branch `wbg-pythonwrapper`. Build properly and without errors. Since I left this project and applied for the funding the bounty you added these following methods:

```auto
secp256k1_ec_privkey_tweak_add
secp256k1_ec_privkey_tweak_mul
secp256k1_ec_pubkey_combine
secp256k1_ec_pubkey_create
secp256k1_ec_pubkey_parse
secp256k1_ec_pubkey_serialize
secp256k1_ec_pubkey_tweak_add
secp256k1_ec_pubkey_tweak_mul
secp256k1_ec_seckey_verify
secp256k1_ecdsa_sign
secp256k1_ecdsa_signature_normalize
secp256k1_ecdsa_signature_parse_compact
secp256k1_ecdsa_signature_parse_der
secp256k1_ecdsa_signature_serialize_compact
secp256k1_ecdsa_signature_serialize_der
secp256k1_ecdsa_verify

```

You are definitely on the right track. I am very happy with the results so far. Keep it up!
