bitcoin-atm

bitcoin atm for pyc inc.

git clone https://9o.is/git/bitcoin-atm.git

gf256poly.js

(6450B)


      1 /*
      2   Ported to JavaScript by Lazar Laszlo 2011 
      3   
      4   lazarsoft@gmail.com, www.lazarsoft.info
      5   
      6 */
      7 
      8 /*
      9 *
     10 * Copyright 2007 ZXing authors
     11 *
     12 * Licensed under the Apache License, Version 2.0 (the "License");
     13 * you may not use this file except in compliance with the License.
     14 * You may obtain a copy of the License at
     15 *
     16 *      http://www.apache.org/licenses/LICENSE-2.0
     17 *
     18 * Unless required by applicable law or agreed to in writing, software
     19 * distributed under the License is distributed on an "AS IS" BASIS,
     20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     21 * See the License for the specific language governing permissions and
     22 * limitations under the License.
     23 */
     24 
     25 
     26 function GF256Poly(field,  coefficients)
     27 {
     28 	if (coefficients == null || coefficients.length == 0)
     29 	{
     30 		throw "System.ArgumentException";
     31 	}
     32 	this.field = field;
     33 	var coefficientsLength = coefficients.length;
     34 	if (coefficientsLength > 1 && coefficients[0] == 0)
     35 	{
     36 		// Leading term must be non-zero for anything except the constant polynomial "0"
     37 		var firstNonZero = 1;
     38 		while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0)
     39 		{
     40 			firstNonZero++;
     41 		}
     42 		if (firstNonZero == coefficientsLength)
     43 		{
     44 			this.coefficients = field.Zero.coefficients;
     45 		}
     46 		else
     47 		{
     48 			this.coefficients = new Array(coefficientsLength - firstNonZero);
     49 			for(var i=0;i<this.coefficients.length;i++)this.coefficients[i]=0;
     50 			//Array.Copy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.length);
     51 			for(var ci=0;ci<this.coefficients.length;ci++)this.coefficients[ci]=coefficients[firstNonZero+ci];
     52 		}
     53 	}
     54 	else
     55 	{
     56 		this.coefficients = coefficients;
     57 	}
     58 	
     59 	this.__defineGetter__("Zero", function()
     60 	{
     61 		return this.coefficients[0] == 0;
     62 	});
     63 	this.__defineGetter__("Degree", function()
     64 	{
     65 		return this.coefficients.length - 1;
     66 	});
     67 	this.__defineGetter__("Coefficients", function()
     68 	{
     69 		return this.coefficients;
     70 	});
     71 	
     72 	this.getCoefficient=function( degree)
     73 	{
     74 		return this.coefficients[this.coefficients.length - 1 - degree];
     75 	}
     76 	
     77 	this.evaluateAt=function( a)
     78 	{
     79 		if (a == 0)
     80 		{
     81 			// Just return the x^0 coefficient
     82 			return this.getCoefficient(0);
     83 		}
     84 		var size = this.coefficients.length;
     85 		if (a == 1)
     86 		{
     87 			// Just the sum of the coefficients
     88 			var result = 0;
     89 			for (var i = 0; i < size; i++)
     90 			{
     91 				result = GF256.addOrSubtract(result, this.coefficients[i]);
     92 			}
     93 			return result;
     94 		}
     95 		var result2 = this.coefficients[0];
     96 		for (var i = 1; i < size; i++)
     97 		{
     98 			result2 = GF256.addOrSubtract(this.field.multiply(a, result2), this.coefficients[i]);
     99 		}
    100 		return result2;
    101 	}
    102 	
    103 	this.addOrSubtract=function( other)
    104 		{
    105 			if (this.field != other.field)
    106 			{
    107 				throw "GF256Polys do not have same GF256 field";
    108 			}
    109 			if (this.Zero)
    110 			{
    111 				return other;
    112 			}
    113 			if (other.Zero)
    114 			{
    115 				return this;
    116 			}
    117 			
    118 			var smallerCoefficients = this.coefficients;
    119 			var largerCoefficients = other.coefficients;
    120 			if (smallerCoefficients.length > largerCoefficients.length)
    121 			{
    122 				var temp = smallerCoefficients;
    123 				smallerCoefficients = largerCoefficients;
    124 				largerCoefficients = temp;
    125 			}
    126 			var sumDiff = new Array(largerCoefficients.length);
    127 			var lengthDiff = largerCoefficients.length - smallerCoefficients.length;
    128 			// Copy high-order terms only found in higher-degree polynomial's coefficients
    129 			//Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff);
    130 			for(var ci=0;ci<lengthDiff;ci++)sumDiff[ci]=largerCoefficients[ci];
    131 			
    132 			for (var i = lengthDiff; i < largerCoefficients.length; i++)
    133 			{
    134 				sumDiff[i] = GF256.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
    135 			}
    136 			
    137 			return new GF256Poly(field, sumDiff);
    138 	}
    139 	this.multiply1=function( other)
    140 		{
    141 			if (this.field!=other.field)
    142 			{
    143 				throw "GF256Polys do not have same GF256 field";
    144 			}
    145 			if (this.Zero || other.Zero)
    146 			{
    147 				return this.field.Zero;
    148 			}
    149 			var aCoefficients = this.coefficients;
    150 			var aLength = aCoefficients.length;
    151 			var bCoefficients = other.coefficients;
    152 			var bLength = bCoefficients.length;
    153 			var product = new Array(aLength + bLength - 1);
    154 			for (var i = 0; i < aLength; i++)
    155 			{
    156 				var aCoeff = aCoefficients[i];
    157 				for (var j = 0; j < bLength; j++)
    158 				{
    159 					product[i + j] = GF256.addOrSubtract(product[i + j], this.field.multiply(aCoeff, bCoefficients[j]));
    160 				}
    161 			}
    162 			return new GF256Poly(this.field, product);
    163 		}
    164 	this.multiply2=function( scalar)
    165 		{
    166 			if (scalar == 0)
    167 			{
    168 				return this.field.Zero;
    169 			}
    170 			if (scalar == 1)
    171 			{
    172 				return this;
    173 			}
    174 			var size = this.coefficients.length;
    175 			var product = new Array(size);
    176 			for (var i = 0; i < size; i++)
    177 			{
    178 				product[i] = this.field.multiply(this.coefficients[i], scalar);
    179 			}
    180 			return new GF256Poly(this.field, product);
    181 		}
    182 	this.multiplyByMonomial=function( degree,  coefficient)
    183 		{
    184 			if (degree < 0)
    185 			{
    186 				throw "System.ArgumentException";
    187 			}
    188 			if (coefficient == 0)
    189 			{
    190 				return this.field.Zero;
    191 			}
    192 			var size = this.coefficients.length;
    193 			var product = new Array(size + degree);
    194 			for(var i=0;i<product.length;i++)product[i]=0;
    195 			for (var i = 0; i < size; i++)
    196 			{
    197 				product[i] = this.field.multiply(this.coefficients[i], coefficient);
    198 			}
    199 			return new GF256Poly(this.field, product);
    200 		}
    201 	this.divide=function( other)
    202 		{
    203 			if (this.field!=other.field)
    204 			{
    205 				throw "GF256Polys do not have same GF256 field";
    206 			}
    207 			if (other.Zero)
    208 			{
    209 				throw "Divide by 0";
    210 			}
    211 			
    212 			var quotient = this.field.Zero;
    213 			var remainder = this;
    214 			
    215 			var denominatorLeadingTerm = other.getCoefficient(other.Degree);
    216 			var inverseDenominatorLeadingTerm = this.field.inverse(denominatorLeadingTerm);
    217 			
    218 			while (remainder.Degree >= other.Degree && !remainder.Zero)
    219 			{
    220 				var degreeDifference = remainder.Degree - other.Degree;
    221 				var scale = this.field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm);
    222 				var term = other.multiplyByMonomial(degreeDifference, scale);
    223 				var iterationQuotient = this.field.buildMonomial(degreeDifference, scale);
    224 				quotient = quotient.addOrSubtract(iterationQuotient);
    225 				remainder = remainder.addOrSubtract(term);
    226 			}
    227 			
    228 			return new Array(quotient, remainder);
    229 		}
    230 }