Skip to content

Commit f562d65

Browse files
authored
Merge pull request #313 from libtom/remove-bn_conversion
#311 with macros
2 parents f109772 + d1013c7 commit f562d65

23 files changed

+703
-580
lines changed

bn_conversion.c

-103
This file was deleted.

bn_mp_get_i32.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_GET_I32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_GET_SIGNED(int32_t, mp_get_i32, mp_get_mag32)
7+
#endif

bn_mp_get_i64.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_GET_I64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_GET_SIGNED(int64_t, mp_get_i64, mp_get_mag64)
7+
#endif

bn_mp_get_mag32.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_GET_MAG32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_GET_MAG(uint32_t, mp_get_mag32)
7+
#endif

bn_mp_get_mag64.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_GET_MAG64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_GET_MAG(uint64_t, mp_get_mag64)
7+
#endif

bn_mp_init_i32.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_INIT_I32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_INIT_INT(mp_init_i32, mp_set_i32, int32_t)
7+
#endif

bn_mp_init_i64.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_INIT_I64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_INIT_INT(mp_init_i64, mp_set_i64, int64_t)
7+
#endif

bn_mp_init_u32.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_INIT_U32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_INIT_INT(mp_init_u32, mp_set_u32, uint32_t)
7+
#endif

bn_mp_init_u64.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_INIT_U64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_INIT_INT(mp_init_u64, mp_set_u64, uint64_t)
7+
#endif

bn_mp_set_i32.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_SET_I32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_SET_SIGNED(mp_set_i32, mp_set_u32, int32_t, uint32_t)
7+
#endif

bn_mp_set_i64.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_SET_I64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_SET_SIGNED(mp_set_i64, mp_set_u64, int64_t, uint64_t)
7+
#endif

bn_mp_set_u32.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_SET_U32_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_SET_UNSIGNED(mp_set_u32, uint32_t)
7+
#endif

bn_mp_set_u64.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_MP_SET_U64_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
MP_SET_UNSIGNED(mp_set_u64, uint64_t)
7+
#endif

demo/test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ static int test_mp_get_ul(void)
726726
return EXIT_FAILURE;
727727
}
728728

729-
for (i = 0; i < ((int)(sizeof(unsigned long)*CHAR_BIT) - 1); ++i) {
729+
for (i = 0; i < ((int)MP_SIZEOF_BITS(unsigned long) - 1); ++i) {
730730
t = (1UL << (i+1)) - 1;
731731
if (!t)
732732
t = ~0UL;
@@ -759,7 +759,7 @@ static int test_mp_get_u64(void)
759759
return EXIT_FAILURE;
760760
}
761761

762-
for (i = 0; i < ((int)(sizeof(unsigned long long)*CHAR_BIT) - 1); ++i) {
762+
for (i = 0; i < (int)(MP_SIZEOF_BITS(unsigned long long) - 1); ++i) {
763763
r = (1ULL << (i+1)) - 1;
764764
if (!r)
765765
r = ~0ULL;

helper.pl

+30-39
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,6 @@ sub patch_file {
222222
return $content;
223223
}
224224

225-
sub version_from_tomcrypt_h {
226-
my $h = read_file(shift);
227-
if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(.*)"/s) {
228-
return "VERSION_PC=$1.$2.$3", "VERSION_LT=1:1", "VERSION=$1.$2.$3$4", "PROJECT_NUMBER=$1.$2.$3$4";
229-
}
230-
else {
231-
die "#define SCRYPT not found in tomcrypt.h";
232-
}
233-
}
234-
235225
sub process_makefiles {
236226
my $write = shift;
237227
my $changed_count = 0;
@@ -308,6 +298,7 @@ sub update_dep
308298
/* SPDX-License-Identifier: Unlicense */
309299
310300
#if !(defined(LTM1) && defined(LTM2) && defined(LTM3))
301+
#define LTM_INSIDE
311302
#if defined(LTM2)
312303
# define LTM3
313304
#endif
@@ -323,12 +314,10 @@ sub update_dep
323314

324315
print "Processing $filename\n";
325316

326-
# convert filename to upper case so we can use it as a define
317+
# convert filename to upper case so we can use it as a define
327318
$define =~ tr/[a-z]/[A-Z]/;
328319
$define =~ tr/\./_/;
329-
print {$class} << "EOS";
330-
# define $define
331-
EOS
320+
print {$class} "# define $define\n";
332321

333322
# now copy text and apply #ifdef as required
334323
my $apply = 0;
@@ -350,68 +339,71 @@ sub update_dep
350339
$apply = 1;
351340
}
352341
while (<$src>) {
353-
if (!($_ =~ /tommath\.h/)) {
342+
if ($_ !~ /tommath\.h/) {
354343
print {$out} $_;
355344
}
356345
}
357346
if ($apply == 1) {
358-
print {$out} << 'EOS';
359-
#endif
360-
EOS
347+
print {$out} "#endif\n";
361348
}
362349
close $src;
363350
close $out;
364351

365352
unlink $filename;
366353
rename 'tmp', $filename;
367354
}
368-
print {$class} << 'EOS';
369-
#endif
370-
EOS
355+
print {$class} "#endif\n#endif\n";
371356

372357
# now do classes
373358
my %depmap;
374359
foreach my $filename (glob 'bn*.c') {
375-
open(my $src, '<', $filename) or die "Can't open source file!\n";
376-
read $src, my $content, -s $src;
377-
close $src;
360+
my $content;
361+
if ($filename =~ "bn_deprecated.c") {
362+
open(my $src, '<', $filename) or die "Can't open source file!\n";
363+
read $src, $content, -s $src;
364+
close $src;
365+
} else {
366+
my $cc = $ENV{'CC'} || 'gcc';
367+
$content = `$cc -E -x c -DLTM_ALL $filename`;
368+
$content =~ s/^# 1 "$filename".*?^# 2 "$filename"//ms;
369+
}
378370

379371
# convert filename to upper case so we can use it as a define
380372
$filename =~ tr/[a-z]/[A-Z]/;
381373
$filename =~ tr/\./_/;
382374

383-
print {$class} << "EOS";
384-
#if defined($filename)
385-
EOS
375+
print {$class} "#if defined($filename)\n";
386376
my $list = $filename;
387377

388378
# strip comments
389379
$content =~ s{/\*.*?\*/}{}gs;
390380

391381
# scan for mp_* and make classes
382+
my @deps = ();
392383
foreach my $line (split /\n/, $content) {
393384
while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*(?=\()|(?<=\()mp\_[a-z_0-9]*(?=,)/g) {
394385
my $a = $&;
395386
next if $a eq "mp_err";
396387
$a =~ tr/[a-z]/[A-Z]/;
397388
$a = 'BN_' . $a . '_C';
398-
if (!($list =~ /$a/)) {
399-
print {$class} << "EOS";
400-
# define $a
401-
EOS
402-
}
403-
$list = $list . ',' . $a;
389+
push @deps, $a;
404390
}
405391
}
392+
@deps = sort(@deps);
393+
foreach my $a (@deps) {
394+
if ($list !~ /$a/) {
395+
print {$class} "# define $a\n";
396+
}
397+
$list = $list . ',' . $a;
398+
}
406399
$depmap{$filename} = $list;
407400

408-
print {$class} << 'EOS';
409-
#endif
410-
411-
EOS
401+
print {$class} "#endif\n\n";
412402
}
413403

414404
print {$class} << 'EOS';
405+
#ifdef LTM_INSIDE
406+
#undef LTM_INSIDE
415407
#ifdef LTM3
416408
# define LTM_LAST
417409
#endif
@@ -442,8 +434,7 @@ sub generate_def {
442434
@files = map { my $x = $_; $x =~ s/^bn_|\.c$//g; $x; } @files;
443435
@files = grep(!/mp_radix_smap/, @files);
444436

445-
@files = grep(!/conversion/, @files);
446-
push(@files, qw(mp_set_i32 mp_set_i64 mp_set_u32 mp_set_u64 mp_set_int mp_set_long mp_set_long_long mp_get_i32 mp_get_i64 mp_get_mag32 mp_get_mag64 mp_get_int mp_get_long mp_get_long_long mp_init_i32 mp_init_i64 mp_init_u32 mp_init_u64 mp_init_set_int));
437+
push(@files, qw(mp_set_int mp_set_long mp_set_long_long mp_get_int mp_get_long mp_get_long_long mp_init_set_int));
447438

448439
my $files = join("\n ", sort(grep(/^mp_/, @files)));
449440
write_file "tommath.def", "; libtommath

0 commit comments

Comments
 (0)