Skip to content

#311 with macros #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 0 additions & 103 deletions bn_conversion.c

This file was deleted.

7 changes: 7 additions & 0 deletions bn_mp_get_i32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_GET_I32_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_GET_SIGNED(int32_t, mp_get_i32, mp_get_mag32)
#endif
7 changes: 7 additions & 0 deletions bn_mp_get_i64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_GET_I64_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_GET_SIGNED(int64_t, mp_get_i64, mp_get_mag64)
#endif
7 changes: 7 additions & 0 deletions bn_mp_get_mag32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_GET_MAG32_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_GET_MAG(uint32_t, mp_get_mag32)
#endif
7 changes: 7 additions & 0 deletions bn_mp_get_mag64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_GET_MAG64_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_GET_MAG(uint64_t, mp_get_mag64)
#endif
7 changes: 7 additions & 0 deletions bn_mp_init_i32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_INIT_I32_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_INIT_INT(mp_init_i32, mp_set_i32, int32_t)
#endif
7 changes: 7 additions & 0 deletions bn_mp_init_i64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_INIT_I64_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_INIT_INT(mp_init_i64, mp_set_i64, int64_t)
#endif
7 changes: 7 additions & 0 deletions bn_mp_init_u32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_INIT_U32_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_INIT_INT(mp_init_u32, mp_set_u32, uint32_t)
#endif
7 changes: 7 additions & 0 deletions bn_mp_init_u64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_INIT_U64_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_INIT_INT(mp_init_u64, mp_set_u64, uint64_t)
#endif
7 changes: 7 additions & 0 deletions bn_mp_set_i32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_SET_I32_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_SET_SIGNED(mp_set_i32, mp_set_u32, int32_t, uint32_t)
#endif
7 changes: 7 additions & 0 deletions bn_mp_set_i64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_SET_I64_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_SET_SIGNED(mp_set_i64, mp_set_u64, int64_t, uint64_t)
#endif
7 changes: 7 additions & 0 deletions bn_mp_set_u32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_SET_U32_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_SET_UNSIGNED(mp_set_u32, uint32_t)
#endif
7 changes: 7 additions & 0 deletions bn_mp_set_u64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "tommath_private.h"
#ifdef BN_MP_SET_U64_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

MP_SET_UNSIGNED(mp_set_u64, uint64_t)
#endif
4 changes: 2 additions & 2 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ static int test_mp_get_ul(void)
return EXIT_FAILURE;
}

for (i = 0; i < ((int)(sizeof(unsigned long)*CHAR_BIT) - 1); ++i) {
for (i = 0; i < ((int)MP_SIZEOF_BITS(unsigned long) - 1); ++i) {
t = (1UL << (i+1)) - 1;
if (!t)
t = ~0UL;
Expand Down Expand Up @@ -759,7 +759,7 @@ static int test_mp_get_u64(void)
return EXIT_FAILURE;
}

for (i = 0; i < ((int)(sizeof(unsigned long long)*CHAR_BIT) - 1); ++i) {
for (i = 0; i < (int)(MP_SIZEOF_BITS(unsigned long long) - 1); ++i) {
r = (1ULL << (i+1)) - 1;
if (!r)
r = ~0ULL;
Expand Down
69 changes: 30 additions & 39 deletions helper.pl
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,6 @@ sub patch_file {
return $content;
}

sub version_from_tomcrypt_h {
my $h = read_file(shift);
if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(.*)"/s) {
return "VERSION_PC=$1.$2.$3", "VERSION_LT=1:1", "VERSION=$1.$2.$3$4", "PROJECT_NUMBER=$1.$2.$3$4";
}
else {
die "#define SCRYPT not found in tomcrypt.h";
}
}

sub process_makefiles {
my $write = shift;
my $changed_count = 0;
Expand Down Expand Up @@ -308,6 +298,7 @@ sub update_dep
/* SPDX-License-Identifier: Unlicense */

#if !(defined(LTM1) && defined(LTM2) && defined(LTM3))
#define LTM_INSIDE
#if defined(LTM2)
# define LTM3
#endif
Expand All @@ -323,12 +314,10 @@ sub update_dep

print "Processing $filename\n";

# convert filename to upper case so we can use it as a define
# convert filename to upper case so we can use it as a define
$define =~ tr/[a-z]/[A-Z]/;
$define =~ tr/\./_/;
print {$class} << "EOS";
# define $define
EOS
print {$class} "# define $define\n";

# now copy text and apply #ifdef as required
my $apply = 0;
Expand All @@ -350,68 +339,71 @@ sub update_dep
$apply = 1;
}
while (<$src>) {
if (!($_ =~ /tommath\.h/)) {
if ($_ !~ /tommath\.h/) {
print {$out} $_;
}
}
if ($apply == 1) {
print {$out} << 'EOS';
#endif
EOS
print {$out} "#endif\n";
}
close $src;
close $out;

unlink $filename;
rename 'tmp', $filename;
}
print {$class} << 'EOS';
#endif
EOS
print {$class} "#endif\n#endif\n";

# now do classes
my %depmap;
foreach my $filename (glob 'bn*.c') {
open(my $src, '<', $filename) or die "Can't open source file!\n";
read $src, my $content, -s $src;
close $src;
my $content;
if ($filename =~ "bn_deprecated.c") {
open(my $src, '<', $filename) or die "Can't open source file!\n";
read $src, $content, -s $src;
close $src;
} else {
my $cc = $ENV{'CC'} || 'gcc';
$content = `$cc -E -x c -DLTM_ALL $filename`;
$content =~ s/^# 1 "$filename".*?^# 2 "$filename"//ms;
}

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

print {$class} << "EOS";
#if defined($filename)
EOS
print {$class} "#if defined($filename)\n";
my $list = $filename;

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

# scan for mp_* and make classes
my @deps = ();
foreach my $line (split /\n/, $content) {
while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*(?=\()|(?<=\()mp\_[a-z_0-9]*(?=,)/g) {
my $a = $&;
next if $a eq "mp_err";
$a =~ tr/[a-z]/[A-Z]/;
$a = 'BN_' . $a . '_C';
if (!($list =~ /$a/)) {
print {$class} << "EOS";
# define $a
EOS
}
$list = $list . ',' . $a;
push @deps, $a;
}
}
@deps = sort(@deps);
foreach my $a (@deps) {
if ($list !~ /$a/) {
print {$class} "# define $a\n";
}
$list = $list . ',' . $a;
}
$depmap{$filename} = $list;

print {$class} << 'EOS';
#endif

EOS
print {$class} "#endif\n\n";
}

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

@files = grep(!/conversion/, @files);
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));
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));

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