Show
Ignore:
Timestamp:
04/24/08 12:20:12 (5 years ago)
Author:
daisuke
Message:

Add binary two's complement hack, so we can use it from Net::OpenID::Consumer

Location:
lang/perl/Crypt-DH-GMP/trunk
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Crypt-DH-GMP/trunk/GMP.xs

    r10267 r10271  
    3030 
    3131static inline 
    32 char *DH_mpz2sv_str(mpz_t *v, unsigned int base) 
     32char *DH_mpz2sv_str(mpz_t *v, unsigned int base, unsigned int *length) 
    3333{ 
    3434    STRLEN len = 0; 
     
    4343        Renew(buf, len - 1, char); /* got one shorter than expected */ 
    4444    } 
     45 
     46    if (length != NULL) 
     47        *length = len; 
     48 
    4549    return buf; 
    4650} 
     
    108112        mpz_init_set_str(mpz_pub_key, pub_key, 0); 
    109113        mpz_powm(mpz_ret, mpz_pub_key, DH_PRIVKEY(dh), DH_P(dh)); 
    110         RETVAL = DH_mpz2sv_str(&mpz_ret, 10); 
     114        RETVAL = DH_mpz2sv_str(&mpz_ret, 10, NULL); 
    111115        mpz_clear(mpz_ret); 
    112116        mpz_clear(mpz_pub_key); 
     
    118122        DH_gmp_t *dh; 
    119123    CODE: 
    120         RETVAL = DH_mpz2sv_str(DH_PRIVKEY_PTR(dh), 10); 
     124        RETVAL = DH_mpz2sv_str(DH_PRIVKEY_PTR(dh), 10, NULL); 
    121125    OUTPUT: 
    122126        RETVAL 
     
    126130        DH_gmp_t *dh; 
    127131    CODE: 
    128         RETVAL = DH_mpz2sv_str(DH_PUBKEY_PTR(dh), 10); 
     132        RETVAL = DH_mpz2sv_str(DH_PUBKEY_PTR(dh), 10, NULL); 
     133    OUTPUT: 
     134        RETVAL 
     135 
     136char * 
     137DH_gmp_pub_key_twoc(dh) 
     138        DH_gmp_t *dh; 
     139    PREINIT: 
     140        unsigned int len = 0; 
     141        unsigned int pad = 0; 
     142        char *buf; 
     143    CODE: 
     144        buf = DH_mpz2sv_str(DH_PUBKEY_PTR(dh), 2, &len); 
     145        pad = (8 - len % 8); 
     146        if (pad <= 0 && *buf == '1') { 
     147            pad = 8; 
     148        } 
     149 
     150        if (pad > 0) { 
     151            unsigned int ipad = 0; 
     152            char *tmp; 
     153            Newxz(tmp, len + pad, char); 
     154            for (ipad = 0; ipad < pad; ipad++) 
     155                *(tmp + ipad) = '0'; 
     156            Copy(buf, tmp + pad, len + 1, char); 
     157            Safefree(buf); 
     158            buf = tmp; 
     159        } 
     160        RETVAL = buf; 
    129161    OUTPUT: 
    130162        RETVAL 
     
    136168        STRLEN n_a; 
    137169    CODE: 
    138         RETVAL = DH_mpz2sv_str(DH_G_PTR(dh), 10); 
     170        RETVAL = DH_mpz2sv_str(DH_G_PTR(dh), 10, NULL); 
    139171        if (items > 1) { 
    140172            mpz_init_set_str( DH_G(dh), (char *) SvPV(ST(1), n_a), 0 ); 
     
    149181        STRLEN n_a; 
    150182    CODE: 
    151         RETVAL = DH_mpz2sv_str(DH_P_PTR(dh), 10); 
     183        RETVAL = DH_mpz2sv_str(DH_P_PTR(dh), 10, NULL); 
    152184        if (items > 1) { 
    153185            mpz_init_set_str( DH_P(dh), (char *) SvPV(ST(1), n_a), 0 ); 
  • lang/perl/Crypt-DH-GMP/trunk/t/01-dh.t

    r9443 r10271  
    2222           ); 
    2323 
    24 =head1 
    25 my $num = '10000000000000000001'; 
    26 my @try = ($num, Math::BigInt->new($num)); 
    27 push @try, Math::Pari->new($num) if $has_pari; 
    28 for my $try (@try) { 
    29     my $type = 'any2bigint(' . (ref($try) || 'scalar') . ')'; 
    30     my $val = Crypt::DH::_any2bigint($try); 
    31     ok($val, $type . ' returns a defined value'); 
    32     is(ref($val), 'Math::BigInt', $type  . ' returns a Math::BigInt'); 
    33     is($val->bstr, $num, $type . ' returns the correct value'); 
    34 } 
    35 =cut 
    36  
    3724for my $pg (@pgs) { 
    3825    my $dh1 = Crypt::DH::GMP->new(g => $pg->{g}, p => $pg->{p});