Elliptic curve arithmetic: Difference between revisions

m
J: style changes - maybe slightly easier to read
m (J: style changes - maybe slightly easier to read)
Line 541:
Follows the C contribution.
 
<lang j>Czero=.: 7_j_
 
zero=: _j_
 
isZero=: 1e20 < |@{.@+.
neg=: +
dbl=: monad define
'pxp_x pyp_y'=. +. xp=. y
if. isZero xp do. yp return. end.
L=. 1.5 * p_x*p_x % p_y
r=. (L*L) - 2*p_x
r j. (L * p_x-r) - p_y
)
add=: 4dyad :0define
'p_x p_y'=. +. p=. x
'q_x q_y'=. +. q=. y
if. x=y do. dbl x return. end.
if. isZero yx do. xy return. end.
if. isZero y do. x return. end.
L=. %~/ +. q-p
r=. (L*L) - p_x + q_x
r j. (L * p_x-r) - p_y
)
 
negmul=: 1dyad _1&*&.+.define
a=. zero
 
for_bit.|.#:y do.
dbl=: 3 :0
if. isZero y do if. ybit return. enddo.
'px py' a=. +.a add yx
end.
r j. py-~L*px-r=. (2*px)-~*~L=. (3*px*px)% 2*py
x=. dbl x
end.
a
)
 
NB. C is 7
add=: 4 :0
from=: j.~ [:(* * 3& |@%:@| ])C-~*~ _7 0 1 p. ]
if. x=y do. dbl x return. end.
if. isZero x do. y return. end.
show=: monad define
if. isZero y do. x return. end.
if. isZero y do. 'Zero' else.
'px py'=. +. x
'qxa qyb'=. ":each +. y
'(',a,', ', b,')'
r j. py-~ L * px-r=. (px+qx)-~*~L=. (qy-py)%qx-px
end.
)
 
mul=: [:{.[:;[:(,dbl)/@]`((add,dbl@])/@])@.[&.>/([:<"0 #:@]),[:< zero,[
 
from=: j.~[:(**3&%:@|)C-~*~
 
show =: (,('( ',[,' , ',' )',~])&":/@+.@])`('Zero',~[)@.(isZero@])
 
 
task=: 3 :0
a=. from 1
b=. from 2
 
smoutput echo 'a = ', show a
smoutput echo 'b = ', show b
smoutput echo 'c = a + b = ', show c =. a add b
smoutput echo 'd = -c = ', show d =. neg c
smoutput echo 'c + d = ', show c add d
smoutput echo 'a + b + d = ', show add/ a, b, d
smoutput echo 'a * 12345 = ', show a mul 12345
)</lang>
{{out}}
<lang j> task 'uit'
a = ( _1.81712 , 1 )
b = ( _1.44225 , 2 )
c = a + b = ( 10.3754 , _33.5245 )
d = -c = ( 10.3754 , 33.5245 )
c + d = Zero
a + b + d = Zero
a * 12345 = ( 10.7586 , 35.3875 3874)</lang>
 
=={{header|Java}}==
6,962

edits