Skip to content

Commit

Permalink
Merge branch 'master' into benchmark2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y authored Oct 9, 2024
2 parents 976ffd6 + a817e29 commit 8c0a3b7
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/Neo.Cryptography.BLS12_381/ConstantTimeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace Neo.Cryptography.BLS12_381;

public static class ConstantTimeUtility
{
public static bool ConstantTimeEq<T>(in T a, in T b) where T : unmanaged
{
ReadOnlySpan<byte> a_bytes = MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in a), 1));
ReadOnlySpan<byte> b_bytes = MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in b), 1));
ReadOnlySpan<ulong> a_u64 = MemoryMarshal.Cast<byte, ulong>(a_bytes);
ReadOnlySpan<ulong> b_u64 = MemoryMarshal.Cast<byte, ulong>(b_bytes);
ulong f = 0;
for (int i = 0; i < a_u64.Length; i++)
f |= a_u64[i] ^ b_u64[i];
for (int i = a_u64.Length * sizeof(ulong); i < a_bytes.Length; i++)
f |= (ulong)a_bytes[i] ^ b_bytes[i];
return f == 0;
var a_bytes = MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in a), 1));
var b_bytes = MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in b), 1));

return CryptographicOperations.FixedTimeEquals(a_bytes, b_bytes);
}

public static T ConditionalSelect<T>(in T a, in T b, bool choice) where T : unmanaged
Expand Down

0 comments on commit 8c0a3b7

Please sign in to comment.