Elliptic curve arithmetic: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed whitespace, optimized some functions, indented the output, used a template for the output section.)
m (→‎{{header|Perl}}: clarify package vs local variables, output fixes)
Line 873: Line 873:
{{trans|C}}
{{trans|C}}
<lang perl>package EC;
<lang perl>package EC;
our ($a, $b) = (0, 7);
{
{
our ($A, $B) = (0, 7);
package EC::Point;
package EC::Point;
sub new { my $class = shift; bless [ @_ ], $class }
sub new { my $class = shift; bless [ @_ ], $class }
Line 906: Line 906:


package Test;
package Test;
my $a = +EC::Point->new(-($EC::b - 1)**(1/3), 1);
my $p = +EC::Point->new(-($EC::B - 1)**(1/3), 1);
my $b = +EC::Point->new(-($EC::b - 4)**(1/3), 2);
my $q = +EC::Point->new(-($EC::B - 4)**(1/3), 2);
print my $c = $a + $b, "\n";
my $s = $p + $q, "\n";
print "$_\n" for $a, $b, $c;
print "$_\n" for $p, $q, $s;
print "check alignement... ";
print "check alignment... ";
print abs(($b->x - $a->x)*(-$c->y - $a->y) - ($b->y - $a->y)*($c->x - $a->x)) < 0.001
print abs(($q->x - $p->x)*(-$s->y - $p->y) - ($q->y - $p->y)*($s->x - $p->x)) < 0.001
? "ok" : "wrong";
? "ok" : "wrong";</lang>
</lang>
{{out}}
{{out}}
<pre>EC-point at x=-1.817121, y=1.000000
<pre>EC-point at x=-1.817121, y=1.000000
EC-point at x=-1.442250, y=2.000000
EC-point at x=-1.442250, y=2.000000
EC-point at x=10.375375, y=-33.524509
EC-point at x=10.375375, y=-33.524509
check alignement... ok
check alignment... ok
</pre>
</pre>