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