authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2022-06-29 07:44:43+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-06-29 07:44:43+02:00
log41533fa6a1fa5e9e9b38a59403930501fd61a259
treee19551646b2e1ac917990a6842c298e3c49b765e
parentb2e4dda0018b83819c9539609eff7e420eca3202
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std/crypto/{25519,pcurves}: make the scalar field order public (#11955)

For 25519, it's very likely that applications would ever need the serialized representation. Expose the value as an integer as in other curves. Rename the internal representation from `field_size` to `field_order` for consistency. Also fix a common typo in `scalar.sub()`.

3 files changed, 22 insertions(+), 12 deletions(-)

lib/std/crypto/25519/scalar.zig+14-10
......@@ -4,10 +4,8 @@ const mem = std.mem;
44
55const NonCanonicalError = std.crypto.errors.NonCanonicalError;
66
7/// 2^252 + 27742317777372353535851937790883648493
8pub const field_size = [32]u8{
9 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, // 2^252+27742317777372353535851937790883648493
10};
7/// The scalar field order.
8pub const field_order: u256 = 7237005577332262213973186563042994240857116359379907606001950938285454250989;
119
1210/// A compressed scalar
1311pub const CompressedScalar = [32]u8;
......@@ -15,6 +13,12 @@ pub const CompressedScalar = [32]u8;
1513/// Zero
1614pub const zero = [_]u8{0} ** 32;
1715
16const field_order_s = s: {
17 var s: [32]u8 = undefined;
18 mem.writeIntLittle(u256, &s, field_order);
19 break :s s;
20};
21
1822/// Reject a scalar whose encoding is not canonical.
1923pub fn rejectNonCanonical(s: CompressedScalar) NonCanonicalError!void {
2024 var c: u8 = 0;
......@@ -22,9 +26,9 @@ pub fn rejectNonCanonical(s: CompressedScalar) NonCanonicalError!void {
2226 var i: usize = 31;
2327 while (true) : (i -= 1) {
2428 const xs = @as(u16, s[i]);
25 const xfield_size = @as(u16, field_size[i]);
26 c |= @intCast(u8, ((xs -% xfield_size) >> 8) & n);
27 n &= @intCast(u8, ((xs ^ xfield_size) -% 1) >> 8);
29 const xfield_order_s = @as(u16, field_order_s[i]);
30 c |= @intCast(u8, ((xs -% xfield_order_s) >> 8) & n);
31 n &= @intCast(u8, ((xs ^ xfield_order_s) -% 1) >> 8);
2832 if (i == 0) break;
2933 }
3034 if (c == 0) {
......@@ -77,7 +81,7 @@ pub fn add(a: CompressedScalar, b: CompressedScalar) CompressedScalar {
7781
7882/// Return -s (mod L)
7983pub fn neg(s: CompressedScalar) CompressedScalar {
80 const fs: [64]u8 = field_size ++ [_]u8{0} ** 32;
84 const fs: [64]u8 = field_order_s ++ [_]u8{0} ** 32;
8185 var sx: [64]u8 = undefined;
8286 mem.copy(u8, sx[0..32], s[0..]);
8387 mem.set(u8, sx[32..], 0);
......@@ -848,7 +852,7 @@ test "scalar25519" {
848852 var buf: [128]u8 = undefined;
849853 try std.testing.expectEqualStrings(try std.fmt.bufPrint(&buf, "{s}", .{std.fmt.fmtSliceHexUpper(&y)}), "1E979B917937F3DE71D18077F961F6CEFF01030405060708010203040506070F");
850854
851 const reduced = reduce(field_size);
855 const reduced = reduce(field_order_s);
852856 try std.testing.expectEqualStrings(try std.fmt.bufPrint(&buf, "{s}", .{std.fmt.fmtSliceHexUpper(&reduced)}), "0000000000000000000000000000000000000000000000000000000000000000");
853857}
854858
......@@ -881,7 +885,7 @@ test "random scalar" {
881885}
882886
883887test "64-bit reduction" {
884 const bytes = field_size ++ [_]u8{0} ** 32;
888 const bytes = field_order_s ++ [_]u8{0} ** 32;
885889 const x = Scalar.fromBytes64(bytes);
886890 try std.testing.expect(x.isZero());
887891}
lib/std/crypto/pcurves/p256/scalar.zig+4-1
......@@ -24,6 +24,9 @@ const Fe = Field(.{
2424 .encoded_length = encoded_length,
2525});
2626
27/// The scalar field order.
28pub const field_order = Fe.field_order;
29
2730/// Reject a scalar whose encoding is not canonical.
2831pub fn rejectNonCanonical(s: CompressedScalar, endian: std.builtin.Endian) NonCanonicalError!void {
2932 return Fe.rejectNonCanonical(s, endian);
......@@ -61,7 +64,7 @@ pub fn neg(s: CompressedScalar, endian: std.builtin.Endian) NonCanonicalError!Co
6164
6265/// Return (a-b) (mod L)
6366pub fn sub(a: CompressedScalar, b: CompressedScalar, endian: std.builtin.Endian) NonCanonicalError!CompressedScalar {
64 return (try Scalar.fromBytes(a, endian)).sub(try Scalar.fromBytes(b.endian)).toBytes(endian);
67 return (try Scalar.fromBytes(a, endian)).sub(try Scalar.fromBytes(b, endian)).toBytes(endian);
6568}
6669
6770/// Return a random scalar
lib/std/crypto/pcurves/p384/scalar.zig+4-1
......@@ -24,6 +24,9 @@ const Fe = Field(.{
2424 .encoded_length = encoded_length,
2525});
2626
27/// The scalar field order.
28pub const field_order = Fe.field_order;
29
2730/// Reject a scalar whose encoding is not canonical.
2831pub fn rejectNonCanonical(s: CompressedScalar, endian: std.builtin.Endian) NonCanonicalError!void {
2932 return Fe.rejectNonCanonical(s, endian);
......@@ -56,7 +59,7 @@ pub fn neg(s: CompressedScalar, endian: std.builtin.Endian) NonCanonicalError!Co
5659
5760/// Return (a-b) (mod L)
5861pub fn sub(a: CompressedScalar, b: CompressedScalar, endian: std.builtin.Endian) NonCanonicalError!CompressedScalar {
59 return (try Scalar.fromBytes(a, endian)).sub(try Scalar.fromBytes(b.endian)).toBytes(endian);
62 return (try Scalar.fromBytes(a, endian)).sub(try Scalar.fromBytes(b, endian)).toBytes(endian);
6063}
6164
6265/// Return a random scalar