Skip to content

Commit 7bc2fd6

Browse files
committed
rewrite blake2_blake2b_copy_locked
1 parent 789dfaf commit 7bc2fd6

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

Modules/blake2module.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -737,42 +737,34 @@ static int
737737
blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy)
738738
{
739739
assert(cpy != NULL);
740+
#define BLAKE2_COPY(TYPE, STATE_ATTR) \
741+
do { \
742+
cpy->STATE_ATTR = Hacl_Hash_ ## TYPE ## _copy(self->STATE_ATTR); \
743+
if (cpy->STATE_ATTR == NULL) { \
744+
goto error; \
745+
} \
746+
} while (0)
740747
switch (self->impl) {
741748
#if HACL_CAN_COMPILE_SIMD256
742-
case Blake2b_256: {
743-
cpy->blake2b_256_state = Hacl_Hash_Blake2b_Simd256_copy(self->blake2b_256_state);
744-
if (cpy->blake2b_256_state == NULL) {
745-
goto error;
746-
}
749+
case Blake2b_256:
750+
BLAKE2_COPY(Blake2b_Simd256, blake2b_256_state);
747751
break;
748-
}
749752
#endif
750753
#if HACL_CAN_COMPILE_SIMD128
751-
case Blake2s_128: {
752-
cpy->blake2s_128_state = Hacl_Hash_Blake2s_Simd128_copy(self->blake2s_128_state);
753-
if (cpy->blake2s_128_state == NULL) {
754-
goto error;
755-
}
754+
case Blake2s_128:
755+
BLAKE2_COPY(Blake2s_Simd128, blake2s_128_state);
756756
break;
757-
}
758757
#endif
759-
case Blake2b: {
760-
cpy->blake2b_state = Hacl_Hash_Blake2b_copy(self->blake2b_state);
761-
if (cpy->blake2b_state == NULL) {
762-
goto error;
763-
}
758+
case Blake2b:
759+
BLAKE2_COPY(Blake2b, blake2b_state);
764760
break;
765-
}
766-
case Blake2s: {
767-
cpy->blake2s_state = Hacl_Hash_Blake2s_copy(self->blake2s_state);
768-
if (cpy->blake2s_state == NULL) {
769-
goto error;
770-
}
761+
case Blake2s:
762+
BLAKE2_COPY(Blake2s, blake2s_state);
771763
break;
772-
}
773764
default:
774765
Py_UNREACHABLE();
775766
}
767+
#undef BLAKE2_COPY
776768
cpy->impl = self->impl;
777769
return 0;
778770

0 commit comments

Comments
 (0)