def test_exp(self): te = common.TermEval(16, 6) self.assertEqual(te.expp_term_deg(1), 0.5) self.assertEqual(te.expp_term_deg(2), 0.25) self.assertEqual(te.expp_term_deg(4), 0.0625) self.assertEqual(te.expp_term([0]), 0.5) self.assertEqual(te.expp_term([10]), 0.5) self.assertEqual(te.expp_term([1, 1]), 0.5) self.assertEqual(te.expp_term([1, 2]), 0.25) self.assertEqual(te.expp_poly([[1]]), 0.5) self.assertEqual(te.expp_poly([[1], [1]]), 0) # always zero self.assertEqual(te.expp_poly([[1], [2]]), 0.5) self.assertEqual(te.expp_poly([[1, 2], [2]]), 0.25) self.assertEqual(te.expp_poly([[1, 2], [3]]), 0.5) self.assertEqual(te.expp_poly([[1, 2, 3, 4, 5], [6]]), 0.5) # xor randomizes self.assertEqual(te.expp_poly([[1, 2, 3], [2]]), 0.375) self.assertEqual(te.expp_poly([[1, 2, 3], [4], [5]]), 0.5) self.assertEqual(te.expp_poly([[1, 2, 3], [4], [5], [6], [7]]), 0.5) self.assertEqual(te.expp_poly([[1, 2, 3], [4], [5], [6], [7, 8]]), 0.5) self.assertEqual(te.expp_poly([[1, 2, 3], [4], [5], [6], [6, 7]]), 0.5) self.assertEqual(te.expp_poly([[1, 2, 3], [1], [2], [3]]), 0.375) # deps
def test_exp_poly(self): """ Verify expected number eval optimized heuristic with brute-force approach """ polys = self.get_test_poly() term_eval = common.TermEval(blocklen=12, deg=3) for idx, poly in enumerate(polys): expp = term_eval.expp_poly(poly) expp2 = term_eval.expp_poly_sim(poly) self.assertAlmostEqual(expp, expp2)
def init(self): """ Initializes state, term_eval engine, input polynomials expected probability. :return: """ logger.info('Initializing HWanalysis') if not self.no_term_map: logger.info('Precomputing term mappings') self.term_map = common.build_term_map(self.deg, self.blocklen) self.term_eval = common.TermEval(blocklen=self.blocklen, deg=self.deg) self.ref_term_eval = common.TermEval(blocklen=self.blocklen, deg=self.deg) self.total_hws = [[0] * common.comb(self.blocklen, x, True) for x in range(self.deg + 1)] self.ref_total_hws = [[0] * common.comb(self.blocklen, x, True) for x in range(self.deg + 1)] self.input_poly_exp = [0] * len(self.input_poly) self.input_poly_hws = [0] * len(self.input_poly) self.input_poly_ref_hws = [0] * len(self.input_poly) self.precompute_input_poly() if self.best_x_combinations is not None and self.best_x_combinations <= 0: self.best_x_combinations = None
def test_polynomials_exp_values(self): """ Simple testing routine to validate expected polynomial results on static polynomials :return: """ poly_test = self.get_testing_polynomials() poly_acc = [0] * len(poly_test) # test polynomials term_eval = common.TermEval(blocklen=self.blocklen, deg=3) for idx, poly in enumerate(poly_test): print('Test polynomial: %02d, %s' % (idx, poly)) expp = term_eval.expp_poly(poly) print(' Expected probability: %s' % expp)
def test_eval1(self): data = b'\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0' # 12 x \xf0 data_bin = common.to_bitarray(data) te = common.TermEval(16, 2) te.load(data_bin) r1 = te.eval_terms(1) self.assertEqual(r1[0], 6) self.assertEqual(r1[1], 6) self.assertEqual(r1[5], 0) r2 = te.eval_terms(2) self.assertEqual(r2[0], 6) # x0x1 self.assertEqual(r2[4], 0) # x0x5 r3 = te.eval_terms(3) self.assertEqual(r3[0], 6) self.assertEqual(r3[2], 0) # x0x1x4 r2x = te.eval_all_terms(3) self.assertEqual(r1, r2x[1]) self.assertEqual(r2, r2x[2]) self.assertEqual(r3, r2x[3])