随着计算机系统的快速发展,经常需要对海量数据和信息做处理,在处理这些数据时经常会遇到很大的数字,无法用int或者long等类型来存储,经常看到有人自己在写或者讨论大数相关的问题,本文描述从开源库polarssl中提取的大数bignum模块,独立出来集成到应用程序中的方法,该模块支持的大数位数不限制。
摘取出来的模块仅仅包含:bignum.c、bignum.h、bn_mul.h三个文件,简单易用。
相关代码和测试代码如下:
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "bignum.h"
5
6 void test_add_mul()
7 {
8 /*
9 //test result:
10 A = 123456789012345678901234567890
11 B = 123456789012345678901234567890
12 X = A + B
13 X = 246913578024691357802469135780
14
15 A = 123456789012345678901234567890
16 B = 123456789012345678901234567890
17 X = A * B
18 X = 15241578753238836750495351562536198787501905199875019052100
19
20 press any key to contiue ...
21 */
22 int ret, temp;
23 mpi A, B, X;
24 size_t n;
25 char a[ 2 * POLARSSL_MPI_MAX_SIZE + 2 ];
26 char b[ 2 * POLARSSL_MPI_MAX_SIZE + 2 ];
27 char x[ 2 * POLARSSL_MPI_MAX_SIZE + 2 ];
28
29 n = sizeof(a);
30 temp = n - 2;
31
32 mpi_init( &A ); mpi_init( &B ); mpi_init( &X );
33
34 MPI_CHK( mpi_read_string( &A, 10,
35 "123456789012345678901234567890") );
36
37 MPI_CHK( mpi_read_string( &B, 10,
38 "123456789012345678901234567890") );
39
40 MPI_CHK( mpi_add_mpi( &X, &A, &B ) );
41
42 memset( a, 0, sizeof(a) );
43 memset( b, 0, sizeof(b) );
44 memset( x, 0, sizeof(x) );
45 n = temp;
46 MPI_CHK( mpi_write_string( &A, 10, a, (size_t *) &n ) );
47 n = temp;
48 MPI_CHK( mpi_write_string( &B, 10, b, (size_t *) &n ) );
49 n = temp;
50 MPI_CHK( mpi_write_string( &X, 10, x, (size_t *) &n ) );
51 printf("A = %s\n", a);
52 printf("B = %s\n", b);
53 printf("X = A + B\n");
54 printf("X = %s\n", x);
55 printf("\n");
56
57 MPI_CHK( mpi_mul_mpi( &X, &A, &B ) );
58
59 memset( a, 0, sizeof(a) );
60 memset( b, 0, sizeof(b) );
61 memset( x, 0, sizeof(x) );
62 n = temp;
63 MPI_CHK( mpi_write_string( &A, 10, a, (size_t *) &n ) );
64 n = temp;
65 MPI_CHK( mpi_write_string( &B, 10, b, (size_t *) &n ) );
66 n = temp;
67 MPI_CHK( mpi_write_string( &X, 10, x, (size_t *) &n ) );
68 printf("A = %s\n", a);
69 printf("B = %s\n", b);
70 printf("X = A * B\n");
71 printf("X = %s\n", x);
72 printf("\n");
73
74 cleanup:
75
76 mpi_free( &A ); mpi_free( &B ); mpi_free( &X );
77 }
78
79 void my_pause()
80 {
81 printf("press any key to contiue ...");
82 getchar();
83 }
84
85 int main( int argc, char *argv[] )
86 {
87
88 test_add_mul();
89
90 my_pause();
91
92 return 0;
2 #include <stdio.h>
3
4 #include "bignum.h"
5
6 void test_add_mul()
7 {
8 /*
9 //test result:
10 A = 123456789012345678901234567890
11 B = 123456789012345678901234567890
12 X = A + B
13 X = 246913578024691357802469135780
14
15 A = 123456789012345678901234567890
16 B = 123456789012345678901234567890
17 X = A * B
18 X = 15241578753238836750495351562536198787501905199875019052100
19
20 press any key to contiue ...
21 */
22 int ret, temp;
23 mpi A, B, X;
24 size_t n;
25 char a[ 2 * POLARSSL_MPI_MAX_SIZE + 2 ];
26 char b[ 2 * POLARSSL_MPI_MAX_SIZE + 2 ];
27 char x[ 2 * POLARSSL_MPI_MAX_SIZE + 2 ];
28
29 n = sizeof(a);
30 temp = n - 2;
31
32 mpi_init( &A ); mpi_init( &B ); mpi_init( &X );
33
34 MPI_CHK( mpi_read_string( &A, 10,
35 "123456789012345678901234567890") );
36
37 MPI_CHK( mpi_read_string( &B, 10,
38 "123456789012345678901234567890") );
39
40 MPI_CHK( mpi_add_mpi( &X, &A, &B ) );
41
42 memset( a, 0, sizeof(a) );
43 memset( b, 0, sizeof(b) );
44 memset( x, 0, sizeof(x) );
45 n = temp;
46 MPI_CHK( mpi_write_string( &A, 10, a, (size_t *) &n ) );
47 n = temp;
48 MPI_CHK( mpi_write_string( &B, 10, b, (size_t *) &n ) );
49 n = temp;
50 MPI_CHK( mpi_write_string( &X, 10, x, (size_t *) &n ) );
51 printf("A = %s\n", a);
52 printf("B = %s\n", b);
53 printf("X = A + B\n");
54 printf("X = %s\n", x);
55 printf("\n");
56
57 MPI_CHK( mpi_mul_mpi( &X, &A, &B ) );
58
59 memset( a, 0, sizeof(a) );
60 memset( b, 0, sizeof(b) );
61 memset( x, 0, sizeof(x) );
62 n = temp;
63 MPI_CHK( mpi_write_string( &A, 10, a, (size_t *) &n ) );
64 n = temp;
65 MPI_CHK( mpi_write_string( &B, 10, b, (size_t *) &n ) );
66 n = temp;
67 MPI_CHK( mpi_write_string( &X, 10, x, (size_t *) &n ) );
68 printf("A = %s\n", a);
69 printf("B = %s\n", b);
70 printf("X = A * B\n");
71 printf("X = %s\n", x);
72 printf("\n");
73
74 cleanup:
75
76 mpi_free( &A ); mpi_free( &B ); mpi_free( &X );
77 }
78
79 void my_pause()
80 {
81 printf("press any key to contiue ...");
82 getchar();
83 }
84
85 int main( int argc, char *argv[] )
86 {
87
88 test_add_mul();
89
90 my_pause();
91
92 return 0;
93 }