Skip to content

Commit

Permalink
Convert unnecessary wrappers into C-defines
Browse files Browse the repository at this point in the history
I would maybe have preferred a "strong alias" instead,
but this SEEMS to compile, at least on cffi 1.16.0, so
I'm using it for now.

TODO: I need help figuring out how to implement a "strong alias"...
  • Loading branch information
James-E-A committed Feb 28, 2024
1 parent 817dfa7 commit 9bb9e5d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
9 changes: 3 additions & 6 deletions cffi_modules/_kem_cffi_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ def make_kem_ffi(build_root, extra_cdefs=frozenset(), extra_c_header_sources=fro
c_header_sources = [dedent("""\
// Public KEM interface
#include "api.h"
int crypto_kem_keypair(uint8_t *pk, uint8_t *sk) {
return %(namespace)scrypto_kem_keypair(pk, sk);}
int crypto_kem_enc(uint8_t *c, uint8_t *key, const uint8_t *pk) {
return %(namespace)scrypto_kem_enc(c, key, pk);}
int crypto_kem_dec(uint8_t *key, const uint8_t *c, const uint8_t *sk) {
return %(namespace)scrypto_kem_dec(key, c, sk);}
#define crypto_kem_keypair %(namespace)scrypto_kem_keypair
#define crypto_kem_enc %(namespace)scrypto_kem_enc
#define crypto_kem_dec %(namespace)scrypto_kem_dec
""")]

cdefs.append(dedent("""\
Expand Down
12 changes: 8 additions & 4 deletions cffi_modules/_mceliece_cffi_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ def make_mceliece_ffi(build_root):
extra_cdefs = [dedent("""\
// Exposed internal interface
typedef ... gf;
int %(namespace)spk_gen(uint8_t *pk, uint8_t *sk, const uint32_t *perm, int16_t *pi, uint64_t *pivots);
void %(namespace)sencrypt(uint8_t *s, const uint8_t *pk, uint8_t *e);
int %(namespace)sdecrypt(uint8_t *e, const uint8_t *sk, const uint8_t *c);
int %(namespace)sgenpoly_gen(gf *out, gf *f);
int pk_gen(uint8_t *pk, uint8_t *sk, const uint32_t *perm, int16_t *pi, uint64_t *pivots);
void encrypt(uint8_t *s, const uint8_t *pk, uint8_t *e);
int decrypt(uint8_t *e, const uint8_t *sk, const uint8_t *c);
int genpoly_gen(gf *out, gf *f);
#define SYS_N ...
#define SYS_T ...
#define GFBITS ...
Expand All @@ -23,6 +23,10 @@ def make_mceliece_ffi(build_root):
#include "decrypt.h"
#include "params.h"
#include "sk_gen.h"
#define pk_gen %(namespace)spk_gen
#define encrypt %(namespace)sencrypt
#define decrypt %(namespace)sdecrypt
#define genpoly_gen %(namespace)sgenpoly_gen
""")]

return make_kem_ffi(build_root=build_root, extra_c_header_sources=extra_c_header_sources, extra_cdefs=extra_cdefs, common_sources=common_sources)
15 changes: 5 additions & 10 deletions cffi_modules/_sign_cffi_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ def make_sign_ffi(build_root, extra_c_header_sources=frozenset(), extra_cdefs=fr
c_header_sources = [dedent("""\
// Public signature interface
#include "api.h"
int crypto_sign_keypair(uint8_t *pk, uint8_t *sk) {
return %(namespace)scrypto_sign_keypair(pk, sk);}
int crypto_sign_signature(uint8_t *sig, size_t *siglen, const uint8_t *m, size_t mlen, const uint8_t *sk) {
return %(namespace)scrypto_sign_signature(sig, siglen, m, mlen, sk);}
int crypto_sign_verify(const uint8_t *sig, size_t siglen, const uint8_t *m, size_t mlen, const uint8_t *pk) {
return %(namespace)scrypto_sign_verify(sig, siglen, m, mlen, pk);}
int crypto_sign(uint8_t *sm, size_t *smlen, const uint8_t *m, size_t mlen, const uint8_t *sk) {
return %(namespace)scrypto_sign(sm, smlen, m, mlen, sk);}
int crypto_sign_open(uint8_t *m, size_t *mlen, const uint8_t *sm, size_t smlen, const uint8_t *pk) {
return %(namespace)scrypto_sign_open(m, mlen, sm, smlen, pk);}
#define crypto_sign_keypair %(namespace)scrypto_sign_keypair
#define crypto_sign_signature %(namespace)scrypto_sign_signature
#define crypto_sign_verify %(namespace)scrypto_sign_verify
#define crypto_sign %(namespace)scrypto_sign
#define crypto_sign_open %(namespace)scrypto_sign_open
""")]

cdefs.append(dedent("""\
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = 'pypqc'
version = '0.0.7.0-a1'
version = '0.0.7.0-a2'
description = 'Python bindings for the "PQClean" post-quantum cryptography library.'
readme = 'README.rst'
license.file = 'COPYING.rst'
Expand Down

0 comments on commit 9bb9e5d

Please sign in to comment.