Commit 49fcc87c authored by Marius Wachtler's avatar Marius Wachtler

Add a pyston specific CAPI extension for converting between a python long and GMP

and add a pycrypto integration test.

In additon fix an assert inside CompiledFunction::speculationFailed
parent d1503272
......@@ -13,3 +13,6 @@
[submodule "lz4"]
path = lz4
url = git://github.com/Cyan4973/lz4.git
[submodule "test/integration/pycrypto"]
path = test/integration/pycrypto
url = https://github.com/dlitz/pycrypto.git
......@@ -138,6 +138,15 @@ PyAPI_FUNC(PyObject *) _PyLong_FormatAdvanced(PyObject *obj,
char *format_spec,
Py_ssize_t format_spec_len) PYSTON_NOEXCEPT;
// Pyston change: Pyston specific API
#define Py_HAVE_LONG_MPZ_API 1
/* _PyLongMPZ: alias for mpz_src, used to not have to pull in the GMP header. */
typedef void* _PyLongMPZ;
/* _PyLong_AsMPZ: Sets the supplied initialized GMP mpz to the value of the supplied long object. */
PyAPI_FUNC(void) _PyLong_AsMPZ(PyObject *, _PyLongMPZ) PYSTON_NOEXCEPT;
/* _PyLong_FromMPZ: Creates a python long object from the supplied GMP mpz value. */
PyAPI_FUNC(PyObject *) _PyLong_FromMPZ(const _PyLongMPZ) PYSTON_NOEXCEPT;
#ifdef __cplusplus
}
#endif
......
......@@ -552,7 +552,7 @@ void CompiledFunction::speculationFailed() {
this->times_speculation_failed++;
if (this->times_speculation_failed >= 4) {
if (this->times_speculation_failed == 4) {
// printf("Killing %p because it failed too many speculations\n", this);
CLFunction* cl = this->clfunc;
......
......@@ -452,6 +452,17 @@ extern "C" PyObject* _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
return rtn;
}
extern "C" void _PyLong_AsMPZ(PyObject* obj, _PyLongMPZ num) noexcept {
RELEASE_ASSERT(obj->cls == long_cls, "needs a long argument");
mpz_set((mpz_ptr)num, ((BoxedLong*)obj)->n);
}
extern "C" PyObject* _PyLong_FromMPZ(const _PyLongMPZ num) noexcept {
BoxedLong* r = new BoxedLong();
mpz_init_set(r->n, (mpz_srcptr)num);
return r;
}
extern "C" Box* createLong(const std::string* s) {
BoxedLong* rtn = new BoxedLong();
int r = mpz_init_set_str(rtn->n, s->c_str(), 10);
......
Subproject commit 7acba5f3a6ff10f1424c309d0d34d2b713233019
From 7303cd6db08b41a513bbd45aad0676a47eb3820a Mon Sep 17 00:00:00 2001
From: Marius Wachtler <undingen@gmail.com>
Date: Tue, 28 Apr 2015 11:49:32 +0200
Subject: [PATCH] fastmath: Add support for Pyston
---
src/_fastmath.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/src/_fastmath.c b/src/_fastmath.c
index e369f5a..c4cf2aa 100644
--- a/src/_fastmath.c
+++ b/src/_fastmath.c
@@ -29,7 +29,14 @@
#include "pycrypto_common.h"
#include <stdio.h>
#include <string.h>
+
+#ifdef Py_HAVE_LONG_MPZ_API
+#ifndef HAVE_LIBGMP
+#error Pyston only supports GMP.
+#endif
+#else
#include <longintrepr.h> /* for conversions */
+#endif
#if HAVE_LIBGMP
# include <gmp.h>
#elif HAVE_LIBMPIR
@@ -60,6 +67,19 @@
static unsigned int sieve_base[10000];
static int rabinMillerTest (mpz_t n, int rounds, PyObject *randfunc);
+#ifdef Py_HAVE_LONG_MPZ_API
+static void
+longObjToMPZ (mpz_t m, PyLongObject * p)
+{
+ _PyLong_AsMPZ ((PyObject*)p, m);
+}
+
+static PyObject *
+mpzToLongObj (mpz_t m)
+{
+ return _PyLong_FromMPZ (m);
+}
+#else
static void
longObjToMPZ (mpz_t m, PyLongObject * p)
{
@@ -113,6 +133,7 @@ mpzToLongObj (mpz_t m)
mpz_clear (temp);
return (PyObject *) l;
}
+#endif
typedef struct
{
--
2.1.0
import subprocess, sys, os, shutil, StringIO
pycrypto_dir = os.path.dirname(os.path.abspath(__file__)) + "/pycrypto"
os.chdir(pycrypto_dir)
for d in ("build", "install"):
if os.path.exists(d):
print "Removing the existing", d, "directory"
shutil.rmtree(d)
devnull = open(os.devnull, "w")
print "-- Patching pycrypto"
patches = ["../pycrypto_0001-fastmath-Add-support-for-Pyston.patch"]
for patch in patches:
out = StringIO.StringIO()
try:
cmd = ["patch", "-p1", "--forward", "-i", patch]
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print e.output
if "Reversed (or previously applied) patch detected! Skipping patch" not in e.output:
raise e
print "-- Building pycrypto"
subprocess.check_call([sys.executable, "setup.py", "build"], stdout=devnull)
print "-- Installing pycrypto"
subprocess.check_call([sys.executable, "setup.py", "install", "--prefix=install"], stdout=devnull)
print "-- Testing pycrypto"
sys.path.append("install/site-packages")
test_string = "test string".ljust(16)
from Crypto.Hash import SHA256, MD5
assert SHA256.new(test_string).hexdigest() == "edce3184097ede907d91c4069c55104785a3a989b9706e5919202d6f5fe2d814"
assert MD5.new(test_string).hexdigest() == "e135865bb047e78e1827b0cf83696725"
from Crypto.Cipher import AES
aes1 = AES.new("pwd1__0123456789")
aes2 = AES.new("pwd2__0123456789")
enc_data = aes1.encrypt(test_string)
enc_data2 = aes2.encrypt(test_string)
assert enc_data != enc_data2
assert aes1.decrypt(enc_data) == test_string
assert aes2.decrypt(enc_data2) == test_string
from Crypto.PublicKey import RSA
from Crypto import Random
key = RSA.generate(1024, Random.new().read)
public_key = key.publickey()
enc_data = public_key.encrypt(test_string, 32)
assert enc_data != test_string
assert key.decrypt(enc_data) == test_string
print "-- Tests finished"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment