Skip to content

Commit

Permalink
dstu
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Aug 2, 2023
1 parent f47bb4a commit b8cfb7c
Show file tree
Hide file tree
Showing 14 changed files with 4,781 additions and 0 deletions.
19 changes: 19 additions & 0 deletions priv/dstu/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// MIT License

open System
open System.IO
open System.Text
open UA.Cryptography
open FSharp.Data

let split (ss:#seq<obj>) (s:string) =
s.Split(ss |> Seq.map string |> Seq.toArray, StringSplitOptions.None)

let eval args =
match split [|"-vars"; "-in"; "-out"|] (args |> String.concat " ") with
| _ -> ()

let ret (_:unit) = 0

[<EntryPoint>]
let main args = args |> eval |> ret
13 changes: 13 additions & 0 deletions priv/dstu/UA.Cryptography.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<VersionPrefix>1.0.0</VersionPrefix>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.7.0" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions priv/dstu/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
</configuration>
17 changes: 17 additions & 0 deletions priv/dstu/dstu.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseAppHost>true</UseAppHost>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="UA.Cryptography.csproj"/>
<PackageReference Include="UA.Cryptography" Version="1.0.0" />
<PackageReference Include="FSharp.Core" Version="4.7.0" />
<PackageReference Include="FSharp.Data" Version="3.3.3" />
<Compile Include="*.fs" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions priv/dstu/dstu.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "dstu", "dstu.fsproj", "{5FC36F19-7275-4F14-B330-21F277A5248E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UA.Cryptography", "UA.Cryptography.csproj", "{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5FC36F19-7275-4F14-B330-21F277A5248E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Debug|x64.ActiveCfg = Debug|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Debug|x64.Build.0 = Debug|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Debug|x86.ActiveCfg = Debug|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Debug|x86.Build.0 = Debug|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Release|Any CPU.Build.0 = Release|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Release|x64.ActiveCfg = Release|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Release|x64.Build.0 = Release|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Release|x86.ActiveCfg = Release|Any CPU
{5FC36F19-7275-4F14-B330-21F277A5248E}.Release|x86.Build.0 = Release|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Debug|x64.ActiveCfg = Debug|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Debug|x64.Build.0 = Debug|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Debug|x86.ActiveCfg = Debug|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Debug|x86.Build.0 = Debug|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Release|Any CPU.Build.0 = Release|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Release|x64.ActiveCfg = Release|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Release|x64.Build.0 = Release|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Release|x86.ActiveCfg = Release|Any CPU
{E59E5DC7-F761-42BF-AF17-CCC21406C8E3}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
20 changes: 20 additions & 0 deletions priv/dstu/license/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2000—2022 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall
be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
107 changes: 107 additions & 0 deletions priv/dstu/src/Key.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using UA.Cryptography;
using UA.Cryptography.EC;
using UA.Cryptography.Internal;

namespace UA.Cryptography
{
public class Key
{
public F2mCurve Curve { get; private set; }
public F2mPoint BasePoint { get; private set; }
public F2mPoint PublicKey { get; private set; }
public BigInteger PrivateKey { get; private set; }

public Key(F2mCurve curve, F2mPoint basePoint, F2mPoint publicKey, BigInteger priveteKey)
{
PublicKey = publicKey;
PrivateKey = priveteKey;
BasePoint = basePoint;
Curve = curve;
}

public Key(F2mCurve curve, F2mPoint basePoint, F2mPoint publicKey)
{
PublicKey = publicKey;
BasePoint = basePoint;
Curve = curve;
}

public static Key Generate(F2mCurve curve)
{
var basePoint = computeBasePoint(curve);

var privateKey = RNG.GetRandomInteger(curve.M);

var p1 = basePoint.Negate();
var publicKey = (F2mPoint)p1.Multiply(privateKey);

return new Key(curve, basePoint, publicKey, privateKey);
}

static F2mPoint computeBasePoint(F2mCurve curve)
{
while (true)
{
var p = computeRandomPoint(curve);
var n = curve.N;
var r = p.Multiply(n);

if (r.X != null || r.Y != null)
continue;

return p;
}
}

static F2mPoint computeRandomPoint(F2mCurve curve)
{
BigInteger u = RNG.GetRandomInteger(curve.M);
var u__element = new F2mFieldElement(curve.M, curve.K1, curve.K2, curve.K3, u);
var a__element = new F2mFieldElement(curve.M, curve.K1, curve.K2, curve.K3, curve.A.ToBigInteger());
var b__element = new F2mFieldElement(curve.M, curve.K1, curve.K2, curve.K3, curve.B.ToBigInteger());

var au__element = u__element.Multiply(u__element).Multiply(a__element);
var w__element = u__element.Multiply(u__element).Multiply(u__element).Add(au__element).Add(b__element);
var z__element = quadraticEquation(curve, u__element.ToBigInteger(), w__element.ToBigInteger());
var point = new F2mPoint(curve, u__element, z__element);

return point;
}

static F2mFieldElement quadraticEquation(F2mCurve curve, BigInteger u, BigInteger w)
{
var w__element = new F2mFieldElement(curve.M, curve.K1, curve.K2, curve.K3, w);
var u__element = new F2mFieldElement(curve.M, curve.K1, curve.K2, curve.K3, u);
var u2__element = u__element.Invert().Square();
var v = (F2mFieldElement)w__element.Multiply(u2__element);
var tr__element = trace(v);
var t__element = halfTrace(v);
var z__element = (F2mFieldElement)t__element.Multiply(u__element);
return z__element;
}

static F2mFieldElement trace(F2mFieldElement x)
{
F2mFieldElement t = x;

for (int i = 1; i < x.M; i++)
{
t = (F2mFieldElement)t.Square().Add(x);
}

return t;
}

static F2mFieldElement halfTrace(F2mFieldElement x)
{
F2mFieldElement t = x;

for (int i = 1; i <= ((x.M - 1) / 2); i++)
{
t = (F2mFieldElement)t.Square().Square().Add(x);
}

return t;
}
}
}
71 changes: 71 additions & 0 deletions priv/dstu/src/Signer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using UA.Cryptography;
using UA.Cryptography.EC;
using UA.Cryptography.Internal;

namespace UA.Cryptography
{
public sealed class Signer
{
private F2mFieldElement _fe;
private BigInteger _e;

private Key _key;

public Signer(Key key)
{
if (null == key)
throw new ArgumentNullException("key");

_key = key;
}

public BigInteger[] SignHash(BigInteger h)
{
if (null == _key.PrivateKey)
throw new InvalidOperationException("null == _key.PrivateKey");

computePreSignature();

var h__element = new F2mFieldElement(_key.Curve.M, _key.Curve.K1, _key.Curve.K2, _key.Curve.K3, h);

var r = h__element.Multiply(_fe).ToBigInteger();
var s = _key.PrivateKey.Multiply(r).Add(_e).Mod(_key.Curve.N);

return new[] { s, r };
}

public bool verifySignature(BigInteger h, BigInteger s, BigInteger r)
{
var sP = _key.BasePoint.Multiply(s);
var rQ = _key.PublicKey.Multiply(r);

var r__ = sP.Add(rQ);

var h__element = new F2mFieldElement(_key.Curve.M, _key.Curve.K1, _key.Curve.K2, _key.Curve.K3, h);

var y2 = h__element.Multiply(r__.X);

if (y2.ToBigInteger().Equals(r))
return true;

return false;
}

public void computePreSignature()
{
while (true)
{
var e = RNG.GetRandomInteger(_key.Curve.M);
var r = _key.BasePoint.Multiply(e);

if (0 != r.X.ToBigInteger().SignValue)
{
_fe = (F2mFieldElement)r.X;
_e = e;
break;
}
}
}
}
}
Loading

0 comments on commit b8cfb7c

Please sign in to comment.