def make_test(value): result = value % (2**130 - 5) h_in = split32(value, 5) h_out = split32(result, 5) print "" print "void test_%d() {" % counter.next() print " uint32_t h[5] = {" + ", ".join(h_in) + "};" print " const uint32_t expected_h[5] = {" + ", ".join(h_out) + "};" print "" print " poly1305_reduce(h);" print " assert(memcmp(h, expected_h, sizeof(h)) == 0);" print "}" print ""
def make_test(term, secret): assert term < 2**(32*5) assert len(secret) == 16 # Several bits in the secret must be cleared clamped = bytearray(secret) for idx in 3, 7, 11, 15: clamped[idx] &= 15 for idx in 4, 8, 12: clamped[idx] &= 252 # Turn the secret into an integer r r = 0 for x in clamped[::-1]: r = (r * 256) + x # Work out what the results (2 or 3) should be p = 2**130 - 5 result = (term * r) % p all_results = [] while result < 2**131: all_results.append(split32(result, 5)) result += p n_results = len(all_results) # Split the term into 32-bit words h_split = split32(term, 5) print "" print "void test_%d() {" % counter.next() print " uint8_t secret[16] = {" + ",".join([str(ord(x)) for x in secret]) + "};" print " uint32_t r[4], rr[4];" print " uint32_t h[5] = {" + ",".join(h_split) + "};" print " int match;"; for x in range(n_results): y = ",".join(all_results[x]) print " uint32_t expected_h_%d[5] = { %s };" % (x+1, y) print "" print " poly1305_load_r(r, rr, secret);" print " poly1305_multiply(h, r, rr);" print " match = !0;" for x in range(n_results): print " match = match && memcmp(h, expected_h_%d, sizeof(h));" % (x+1) print " assert(match == 0);" print "}" print ""
def make_test(term1, term2): assert term1 < 2**(32*5) assert term2 < 2**(32*5) t1_split = split32(term1, 5) t2_split = split32(term2, 5) res_split = split32(term1 + term2, 5) print "" print "void test_%d() {" % counter.next() print " uint32_t h[5] = {" + ",".join(t1_split) + "};" print " uint32_t m[5] = {" + ",".join(t2_split) + "};" print " uint32_t w[5] = {" + ",".join(res_split) + "};" print " poly1305_accumulate(h, m);" print " assert(0 == memcmp(h, w, sizeof(h)));" print "}" print ""
def make_test(term1, term2): assert term1 < 2**(32 * 5) assert term2 < 2**(32 * 5) t1_split = split32(term1, 5) t2_split = split32(term2, 5) res_split = split32(term1 + term2, 5) print("") print("void test_%d() {" % next(counter)) print(" uint32_t h[5] = {" + ",".join(t1_split) + "};") print(" uint32_t m[5] = {" + ",".join(t2_split) + "};") print(" uint32_t w[5] = {" + ",".join(res_split) + "};") print(" poly1305_accumulate(h, m);") print(" assert(0 == memcmp(h, w, sizeof(h)));") print("}") print("")