authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-14 16:33:37+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-16 22:35:27-07:00
log6af9bc8c686c5eeaff274fa7ebb96b1ead3212ce
tree511f9171ac6d965329e316275b43c4f58ab1bb32
parent5f9953f41ff7761cdf86c211c91de7470425771c

Initialize structures directly

Suggested by @kubkon, thanks!

3 files changed, 7 insertions(+), 13 deletions(-)

lib/std/crypto/25519/curve25519.zig+1-1
...@@ -76,7 +76,7 @@ pub const Curve25519 = struct {...@@ -76,7 +76,7 @@ pub const Curve25519 = struct {
76 if (x2.isZero()) {76 if (x2.isZero()) {
77 return error.IdentityElement;77 return error.IdentityElement;
78 }78 }
79 return @as(Curve25519, .{ .x = x2 });79 return Curve25519 { .x = x2 };
80 }80 }
8181
82 /// Multiply a Curve25519 point by a scalar after "clamping" it.82 /// Multiply a Curve25519 point by a scalar after "clamping" it.
lib/std/crypto/25519/edwards25519.zig+1-1
...@@ -34,7 +34,7 @@ pub const Edwards25519 = struct {...@@ -34,7 +34,7 @@ pub const Edwards25519 = struct {
34 x.cMov(x.mul(Fe.sqrtm1()), 1 - @boolToInt(has_m_root));34 x.cMov(x.mul(Fe.sqrtm1()), 1 - @boolToInt(has_m_root));
35 x.cMov(x.neg(), @boolToInt(x.isNegative()) ^ (s[31] >> 7));35 x.cMov(x.neg(), @boolToInt(x.isNegative()) ^ (s[31] >> 7));
36 const t = x.mul(y);36 const t = x.mul(y);
37 return @as(Edwards25519, .{ .x = x, .y = y, .z = z, .t = t });37 return Edwards25519 { .x = x, .y = y, .z = z, .t = t };
38 }38 }
3939
40 /// Encode an Edwards25519 point.40 /// Encode an Edwards25519 point.
lib/std/crypto/25519/ristretto255.zig+5-11
...@@ -13,9 +13,8 @@ pub const Ristretto255 = struct {...@@ -13,9 +13,8 @@ pub const Ristretto255 = struct {
13 p: Curve = undefined,13 p: Curve = undefined,
1414
15 fn sqrtRatioM1(u: Fe, v: Fe) !Fe {15 fn sqrtRatioM1(u: Fe, v: Fe) !Fe {
16 const v3 = v.sq().mul(v); // v3 = v^316 const v3 = v.sq().mul(v); // v^3
17 var x = v3.sq().mul(u).mul(v). // x = uv^717 var x = v3.sq().mul(u).mul(v).pow2523().mul(v3).mul(u); // uv^3(uv^7)^((q-5)/8)
18 pow2523().mul(v3).mul(u); // x = uv^3(uv^7)^((q-5)/8)
19 const vxx = x.sq().mul(v); // vx^218 const vxx = x.sq().mul(v); // vx^2
20 const m_root_check = vxx.sub(u); // vx^2-u19 const m_root_check = vxx.sub(u); // vx^2-u
21 const p_root_check = vxx.add(u); // vx^2+u20 const p_root_check = vxx.add(u); // vx^2+u
...@@ -77,7 +76,7 @@ pub const Ristretto255 = struct {...@@ -77,7 +76,7 @@ pub const Ristretto255 = struct {
77 .z = Fe.one(),76 .z = Fe.one(),
78 .t = t,77 .t = t,
79 };78 };
80 return @as(Ristretto255, .{ .p = p });79 return Ristretto255 { .p = p };
81 }80 }
8281
83 /// Encode to a Ristretto255 representative.82 /// Encode to a Ristretto255 representative.
...@@ -87,25 +86,20 @@ pub const Ristretto255 = struct {...@@ -87,25 +86,20 @@ pub const Ristretto255 = struct {
87 const zmy = p.z.sub(p.y); // Z-Y86 const zmy = p.z.sub(p.y); // Z-Y
88 u1_ = u1_.mul(zmy); // (Z+Y)*(Z-Y)87 u1_ = u1_.mul(zmy); // (Z+Y)*(Z-Y)
89 const u2_ = p.x.mul(p.y); // X*Y88 const u2_ = p.x.mul(p.y); // X*Y
90
91 const u1_u2u2 = u2_.sq().mul(u1_); // u1*u2^289 const u1_u2u2 = u2_.sq().mul(u1_); // u1*u2^2
92
93 const inv_sqrt = sqrtRatioM1(Fe.one(), u1_u2u2) catch unreachable;90 const inv_sqrt = sqrtRatioM1(Fe.one(), u1_u2u2) catch unreachable;
94 const den1 = inv_sqrt.mul(u1_);91 const den1 = inv_sqrt.mul(u1_);
95 const den2 = inv_sqrt.mul(u2_);92 const den2 = inv_sqrt.mul(u2_);
96 const z_inv = den1.mul(den2).mul(p.t); // den1*den2*T93 const z_inv = den1.mul(den2).mul(p.t); // den1*den2*T
97
98 const ix = p.x.mul(Fe.sqrtm1()); // X*sqrt(-1)94 const ix = p.x.mul(Fe.sqrtm1()); // X*sqrt(-1)
99 const iy = p.y.mul(Fe.sqrtm1()); // Y*sqrt(-1)95 const iy = p.y.mul(Fe.sqrtm1()); // Y*sqrt(-1)
100 const eden = den1.mul(Fe.edwards25519sqrtamd()); // den1/sqrt(a-d)96 const eden = den1.mul(Fe.edwards25519sqrtamd()); // den1/sqrt(a-d)
101
102 const t_z_inv = p.t.mul(z_inv); // T*z_inv97 const t_z_inv = p.t.mul(z_inv); // T*z_inv
103 const rotate = @boolToInt(t_z_inv.isNegative());
10498
99 const rotate = @boolToInt(t_z_inv.isNegative());
105 var x = p.x;100 var x = p.x;
106 var y = p.y;101 var y = p.y;
107 var den_inv = den2;102 var den_inv = den2;
108
109 x.cMov(iy, rotate);103 x.cMov(iy, rotate);
110 y.cMov(ix, rotate);104 y.cMov(ix, rotate);
111 den_inv.cMov(eden, rotate);105 den_inv.cMov(eden, rotate);
...@@ -131,7 +125,7 @@ pub const Ristretto255 = struct {...@@ -131,7 +125,7 @@ pub const Ristretto255 = struct {
131 /// Return error.WeakPublicKey if the resulting element is125 /// Return error.WeakPublicKey if the resulting element is
132 /// the identity element.126 /// the identity element.
133 pub inline fn mul(p: Ristretto255, s: [32]u8) !Ristretto255 {127 pub inline fn mul(p: Ristretto255, s: [32]u8) !Ristretto255 {
134 return @as(Ristretto255, .{ .p = try p.p.mul(s) });128 return Ristretto255 { .p = try p.p.mul(s) };
135 }129 }
136};130};
137131