no commit message

This commit is contained in:
niandeng
2020-09-17 18:40:01 +08:00
parent 4382915d09
commit d957abceaa
283 changed files with 86935 additions and 63 deletions
@@ -0,0 +1,90 @@
package com.hurlant.math
{
use namespace bi_internal;
internal class BarrettReduction implements IReduction
{
private var m:BigInteger;
private var r2:BigInteger;
private var q3:BigInteger;
private var mu:BigInteger;
public function BarrettReduction(m:BigInteger) {
// setup Barrett
r2 = new BigInteger;
q3 = new BigInteger;
BigInteger.ONE.dlShiftTo(2*m.t, r2);
mu = r2.divide(m);
this.m = m;
}
public function revert(x:BigInteger):BigInteger
{
return x;
}
/**
*
* @param x
* @param y
* @param r = x*y mod m; x != r
*
*/
public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void
{
x.multiplyTo(y, r);
reduce(r);
}
/**
*
* @param x
* @param r = x^2 mod m; x != r
*
*/
public function sqrTo(x:BigInteger, r:BigInteger):void
{
x.squareTo(r);
reduce(r);
}
public function convert(x:BigInteger):BigInteger
{
if (x.s<0 || x.t>2*m.t) {
return x.mod(m);
} else if (x.compareTo(m)<0) {
return x;
} else {
var r:BigInteger = new BigInteger;
x.copyTo(r);
reduce(r);
return r;
}
}
/**
*
* @param x = x mod m (HAC 14.42)
*
*/
public function reduce(lx:BigInteger):void
{
var x:BigInteger = lx as BigInteger;
x.drShiftTo(m.t-1,r2);
if (x.t>m.t+1) {
x.t = m.t+1;
x.clamp();
}
mu.multiplyUpperTo(r2, m.t+1, q3);
m.multiplyLowerTo(q3, m.t+1, r2);
while (x.compareTo(r2)<0) {
x.dAddOffset(1, m.t+1);
}
x.subTo(r2,x);
while (x.compareTo(m)>=0) {
x.subTo(m,x);
}
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,35 @@
package com.hurlant.math
{
use namespace bi_internal;
/**
* Modular reduction using "classic" algorithm
*/
internal class ClassicReduction implements IReduction
{
private var m:BigInteger;
public function ClassicReduction(m:BigInteger) {
this.m = m;
}
public function convert(x:BigInteger):BigInteger {
if (x.s<0 || x.compareTo(m)>=0) {
return x.mod(m);
}
return x;
}
public function revert(x:BigInteger):BigInteger {
return x;
}
public function reduce(x:BigInteger):void {
x.divRemTo(m, null,x);
}
public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void {
x.multiplyTo(y,r);
reduce(r);
}
public function sqrTo(x:BigInteger, r:BigInteger):void {
x.squareTo(r);
reduce(r);
}
}
}
+11
View File
@@ -0,0 +1,11 @@
package com.hurlant.math
{
internal interface IReduction
{
function convert(x:BigInteger):BigInteger;
function revert(x:BigInteger):BigInteger;
function reduce(x:BigInteger):void;
function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void;
function sqrTo(x:BigInteger, r:BigInteger):void;
}
}
@@ -0,0 +1,85 @@
package com.hurlant.math
{
use namespace bi_internal;
/**
* Montgomery reduction
*/
internal class MontgomeryReduction implements IReduction
{
private var m:BigInteger;
private var mp:int;
private var mpl:int;
private var mph:int;
private var um:int;
private var mt2:int;
public function MontgomeryReduction(m:BigInteger) {
this.m = m;
mp = m.invDigit();
mpl = mp & 0x7fff;
mph = mp>>15;
um = (1<<(BigInteger.DB-15))-1;
mt2 = 2*m.t;
}
/**
* xR mod m
*/
public function convert(x:BigInteger):BigInteger {
var r:BigInteger = new BigInteger;
x.abs().dlShiftTo(m.t, r);
r.divRemTo(m, null, r);
if (x.s<0 && r.compareTo(BigInteger.ZERO)>0) {
m.subTo(r,r);
}
return r;
}
/**
* x/R mod m
*/
public function revert(x:BigInteger):BigInteger {
var r:BigInteger = new BigInteger;
x.copyTo(r);
reduce(r);
return r;
}
/**
* x = x/R mod m (HAC 14.32)
*/
public function reduce(x:BigInteger):void {
while (x.t<=mt2) { // pad x so am has enough room later
x.a[x.t++] = 0;
}
for (var i:int=0; i<m.t; ++i) {
// faster way of calculating u0 = x[i]*mp mod DV
var j:int = x.a[i]&0x7fff;
var u0:int = (j*mpl+(((j*mph+(x.a[i]>>15)*mpl)&um)<<15))&BigInteger.DM;
// use am to combine the multiply-shift-add into one call
j = i+m.t;
x.a[j] += m.am(0, u0, x, i, 0, m.t);
// propagate carry
while (x.a[j]>=BigInteger.DV) {
x.a[j] -= BigInteger.DV;
x.a[++j]++;
}
}
x.clamp();
x.drShiftTo(m.t, x);
if (x.compareTo(m)>=0) {
x.subTo(m,x);
}
}
/**
* r = "x^2/R mod m"; x != r
*/
public function sqrTo(x:BigInteger, r:BigInteger):void {
x.squareTo(r);
reduce(r);
}
/**
* r = "xy/R mod m"; x,y != r
*/
public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void {
x.multiplyTo(y,r);
reduce(r);
}
}
}
+34
View File
@@ -0,0 +1,34 @@
package com.hurlant.math
{
use namespace bi_internal;
/**
* A "null" reducer
*/
public class NullReduction implements IReduction
{
public function revert(x:BigInteger):BigInteger
{
return x;
}
public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void
{
x.multiplyTo(y,r);
}
public function sqrTo(x:BigInteger, r:BigInteger):void
{
x.squareTo(r);
}
public function convert(x:BigInteger):BigInteger
{
return x;
}
public function reduce(x:BigInteger):void
{
}
}
}
+11
View File
@@ -0,0 +1,11 @@
/**
* bi_internal
*
* A namespace. w00t.
* Copyright (c) 2007 Henri Torgemane
*
* See LICENSE.txt for full license information.
*/
package com.hurlant.math {
public namespace bi_internal = "http://crypto.hurlant.com/BigInteger";
}