Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from todofixthis/develop
Browse files Browse the repository at this point in the history
1.0.0a4
  • Loading branch information
todofixthis authored Mar 26, 2017
2 parents fb61d35 + 14606f1 commit af3429f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
33 changes: 30 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
C Curl Extension for PyOTA
==========================
.. DANGER::
⚠️ **THIS VERSION IS EXPERIMENTAL AND HAS NOT BEEN VETTED FOR STABILITY/SECURITY YET; DO NOT USE IN PRODUCTION CODE!!!** ⚠️
⚠️ **THIS VERSION IS EXPERIMENTAL AND HAS NOT BEEN VETTED FOR STABILITY/SECURITY YET; DO NOT USE IN PRODUCTION CODE!!!** ⚠️

This is an extension for the crypto functionality in `PyOTA`_, the Python API
This is an extension for the crypto functionality in `PyOTA <https://pypi.python.org/pypi/PyOTA>`_, the Python API
for IOTA. When installed, it will significantly boost the performance of
PyOTA's crypto functionality.

This extension is installed as an add-on to the ``pyota`` package::

pip install pyota[ccurl]

.. _PyOTA: https://pypi.python.org/pypi/PyOTA
Compatibility
-------------
This extension is currently compatible with Python 3.6 and 3.5 only.

`I've documented the issues with the Python 2 C API <https://github.com/todofixthis/pyota-ccurl/issues/4>`_.
Adding Python 2 support is on my list, but it may take me a bit before I can get to it.

If you have experience with C and are interested in getting involved,
please reach out to me on the `IOTA Slack network <https://iotatangle.slack.com/>`_ or submit a pull request.

Testing
-------
The easiest way to test the extension is to install it and then run the PyOTA unit tests.

Here's a modified ``tox.ini`` file that for PyOTA that will install the C extension before running unit tests::

[tox]
envlist = py27, py35, py36

[testenv]
commands =
pip install --pre pyota-ccurl
nosetests
deps =
mock
nose

Note that this file needs to be installed in the PyOTA codebase!
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
name = 'PyOTA-CCurl',
description = 'C Curl extension for PyOTA',
url = 'https://github.com/todofixthis/pyota-ccurl',
version = '1.0.0a3',
version = '1.0.0a4',

long_description = long_description,

Expand Down
15 changes: 7 additions & 8 deletions src/ccurlmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ _Curl_transform(Curl *self)
{
// Adapted from https://github.com/iotaledger/ccurl/blob/master/src/lib/Curl.c
trit_t scratchpad[STATE_LENGTH];
int scratchpadIndex = 0;
int scratchpadIndexSave;
int round, scratchpadIndex=0, scratchpadIndexSave, stateIndex;

for (int round = 0; round < NUMBER_OF_ROUNDS; round++) {
for (round = 0; round < NUMBER_OF_ROUNDS; round++) {
memcpy(scratchpad, self->_state, STATE_LENGTH * sizeof(trit_t));

for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
for (stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
scratchpadIndexSave = scratchpadIndex;
scratchpadIndex += (scratchpadIndex < 365 ? 364 : -365);
self->_state[stateIndex] = TRUTH_TABLE[scratchpad[scratchpadIndexSave ] + scratchpad[scratchpadIndex ] * 3 + 4];
Expand Down Expand Up @@ -82,13 +81,13 @@ Curl_absorb(Curl *self, PyObject *args, PyObject *kwds)
incoming_item = PyList_GetItem(incoming, i);

if ((incoming_item == NULL) || ! PyLong_Check(incoming_item)) {
PyErr_SetString(PyExc_ValueError, "`trits` argument contains non-numeric values.");
PyErr_Format(PyExc_ValueError, "`trits` argument contains non-numeric value at index %u.", i);
return NULL;
}

incoming_value = (trit_t)PyLong_AsLong(incoming_item);
if ((incoming_value < -1) || (incoming_value > 1)) {
PyErr_SetString(PyExc_ValueError, "`trits` argument contains values outside range [-1, 1].");
PyErr_Format(PyExc_ValueError, "`trits` argument contains value outside range [-1, 1] at index %u.", i);
return NULL;
}

Expand All @@ -111,7 +110,7 @@ static PyObject*
Curl_squeeze(Curl *self, PyObject *args, PyObject *kwds)
{
PyObject *incoming;
int incoming_count;
int i, incoming_count;

static char *kwlist[] = {"trits", NULL};

Expand All @@ -135,7 +134,7 @@ Curl_squeeze(Curl *self, PyObject *args, PyObject *kwds)
// Can't use ``memcpy`` here because we have to convert ints into ``PyLongObject``.
// This isn't the slow part of Curl (that honor is reserved for ``_Curl_transform``),
// so it shouldn't be that big of a problem.
for (int i=0; i < HASH_LENGTH; i++) {
for (i=0; i < HASH_LENGTH; i++) {
PyList_SetItem(incoming, i, PyLong_FromLong(self->_state[i]));
}

Expand Down

0 comments on commit af3429f

Please sign in to comment.