Skip to content

Commit abfa1af

Browse files
committed
update clinic directives
1 parent 64e1513 commit abfa1af

File tree

6 files changed

+194
-130
lines changed

6 files changed

+194
-130
lines changed

Modules/blake2module.c

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Written in 2013 by Dmitry Chestnykh <dmitry@codingrobots.com>
33
* Modified for CPython by Christian Heimes <christian@python.org>
44
* Updated to use HACL* by Jonathan Protzenko <jonathan@protzenko.fr>
5+
* Refactored by Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
56
*
67
* To the extent possible under law, the author have dedicated all
78
* copyright and related and neighboring rights to this software to
@@ -64,13 +65,7 @@
6465
#include "_hacl/Hacl_Hash_Blake2b_Simd256.h"
6566
#endif
6667

67-
// MODULE TYPE SLOTS
68-
69-
static PyType_Spec blake2b_type_spec;
70-
static PyType_Spec blake2s_type_spec;
71-
72-
PyDoc_STRVAR(blake2module__doc__,
73-
"_blake2 provides BLAKE2b and BLAKE2s for hashlib\n");
68+
// --- BLAKE-2 module state ---------------------------------------------------
7469

7570
typedef struct {
7671
PyTypeObject *blake2b_type;
@@ -87,15 +82,63 @@ get_blake2module_state(PyObject *module)
8782
return (blake2module_state *)state;
8883
}
8984

90-
#if defined(HACL_CAN_COMPILE_SIMD128) || defined(HACL_CAN_COMPILE_SIMD256)
9185
static inline blake2module_state *
92-
blake2_get_state_from_type(PyTypeObject *module)
86+
get_blake2module_state_by_cls(PyTypeObject *cls)
9387
{
94-
void *state = _PyType_GetModuleState(module);
88+
void *state = _PyType_GetModuleState(cls);
9589
assert(state != NULL);
9690
return (blake2module_state *)state;
9791
}
92+
93+
// --- BLAKE-2 object ---------------------------------------------------------
94+
95+
// The HACL* API does not offer an agile API that can deal with either Blake2S
96+
// or Blake2B -- the reason is that the underlying states are optimized (uint32s
97+
// for S, uint64s for B). Therefore, we use a tagged union in this module to
98+
// correctly dispatch. Note that the previous incarnation of this code
99+
// transformed the Blake2b implementation into the Blake2s one using a script,
100+
// so this is an improvement.
101+
//
102+
// The 128 and 256 versions are only available if i) we were able to compile
103+
// them, and ii) if the CPU we run on also happens to have the right instruction
104+
// set.
105+
typedef enum { Blake2s, Blake2b, Blake2s_128, Blake2b_256 } blake2_impl;
106+
107+
typedef struct {
108+
PyObject_HEAD
109+
HASHLIB_MUTEX_API
110+
union {
111+
Hacl_Hash_Blake2s_state_t *blake2s_state;
112+
Hacl_Hash_Blake2b_state_t *blake2b_state;
113+
#if HACL_CAN_COMPILE_SIMD128
114+
Hacl_Hash_Blake2s_Simd128_state_t *blake2s_128_state;
98115
#endif
116+
#if HACL_CAN_COMPILE_SIMD256
117+
Hacl_Hash_Blake2b_Simd256_state_t *blake2b_256_state;
118+
#endif
119+
};
120+
blake2_impl impl;
121+
} Blake2Object;
122+
123+
#define _Blake2Object_CAST(op) ((Blake2Object *)(op))
124+
125+
// --- BLAKE-2 module clinic configuration ------------------------------------
126+
127+
/*[clinic input]
128+
module _blake2
129+
class _blake2.blake2b "Blake2Object *" "clinic_state()->blake2b_type"
130+
class _blake2.blake2s "Blake2Object *" "clinic_state()->blake2s_type"
131+
[clinic start generated code]*/
132+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7e2b2b3b67a72f18]*/
133+
134+
#define clinic_state() (get_blake2module_state_by_cls(Py_TYPE(self)))
135+
#include "clinic/blake2module.c.h"
136+
#undef clinic_state
137+
138+
// MODULE TYPE SLOTS
139+
140+
static PyType_Spec blake2b_type_spec;
141+
static PyType_Spec blake2s_type_spec;
99142

100143
static int
101144
blake2module_traverse(PyObject *module, visitproc visit, void *arg)
@@ -280,6 +323,9 @@ static PyModuleDef_Slot blake2module_slots[] = {
280323
{0, NULL}
281324
};
282325

326+
PyDoc_STRVAR(blake2module__doc__,
327+
"_blake2 provides BLAKE2b and BLAKE2s for hashlib\n");
328+
283329
static struct PyModuleDef blake2module_def = {
284330
PyModuleDef_HEAD_INIT,
285331
.m_name = "_blake2",
@@ -299,18 +345,6 @@ PyInit__blake2(void)
299345

300346
// IMPLEMENTATION OF METHODS
301347

302-
// The HACL* API does not offer an agile API that can deal with either Blake2S
303-
// or Blake2B -- the reason is that the underlying states are optimized (uint32s
304-
// for S, uint64s for B). Therefore, we use a tagged union in this module to
305-
// correctly dispatch. Note that the previous incarnation of this code
306-
// transformed the Blake2b implementation into the Blake2s one using a script,
307-
// so this is an improvement.
308-
//
309-
// The 128 and 256 versions are only available if i) we were able to compile
310-
// them, and ii) if the CPU we run on also happens to have the right instruction
311-
// set.
312-
typedef enum { Blake2s, Blake2b, Blake2s_128, Blake2b_256 } blake2_impl;
313-
314348
static inline bool
315349
is_blake2b(blake2_impl impl)
316350
{
@@ -327,7 +361,7 @@ static inline blake2_impl
327361
type_to_impl(PyTypeObject *type)
328362
{
329363
#if defined(HACL_CAN_COMPILE_SIMD128) || defined(HACL_CAN_COMPILE_SIMD256)
330-
blake2module_state *state = blake2_get_state_from_type(type);
364+
blake2module_state *state = get_blake2module_state_by_cls(type);
331365
#endif
332366
if (!strcmp(type->tp_name, blake2b_type_spec.name)) {
333367
#if HACL_CAN_COMPILE_SIMD256
@@ -346,34 +380,6 @@ type_to_impl(PyTypeObject *type)
346380
Py_UNREACHABLE();
347381
}
348382

349-
typedef struct {
350-
PyObject_HEAD
351-
HASHLIB_MUTEX_API
352-
union {
353-
Hacl_Hash_Blake2s_state_t *blake2s_state;
354-
Hacl_Hash_Blake2b_state_t *blake2b_state;
355-
#if HACL_CAN_COMPILE_SIMD128
356-
Hacl_Hash_Blake2s_Simd128_state_t *blake2s_128_state;
357-
#endif
358-
#if HACL_CAN_COMPILE_SIMD256
359-
Hacl_Hash_Blake2b_Simd256_state_t *blake2b_256_state;
360-
#endif
361-
};
362-
blake2_impl impl;
363-
} Blake2Object;
364-
365-
#define _Blake2Object_CAST(op) ((Blake2Object *)(op))
366-
367-
#include "clinic/blake2module.c.h"
368-
369-
/*[clinic input]
370-
module _blake2
371-
class _blake2.blake2b "Blake2Object *" "&PyBlake2_BLAKE2bType"
372-
class _blake2.blake2s "Blake2Object *" "&PyBlake2_BLAKE2sType"
373-
[clinic start generated code]*/
374-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b7526666bd18af83]*/
375-
376-
377383
static Blake2Object *
378384
new_Blake2Object(PyTypeObject *type)
379385
{

Modules/hmacmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ get_hmacmodule_state_by_cls(PyTypeObject *cls)
377377
return (hmacmodule_state *)state;
378378
}
379379

380-
// --- HMAC Object ------------------------------------------------------------
380+
// --- HMAC object ------------------------------------------------------------
381381

382382
typedef Hacl_Streaming_HMAC_agile_state HACL_HMAC_state;
383383

Modules/md5module.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Andrew Kuchling (amk@amk.ca)
99
Greg Stein (gstein@lyra.org)
1010
Trevor Perrin (trevp@trevp.net)
11+
Bénédikt Tran (10796600+picnixz@users.noreply.github.com)
1112
1213
Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
1314
Licensed to PSF under a Contributor Agreement.
@@ -23,30 +24,14 @@
2324
#include "Python.h"
2425
#include "hashlib.h"
2526

26-
/*[clinic input]
27-
module _md5
28-
class MD5Type "MD5object *" "&PyType_Type"
29-
[clinic start generated code]*/
30-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
27+
#include "_hacl/Hacl_Hash_MD5.h"
3128

3229
/* The MD5 block size and message digest sizes, in bytes */
3330

3431
#define MD5_BLOCKSIZE 64
3532
#define MD5_DIGESTSIZE 16
3633

37-
#include "_hacl/Hacl_Hash_MD5.h"
38-
39-
40-
typedef struct {
41-
PyObject_HEAD
42-
HASHLIB_MUTEX_API
43-
Hacl_Hash_MD5_state_t *state;
44-
} MD5object;
45-
46-
#define _MD5object_CAST(op) ((MD5object *)(op))
47-
48-
#include "clinic/md5module.c.h"
49-
34+
// --- MD5 module state -------------------------------------------------------
5035

5136
typedef struct {
5237
PyTypeObject *md5_type;
@@ -60,6 +45,36 @@ get_md5module_state(PyObject *module)
6045
return (md5module_state *)state;
6146
}
6247

48+
static inline md5module_state *
49+
get_md5module_state_by_cls(PyTypeObject *cls)
50+
{
51+
void *state = PyType_GetModuleState(cls);
52+
assert(state != NULL);
53+
return (md5module_state *)state;
54+
}
55+
56+
// --- MD5 object -------------------------------------------------------------
57+
58+
typedef struct {
59+
PyObject_HEAD
60+
HASHLIB_MUTEX_API
61+
Hacl_Hash_MD5_state_t *state;
62+
} MD5object;
63+
64+
#define _MD5object_CAST(op) ((MD5object *)(op))
65+
66+
// --- MD5 module clinic configuration ----------------------------------------
67+
68+
/*[clinic input]
69+
module _md5
70+
class MD5Type "MD5object *" "clinic_state()->md5_type"
71+
[clinic start generated code]*/
72+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5451859a6c7e20d]*/
73+
74+
#define clinic_state() (get_md5module_state_by_cls(Py_TYPE(self)))
75+
#include "clinic/md5module.c.h"
76+
#undef clinic_state
77+
6378
static MD5object *
6479
newMD5object(md5module_state *state)
6580
{
@@ -107,7 +122,7 @@ static PyObject *
107122
MD5Type_copy_impl(MD5object *self, PyTypeObject *cls)
108123
/*[clinic end generated code: output=bf055e08244bf5ee input=d89087dcfb2a8620]*/
109124
{
110-
md5module_state *state = PyType_GetModuleState(cls);
125+
md5module_state *state = get_md5module_state_by_cls(cls);
111126

112127
MD5object *newobj;
113128
if ((newobj = newMD5object(state)) == NULL) {

Modules/sha1module.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Andrew Kuchling (amk@amk.ca)
99
Greg Stein (gstein@lyra.org)
1010
Trevor Perrin (trevp@trevp.net)
11+
Bénédikt Tran (10796600+picnixz@users.noreply.github.com)
1112
1213
Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
1314
Licensed to PSF under a Contributor Agreement.
@@ -24,29 +25,14 @@
2425
#include "pycore_strhex.h" // _Py_strhex()
2526
#include "pycore_typeobject.h" // _PyType_GetModuleState()
2627

27-
/*[clinic input]
28-
module _sha1
29-
class SHA1Type "SHA1object *" "&PyType_Type"
30-
[clinic start generated code]*/
31-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
32-
3328
/* The SHA1 block size and message digest sizes, in bytes */
3429

3530
#define SHA1_BLOCKSIZE 64
3631
#define SHA1_DIGESTSIZE 20
3732

3833
#include "_hacl/Hacl_Hash_SHA1.h"
3934

40-
typedef struct {
41-
PyObject_HEAD
42-
HASHLIB_MUTEX_API
43-
Hacl_Hash_SHA1_state_t *state;
44-
} SHA1object;
45-
46-
#define _SHA1object_CAST(op) ((SHA1object *)(op))
47-
48-
#include "clinic/sha1module.c.h"
49-
35+
// --- SHA-1 module state -----------------------------------------------------
5036

5137
typedef struct {
5238
PyTypeObject *sha1_type;
@@ -60,6 +46,36 @@ get_sha1module_state(PyObject *module)
6046
return (sha1module_state *)state;
6147
}
6248

49+
static inline sha1module_state *
50+
get_sha1module_state_by_cls(PyTypeObject *cls)
51+
{
52+
void *state = PyType_GetModuleState(cls);
53+
assert(state != NULL);
54+
return (sha1module_state *)state;
55+
}
56+
57+
// --- SHA-1 object -----------------------------------------------------------
58+
59+
typedef struct {
60+
PyObject_HEAD
61+
HASHLIB_MUTEX_API
62+
Hacl_Hash_SHA1_state_t *state;
63+
} SHA1object;
64+
65+
#define _SHA1object_CAST(op) ((SHA1object *)(op))
66+
67+
// --- SHA-1 module clinic configuration --------------------------------------
68+
69+
/*[clinic input]
70+
module _sha1
71+
class SHA1Type "SHA1object *" "clinic_state()->sha1_type"
72+
[clinic start generated code]*/
73+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=afc62adaf06c713f]*/
74+
75+
#define clinic_state() (get_sha1module_state_by_cls(Py_TYPE(self)))
76+
#include "clinic/sha1module.c.h"
77+
#undef clinic_state
78+
6379
static SHA1object *
6480
newSHA1object(sha1module_state *state)
6581
{
@@ -111,7 +127,7 @@ static PyObject *
111127
SHA1Type_copy_impl(SHA1object *self, PyTypeObject *cls)
112128
/*[clinic end generated code: output=b32d4461ce8bc7a7 input=6c22e66fcc34c58e]*/
113129
{
114-
sha1module_state *state = _PyType_GetModuleState(cls);
130+
sha1module_state *state = get_sha1module_state_by_cls(cls);
115131

116132
SHA1object *newobj;
117133
if ((newobj = newSHA1object(state)) == NULL) {

0 commit comments

Comments
 (0)