Elliptic curve arithmetic: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
m (→{{header|Wren}}: Minor tidy) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1,029:
a + b + d = Zero
a * 12345 = (10.759,35.387)</pre>
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
''Also works with gojq and fq''
'''Preliminaries'''
<syntaxhighlight lang=jq>
def round($ndec): pow(10;$ndec) as $p | . * $p | round / $p;
def idiv2: (. - (. % 2)) / 2;
def bitwise:
recurse( if . >= 2 then idiv2 else empty end) | . % 2;
def bitwise_and_nonzero($x; $y):
[$x|bitwise] as $x
| [$y|bitwise] as $y
| ([$x, $y] | map(length) | min) as $min
| any(range(0; $min) ; $x[.] == 1 and $y[.] == 1);
</syntaxhighlight>
'''Elliptic Curve Arithmetic'''
<syntaxhighlight lang=jq>
def Pt(x;y): [x, y];
def isPt: type == "array" and length == 2 and (map(type)|unique) == ["number"];
def zero: Pt(infinite; infinite);
def isZero: .[0] | (isinfinite or . == 0);
def C: 7;
def fromNum: Pt((.*. - C)|cbrt; .) ;
def double:
if isZero then .
else . as [$x,$y]
| ((3 * ($x * $x)) / (2 * .[1])) as $l
| ($l*$l - 2*$x) as $t
| Pt($t; $l*($x - $t) - $y)
end;
def minus: .[1] *= -1;
def plus($other):
if ($other|isPt|not) then "Argument of plus(Pt) must be a Pt object but got \(.)." | error
elif (.[0] == $other[0] and .[1] == $other[1]) then double
elif isZero then $other
elif ($other|isZero) then .
else . as [$x, $y]
| (if $other[0] == $x then infinite
else (($other[1] - $y) / ($other[0] - $x)) end) as $l
| ($l*$l - $x - $other[0]) as $t
| Pt($t; $l*($x-$t) - $y)
end;
def plus($a; $b): $a | plus($b);
def mult($n):
if ($n|type) != "number" or ($n | ( . != floor))
then "Argument must be an integer, not \($n)." | error
else { r: zero,
p: .,
i: 1 }
| until (.i > $n;
if bitwise_and_nonzero(.i; $n) then .r = plus(.r;.p) else . end
| .p |= double
| .i *= 2 )
| .r
end;
def toString:
if isZero then "Zero"
else map(round(3))
end;
def a: 1|fromNum;
def b: 2|fromNum;
def c: plus(a; b);
def d: c | minus;
def task:
"a = \(a|toString)",
"b = \(b|toString)",
"c = a + b = \(c|toString)",
"d = -c = \(d|toString)",
"c + d = \(plus(c; d)|toString)",
"(a+b) + d = \(plus(plus(a; b);d)|toString)",
"a * 12345 = \(a | mult(12345) | toString)"
;
task
</syntaxhighlight>
{{output}}
<pre>
a = [-1.817,1]
b = [-1.442,2]
c = a + b = [10.375,-33.525]
d = -c = [10.375,33.525]
c + d = Zero
(a+b) + d = Zero
a * 12345 = [10.759,35.387]
</pre>
=={{header|Julia}}==
Line 1,914 ⟶ 2,019:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>
our ($A, $B) = (0, 7);
our class Point {
has ($.x, $.y);
multi method new(
$x, $y where $y**2
) {
multi method
multi method gist(::?CLASS:U:) {
}
multi prefix:<->(Point $p) is export { Point.new: x => $p.x, y => -$p.y }
multi prefix:<->(
multi infix:<->(Point $a, Point $b) is export { $a + -$b }
multi infix:<+>(
multi infix:<+>(Point $p,
multi infix:<*>(Point $u, Int $n) is export { $n * $u }
multi infix:<*>(Int $n,
multi infix:<*>(0, Point) is export {
multi infix:<*>(1, Point $p) is export { $p }
multi infix:<*>(2, Point $p) is export {
my $l = (3*$p.x**2 + $A) / (2 *$p.y);
my $y = $l*($p.x - my $x = $l**2 - 2*$p.x) - $p.y;
$p.
}
multi infix:<*>(Int $n where $n > 2, Point $p) is export {
2 * ($n div 2 * $p) + $n % 2 * $p;
}
multi infix:<+>(Point $p, Point $q) is export {
if $p.x ~~ $q.x {
}
else {
}
}
}
import EC;
say my $p = EC::Point.new: x => $_, y => sqrt(abs($_**3 + $EC::A*$_ + $EC::B)) given 1;
say my $q = EC::Point.new: x => $_, y => sqrt(abs($_**3 + $EC::A*$_ + $EC::B)) given 2;
say my $s = $p + $q;
use Test;
is abs(($p.x - $q.x)*(-$s.y - $q.y) - ($p.y - $q.y)*($s.x - $q.x)), 0, "S, P and Q are aligned";</syntaxhighlight>
{{out}}
<pre>EC Point at x=1, y=2.8284271247461903
EC Point at x=2, y=3.872983346207417
EC Point at x=-1.9089023002066448, y=0.21008487055753378
ok 1 - S, P and Q are aligned</pre>
=={{header|REXX}}==
Line 2,347 ⟶ 2,456:
{{trans|C}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var C = 7
Line 2,423 ⟶ 2,532:
c + d = Zero
a + b + d = Zero
a * 12345 = (10.759, 35.
</pre>
|