def testUpdateLFSR(self):
     state = 53
     poly = 105
     n = 7
     new_state = tp1.update_LFSR(state, poly, n)[1]
     self.assertEqual(tp1.update_LFSR(state, poly, n), (1, 26))
     self.assertEqual(tp1.update_LFSR(new_state, poly, n), (0, 13))
    def testOutputAllLfsr(self):
        state = tp1.getState1(2015686042)
        output = 0

        for i in range(40):
            outputLfsr, state = tp1.update_LFSR(state, tp1.getlfsr1()[1], tp1.getlfsr1()[0])
            output ^= outputLfsr << i

        self.assertEqual(output, int("0101100110100010001011010010111010011000"[::-1], 2))

        state = tp1.getState2(2015686042)
        output = 0

        for i in range(40):
            outputLfsr, state = tp1.update_LFSR(state, tp1.getlfsr2()[1], tp1.getlfsr2()[0])
            output ^= outputLfsr << i

        self.assertEqual(output, int("1101110010011100101111001011100111101100"[::-1], 2))

        state = tp1.getState3(2015686042)
        output = 0

        for i in range(40):
            outputLfsr, state = tp1.update_LFSR(state, tp1.getlfsr3()[1], tp1.getlfsr3()[0])
            output ^= outputLfsr << i

        self.assertEqual(output, int("1000001111001101011010101001100110011011"[::-1], 2))
    def testGeneratorWithLFSR1(self):
        cle = random.randint(0, 1 << 33)
        res = tp1.Generator(cle, 300)

        taille_lfsr1 = tp1.getlfsr1()[0]

        etat_initial_aleatoire = 0

        for i in range(0, taille_lfsr1):
            etat_initial_aleatoire += ((cle >> i) & 1) << i

        countTrueTest = 0

        etat = 0
        while etat < (1 << taille_lfsr1):
            lfsr_state = etat
            lfsr1_300bits = 0

            for j in range(300):
                output, lfsr_state = tp1.update_LFSR(lfsr_state, tp1.getlfsr1()[1], taille_lfsr1)
                lfsr1_300bits += output << j

            if etat != etat_initial_aleatoire:
                self.assertFalse(tp1.statistical_test(lfsr1_300bits, res, 300))
            else:
                self.assertTrue(tp1.statistical_test(lfsr1_300bits, res, 300))
                countTrueTest += 1

            etat += 1

        if countTrueTest != 1:
            self.fail("Le nombre de test true devrait etre 1 or il est egal a " + str(countTrueTest))