part of J8 is openssl libeay on windows, and I believe its either also 
distributed or presumed on other platforms.  It includes a BN library.  As a 
start,

I need some help with the code below. after loading (windows 64 j802),

a =. dec2bn 1231231144 NB. looks ok.
┌──┬───────────┬──────────┐ 
│10│_1181373152│1231231144│ 
└──┴───────────┴──────────┘

|. a. i. bn2dec a

(hopefully doesn't crash)

I don't really know what gets returned yet, but what is worrysome is that 
repeated calls return slightly different values.

Some issues I don't know how to deal with in the code below,

BNnum_bytes is a macro, and error 2 0 occurs if I try to call it.  Is there a 
workaround?

BN_dec2bn (called above) actually has a pointer to pointer first argument 
(**BIGNUM).  Is there something special I have to do in the function signature, 
or when calling?  I'm pretending its just a pointer.



require 'dll' 

sslp =: IFWIN pick '';'D:\OpenSSL-Win64\bin\' 
sslp =: IFWIN pick ''; '/',~ jpath '~bin'  NB. with J802.  cut this line if you 
wish to point to downloaded folder 
NB. OPENSSL =: jpath '~system/ssleay32.dll ' 
NB.OPENSSL =: sslp , '\ssleay32.dll ' 
OPENSSL =: sslp , (IFIOS + (;: 'Win Linux Android Darwin') i. <UNAME_z_) pick 
'libeay32.dll '; (2 $ <'libssl.so.1.0.0 '),  (2 $ <'/usr/lib/libssl.dylib ') 
NB.OPENSSL =: sslp , (IFIOS + (;: 'Win Linux Android Darwin') i. <UNAME_z_) 
pick 'libeay32.dll '; (2 $ <'libssl.so ');  (2 $ <'libssl.0.9.8.dylib ') 

SSLE =: sslp , '\openssl' 
ssl =: 1 : '(OPENSSL , m)&cd' 

BNctxnew =: ' BN_CTX_new  *i' ssl 
BNnew =: ' BN_new  *i' ssl  NB. probably don't use... unamanaged memory 
version. 
BNmul =: ' BN_mul  + l *x *x *x *x' ssl 
NB. BIGNUM *BN_bin2bn(const unsigned char *s,int len,BIGNUM *ret) 
BN2bn =: ' BN_bin2bn *x *c l *x' ssl 
NB. int BN_bn2bin(const BIGNUM *a, unsigned char *to) 
BN2bin =: ' BN_bn2bin l *i *c' ssl 
BN_bn2hex=: ' BN_bn2bin l *i *c' ssl NB.char * BN_bn2hex(const BIGNUM *a); 
BN_hex2bn =:  ' BN_bn2bin l *i *c' ssl  NB.(BIGNUM **a, const char *str); 
BNnum_bytes=: ' BN_num_bytes  i *i' ssl  NB. doest work as its macro? 
NB. int BN_num_bits(const BIGNUM *a) 
BN_num_bits=: ' BN_num_bits  i *i' ssl 
num_bytes =: BN_num_bits 
NB. char *BN_bn2dec(const BIGNUM *num) 
BN2dec=: ' BN_bn2dec  *c *i' ssl 
NB. int BN_dec2bn(BIGNUM **num, const char *str) 
dec2BN=: ' BN_dec2bn  x *i *c' ssl 
NB. BN_print(BIO *fp, const BIGNUM *a); 


pD =: 1!:2&2 

dec2bn=: 3 : 0 
o =. >BNnew 0{.a. 
NB. o =. 0{.a. 
NB.pD i=. dec2BN (o);(a. {~ 256 #. inv  y) 
pD i=. dec2BN (o);(": y) 
2 {. i 
) 

bn2dec=: 3 : 0 
NB.len =. BNnum_bytes y 
NB.o =. len # '0' 
pD y 
pD o =. BN2dec (, 1 { y) 
NB.pD a =.BNnum_bytes ,<o 
memr (0{::o),0,(0{::y),2 
)





----- Original Message -----
From: aai <agroeneveld...@gmail.com>
To: programm...@jsoftware.com
Cc: 
Sent: Wednesday, September 2, 2015 3:50 AM
Subject: Re: [Jprogramming] Comparing J speed

The main reason for speed difference is that big integer calculation in 
Haskell is based on the GNU Multiple Precision Arithmetic Library 
(/GMP/), much faster than J's extended precision number calculation.

Op 2-9-2015 om 02:32 schreef Jon Hough:
> In this talk https://www.youtube.com/watch?v=apBWkBDVlow
> the presenter attempts to show Haskell hasn't sacrificed speed for 
> expressiveness by comparing a Java Fibonacci calculator to his Haskell 
> one.(skip to the 18:00 mark).Essentially, to calculate the 475000th Fibonacci 
> number, it took his Java program ~8 seconds, while the very terse Haskell 
> program took ~6 seconds.
> So I tried to do the same in J. My first attempt, used a tacit, memoized verb
> fib1 =:1:`(($:@:<:) + ($:@:-&2))@.(2&<)M.
>
>
> However, this gives a stack error for large numbers (~100000). So I decided 
> to make an imperative verb,
>
>
> fib2 =: 3 : 0 x1 =. x:1 x2 =. x:1 c =. 0 while. c < y do. tmp =.  x1 x1 =. x2 
> x2 =. tmp + x1 c=.c+1 end. x2
>
>
>
>
>
>
>
>
>
>
>
>
> )
>
>
> This gets there, I can calculate the 475000th Fibonacci number, but
>
>
> timespacex 'fib2 475000'
>
>
>
>
> 36.183 1.31558e6
>
>
> It takes 36 seconds (of course, my hardware is different to that in the 
> presentation, but still...).
>
>
> Is there a speedier way to do this in J? Preferably a tacit one liner would 
> also be good.
>
>
> Thanks,
> Jon
>  
>                           
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to