示例#1
0
 def test_finite_field(self):
     """ Ensures finite fields work in the finite field case. """
     p1 = sympy.Poly([1, 5, 3, 7, 3], sympy.abc.x, domain=sympy.GF(11))
     p2 = sympy.Poly([0], sympy.abc.x, domain=sympy.GF(11))
     p3 = sympy.Poly([10], sympy.abc.x, domain=sympy.GF(11))
     self.assertEqual(symbaudio.utils.poly.max_coeff(p1), 7)
     self.assertEqual(symbaudio.utils.poly.max_coeff(p2), 0)
     self.assertEqual(symbaudio.utils.poly.max_coeff(p3), 10)
示例#2
0
文件: ntru.py 项目: ch4perone/NTRU
def invertPolynomial(f, p, q):
    x = sym.Symbol('x')
    Fp = sym.polys.polytools.invert(f,
                                    x**N - 1,
                                    domain=sym.GF(p, symmetric=False))
    Fq = sym.polys.polytools.invert(f,
                                    x**N - 1,
                                    domain=sym.GF(q, symmetric=False))
    return Fp, Fq
def analyze_file(file, framewidth, use_finite, samps):
    """ Runs compression tests on file, and logs results to stdout.

    Arguments:
    file -- the file to analyze (assumed to be a .wav)
    framewidth -- the number of points in each compression frame
    use_finite -- constructs coefficients over a finite field

    Requires: an even number of samples per frame.
    """
    assert (framewidth % 2 == 0)
    audio = symbaudio.analysis.audio.AudioFile(file)

    framecount = audio.sample_count // framewidth

    timer = symbaudio.utils.time.PerfTimer()
    k = framewidth // 2
    dom = "QQ"
    if use_finite:
        dom = sympy.GF(65537)
    for i in random.sample(range(0, framecount), samps):
        window = audio.raw[i * framewidth:(i + 1) * framewidth, 0]

        timer.reset()
        (num, dom) = symbaudio.compression.xgcd.reconstruct_rational(k, window)

        ts = timer.get_elapsed_ns()
        max_coeff = max(symbaudio.utils.poly.max_coeff(num),
                        symbaudio.utils.poly.max_coeff(dom))
        zero_cnt = symbaudio.utils.poly.count_zeros(
            num) + symbaudio.utils.poly.count_zeros(dom)
        zero_orig = framecount - numpy.count_nonzero(window)
        coeff_cnt = num.degree() + dom.degree()

        try:
            print("%i,%i,%i,%i,%i,%i" %
                  (i, ts, max_coeff, zero_cnt, zero_orig, coeff_cnt))
        except Exception:
            sys.stderr.write("Unexpected value in frame %s\n" % str(i))
示例#4
0
            if matrix_stab[l, j] == 1:
                matrix_stab[l, :] = (matrix_stab[l, :] + matrix_stab[j, :]) % 2

    for l in range(j-1, 0, -1):
        for j in range(l):
            if matrix_stab[j, l] == 1:
                matrix_stab[j, :] = (matrix_stab[j, :] + matrix_stab[l, :]) % 2

    return matrix_stab, qubitpermutation


############################
### Bravyi Haah matrices ###
############################

ZERO = sympy.GF(2).zero
ONE = sympy.GF(2).one

LMAT = sympy.Matrix([[ZERO, ZERO, ZERO, ZERO, ONE, ONE, ONE, ONE],
                     [ZERO, ZERO, ZERO, ZERO, ONE, ONE, ONE, ONE]])

MMAT = sympy.Matrix([[ONE, ONE, ONE, ZERO, ZERO, ZERO],
                     [ZERO, ZERO, ZERO, ONE, ONE, ONE]])

S1MAT = sympy.Matrix([[ZERO, ONE, ZERO, ONE, ZERO, ONE, ZERO, ONE],
                      [ZERO, ZERO, ONE, ONE, ZERO, ZERO, ONE, ONE],
                      [ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE]])

S2MAT = sympy.Matrix([[ONE, ZERO, ONE, ONE, ZERO, ONE],
                      [ZERO, ONE, ONE, ZERO, ONE, ONE],
                      [ZERO, ZERO, ZERO, ZERO, ZERO, ZERO]])
示例#5
0
 def test_finite_field(self):
     """ Ensures finite fields work in the finite field case. """
     p = sympy.Poly([0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0], sympy.abc.x, domain=sympy.GF(11))
     self.assertEqual(symbaudio.utils.poly.count_zeros(p), 5)
示例#6
0
 def test_finite_field(self):
     """ Reconstructs a simple recurrence over the finite field GF(5). """
     (n, d) = symbaudio.compression.xgcd.reconstruct_rational(1, [3,1], dom=sympy.GF(5))
     self.assertEqual(n.all_coeffs(), [-1])
     self.assertEqual(d.all_coeffs(), [-1,-2])