Skip to content

Commit a7fb5f8

Browse files
Initial commit
1 parent 04840da commit a7fb5f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+15311
-0
lines changed

License

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Coding Enthusiast
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
readme placeholder

Src/FinderOuter.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29519.181
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FinderOuter", "FinderOuter\FinderOuter.csproj", "{5D4D8B5B-B186-46EA-AAA8-56DBFBBF0102}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{CDF19237-9509-4FEB-9470-202F9864D8A6}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{5D4D8B5B-B186-46EA-AAA8-56DBFBBF0102}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{5D4D8B5B-B186-46EA-AAA8-56DBFBBF0102}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{5D4D8B5B-B186-46EA-AAA8-56DBFBBF0102}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{5D4D8B5B-B186-46EA-AAA8-56DBFBBF0102}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{CDF19237-9509-4FEB-9470-202F9864D8A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{CDF19237-9509-4FEB-9470-202F9864D8A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{CDF19237-9509-4FEB-9470-202F9864D8A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{CDF19237-9509-4FEB-9470-202F9864D8A6}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {BDB15806-2924-42EB-A0EB-3EBAEDF71E43}
30+
EndGlobalSection
31+
EndGlobal

Src/FinderOuter/App.xaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Application xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:local="clr-namespace:FinderOuter"
4+
x:Class="FinderOuter.App">
5+
<Application.DataTemplates>
6+
<local:ViewLocator/>
7+
</Application.DataTemplates>
8+
9+
<Application.Styles>
10+
<StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml"/>
11+
<StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseLight.xaml"/>
12+
</Application.Styles>
13+
</Application>

Src/FinderOuter/App.xaml.cs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// The FinderOuter
2+
// Copyright (c) 2020 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using Avalonia;
7+
using Avalonia.Controls.ApplicationLifetimes;
8+
using Avalonia.Markup.Xaml;
9+
using FinderOuter.ViewModels;
10+
using FinderOuter.Views;
11+
12+
namespace FinderOuter
13+
{
14+
public class App : Application
15+
{
16+
public override void Initialize()
17+
{
18+
AvaloniaXamlLoader.Load(this);
19+
}
20+
21+
public override void OnFrameworkInitializationCompleted()
22+
{
23+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
24+
{
25+
desktop.MainWindow = new MainWindow
26+
{
27+
DataContext = new MainWindowViewModel(),
28+
};
29+
}
30+
31+
base.OnFrameworkInitializationCompleted();
32+
}
33+
}
34+
}

Src/FinderOuter/Assets/Avalonia.jpg

45 KB
Loading

Src/FinderOuter/Assets/Icon.ico

373 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// The FinderOuter
2+
// Copyright (c) 2020 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using System;
7+
using System.Numerics;
8+
9+
namespace FinderOuter.Backend.Cryptography.Arithmetic
10+
{
11+
/// <summary>
12+
/// https://en.wikipedia.org/wiki/Legendre_symbol
13+
/// </summary>
14+
public static class Legendre
15+
{
16+
/// <summary>
17+
/// Finds Legendre symbol for a given pair of integers (a,p) where p is an odd prime.
18+
/// </summary>
19+
/// <param name="n"></param>
20+
/// <param name="p"></param>
21+
/// <returns></returns>
22+
public static int Symbol(BigInteger n, BigInteger p)
23+
{
24+
if (p < 2)
25+
{
26+
throw new ArgumentOutOfRangeException(nameof(p), $"{nameof(p)} must be >= 2");
27+
}
28+
if (n == 0 || n == 1)
29+
{
30+
return (int)n;
31+
}
32+
33+
int result;
34+
if (n.IsEven)
35+
{
36+
result = Symbol(n / 2, p);
37+
if (((p * p - 1) & 8) != 0) // instead of dividing by 8, shift the mask bit
38+
{
39+
result = -result;
40+
}
41+
}
42+
else
43+
{
44+
result = Symbol(p % n, n);
45+
if (((n - 1) * (p - 1) & 4) != 0) // instead of dividing by 4, shift the mask bit
46+
{
47+
result = -result;
48+
}
49+
}
50+
return result;
51+
}
52+
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// The FinderOuter
2+
// Copyright (c) 2020 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using System;
7+
using System.Numerics;
8+
9+
namespace FinderOuter.Backend.Cryptography.Arithmetic
10+
{
11+
public static class SquareRoot
12+
{
13+
/// <summary>
14+
/// Finds N such that N % P = A using Tonelli-Shanks algorithm.
15+
/// </summary>
16+
public static BigInteger FindSquareRoot(BigInteger a, BigInteger p)
17+
{
18+
return TonelliShanks(a, p);
19+
}
20+
21+
22+
private static BigInteger TonelliShanks(BigInteger a, BigInteger p)
23+
{
24+
if (a >= p)
25+
{
26+
throw new Exception("The residue, 'a' cannot be greater than the modulus 'p'!");
27+
}
28+
if (Legendre.Symbol(a, p) != 1) // a^(p-1 / 2) % p == p-1
29+
{
30+
throw new ArithmeticException($"Parameter 'a' is not a quadratic residue, mod 'p'");
31+
}
32+
// This will be true for secp256k1 curve prime
33+
if (p % 4 == 3)
34+
{
35+
return BigInteger.ModPow(a, (p + 1) / 4, p);
36+
}
37+
38+
//Initialize
39+
BigInteger s = p - 1;
40+
BigInteger e = 0;
41+
while (s % 2 == 0)
42+
{
43+
s /= 2;
44+
e += 1;
45+
}
46+
47+
48+
BigInteger n = FindGenerator(p);
49+
50+
BigInteger x = BigInteger.ModPow(a, (s + 1) / 2, p);
51+
BigInteger b = BigInteger.ModPow(a, s, p);
52+
BigInteger g = BigInteger.ModPow(n, s, p);
53+
BigInteger r = e;
54+
BigInteger m = Order(b, p);
55+
if (m == 0)
56+
{
57+
return x;
58+
}
59+
60+
while (m > 0)
61+
{
62+
x = (x * BigInteger.ModPow(g, TwoExp(r - m - 1), p)) % p;
63+
b = (b * BigInteger.ModPow(g, TwoExp(r - m), p)) % p;
64+
g = BigInteger.ModPow(g, TwoExp(r - m), p);
65+
r = m;
66+
m = Order(b, p);
67+
}
68+
69+
return x;
70+
}
71+
72+
private static BigInteger FindGenerator(BigInteger p)
73+
{
74+
BigInteger n = 2;
75+
while (BigInteger.ModPow(n, (p - 1) / 2, p) == 1)
76+
{
77+
n++;
78+
}
79+
80+
return n;
81+
}
82+
83+
84+
85+
86+
private static BigInteger Order(BigInteger b, BigInteger p)
87+
{
88+
BigInteger m = 1;
89+
BigInteger e = 0;
90+
91+
while (BigInteger.ModPow(b, m, p) != 1)
92+
{
93+
m *= 2;
94+
e++;
95+
}
96+
97+
return e;
98+
}
99+
100+
private static BigInteger TwoExp(BigInteger exp)
101+
{
102+
return BigInteger.Pow(2, (int)exp);
103+
}
104+
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// The FinderOuter
2+
// Copyright (c) 2020 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using FinderOuter.Backend.Encoders;
7+
using System.Numerics;
8+
9+
namespace FinderOuter.Backend.Cryptography.Asymmetric.EllipticCurve
10+
{
11+
public sealed class SecP256k1 : ECurveFpBase
12+
{
13+
public override string Name => "secp256k1";
14+
public override BigInteger P => Base16.ToBigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", true);
15+
public override BigInteger A => BigInteger.Zero;
16+
public override BigInteger B => new BigInteger(7);
17+
public override BigInteger N => Base16.ToBigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", true);
18+
public override EllipticCurvePoint G => new EllipticCurvePoint(
19+
Base16.ToBigInt("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", true),
20+
Base16.ToBigInt("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", true));
21+
public override short H => 1;
22+
public override int SizeInBits => 256;
23+
public override int NSizeInBits => 256;
24+
public override int SecurityLevel => 128;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// The FinderOuter
2+
// Copyright (c) 2020 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using System.Numerics;
7+
8+
namespace FinderOuter.Backend.Cryptography.Asymmetric.EllipticCurve
9+
{
10+
public abstract class ECurveFpBase : IECurveFp
11+
{
12+
public abstract string Name { get; }
13+
public abstract BigInteger P { get; }
14+
public abstract BigInteger A { get; }
15+
public abstract BigInteger B { get; }
16+
public abstract BigInteger N { get; }
17+
public abstract EllipticCurvePoint G { get; }
18+
public abstract short H { get; }
19+
public virtual byte[] Seed => null;
20+
public abstract int SizeInBits { get; }
21+
public abstract int NSizeInBits { get; }
22+
public abstract int SecurityLevel { get; }
23+
24+
25+
public bool IsOnCurve(EllipticCurvePoint point)
26+
{
27+
if (point == EllipticCurvePoint.InfinityPoint)
28+
{
29+
return true;
30+
}
31+
// Big*Big is faster than Pow(Big,2). Only true for 2 though.
32+
BigInteger rem = ((point.Y * point.Y) - BigInteger.Pow(point.X, 3) - (A * point.X) - B) % P;
33+
return rem == 0;
34+
}
35+
36+
}
37+
}

0 commit comments

Comments
 (0)