-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to monorepo: Neo.Cryptography.BLS12_381 (#3077)
* Move BLS * Pack Neo.Cryptography.BLS12_381 * Add file headers --------- Co-authored-by: Jimmy <jinghui@wayne.edu>
- Loading branch information
Showing
42 changed files
with
7,544 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// Bls12.Adder.cs file belongs to the neo project and is free | ||
// software distributed under the MIT software license, see the | ||
// accompanying file LICENSE in the main directory of the | ||
// repository or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System.Runtime.CompilerServices; | ||
using static Neo.Cryptography.BLS12_381.MillerLoopUtility; | ||
|
||
namespace Neo.Cryptography.BLS12_381; | ||
|
||
partial class Bls12 | ||
{ | ||
class Adder : IMillerLoopDriver<Fp12> | ||
{ | ||
public G2Projective Curve; | ||
public readonly G2Affine Base; | ||
public readonly G1Affine P; | ||
|
||
public Adder(in G1Affine p, in G2Affine q) | ||
{ | ||
Curve = new(q); | ||
Base = q; | ||
P = p; | ||
} | ||
|
||
Fp12 IMillerLoopDriver<Fp12>.DoublingStep(in Fp12 f) | ||
{ | ||
var coeffs = DoublingStep(ref Curve); | ||
return Ell(in f, in coeffs, in P); | ||
} | ||
|
||
Fp12 IMillerLoopDriver<Fp12>.AdditionStep(in Fp12 f) | ||
{ | ||
var coeffs = AdditionStep(ref Curve, in Base); | ||
return Ell(in f, in coeffs, in P); | ||
} | ||
|
||
#region IMillerLoopDriver<T> | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Fp12 Square(in Fp12 f) => f.Square(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Fp12 Conjugate(in Fp12 f) => f.Conjugate(); | ||
|
||
public static Fp12 One | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => Fp12.One; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
Fp12 IMillerLoopDriver<Fp12>.Square(in Fp12 f) => Adder.Square(f); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
Fp12 IMillerLoopDriver<Fp12>.Conjugate(in Fp12 f) => Adder.Conjugate(f); | ||
Fp12 IMillerLoopDriver<Fp12>.One | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => Adder.One; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// Bls12.cs file belongs to the neo project and is free | ||
// software distributed under the MIT software license, see the | ||
// accompanying file LICENSE in the main directory of the | ||
// repository or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using static Neo.Cryptography.BLS12_381.ConstantTimeUtility; | ||
using static Neo.Cryptography.BLS12_381.MillerLoopUtility; | ||
|
||
namespace Neo.Cryptography.BLS12_381; | ||
|
||
public static partial class Bls12 | ||
{ | ||
public static Gt Pairing(in G1Affine p, in G2Affine q) | ||
{ | ||
var either_identity = p.IsIdentity | q.IsIdentity; | ||
var p2 = ConditionalSelect(in p, in G1Affine.Generator, either_identity); | ||
var q2 = ConditionalSelect(in q, in G2Affine.Generator, either_identity); | ||
|
||
var adder = new Adder(p2, q2); | ||
|
||
var tmp = MillerLoop<Fp12, Adder>(adder); | ||
var tmp2 = new MillerLoopResult(ConditionalSelect(in tmp, in Fp12.One, either_identity)); | ||
return tmp2.FinalExponentiation(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// ConstantTimeUtility.cs file belongs to the neo project and is free | ||
// software distributed under the MIT software license, see the | ||
// accompanying file LICENSE in the main directory of the | ||
// repository or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
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] ^ a_bytes[i]; | ||
return f == 0; | ||
} | ||
|
||
public static T ConditionalSelect<T>(in T a, in T b, bool choice) where T : unmanaged | ||
{ | ||
return choice ? b : a; | ||
} | ||
|
||
public static void ConditionalAssign<T>(this ref T self, in T other, bool choice) where T : unmanaged | ||
{ | ||
self = ConditionalSelect(in self, in other, choice); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// Constants.cs file belongs to the neo project and is free | ||
// software distributed under the MIT software license, see the | ||
// accompanying file LICENSE in the main directory of the | ||
// repository or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
namespace Neo.Cryptography.BLS12_381; | ||
|
||
static class Constants | ||
{ | ||
public const ulong BLS_X = 0xd201_0000_0001_0000; | ||
public const bool BLS_X_IS_NEGATIVE = true; | ||
} |
Oops, something went wrong.