| 1 |
"""Make unit test for ladder_step() in x25519.c""" |
| 2 |
|
| 3 |
from common import counter, make_main, split64, bin2int |
| 4 |
from hashlib import sha256 |
| 5 |
import struct |
| 6 |
|
| 7 |
|
| 8 |
def ref(x2, z2, x3, z3, x1): |
| 9 |
mod = 2**255 - 19 |
| 10 |
|
| 11 |
x4 = (x2**2 - z2**2)**2 |
| 12 |
z4 = 4*x2*z2*(x2**2 + 486662*x2*z2 + z2**2) |
| 13 |
x5 = 4*((x2*x3 - z2*z3)**2) |
| 14 |
z5 = 4*((x2*z3 - z2*x3)**2)*x1 |
| 15 |
|
| 16 |
return x4 % mod, z4 % mod, x5 % mod, z5 % mod |
| 17 |
|
| 18 |
|
| 19 |
def make_test_max(): |
| 20 |
|
| 21 |
v = ["0x%08X" % (2**26-1)] * 10 |
| 22 |
|
| 23 |
modulus = 2**255 - 19 |
| 24 |
base = [0, 26, 51, 77, 102, 128, 153, 179, 204, 230] |
| 25 |
n = 0 |
| 26 |
for i in range(10): |
| 27 |
n += (2**26 - 1) * (2**(base[i])) |
| 28 |
n %= modulus |
| 29 |
x2_out, z2_out, x3_out, z3_out = ref(n, n, n, n, n) |
| 30 |
|
| 31 |
|
| 32 |
words = split64(x2_out) |
| 33 |
x2outx = words + ["0"] * (4 - len(words)) |
| 34 |
words = split64(z2_out) |
| 35 |
z2outx = words + ["0"] * (4 - len(words)) |
| 36 |
words = split64(x3_out) |
| 37 |
x3outx = words + ["0"] * (4 - len(words)) |
| 38 |
words = split64(z3_out) |
| 39 |
z3outx = words + ["0"] * (4 - len(words)) |
| 40 |
|
| 41 |
print("") |
| 42 |
print("void test_%d(void) {" % next(counter)) |
| 43 |
print(" uint32_t x2[10] = { " + ",".join(v) + " };") |
| 44 |
print(" uint32_t z2[10] = { " + ",".join(v) + " };") |
| 45 |
print(" uint32_t x3[10] = { " + ",".join(v) + " };") |
| 46 |
print(" uint32_t z3[10] = { " + ",".join(v) + " };") |
| 47 |
print(" uint32_t xp[10] = { " + ",".join(v) + " };") |
| 48 |
print(" const uint64_t x2_out_ref[4] = {" + ", ".join(x2outx) + "};") |
| 49 |
print(" const uint64_t z2_out_ref[4] = {" + ", ".join(z2outx) + "};") |
| 50 |
print(" const uint64_t x3_out_ref[4] = {" + ", ".join(x3outx) + "};") |
| 51 |
print(" const uint64_t z3_out_ref[4] = {" + ", ".join(z3outx) + "};") |
| 52 |
print(" uint64_t x2_out[4] = { 0 };") |
| 53 |
print(" uint64_t z2_out[4] = { 0 };") |
| 54 |
print(" uint64_t x3_out[4] = { 0 };") |
| 55 |
print(" uint64_t z3_out[4] = { 0 };") |
| 56 |
|
| 57 |
print("") |
| 58 |
|
| 59 |
print(" ladder_step(x2, z2, x3, z3, xp);") |
| 60 |
print(" convert_le25p5_to_le64(x2_out, x2);") |
| 61 |
print(" convert_le25p5_to_le64(z2_out, z2);") |
| 62 |
print(" convert_le25p5_to_le64(x3_out, x3);") |
| 63 |
print(" convert_le25p5_to_le64(z3_out, z3);") |
| 64 |
print(" reduce_25519_le64(x2_out);") |
| 65 |
print(" reduce_25519_le64(z2_out);") |
| 66 |
print(" reduce_25519_le64(x3_out);") |
| 67 |
print(" reduce_25519_le64(z3_out);") |
| 68 |
|
| 69 |
print("") |
| 70 |
|
| 71 |
print(" assert(0 == memcmp(x2_out, x2_out_ref, sizeof x2_out));") |
| 72 |
print(" assert(0 == memcmp(z2_out, z2_out_ref, sizeof z2_out));") |
| 73 |
print(" assert(0 == memcmp(x3_out, x3_out_ref, sizeof x3_out));") |
| 74 |
print(" assert(0 == memcmp(z3_out, z3_out_ref, sizeof z3_out));") |
| 75 |
print("}") |
| 76 |
|
| 77 |
|
| 78 |
def make_test(x2, z2, x3, z3, xp): |
| 79 |
|
| 80 |
x2_out, z2_out, x3_out, z3_out = ref(x2, z2, x3, z3, xp) |
| 81 |
|
| 82 |
|
| 83 |
words = split64(x2) |
| 84 |
x2x = words + ["0"] * (4 - len(words)) |
| 85 |
words = split64(z2) |
| 86 |
z2x = words + ["0"] * (4 - len(words)) |
| 87 |
words = split64(x3) |
| 88 |
x3x = words + ["0"] * (4 - len(words)) |
| 89 |
words = split64(z3) |
| 90 |
z3x = words + ["0"] * (4 - len(words)) |
| 91 |
words = split64(xp) |
| 92 |
xpx = words + ["0"] * (4 - len(words)) |
| 93 |
|
| 94 |
|
| 95 |
words = split64(x2_out) |
| 96 |
x2outx = words + ["0"] * (4 - len(words)) |
| 97 |
words = split64(z2_out) |
| 98 |
z2outx = words + ["0"] * (4 - len(words)) |
| 99 |
words = split64(x3_out) |
| 100 |
x3outx = words + ["0"] * (4 - len(words)) |
| 101 |
words = split64(z3_out) |
| 102 |
z3outx = words + ["0"] * (4 - len(words)) |
| 103 |
|
| 104 |
print("") |
| 105 |
print("void test_%d(void) {" % next(counter)) |
| 106 |
print(" const uint64_t x2_in[4] = {" + ", ".join(x2x) + "};") |
| 107 |
print(" const uint64_t z2_in[4] = {" + ", ".join(z2x) + "};") |
| 108 |
print(" const uint64_t x3_in[4] = {" + ", ".join(x3x) + "};") |
| 109 |
print(" const uint64_t z3_in[4] = {" + ", ".join(z3x) + "};") |
| 110 |
print(" const uint64_t xp_in[4] = {" + ", ".join(xpx) + "};") |
| 111 |
|
| 112 |
print(" const uint64_t x2_out_ref[4] = {" + ", ".join(x2outx) + "};") |
| 113 |
print(" const uint64_t z2_out_ref[4] = {" + ", ".join(z2outx) + "};") |
| 114 |
print(" const uint64_t x3_out_ref[4] = {" + ", ".join(x3outx) + "};") |
| 115 |
print(" const uint64_t z3_out_ref[4] = {" + ", ".join(z3outx) + "};") |
| 116 |
|
| 117 |
print(" uint32_t x2[10] = { 0 };") |
| 118 |
print(" uint32_t z2[10] = { 0 };") |
| 119 |
print(" uint32_t x3[10] = { 0 };") |
| 120 |
print(" uint32_t z3[10] = { 0 };") |
| 121 |
print(" uint32_t xp[10] = { 0 };") |
| 122 |
|
| 123 |
print(" uint64_t x2_out[4] = { 0 };") |
| 124 |
print(" uint64_t z2_out[4] = { 0 };") |
| 125 |
print(" uint64_t x3_out[4] = { 0 };") |
| 126 |
print(" uint64_t z3_out[4] = { 0 };") |
| 127 |
|
| 128 |
print("") |
| 129 |
|
| 130 |
print(" convert_le64_to_le25p5(x2, x2_in);") |
| 131 |
print(" convert_le64_to_le25p5(z2, z2_in);") |
| 132 |
print(" convert_le64_to_le25p5(x3, x3_in);") |
| 133 |
print(" convert_le64_to_le25p5(z3, z3_in);") |
| 134 |
print(" convert_le64_to_le25p5(xp, xp_in);") |
| 135 |
|
| 136 |
print(" ladder_step(x2, z2, x3, z3, xp);") |
| 137 |
|
| 138 |
print(" convert_le25p5_to_le64(x2_out, x2);") |
| 139 |
print(" convert_le25p5_to_le64(z2_out, z2);") |
| 140 |
print(" convert_le25p5_to_le64(x3_out, x3);") |
| 141 |
print(" convert_le25p5_to_le64(z3_out, z3);") |
| 142 |
print(" reduce_25519_le64(x2_out);") |
| 143 |
print(" reduce_25519_le64(z2_out);") |
| 144 |
print(" reduce_25519_le64(x3_out);") |
| 145 |
print(" reduce_25519_le64(z3_out);") |
| 146 |
|
| 147 |
print("") |
| 148 |
print(" assert(0 == memcmp(x2_out, x2_out_ref, sizeof x2_out));") |
| 149 |
print(" assert(0 == memcmp(z2_out, z2_out_ref, sizeof z2_out));") |
| 150 |
print(" assert(0 == memcmp(x3_out, x3_out_ref, sizeof x3_out));") |
| 151 |
print(" assert(0 == memcmp(z3_out, z3_out_ref, sizeof z3_out));") |
| 152 |
print("}") |
| 153 |
|
| 154 |
|
| 155 |
def make_limb(seed): |
| 156 |
result = bin2int(sha256(struct.pack(">I", seed)).digest()) & ((2**255)-1) |
| 157 |
return result |
| 158 |
|
| 159 |
|
| 160 |
print("#include <assert.h>") |
| 161 |
print("#include <string.h>") |
| 162 |
print("#include <stdint.h>") |
| 163 |
print("#include <stdio.h>") |
| 164 |
print("void convert_le25p5_to_le64(uint64_t out[4], const uint32_t in[10]);") |
| 165 |
print("void convert_le64_to_le25p5(uint32_t out[10], const uint64_t in[4]);") |
| 166 |
print("void reduce_25519_le64(uint64_t x[4]);") |
| 167 |
print("void ladder_step(uint32_t x2[10], uint32_t z2[10], uint32_t x3[10], uint32_t z3[10], const uint32_t xp[10]);") |
| 168 |
|
| 169 |
make_test_max() |
| 170 |
make_test(0, 0, 0, 0, 0) |
| 171 |
make_test(1, 1, 1, 1, 1) |
| 172 |
make_test(6000, 10, 1000, 19999, 18888) |
| 173 |
|
| 174 |
for x in range(50): |
| 175 |
x2 = make_limb(1000 + x) |
| 176 |
z2 = make_limb(2000 + x) |
| 177 |
x3 = make_limb(3000 + x) |
| 178 |
z3 = make_limb(4000 + x) |
| 179 |
xp = make_limb(5000 + x) |
| 180 |
make_test(x2, z2, x3, z3, xp) |
| 181 |
|
| 182 |
make_main() |