1# Before `make install' is performed this script should be runnable with 2# `make test'. After `make install' it should work as `perl test.pl' 3 4######################### 5 6# change 'tests => 1' to 'tests => last_test_to_print'; 7 8use Test; 9BEGIN { plan tests => 52 }; 10use Crypt::OpenSSL::Bignum; 11use Crypt::OpenSSL::Bignum::CTX; 12 13######################### 14 15# Insert your test code below, the Test module is use()ed here so read 16# its man page ( perldoc Test ) for help writing this test script. 17 18use strict; 19 20sub _new_bn 21{ 22 return Crypt::OpenSSL::Bignum->new_from_word( shift ); 23} 24 25my $bn; 26my $decimal_string = "2342234235235235235"; 27$bn = Crypt::OpenSSL::Bignum->new_from_decimal( $decimal_string ); 28ok( $bn ); 29ok( $bn->to_decimal() eq $decimal_string ); 30 31my $hex_string = "7f"; 32$bn = Crypt::OpenSSL::Bignum->new_from_hex( $hex_string ); 33ok( $bn ); 34ok( $bn->to_hex() eq uc( $hex_string ) ); 35ok( $bn->to_decimal() eq '127' ); 36 37my $bin_string = pack( "C*", 2, 0 ); 38$bn = Crypt::OpenSSL::Bignum->new_from_bin( $bin_string ); 39ok( $bn ); 40ok( $bn->to_bin() eq $bin_string ); 41ok( $bn->to_decimal() eq '512' ); 42 43my $bn23 = _new_bn( 23 ); 44my $bn25 = _new_bn( 25 ); 45 46ok( $bn23->cmp($ bn25 ) == -1 ); 47ok( $bn25->cmp( $bn23 ) == 1 ); 48ok( $bn23->cmp( $bn23 ) == 0 ); 49ok( $bn23->equals( $bn23 ) ); 50 51my $bn_copy = $bn->copy(); 52ok( $bn_copy ne $bn ); 53ok( $bn->equals( $bn_copy ) ); 54 55my $ptr = $bn->pointer_copy(); 56ok( ! ref $ptr ); 57ok( $bn + 0 != $ptr ); 58my $from_ptr = Crypt::OpenSSL::Bignum->bless_pointer( $ptr ); 59ok( $bn->equals( $from_ptr ) ); 60 61 62my $zero = Crypt::OpenSSL::Bignum->zero(); 63my $one = Crypt::OpenSSL::Bignum->one(); 64 65ok( $one->is_one() ); 66ok( !$zero->is_one() ); 67 68ok( $zero->is_zero() ); 69ok( !$one->is_zero() ); 70 71ok( !$zero->is_odd() ); 72ok( $one->is_odd() ); 73 74my $word = 0xffffeeee; 75ok( _new_bn($word)->get_word() == $word ); 76 77# test creation from object rather than class string. 78my $bn2 = $bn->new_from_bin( $bin_string ); 79ok( $bn2 ); 80ok( $bn2->to_bin() eq $bn->to_bin() ); 81 82ok( '48' eq $bn23->add( $bn25 )->to_decimal() ); 83$bn = _new_bn( 18 ); 84$bn->add( $one, $bn ); 85ok( 19 == $bn->get_word() ); 86 87ok( '-2' eq $bn23->sub( $bn25 )->to_decimal() ); 88$bn = _new_bn( 18 ); 89$bn->sub( $one, $bn ); 90ok( 17 == $bn->get_word() ); 91 92my $ctx = Crypt::OpenSSL::Bignum::CTX->new(); 93 94ok( $ctx ); 95ok( 575 == $bn23->mul( $bn25, $ctx )->get_word() ); 96ok( 575 == $bn23->mul( $bn25, $ctx, $bn )->get_word() ); 97ok( 575 == $bn->get_word() ); 98 99ok( 2 == $bn25->mod( $bn23, $ctx )->get_word() ); 100ok( 2 == $bn25->mod( $bn23, $ctx, $bn )->get_word() ); 101ok( 2 == $bn->get_word() ); 102 103my $bn6 = _new_bn( 6 ); 104my $bn3 = _new_bn( 3 ); 105 106my( $quotient, $remainder ) = $bn25->div( $bn23, $ctx ); 107ok( $quotient->is_one ); 108ok( 2 == $remainder->get_word() ); 109my( $quotient2, $remainder2 ) = 110 $bn25->div( $bn6, $ctx, $quotient, $remainder ); 111ok( $quotient2 == $quotient ); 112ok( $remainder2 == $remainder ); 113ok( 4 == $quotient->get_word() ); 114ok( $remainder->is_one ); 115my( $quotient3, $remainder3 ) = 116 $bn25->div( $bn6, $ctx, $quotient ); 117ok( $quotient3 == $quotient ); 118ok( 4 == $quotient->get_word() ); 119ok( $remainder3->is_one() ); 120 121ok( 6 == _new_bn( 18 )->gcd( _new_bn( 42 ), $ctx )->get_word() ); 122ok( 5 == $bn23->mod_mul( $bn25, $bn6, $ctx )->get_word() ); 123ok( 729 == $bn3->exp( $bn6, $ctx )->get_word() ); 124ok( 4 == $bn3->mod_exp( $bn6, $bn25, $ctx )->get_word() ); 125ok( 36 == $bn6->sqr( $ctx )->get_word() ); 126ok( 12 == $bn23->mod_inverse( $bn25, $ctx )->get_word() ); 127