Montgomery reduction: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (šyntax highlighting fixup automation) |
|||
Line 20: | Line 20: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">T Montgomery |
||
BigInt m |
BigInt m |
||
Int n |
Int n |
||
Line 77: | Line 77: | ||
print(mont.reduce(prod)) |
print(mont.reduce(prod)) |
||
print("\nAlternate computation of x1 ^ x2 mod m:") |
print("\nAlternate computation of x1 ^ x2 mod m:") |
||
print(pow(x1, x2, m))</ |
print(pow(x1, x2, m))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 103: | Line 103: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 219: | Line 219: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>b : 2 |
<pre>b : 2 |
||
Line 243: | Line 243: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Numerics; |
using System.Numerics; |
||
Line 334: | Line 334: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>b : 2 |
<pre>b : 2 |
||
Line 357: | Line 357: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include<iostream> |
||
#include<conio.h> |
#include<conio.h> |
||
using namespace std; |
using namespace std; |
||
Line 442: | Line 442: | ||
cout<<"Montgomery domain representation = "<<e; |
cout<<"Montgomery domain representation = "<<e; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="d">import std.bigint; |
||
import std.stdio; |
import std.stdio; |
||
Line 544: | Line 544: | ||
writeln("\nAlternate computation of x1 ^ x2 mod m :"); |
writeln("\nAlternate computation of x1 ^ x2 mod m :"); |
||
writeln(x1.modPow(x2, m)); |
writeln(x1.modPow(x2, m)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>b : 2 |
<pre>b : 2 |
||
Line 569: | Line 569: | ||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
{{works with|Factor|0.99 2020-08-14}} |
{{works with|Factor|0.99 2020-08-14}} |
||
< |
<syntaxhighlight lang="factor">USING: io kernel locals math math.bitwise math.functions |
||
prettyprint ; |
prettyprint ; |
||
Line 606: | Line 606: | ||
"Library-based computation of x1^x2 mod m: " write |
"Library-based computation of x1^x2 mod m: " write |
||
x1 x2 m ^mod . |
x1 x2 m ^mod . |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 619: | Line 619: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 726: | Line 726: | ||
fmt.Println("\nLibrary-based computation of x1 ^ x2 mod m:") |
fmt.Println("\nLibrary-based computation of x1 ^ x2 mod m:") |
||
fmt.Println(new(big.Int).Exp(x1, x2, m)) |
fmt.Println(new(big.Int).Exp(x1, x2, m)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 752: | Line 752: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="java">import java.math.BigInteger; |
||
public class MontgomeryReduction { |
public class MontgomeryReduction { |
||
Line 829: | Line 829: | ||
System.out.println(x1.modPow(x2, m)); |
System.out.println(x1.modPow(x2, m)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>b : 2 |
<pre>b : 2 |
||
Line 853: | Line 853: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="julia">""" base 2 type Montgomery numbers """ |
||
struct Montgomery2 |
struct Montgomery2 |
||
m::BigInt |
m::BigInt |
||
Line 923: | Line 923: | ||
testmontgomery2() |
testmontgomery2() |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
b : 2 |
b : 2 |
||
Line 948: | Line 948: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.3 |
||
import java.math.BigInteger |
import java.math.BigInteger |
||
Line 1,020: | Line 1,020: | ||
println("\nLibrary-based computation of x1 ^ x2 mod m :") |
println("\nLibrary-based computation of x1 ^ x2 mod m :") |
||
println(x1.modPow(x2, m)) |
println(x1.modPow(x2, m)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,048: | Line 1,048: | ||
{{trans|D}} |
{{trans|D}} |
||
{{libheader|bignum}} |
{{libheader|bignum}} |
||
< |
<syntaxhighlight lang="nim">import bignum |
||
# Missing functions in "bignum". |
# Missing functions in "bignum". |
||
Line 1,134: | Line 1,134: | ||
echo mont.reduce(prod) |
echo mont.reduce(prod) |
||
echo "\nAlternate computation of x1^x2 mod m:" |
echo "\nAlternate computation of x1^x2 mod m:" |
||
echo x1.exp(x2, m)</ |
echo x1.exp(x2, m)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,160: | Line 1,160: | ||
{{trans|Raku}} |
{{trans|Raku}} |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use bigint; |
||
use ntheory qw(powmod); |
use ntheory qw(powmod); |
||
Line 1,203: | Line 1,203: | ||
print montgomery_reduce($m, $prod) . "\n"; |
print montgomery_reduce($m, $prod) . "\n"; |
||
printf "Built-in op computation x1**x2 mod m: %s\n", powmod($x1, $x2, $m);</ |
printf "Built-in op computation x1**x2 mod m: %s\n", powmod($x1, $x2, $m);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Original x1: 540019781128412936473322405310 |
<pre>Original x1: 540019781128412936473322405310 |
||
Line 1,216: | Line 1,216: | ||
{{trans|D}} |
{{trans|D}} |
||
{{libheader|Phix/mpfr}} |
{{libheader|Phix/mpfr}} |
||
<!--< |
<!--<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;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 1,296: | Line 1,296: | ||
<span style="color: #7060A8;">mpz_powm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">mpz_powm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,318: | Line 1,318: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de **Mod (X Y N) |
||
(let M 1 |
(let M 1 |
||
(loop |
(loop |
||
Line 1,370: | Line 1,370: | ||
Base (reduce (* Base Base)) ) ) |
Base (reduce (* Base Base)) ) ) |
||
(prinl (reduce Prod)) |
(prinl (reduce Prod)) |
||
(prinl "Montgomery computation of x1 \^ x2 mod m : " (**Mod X1 X2 M)) )</ |
(prinl "Montgomery computation of x1 \^ x2 mod m : " (**Mod X1 X2 M)) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>b : 2 |
<pre>b : 2 |
||
Line 1,391: | Line 1,391: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="python">class Montgomery: |
||
BASE = 2 |
BASE = 2 |
||
Line 1,447: | Line 1,447: | ||
print mont.reduce(prod) |
print mont.reduce(prod) |
||
print "\nAlternate computation of x1 ^ x2 mod m :" |
print "\nAlternate computation of x1 ^ x2 mod m :" |
||
print pow(x1, x2, m)</ |
print pow(x1, x2, m)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>b : 2 |
<pre>b : 2 |
||
Line 1,471: | Line 1,471: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang typed/racket |
||
(require math/number-theory) |
(require math/number-theory) |
||
Line 1,519: | Line 1,519: | ||
(define mr (montgomery-reduce-fn m b)) |
(define mr (montgomery-reduce-fn m b)) |
||
(check-equal? (mr R1 n) x1) |
(check-equal? (mr R1 n) x1) |
||
(check-equal? (mr R2 n) x2)))</ |
(check-equal? (mr R2 n) x2)))</syntaxhighlight> |
||
Tests, which are courtesy of #Go implementation, all pass. |
Tests, which are courtesy of #Go implementation, all pass. |
||
Line 1,528: | Line 1,528: | ||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub montgomery-reduce($m, $a is copy) { |
||
for 0..$m.msb { |
for 0..$m.msb { |
||
$a += $m if $a +& 1; |
$a += $m if $a +& 1; |
||
Line 1,560: | Line 1,560: | ||
say montgomery-reduce($m, $prod); |
say montgomery-reduce($m, $prod); |
||
say "Built-in op computation x1**x2 mod m: ", $x1.expmod($x2, $m);</ |
say "Built-in op computation x1**x2 mod m: ", $x1.expmod($x2, $m);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Original x1: 540019781128412936473322405310 |
<pre>Original x1: 540019781128412936473322405310 |
||
Line 1,573: | Line 1,573: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|zkl}} |
{{trans|zkl}} |
||
< |
<syntaxhighlight lang="ruby">func montgomeryReduce(m, a) { |
||
{ |
{ |
||
a += m if a.is_odd |
a += m if a.is_odd |
||
Line 1,606: | Line 1,606: | ||
say(montgomeryReduce(m, prod)) |
say(montgomeryReduce(m, prod)) |
||
say("Library-based computation of x1^x2 mod m: ", x1.powmod(x2, m))</ |
say("Library-based computation of x1^x2 mod m: ", x1.powmod(x2, m))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,620: | Line 1,620: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{in progress|lang=Tcl|day=25|month=06|year=2012}} |
{{in progress|lang=Tcl|day=25|month=06|year=2012}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
proc montgomeryReduction {m mDash T n {b 2}} { |
proc montgomeryReduction {m mDash T n {b 2}} { |
||
Line 1,634: | Line 1,634: | ||
set A [expr {$A / ($b ** $n)}] |
set A [expr {$A / ($b ** $n)}] |
||
return [expr {$A >= $m ? $A - $m : $A}] |
return [expr {$A >= $m ? $A - $m : $A}] |
||
}</ |
}</syntaxhighlight> |
||
<!-- Not quite sure how to demonstrate this working; examples above aren't very clear… --> |
<!-- Not quite sure how to demonstrate this working; examples above aren't very clear… --> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Imports System.Numerics |
||
Imports System.Runtime.CompilerServices |
Imports System.Runtime.CompilerServices |
||
Line 1,734: | Line 1,734: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>b : 2 |
<pre>b : 2 |
||
Line 1,759: | Line 1,759: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-big}} |
{{libheader|Wren-big}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/big" for BigInt |
||
class Montgomery { |
class Montgomery { |
||
Line 1,825: | Line 1,825: | ||
System.print(mont.reduce(prod)) |
System.print(mont.reduce(prod)) |
||
System.print("\nLibrary-based computation of x1 ^ x2 mod m :") |
System.print("\nLibrary-based computation of x1 ^ x2 mod m :") |
||
System.print(x1.modPow(x2, m))</ |
System.print(x1.modPow(x2, m))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,853: | Line 1,853: | ||
{{Trans|Go}} |
{{Trans|Go}} |
||
Uses GMP (GNU Multi Precision library). |
Uses GMP (GNU Multi Precision library). |
||
< |
<syntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"); // libGMP |
||
fcn montgomeryReduce(modulus,T){ |
fcn montgomeryReduce(modulus,T){ |
||
Line 1,864: | Line 1,864: | ||
if(a>=modulus) a.sub(modulus); |
if(a>=modulus) a.sub(modulus); |
||
a |
a |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl"> // magic numbers from the Go solution |
||
//b:= 2; |
//b:= 2; |
||
//n:= 100; |
//n:= 100; |
||
Line 1,899: | Line 1,899: | ||
} |
} |
||
println(montgomeryReduce(m,prod)); |
println(montgomeryReduce(m,prod)); |
||
println("Library-based computation of x1 ^ x2 mod m: ",x1.powm(x2,m));</ |
println("Library-based computation of x1 ^ x2 mod m: ",x1.powm(x2,m));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |