]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[crypto] Add bigint_copy() as a convenient wrapper macro
authorMichael Brown <mcb30@ipxe.org>
Fri, 19 Jan 2024 12:29:29 +0000 (12:29 +0000)
committerMichael Brown <mcb30@ipxe.org>
Fri, 19 Jan 2024 12:29:29 +0000 (12:29 +0000)
Big integers may be efficiently copied using bigint_shrink() (which
will always copy only the size of the destination integer), but this
is potentially confusing to a reader of the code.

Provide bigint_copy() as an alias for bigint_shrink() so that the
intention of the calling code may be more obvious.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/include/ipxe/bigint.h
src/tests/bigint_test.c

index 36138dd64c192fa00a2443cbfef04892ec1a6d9c..820d306b855577153f43d8774175201f0fe82a08 100644 (file)
@@ -8,6 +8,8 @@
 
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
+#include <assert.h>
+
 /**
  * Define a big-integer type
  *
@@ -176,6 +178,17 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
                            (dest)->element, dest_size );               \
        } while ( 0 )
 
+/**
+ * Copy big integer
+ *
+ * @v source           Source big integer
+ * @v dest             Destination big integer
+ */
+#define bigint_copy( source, dest ) do {                               \
+       build_assert ( sizeof ( *(source) ) == sizeof ( *(dest) ) );    \
+       bigint_shrink ( (source), (dest) );                             \
+       } while ( 0 )
+
 /**
  * Multiply big integers
  *
index 02568dffb012695d19300847a69ff99f49cd9d90..484c5913406ae77b6d785806131462d901b5e150 100644 (file)
@@ -149,6 +149,16 @@ void bigint_shrink_sample ( const bigint_element_t *source0,
        bigint_shrink ( source, dest );
 }
 
+void bigint_copy_sample ( const bigint_element_t *source0,
+                         bigint_element_t *dest0, unsigned int size ) {
+       const bigint_t ( size ) *source __attribute__ (( may_alias ))
+               = ( ( const void * ) source0 );
+       bigint_t ( size ) *dest __attribute__ (( may_alias ))
+               = ( ( void * ) dest0 );
+
+       bigint_copy ( source, dest );
+}
+
 void bigint_multiply_sample ( const bigint_element_t *multiplicand0,
                              unsigned int multiplicand_size,
                              const bigint_element_t *multiplier0,