Skip to content

Commit

Permalink
added alltests.py. Added slow multiply method and a unit test, change…
Browse files Browse the repository at this point in the history
…d var names in Polynomial.__divmod__
  • Loading branch information
andrew@fry committed Mar 8, 2010
1 parent 6439517 commit da11c51
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
7 changes: 7 additions & 0 deletions alltests.py
@@ -0,0 +1,7 @@
import unittest

from rstest import *
from polynomialtest import *
from fftest import *

unittest.main()
21 changes: 21 additions & 0 deletions ff.py
Expand Up @@ -101,3 +101,24 @@ def __rdiv__(self, other):
def __repr__(self):
n = self.__class__.__name__
return "%s(%r)" % (n, int(self))

def multiply(self, other):
"""A slow multiply method. This method gives the same results as the
other multiply method, but is implemented to illustrate how it works
and how the above tables were generated.
This procedure is called Peasant's Algorithm (I believe)
"""
a = int(self)
b = int(other)

p = a
r = 0
while b:
if b & 1: r = r ^ p
b = b >> 1
p = p << 1
if p & 0x100: p = p ^ 0x11b

return GF256int(r)

9 changes: 9 additions & 0 deletions fftest.py
@@ -1,4 +1,5 @@
import unittest
import itertools

from ff import GF256int

Expand Down Expand Up @@ -41,5 +42,13 @@ def test_fermats_theorem(self):
for x in range(1,256):
self.assertEqual(GF256int(x)**255, 1)

def test_other_multiply(self):

a = GF256int(3)
b = GF256int(9)

self.assertEqual(a * b, a.multiply(b))


if __name__ == "__main__":
unittest.main()
20 changes: 9 additions & 11 deletions polynomial.py
Expand Up @@ -95,38 +95,36 @@ def __floordiv__(self, other):
return divmod(self, other)[0]
def __mod__(self, other):
return divmod(self, other)[1]
def __divmod__(self, other):
def __divmod__(dividend, divisor):
# See how many times the highest order term
# of other can go into the highest order term of self
# of the divisor can go into the highest order term of the dividend

# "self"
dividend_power = len(self) - 1
dividend_coefficient = self.coefficients[0]
dividend_power = len(dividend) - 1
dividend_coefficient = dividend.coefficients[0]

# "other"
divisor_power = len(other) - 1
divisor_coefficient = other.coefficients[0]
divisor_power = len(divisor) - 1
divisor_coefficient = divisor.coefficients[0]

quotient_power = dividend_power - divisor_power
if quotient_power < 0:
# Doesn't divide at all, return 0 for the quotient and the entire
# dividend as the remander
return Polynomial((0,)), self
return Polynomial((0,)), dividend

# Compute how many times the highest order term in the divisor goes
# into the dividend
quotient_coefficient = dividend_coefficient / divisor_coefficient
quotient = Polynomial( (quotient_coefficient,) + (0,) * quotient_power )

remander = self - quotient * other
remander = dividend - quotient * divisor

if remander.coefficients == (0,):
# Goes in evenly with no remainder, we're done
return quotient, remander

# There was a remander, see how many times the remainder goes into the
# divisor
morequotient, remander = divmod(remander, other)
morequotient, remander = divmod(remander, divisor)
return quotient + morequotient, remander

def __eq__(self, other):
Expand Down

0 comments on commit da11c51

Please sign in to comment.