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 {
7676 if (x2.isZero()) {
7777 return error.IdentityElement;
7878 }
79 return @as(Curve25519, .{ .x = x2 });
79 return Curve25519 { .x = x2 };
8080 }
8181
8282 /// 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 {
3434 x.cMov(x.mul(Fe.sqrtm1()), 1 - @boolToInt(has_m_root));
3535 x.cMov(x.neg(), @boolToInt(x.isNegative()) ^ (s[31] >> 7));
3636 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 };
3838 }
3939
4040 /// Encode an Edwards25519 point.
lib/std/crypto/25519/ristretto255.zig+5-11
......@@ -13,9 +13,8 @@ pub const Ristretto255 = struct {
1313 p: Curve = undefined,
1414
1515 fn sqrtRatioM1(u: Fe, v: Fe) !Fe {
16 const v3 = v.sq().mul(v); // v3 = v^3
17 var x = v3.sq().mul(u).mul(v). // x = uv^7
18 pow2523().mul(v3).mul(u); // x = uv^3(uv^7)^((q-5)/8)
16 const v3 = v.sq().mul(v); // v^3
17 var x = v3.sq().mul(u).mul(v).pow2523().mul(v3).mul(u); // uv^3(uv^7)^((q-5)/8)
1918 const vxx = x.sq().mul(v); // vx^2
2019 const m_root_check = vxx.sub(u); // vx^2-u
2120 const p_root_check = vxx.add(u); // vx^2+u
......@@ -77,7 +76,7 @@ pub const Ristretto255 = struct {
7776 .z = Fe.one(),
7877 .t = t,
7978 };
80 return @as(Ristretto255, .{ .p = p });
79 return Ristretto255 { .p = p };
8180 }
8281
8382 /// Encode to a Ristretto255 representative.
......@@ -87,25 +86,20 @@ pub const Ristretto255 = struct {
8786 const zmy = p.z.sub(p.y); // Z-Y
8887 u1_ = u1_.mul(zmy); // (Z+Y)*(Z-Y)
8988 const u2_ = p.x.mul(p.y); // X*Y
90
9189 const u1_u2u2 = u2_.sq().mul(u1_); // u1*u2^2
92
9390 const inv_sqrt = sqrtRatioM1(Fe.one(), u1_u2u2) catch unreachable;
9491 const den1 = inv_sqrt.mul(u1_);
9592 const den2 = inv_sqrt.mul(u2_);
9693 const z_inv = den1.mul(den2).mul(p.t); // den1*den2*T
97
9894 const ix = p.x.mul(Fe.sqrtm1()); // X*sqrt(-1)
9995 const iy = p.y.mul(Fe.sqrtm1()); // Y*sqrt(-1)
10096 const eden = den1.mul(Fe.edwards25519sqrtamd()); // den1/sqrt(a-d)
101
10297 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());
105100 var x = p.x;
106101 var y = p.y;
107102 var den_inv = den2;
108
109103 x.cMov(iy, rotate);
110104 y.cMov(ix, rotate);
111105 den_inv.cMov(eden, rotate);
......@@ -131,7 +125,7 @@ pub const Ristretto255 = struct {
131125 /// Return error.WeakPublicKey if the resulting element is
132126 /// the identity element.
133127 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) };
135129 }
136130};
137131