| ... | ... | @@ -39,8 +39,11 @@ pub const Curve25519 = struct { |
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | | /// Multiply a point by the cofactor |
| 43 | | pub const clearCofactor = @compileError("TODO what was this function supposed to do? it didn't compile successfully"); |
| 42 | /// Multiply a point by the cofactor, returning WeakPublicKey if the element is in a small-order group. |
| 43 | pub fn clearCofactor(p: Curve25519) WeakPublicKeyError!Curve25519 { |
| 44 | const cofactor = [_]u8{8} ++ [_]u8{0} ** 31; |
| 45 | return ladder(p, cofactor, 4) catch return error.WeakPublicKey; |
| 46 | } |
| 44 | 47 | |
| 45 | 48 | fn ladder(p: Curve25519, s: [32]u8, comptime bits: usize) IdentityElementError!Curve25519 { |
| 46 | 49 | var x1 = p.x; |
| ... | ... | @@ -94,8 +97,7 @@ pub const Curve25519 = struct { |
| 94 | 97 | /// the identity element or error.WeakPublicKey if the public |
| 95 | 98 | /// key is a low-order point. |
| 96 | 99 | pub fn mul(p: Curve25519, s: [32]u8) (IdentityElementError || WeakPublicKeyError)!Curve25519 { |
| 97 | | const cofactor = [_]u8{8} ++ [_]u8{0} ** 31; |
| 98 | | _ = ladder(p, cofactor, 4) catch return error.WeakPublicKey; |
| 100 | _ = try p.clearCofactor(); |
| 99 | 101 | return try ladder(p, s, 256); |
| 100 | 102 | } |
| 101 | 103 | |
| ... | ... | @@ -148,6 +150,7 @@ test "curve25519 small order check" { |
| 148 | 150 | }, |
| 149 | 151 | }; |
| 150 | 152 | for (small_order_ss) |small_order_s| { |
| 153 | try std.testing.expectError(error.WeakPublicKey, Curve25519.fromBytes(small_order_s).clearCofactor()); |
| 151 | 154 | try std.testing.expectError(error.WeakPublicKey, Curve25519.fromBytes(small_order_s).mul(s)); |
| 152 | 155 | var extra = small_order_s; |
| 153 | 156 | extra[31] ^= 0x80; |