Elliptic curve arithmetic: Difference between revisions
Content added Content deleted
m (→{{header|Sage}}: updated to work with Python 3) |
Alextretyak (talk | contribs) (Added 11l) |
||
Line 42: | Line 42: | ||
<br>for any point '''P''' and integer '''n''', the point '''P + P + ... + P''' ('''n''' times). |
<br>for any point '''P''' and integer '''n''', the point '''P + P + ... + P''' ('''n''' times). |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<lang 11l>T Point |
|||
Float x, y |
|||
F (x = Float.infinity, y = Float.infinity) |
|||
.x = x |
|||
.y = y |
|||
F.const copy() |
|||
R Point(.x, .y) |
|||
F.const is_zero() |
|||
R .x > 1e20 | .x < -1e20 |
|||
F neg() |
|||
R Point(.x, -.y) |
|||
F dbl() |
|||
I .is_zero() |
|||
R .copy() |
|||
I .y == 0 |
|||
R Point() |
|||
V l = (3 * .x * .x) / (2 * .y) |
|||
V x = l * l - 2 * .x |
|||
R Point(x, l * (.x - x) - .y) |
|||
F add(q) |
|||
I .x == q.x & .y == q.y |
|||
R .dbl() |
|||
I .is_zero() |
|||
R q.copy() |
|||
I q.is_zero() |
|||
R .copy() |
|||
I q.x - .x == 0 |
|||
R Point() |
|||
V l = (q.y - .y) / (q.x - .x) |
|||
V x = l * l - .x - q.x |
|||
R Point(x, l * (.x - x) - .y) |
|||
F mul(n) |
|||
V p = .copy() |
|||
V r = Point() |
|||
V i = 1 |
|||
L i <= n |
|||
I i [&] n |
|||
r = r.add(p) |
|||
p = p.dbl() |
|||
i <<= 1 |
|||
R r |
|||
F String() |
|||
R ‘(#.3, #.3)’.format(.x, .y) |
|||
V Point_b = 7 |
|||
F show(s, p) |
|||
print(s‘ ’(I p.is_zero() {‘Zero’} E p)) |
|||
F from_y(y) |
|||
V n = y * y - Point_b |
|||
V x = I n >= 0 {n ^ (1. / 3)} E -((-n) ^ (1. / 3)) |
|||
R Point(x, y) |
|||
V a = from_y(1) |
|||
V b = from_y(2) |
|||
show(‘a =’, a) |
|||
show(‘b =’, b) |
|||
V c = a.add(b) |
|||
show(‘c = a + b =’, c) |
|||
V d = c.neg() |
|||
show(‘d = -c =’, d) |
|||
show(‘c + d =’, c.add(d)) |
|||
show(‘a + b + d =’, a.add(b.add(d))) |
|||
show(‘a * 12345 =’, a.mul(12345))</lang> |
|||
{{out}} |
|||
<pre> |
|||
a = (-1.817, 1.000) |
|||
b = (-1.442, 2.000) |
|||
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|C}}== |
=={{header|C}}== |