From f954cc531b5592539bd75a21706a5749b6fc5cd9 Mon Sep 17 00:00:00 2001 From: Frank Denis <124872+jedisct1@users.noreply.github.com> Date: Thu, 6 Jul 2023 05:36:44 +0200 Subject: [PATCH] Update for Zig 0.11.0 (#1611) `@truncate()` and `@bitCast()` now do type inference, so the code can be simplified a bit. --- fiat-zig/src/curve25519_32.zig | 7 +++---- fiat-zig/src/curve25519_64.zig | 7 +++---- fiat-zig/src/curve25519_scalar_32.zig | 7 +++---- fiat-zig/src/curve25519_scalar_64.zig | 7 +++---- fiat-zig/src/curve25519_solinas_64.zig | 7 +++---- fiat-zig/src/p224_32.zig | 7 +++---- fiat-zig/src/p224_64.zig | 7 +++---- fiat-zig/src/p256_32.zig | 7 +++---- fiat-zig/src/p256_64.zig | 7 +++---- fiat-zig/src/p256_scalar_32.zig | 7 +++---- fiat-zig/src/p256_scalar_64.zig | 7 +++---- fiat-zig/src/p384_32.zig | 7 +++---- fiat-zig/src/p384_64.zig | 7 +++---- fiat-zig/src/p384_scalar_32.zig | 7 +++---- fiat-zig/src/p384_scalar_64.zig | 7 +++---- fiat-zig/src/p434_64.zig | 7 +++---- fiat-zig/src/p448_solinas_32.zig | 7 +++---- fiat-zig/src/p448_solinas_64.zig | 7 +++---- fiat-zig/src/p521_32.zig | 7 +++---- fiat-zig/src/p521_64.zig | 7 +++---- fiat-zig/src/poly1305_32.zig | 7 +++---- fiat-zig/src/poly1305_64.zig | 7 +++---- fiat-zig/src/secp256k1_dettman_32.zig | 7 +++---- fiat-zig/src/secp256k1_dettman_64.zig | 7 +++---- fiat-zig/src/secp256k1_montgomery_32.zig | 7 +++---- fiat-zig/src/secp256k1_montgomery_64.zig | 7 +++---- fiat-zig/src/secp256k1_montgomery_scalar_32.zig | 7 +++---- fiat-zig/src/secp256k1_montgomery_scalar_64.zig | 7 +++---- inversion/zig/inversion.zig | 2 +- src/Stringification/Zig.v | 7 +++---- 30 files changed, 88 insertions(+), 117 deletions(-) diff --git a/fiat-zig/src/curve25519_32.zig b/fiat-zig/src/curve25519_32.zig index 49ba39aee0..d879addeb6 100644 --- a/fiat-zig/src/curve25519_32.zig +++ b/fiat-zig/src/curve25519_32.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type LooseFieldElement is a field element with loose bounds. diff --git a/fiat-zig/src/curve25519_64.zig b/fiat-zig/src/curve25519_64.zig index be0808bead..ef86c4da01 100644 --- a/fiat-zig/src/curve25519_64.zig +++ b/fiat-zig/src/curve25519_64.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type LooseFieldElement is a field element with loose bounds. diff --git a/fiat-zig/src/curve25519_scalar_32.zig b/fiat-zig/src/curve25519_scalar_32.zig index 262daaaadb..ba1c113548 100644 --- a/fiat-zig/src/curve25519_scalar_32.zig +++ b/fiat-zig/src/curve25519_scalar_32.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/curve25519_scalar_64.zig b/fiat-zig/src/curve25519_scalar_64.zig index 8d4de78618..0a3e14a8dd 100644 --- a/fiat-zig/src/curve25519_scalar_64.zig +++ b/fiat-zig/src/curve25519_scalar_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/curve25519_solinas_64.zig b/fiat-zig/src/curve25519_solinas_64.zig index c011a0a857..d3b1789cd4 100644 --- a/fiat-zig/src/curve25519_solinas_64.zig +++ b/fiat-zig/src/curve25519_solinas_64.zig @@ -16,12 +16,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } /// The function addcarryxU64 is an addition with carry. diff --git a/fiat-zig/src/p224_32.zig b/fiat-zig/src/p224_32.zig index efb2790123..640d739cf0 100644 --- a/fiat-zig/src/p224_32.zig +++ b/fiat-zig/src/p224_32.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p224_64.zig b/fiat-zig/src/p224_64.zig index 06272b3b1a..6fbf854c73 100644 --- a/fiat-zig/src/p224_64.zig +++ b/fiat-zig/src/p224_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p256_32.zig b/fiat-zig/src/p256_32.zig index 07e23d414b..2d3928c9dd 100644 --- a/fiat-zig/src/p256_32.zig +++ b/fiat-zig/src/p256_32.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p256_64.zig b/fiat-zig/src/p256_64.zig index 75dc4d07f0..17d59a54d2 100644 --- a/fiat-zig/src/p256_64.zig +++ b/fiat-zig/src/p256_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p256_scalar_32.zig b/fiat-zig/src/p256_scalar_32.zig index d78a4b14af..b4d6ff2727 100644 --- a/fiat-zig/src/p256_scalar_32.zig +++ b/fiat-zig/src/p256_scalar_32.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p256_scalar_64.zig b/fiat-zig/src/p256_scalar_64.zig index d749884731..4c670e1b40 100644 --- a/fiat-zig/src/p256_scalar_64.zig +++ b/fiat-zig/src/p256_scalar_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p384_32.zig b/fiat-zig/src/p384_32.zig index a3210804c3..e3c9bf9783 100644 --- a/fiat-zig/src/p384_32.zig +++ b/fiat-zig/src/p384_32.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p384_64.zig b/fiat-zig/src/p384_64.zig index 2d7e525129..e3fa1cfe4a 100644 --- a/fiat-zig/src/p384_64.zig +++ b/fiat-zig/src/p384_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p384_scalar_32.zig b/fiat-zig/src/p384_scalar_32.zig index 0dca71eedc..e17883edb1 100644 --- a/fiat-zig/src/p384_scalar_32.zig +++ b/fiat-zig/src/p384_scalar_32.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p384_scalar_64.zig b/fiat-zig/src/p384_scalar_64.zig index 9a89d0efd2..957028d6e9 100644 --- a/fiat-zig/src/p384_scalar_64.zig +++ b/fiat-zig/src/p384_scalar_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p434_64.zig b/fiat-zig/src/p434_64.zig index 92d0fe3f68..0be723d5e3 100644 --- a/fiat-zig/src/p434_64.zig +++ b/fiat-zig/src/p434_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/p448_solinas_32.zig b/fiat-zig/src/p448_solinas_32.zig index 51f288def4..fd75bbeacd 100644 --- a/fiat-zig/src/p448_solinas_32.zig +++ b/fiat-zig/src/p448_solinas_32.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type LooseFieldElement is a field element with loose bounds. diff --git a/fiat-zig/src/p448_solinas_64.zig b/fiat-zig/src/p448_solinas_64.zig index 4a0aaf54e0..921adae629 100644 --- a/fiat-zig/src/p448_solinas_64.zig +++ b/fiat-zig/src/p448_solinas_64.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type LooseFieldElement is a field element with loose bounds. diff --git a/fiat-zig/src/p521_32.zig b/fiat-zig/src/p521_32.zig index f29542c75e..a7cff39e37 100644 --- a/fiat-zig/src/p521_32.zig +++ b/fiat-zig/src/p521_32.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type LooseFieldElement is a field element with loose bounds. diff --git a/fiat-zig/src/p521_64.zig b/fiat-zig/src/p521_64.zig index 80cb0f2b81..5fd41d6378 100644 --- a/fiat-zig/src/p521_64.zig +++ b/fiat-zig/src/p521_64.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type LooseFieldElement is a field element with loose bounds. diff --git a/fiat-zig/src/poly1305_32.zig b/fiat-zig/src/poly1305_32.zig index a0ba34a4bf..708df9e03a 100644 --- a/fiat-zig/src/poly1305_32.zig +++ b/fiat-zig/src/poly1305_32.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type LooseFieldElement is a field element with loose bounds. diff --git a/fiat-zig/src/poly1305_64.zig b/fiat-zig/src/poly1305_64.zig index af037dd614..fd058b74b8 100644 --- a/fiat-zig/src/poly1305_64.zig +++ b/fiat-zig/src/poly1305_64.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type LooseFieldElement is a field element with loose bounds. diff --git a/fiat-zig/src/secp256k1_dettman_32.zig b/fiat-zig/src/secp256k1_dettman_32.zig index 56c27c4240..8668e81be0 100644 --- a/fiat-zig/src/secp256k1_dettman_32.zig +++ b/fiat-zig/src/secp256k1_dettman_32.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } /// The function mul multiplies two field elements. diff --git a/fiat-zig/src/secp256k1_dettman_64.zig b/fiat-zig/src/secp256k1_dettman_64.zig index 15340048d0..bf35ca3190 100644 --- a/fiat-zig/src/secp256k1_dettman_64.zig +++ b/fiat-zig/src/secp256k1_dettman_64.zig @@ -21,12 +21,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } /// The function mul multiplies two field elements. diff --git a/fiat-zig/src/secp256k1_montgomery_32.zig b/fiat-zig/src/secp256k1_montgomery_32.zig index db2534e551..81c085d64f 100644 --- a/fiat-zig/src/secp256k1_montgomery_32.zig +++ b/fiat-zig/src/secp256k1_montgomery_32.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/secp256k1_montgomery_64.zig b/fiat-zig/src/secp256k1_montgomery_64.zig index 20daaf1f0f..b2e8e4f6e9 100644 --- a/fiat-zig/src/secp256k1_montgomery_64.zig +++ b/fiat-zig/src/secp256k1_montgomery_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/secp256k1_montgomery_scalar_32.zig b/fiat-zig/src/secp256k1_montgomery_scalar_32.zig index 65dec125b7..2ec9224422 100644 --- a/fiat-zig/src/secp256k1_montgomery_scalar_32.zig +++ b/fiat-zig/src/secp256k1_montgomery_scalar_32.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/fiat-zig/src/secp256k1_montgomery_scalar_64.zig b/fiat-zig/src/secp256k1_montgomery_scalar_64.zig index 318fb0a4e5..05870587ca 100644 --- a/fiat-zig/src/secp256k1_montgomery_scalar_64.zig +++ b/fiat-zig/src/secp256k1_montgomery_scalar_64.zig @@ -26,12 +26,11 @@ inline fn cast(comptime DestType: type, target: anytype) DestType { const dest = @typeInfo(DestType).Int; const source = @typeInfo(@TypeOf(target)).Int; if (dest.bits < source.bits) { - return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)); - } else { - return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); + const T = std.meta.Int(source.signedness, dest.bits); + return @bitCast(@as(T, @truncate(target))); } } - return @as(DestType, target); + return target; } // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. diff --git a/inversion/zig/inversion.zig b/inversion/zig/inversion.zig index d6d2b5482c..22a9acd22d 100644 --- a/inversion/zig/inversion.zig +++ b/inversion/zig/inversion.zig @@ -46,7 +46,7 @@ fn fieldElement(comptime Field: type) type { } var v_opp: Field.Limbs = undefined; fiat.opp(&v_opp, v); - fiat.selectznz(&v, @truncate(u1, f[f.len - 1] >> (@bitSizeOf(Field.Word) - 1)), v, v_opp); + fiat.selectznz(&v, @truncate(f[f.len - 1] >> (@bitSizeOf(Field.Word) - 1)), v, v_opp); const precomp = comptime x: { var precomp: Field.Limbs = undefined; diff --git a/src/Stringification/Zig.v b/src/Stringification/Zig.v index 558be383e8..421bf92b14 100644 --- a/src/Stringification/Zig.v +++ b/src/Stringification/Zig.v @@ -105,12 +105,11 @@ Module Zig. " const dest = @typeInfo(DestType).Int;"; " const source = @typeInfo(@TypeOf(target)).Int;"; " if (dest.bits < source.bits) {"; - " return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target));"; - " } else {"; - " return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target));"; + " const T = std.meta.Int(source.signedness, dest.bits);"; + " return @bitCast(@as(T, @truncate(target)));"; " }"; " }"; - " return @as(DestType, target);"; + " return target;"; "}"] ++ (if skip_typedefs then []