Jump to content

Elliptic curve arithmetic: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 46:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T Point
Float x, y
 
Line 118:
show(‘c + d =’, c.add(d))
show(‘a + b + d =’, a.add(b.add(d)))
show(‘a * 12345 =’, a.mul(12345))</langsyntaxhighlight>
 
{{out}}
Line 132:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 205:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 219:
=={{header|C++}}==
Uses C++11 or later
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
 
Line 426:
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 455:
=={{header|D}}==
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.string;
 
enum bCoeff = 7;
Line 532:
writeln("a + b + d = ", a + b + d);
writeln("a * 12345 = ", a * 12345);
}</langsyntaxhighlight>
{{out}}
<pre>a = (-1.817, 1.000)
Line 544:
=={{header|EchoLisp}}==
===Arithmetic===
<langsyntaxhighlight lang="scheme">
(require 'struct)
(decimals 4)
Line 608:
(printf "%d additions a+(a+(a+...))) → %a" n e)
(printf "multiplication a x %d → %a" n (E-mul a n)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 631:
===Plotting===
;; Result at http://www.echolalie.org/echolisp/help.html#plot-xy
<langsyntaxhighlight lang="scheme">
(define (E-plot (r 3))
(define (Ellie x y) (- (* y y) (* x x x) 7))
Line 648:
(plot-segment R.x R.y R.x (- R.y))
)
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 744:
show("a + b + d = ", add(a, add(b, d)))
show("a * 12345 = ", mul(a, 12345))
}</langsyntaxhighlight>
{{out}}
<pre>a = (-1.817, 1.000)
Line 756:
=={{header|Haskell}}==
First, some useful imports:
<langsyntaxhighlight lang="haskell">import Data.Monoid
import Control.Monad (guard)
import Test.QuickCheck (quickCheck)</langsyntaxhighlight>
 
The datatype for a point on an elliptic curve:
 
<langsyntaxhighlight lang="haskell">import Data.Monoid
 
data Elliptic = Elliptic Double Double | Zero
Line 776:
 
inv Zero = Zero
inv (Elliptic x y) = Elliptic x (-y)</langsyntaxhighlight>
 
Points on elliptic curve form a monoid:
<langsyntaxhighlight lang="haskell">instance Monoid Elliptic where
mempty = Zero
 
Line 791:
mkElliptic l = let x = l^2 - x1 - x2
y = l*(x1 - x) - y1
in Elliptic x y</langsyntaxhighlight>
 
Examples given in other solutions:
 
<langsyntaxhighlight lang="haskell">ellipticX b y = Elliptic (qroot (y^2 - b)) y
where qroot x = signum x * abs x ** (1/3)</langsyntaxhighlight>
 
<pre>λ> let a = ellipticX 7 1
Line 816:
 
1. direct monoidal solution:
<langsyntaxhighlight lang="haskell">mult :: Int -> Elliptic -> Elliptic
mult n = mconcat . replicate n</langsyntaxhighlight>
 
2. efficient recursive solution:
<langsyntaxhighlight lang="haskell">n `mult` p
| n == 0 = Zero
| n == 1 = p
Line 826:
| n < 0 = inv ((-n) `mult` p)
| even n = 2 `mult` ((n `div` 2) `mult` p)
| odd n = p <> (n -1) `mult` p</langsyntaxhighlight>
 
<pre>λ> 12345 `mult` a
Line 835:
We use QuickCheck to test general properties of points on arbitrary elliptic curve.
 
<langsyntaxhighlight lang="haskell">-- for given a, b and x returns a point on the positive branch of elliptic curve (if point exists)
elliptic a b Nothing = Just Zero
elliptic a b (Just x) =
Line 853:
commutativity a b x1 x2 =
let p = elliptic a b
in p x1 <> p x2 == p x2 <> p x1</langsyntaxhighlight>
 
<pre>λ> quickCheck addition
Line 865:
Follows the C contribution.
 
<langsyntaxhighlight lang="j">zero=: _j_
isZero=: 1e20 < |@{.@+.
Line 922:
echo 'a + b + d = ', show add/ a, b, d
echo 'a * 12345 = ', show a mul 12345
)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="j"> task ''
a = (_1.81712, 1)
b = (_1.44225, 2)
Line 931:
c + d = Zero
a + b + d = Zero
a * 12345 = (10.7586, 35.3874)</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|D}}
<langsyntaxhighlight lang="java">import static java.lang.Math.*;
import java.util.Locale;
 
Line 1,021:
return String.format(Locale.US, "(%.3f,%.3f)", this.x, this.y);
}
}</langsyntaxhighlight>
<pre>a = (-1.817,1.000)
b = (-1.442,2.000)
Line 1,034:
{{trans|Python}}
 
<langsyntaxhighlight lang="julia">struct Point{T<:AbstractFloat}
x::T
y::T
Line 1,092:
@show c + d
@show a + b + d
@show 12345a</langsyntaxhighlight>
 
{{out}}
Line 1,105:
===Julia 1.x compatible version===
Adds assertion checks for points to be on the curve.
<langsyntaxhighlight lang="julia">
using Printf
import Base.in
Line 1,190:
@show a + b + d
@show 12345a
</syntaxhighlight>
</lang>
Output: Same as the original, Julia 0.6 version code.
 
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.4
 
const val C = 7
Line 1,252:
println("a + b + d = ${a + b + d}")
println("a * 12345 = ${a * 12345}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,267:
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
const B = 7
Line 1,348:
echo "c + d = ", c + d
echo "a + b + d = ", a + b + d
echo "a * 12345 = ", a * 12345</langsyntaxhighlight>
 
{{out}}
Line 1,362:
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil].
Some float-precision issues but overall works.
<syntaxhighlight lang="ocaml">
<lang OCaml>
(* Task : Elliptic_curve_arithmetic *)
 
Line 1,496:
print_output ();
print_output ()
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,518:
=={{header|PARI/GP}}==
The examples were borrowed from C, though the coding is built-in for GP and so not ported.
<langsyntaxhighlight lang="parigp">e=ellinit([0,7]);
a=[-6^(1/3),1]
b=[-3^(1/3),2]
Line 1,525:
elladd(e,c,d)
elladd(e,elladd(e,a,b),d)
ellmul(e,a,12345)</langsyntaxhighlight>
{{output}}
<pre>%1 = [-1.8171205928321396588912117563272605024, 1]
Line 1,537:
=={{header|Perl}}==
{{trans|C}}
<langsyntaxhighlight lang="perl">package EC;
{
our ($A, $B) = (0, 7);
Line 1,577:
print "check alignment... ";
print abs(($q->x - $p->x)*(-$s->y - $p->y) - ($q->y - $p->y)*($s->x - $p->x)) < 0.001
? "ok" : "wrong";</langsyntaxhighlight>
{{out}}
<pre>EC-point at x=-1.817121, y=1.000000
Line 1,587:
=={{header|Phix}}==
{{Trans|C}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">X</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bCoeff</span><span style="color: #0000FF;">=</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">INF</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e300</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1e300</span>
Line 1,667:
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"a + b + d = "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)))</span>
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"a * 12345 = "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12345</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,680:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 16)
(load "@lib/math.l")
(setq *B 7)
Line 1,758:
(prinl "D + C: " (prn (add C D)))
(prinl "A + B + D: " (prn (add A (add B D))))
(prinl "A * 12345: " (prn (mul A 12345)))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,772:
=={{header|Python}}==
{{trans|C}}
<langsyntaxhighlight lang="python">#!/usr/bin/env python3
 
class Point:
Line 1,846:
show("c + d =", c.add(d))
show("a + b + d =", a.add(b.add(d)))
show("a * 12345 =", a.mul(12345))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,859:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define a 0) (define b 7)
Line 1,886:
[(even? n) (⊗ (⊗ p (/ n 2)) 2)]
[(odd? n) (⊕ p (⊗ p (- n 1)))]))
</syntaxhighlight>
</lang>
Test:
<langsyntaxhighlight lang="racket">
(define (root3 x) (* (sgn x) (expt (abs x) 1/3)))
(define (y->point y) (vector (root3 (- (* y y) b)) y))
Line 1,900:
(displayln (~a "p+(q+(-(p+q))) = 0 " (zero? (⊕ p (⊕ q (neg (⊕ p q)))))))
(displayln (~a "p*12345 " (⊗ p 12345)))
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="racket">
p = #(-1.8171205928321397 1)
q = #(-1.4422495703074083 2)
Line 1,910:
p+(q+(-(p+q))) = 0 #t
p*12345 #(10.758570529320806 35.387434774282106)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>unit module EC;
our ($A, $B) = (0, 7);
 
Line 1,962:
say my $s = $p + $q;
 
say "checking alignment: ", abs ($p.x - $q.x)*(-$s.y - $q.y) - ($p.y - $q.y)*($s.x - $q.x);</langsyntaxhighlight>
{{out}}
<pre>EC Point at x=1, y=2.8284271247461903
Line 1,974:
 
Also, some code was added to have the output better aligned &nbsp; (for instance, negative and positive numbers).
<langsyntaxhighlight lang="rexx">/*REXX program defines (for any 2 points on the curve), returns the sum of the 2 points.*/
numeric digits 100 /*try to ensure a min. of accuracy loss*/
a= func(1) ; say ' a = ' show(a)
Line 2,009:
do until d==a; d=min(d+d,a); numeric digits d; o=0
do until o=g; o=g; g=format((m*g**y+x)/y/g**m,,d-2); end /*until o=g*/
end /*until d==a*/; _=g*sign(ox); if oy<0 then _=1/_; return _</langsyntaxhighlight>
{{out|output}}
<pre>
Line 2,022:
=={{header|Sage}}==
Examples from C, using the built-in Elliptic curves library.
<langsyntaxhighlight lang="sage">Ellie = EllipticCurve(RR,[0,7]) # RR = field of real numbers
 
# a point (x,y) on Ellie, given y
Line 2,039:
print('S = P + Q',S)
print('P+Q-S', P+Q-S)
print('P*12345' ,P*12345)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,054:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">module EC {
 
var A = 0
Line 2,149:
say var s = (p + q)
 
say ("checking alignment: ", abs((p.x - q.x)*(-s.y - q.y) - (p.y - q.y)*(s.x - q.x)) < 1e-20)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,160:
=={{header|Tcl}}==
{{trans|C}}
<langsyntaxhighlight lang="tcl">set C 7
set zero {x inf y inf}
proc tcl::mathfunc::cuberoot n {
Line 2,203:
}
return $r
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">proc show {s p} {
if {[iszero $p]} {
puts "${s}Zero"
Line 2,227:
show "c + d = " [add $c $d]
show "a + b + d = " [add $a [add $b $d]]
show "a * 12345 = " [multiply $a 12345]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,241:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">import math
 
const b_coeff = 7
Line 2,331:
show("a + b + d = ", add(a, add(b, d)))
show("a * 12345 = ", mul(mut a, 12345))
}</langsyntaxhighlight>
 
{{out}}
Line 2,347:
{{trans|C}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var C = 7
Line 2,413:
System.print("c + d = %(c + d)")
System.print("a + b + d = %(a + b + d)")
System.print("a * 12345 = %(a * 12345)")</langsyntaxhighlight>
 
{{out}}
Line 2,428:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">const C=7, INFINITY=(0.0).inf;
 
fcn zero{ T(INFINITY, INFINITY) }
Line 2,466:
}
r
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn show(str,p)
{ println(str, is_zero(p) and "Zero" or "(%.3f, %.3f)".fmt(p.xplode())) }
Line 2,482:
show("c + d = ", add(c, d));
show("a + b + d = ", add(a, add(b, d)));
show("a * 12345 = ", mul(a, 12345.0));</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.