| 1 |
const std = @import("std"); |
| 2 |
const fmt = std.fmt; |
| 3 |
|
| 4 |
fn testVector(comptime fiat: type, expected_s: []const u8) !void { |
| 5 |
// Find the type of the limbs and the size of the serialized representation. |
| 6 |
const repr = switch (@typeInfo(@TypeOf(fiat.fromBytes))) { |
| 7 |
.@"fn" => |f| .{ |
| 8 |
.Limbs = switch (@typeInfo(f.params[0].type.?)) { |
| 9 |
.pointer => |p| p.child, |
| 10 |
else => unreachable, |
| 11 |
}, |
| 12 |
.bytes = f.params[1].type.?, |
| 13 |
}, |
| 14 |
else => unreachable, |
| 15 |
}; |
| 16 |
const Limbs = repr.Limbs; |
| 17 |
const Bytes = repr.bytes; |
| 18 |
const encoded_length = @sizeOf(Bytes); |
| 19 |
|
| 20 |
// Trigger most available functions. |
| 21 |
var as = [_]u8{0x01} ** encoded_length; |
| 22 |
var a: Limbs = undefined; |
| 23 |
fiat.fromBytes(&a, as); |
| 24 |
if (@hasDecl(fiat, "fromMontgomery")) fiat.fromMontgomery(&a, a); |
| 25 |
var b: Limbs = undefined; |
| 26 |
fiat.opp(&b, a); |
| 27 |
if (@hasDecl(fiat, "carrySquare")) fiat.carrySquare(&a, a) else fiat.square(&a, a); |
| 28 |
if (@hasDecl(fiat, "carryMul")) fiat.carryMul(&b, a, b) else fiat.mul(&b, a, b); |
| 29 |
fiat.add(&b, a, b); |
| 30 |
fiat.sub(&a, b, a); |
| 31 |
if (@hasDecl(fiat, "carry")) fiat.carry(&a, a); |
| 32 |
if (@hasDecl(fiat, "toMontgomery")) fiat.toMontgomery(&a, a); |
| 33 |
fiat.toBytes(&as, a); |
| 34 |
|
| 35 |
// Check that the result matches the expected one. |
| 36 |
var expected: [as.len]u8 = undefined; |
| 37 |
_ = try fmt.hexToBytes(&expected, expected_s); |
| 38 |
try std.testing.expectEqualSlices(u8, &expected, &as); |
| 39 |
} |
| 40 |
|
| 41 |
test "curve25519" { |
| 42 |
const expected = "ecb7120fadeccd50753ba3ac57a4922254279cb26ac4bf5c9b7bfd20e64c557f"; |
| 43 |
try testVector(@import("curve25519_32.zig"), expected); |
| 44 |
try testVector(@import("curve25519_64.zig"), expected); |
| 45 |
} |
| 46 |
|
| 47 |
test "p256" { |
| 48 |
const expected = "aee41f6077662dccf5aaebb7f4c4acab16ef34e8baacbdeddaa8db720b82527d"; |
| 49 |
try testVector(@import("p256_32.zig"), expected); |
| 50 |
try testVector(@import("p256_64.zig"), expected); |
| 51 |
} |
| 52 |
|
| 53 |
test "p384" { |
| 54 |
const expected = "bec9b37c6d3f51a25a0fecf036c9753d5bb5fd347a5ee40bf7a51e61ae0b810e5b580c77a966ac7ac3b43e6111be49b4"; |
| 55 |
try testVector(@import("p384_64.zig"), expected); |
| 56 |
} |
| 57 |
|
| 58 |
test "p448_solinas" { |
| 59 |
const expected = "8710971b9e1e9d19940c83f769da48b51f88ee52b51574d02a83d92d08e4bb906231fdc58b4e0ecb843bef9f4df89f44e68420b94ee170fd"; |
| 60 |
try testVector(@import("p448_solinas_64.zig"), expected); |
| 61 |
} |
| 62 |
|
| 63 |
test "p521" { |
| 64 |
const expected = "beecda88f62311be2a5743ef5a86711c87b19b45afd8c16ad3fbe38bf31a02a90f361cc2274d32d73b6044e84b6f52f5577a5cfe5f81620364846404648362016000"; |
| 65 |
try testVector(@import("p521_64.zig"), expected); |
| 66 |
} |
| 67 |
|
| 68 |
test "poly1305" { |
| 69 |
const expected = "cc944af0850b81e63b81b6dbf0f5eacf00"; |
| 70 |
try testVector(@import("poly1305_32.zig"), expected); |
| 71 |
try testVector(@import("poly1305_64.zig"), expected); |
| 72 |
} |
| 73 |
|
| 74 |
test "secp256k1_montgomery" { |
| 75 |
const expected = "aaa4b177db43ac4d443d0171c3bd2ec9db6c0bf91c1941217b81d250614324dc"; |
| 76 |
try testVector(@import("secp256k1_montgomery_64.zig"), expected); |
| 77 |
} |
| 78 |
|
| 79 |
test "sm2" { |
| 80 |
const expected = "e8ebc77c1c0a46d06f64f1155a55c4a7f98f6a896f584433def06a4cd9bcb3be"; |
| 81 |
try testVector(@import("sm2_32.zig"), expected); |
| 82 |
try testVector(@import("sm2_64.zig"), expected); |
| 83 |
} |
| 84 |
|
| 85 |
test "sm2scalar" { |
| 86 |
const expected = "d2b9c5b06df4aab19daec578107eaf2a0c38f57f7483f6f24cc6dea78ac89a1f"; |
| 87 |
try testVector(@import("sm2_scalar_32.zig"), expected); |
| 88 |
try testVector(@import("sm2_scalar_64.zig"), expected); |
| 89 |
} |