Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Add project files
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Jun 25, 2022
1 parent 2bc2a77 commit a43c7bd
Show file tree
Hide file tree
Showing 41 changed files with 6,937 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
###############################
# Core EditorConfig Options #
###############################

# dotnet-format requires version 3.1.37601
# dotnet tool update -g dotnet-format
# remember to have: git config --global core.autocrlf false #(which is usually default)

root = true

# Every file

[*]
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
end_of_line = lf

dotnet_diagnostic.CS1591.severity = silent
39 changes: 39 additions & 0 deletions Neo.Cryptography.BLS12_381.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32519.379
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{36DA9DE1-958C-4888-BD69-11C612A6AFCA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{5D525A2B-247C-4FDE-839C-1F5D42DF8AE3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Cryptography.BLS12_381", "src\Neo.Cryptography.BLS12_381\Neo.Cryptography.BLS12_381.csproj", "{5355A12D-A614-4C74-B2F8-C8FB0686D717}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Cryptography.BLS12_381.Tests", "tests\Neo.Cryptography.BLS12_381.Tests\Neo.Cryptography.BLS12_381.Tests.csproj", "{8090336E-33F6-4A1B-BED7-0FEE79E2D1F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5355A12D-A614-4C74-B2F8-C8FB0686D717}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5355A12D-A614-4C74-B2F8-C8FB0686D717}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5355A12D-A614-4C74-B2F8-C8FB0686D717}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5355A12D-A614-4C74-B2F8-C8FB0686D717}.Release|Any CPU.Build.0 = Release|Any CPU
{8090336E-33F6-4A1B-BED7-0FEE79E2D1F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8090336E-33F6-4A1B-BED7-0FEE79E2D1F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8090336E-33F6-4A1B-BED7-0FEE79E2D1F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8090336E-33F6-4A1B-BED7-0FEE79E2D1F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5355A12D-A614-4C74-B2F8-C8FB0686D717} = {36DA9DE1-958C-4888-BD69-11C612A6AFCA}
{8090336E-33F6-4A1B-BED7-0FEE79E2D1F5} = {5D525A2B-247C-4FDE-839C-1F5D42DF8AE3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {879715CD-5002-4C10-9EDD-A96741813CF7}
EndGlobalSection
EndGlobal
38 changes: 38 additions & 0 deletions src/Neo.Cryptography.BLS12_381/Bls12.Adder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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);
}

static Fp12 IMillerLoopDriver<Fp12>.SquareOutput(in Fp12 f) => f.Square();

static Fp12 IMillerLoopDriver<Fp12>.Conjugate(in Fp12 f) => f.Conjugate();

static Fp12 IMillerLoopDriver<Fp12>.One => Fp12.One;
}
}
20 changes: 20 additions & 0 deletions src/Neo.Cryptography.BLS12_381/Bls12.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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();
}
}
31 changes: 31 additions & 0 deletions src/Neo.Cryptography.BLS12_381/ConstantTimeUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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);
}
}
7 changes: 7 additions & 0 deletions src/Neo.Cryptography.BLS12_381/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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;
}
Loading

0 comments on commit a43c7bd

Please sign in to comment.