at last...
now its there. done. It only took like 4 year :)
Go check it out - contains all sourcecode for a working ECC solution! - look for the Master Thesis for source
I've used Menezes Vanstone ECC for cleartext encoding.
Also available: general application for calculation on any curve - any size!
Sunday, October 10, 2010
Wednesday, July 07, 2010
binary expansion in C# - converting BigInteger to binary form
This source code will do the job.
Add appropriate controls and fire the number: 174050332293622031404857552280219410364023488927386650641
and you'll get:
01110001100100101011100101011111111111001000110110100111100001100011000100000001000111101101011010110010010011001101110101010111001111111001011101111010000100011110011110010100100000010001
with a bunch of more noughts in front.
What a happy day. :)
Add appropriate controls and fire the number: 174050332293622031404857552280219410364023488927386650641
and you'll get:
01110001100100101011100101011111111111001000110110100111100001100011000100000001000111101101011010110010010011001101110101010111001111111001011101111010000100011110011110010100100000010001
with a bunch of more noughts in front.
What a happy day. :)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Numerics;
namespace binaryexpansion
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BigInteger t = BigInteger.Parse(inputnr.Text);
selResult.Text = calcBinary(t);
//the line below would work if t was an integer - so nice and easy, but no dice with BigIntegers
//string t2 = Convert.ToString(t, 2);
}
private string calcBinary(BigInteger tee) {
BigInteger t = tee;
//4 times the length because it takes about 3 times the number of digits to write a number i binary form
int len = 4*(inputnr.Text).Length;
string fullnum= " ";
for (int i = len; i >= 0; i--)
{
int inum;
BigInteger num = (t >> i)&1;
if(num == 1){
//ugly but easy
inum = 1;}
else if(num == 0){
inum = 0;}
else{
inum=666;
}
fullnum = fullnum + Convert.ToString(inum);
}
return fullnum;
}
}
}
Sunday, July 04, 2010
Huge cryptographically secure random numbers
Just a tiny implementation example on how to use .NET crypto-package to create really huge pseudo random numbers.
Add approprite controls and rock.
(in my example I just kicked out the number on a webpage - feel free to do it where ever you like)
Add approprite controls and rock.
(in my example I just kicked out the number on a webpage - feel free to do it where ever you like)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Numerics;
using System.Security.Cryptography;
namespace numbers
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private static BigInteger GetRandom()
{
RandomNumberGenerator ran = RNGCryptoServiceProvider.Create();
byte[] ranbytes = new Byte[256];
//ex: Byte[20] gives 8 bit * 20 = 160 bit ~ 50 digits
//ex: Byte[2] gives 8 bit * 2 = 16 bit = +- 32768
//ex: Byte[128] gives 8 bit * 128 = 1024 bit ~ 300 digits
//fill the byte array with cryptographically strong random bytes
ran.GetBytes(ranbytes);
//put the random bytes into a bigInteger
BigInteger mynum = new BigInteger(ranbytes);
return mynum;
}
protected void Button1_Click(object sender, EventArgs e)
{
//Roll the really really big dice :)
BigInteger roll = GetRandom();
result.Text= roll.ToString();
}
}
}
Etiketter:
big integers,
C#,
code example,
pseudo random,
random curve
This will work! P + Q Point addition in C#.NET code
Add appropriate controls and it will work to calculate P + Q, addition of two points on an elliptic curve over a finite field Fp.
And it all works with big Integers.
Be sure that you only use valid numbers (prime for Fp and points that acctually ARE on the curve)
Next version will contain valid curves generated from startpoint and startvalues , plus it will contain an implementation of the double and add algorithm.
Sorry for the horrible buggy primitive spaghetti code without validation and exception handling for now - but I just made it and it works! - tested against a huge amount of numeric examples calculated by hand + toy examples provided by Hoffman, Pipher and Silverman.
It will not handle input of point at infinity. But you already know that P + Ø = P so... here we go.
The BigInteger is a part of the brand new package System.Numerics in .NET 4.0
more about that one here -->
http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx
For some reason the System.Numerics is not automatically added to projects - select menu project/Add Reference../ select tab: .NET and find System. Numerics.
And it all works with big Integers.
Be sure that you only use valid numbers (prime for Fp and points that acctually ARE on the curve)
Next version will contain valid curves generated from startpoint and startvalues , plus it will contain an implementation of the double and add algorithm.
Sorry for the horrible buggy primitive spaghetti code without validation and exception handling for now - but I just made it and it works! - tested against a huge amount of numeric examples calculated by hand + toy examples provided by Hoffman, Pipher and Silverman.
It will not handle input of point at infinity. But you already know that P + Ø = P so... here we go.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Numerics;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
calcPoints();
}
private BigInteger calcLambda1(BigInteger y2, BigInteger y1, BigInteger x2, BigInteger x1, BigInteger modspace) {
BigInteger lam1 = (y2 - y1) * CalcInverse((x2 - x1), modspace);
return lam1;
}
private BigInteger calcLambda2(BigInteger x1, BigInteger y1, BigInteger A, BigInteger modspace)
{
BigInteger lam2= (3 * x1 * x1 + A)*CalcInverse((2 * y1), modspace);
return lam2;
}
private BigInteger calcXFinal(BigInteger Lambda, BigInteger x1, BigInteger x2, BigInteger modspace) {
BigInteger bigX= (Lambda*Lambda - x1 - x2)%modspace;
if (bigX < 0) { bigX = bigX + modspace; }
return bigX;
}
private BigInteger calcYFinal(BigInteger Lambda, BigInteger x1, BigInteger x3, BigInteger y1, BigInteger modspace)
{
BigInteger bigY= (Lambda*(x1-x3) - y1)%modspace;
if (bigY < 0) { bigY = bigY + modspace; }
return bigY;
}
private BigInteger CalcInverse(BigInteger n, BigInteger p)
{
BigInteger x = 1;
BigInteger y = 0;
BigInteger a = p;
BigInteger b = n;
BigInteger q, t;
BigInteger res;
while (b != 0)
{
t = b;
//q2 = Math.floor(a/t);
q = BigInteger.Divide(a, t);
b = a - q * t;
a = t;
t = x;
x = y - q * t;
y = t;
}
if (y < 0)
{
res = y + p;
}
else
{
res = y;
}
return res;
}
private void calcPoints() {
BigInteger inputA = BigInteger.Parse(paramA.Text);
BigInteger inputB = BigInteger.Parse(paramB.Text);
BigInteger inputx1 = BigInteger.Parse(paramx1.Text);
BigInteger inputy1 = BigInteger.Parse(paramy1.Text);
BigInteger inputx2 = BigInteger.Parse(paramx2.Text);
BigInteger inputy2 = BigInteger.Parse(paramy2.Text);
BigInteger modp = BigInteger.Parse(paramp.Text);
if ((inputx1 == inputx2) && (inputy1 != inputy2) )
{
resultx3.Text = "point at infinity";
resulty3.Text = "point at infinity";
}
else {
if ((inputx1 == inputx2) && (inputy1 == inputy2))
{
BigInteger l2 = (calcLambda2(inputx1,inputy1,inputA, modp))%modp;
dontpanic.Text = l2.ToString();
BigInteger resl2x3 = (calcXFinal(l2, inputx1, inputx2, modp))%modp;
BigInteger resl2y3 = (calcYFinal(l2, inputx1, resl2x3, inputy1, modp))%modp;
resultx3.Text = resl2x3.ToString();
resulty3.Text = resl2y3.ToString();
}
else {
BigInteger l1 = (calcLambda1(inputy2,inputy1, inputx2,inputx1, modp))%modp;
dontpanic.Text = l1.ToString();
BigInteger resl1x3 = (calcXFinal(l1, inputx1, inputx2, modp))%modp;
BigInteger resl1y3 = (calcYFinal(l1, inputx1, resl1x3, inputy1, modp))%modp;
resultx3.Text = resl1x3.ToString();
resulty3.Text = resl1y3.ToString();
}
}
}
}
The BigInteger is a part of the brand new package System.Numerics in .NET 4.0
more about that one here -->
http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx
For some reason the System.Numerics is not automatically added to projects - select menu project/Add Reference../ select tab: .NET and find System. Numerics.
Etiketter:
add algorithm,
add points,
addition,
big integers,
C#,
code example,
modular inverse,
running example
Thursday, July 01, 2010
algorithm for modular inverse in Fp
not the most effective maybe - but at least ONE way of doing it:
A binary algorithm:
You need a prime p for Fp and a number a less than p-1 (larger than 0)
The output will be a^-1 mod p
If you need to calculate b/a mod p ( equals ba^-1 mod a) just substitute x1 with b instead of 1 in the pseudo-code below:
A binary algorithm:
You need a prime p for Fp and a number a less than p-1 (larger than 0)
The output will be a^-1 mod p
If you need to calculate b/a mod p ( equals ba^-1 mod a) just substitute x1 with b instead of 1 in the pseudo-code below:
1. s=a, t=p
2. x1 = 1, x2 = 0
3. while (s!= 1 && t != 1) do
3.1. While s even do
s=s/2
if (x1 even) x1 = x1/2 else x1 = (x1+p)/2
3.2. While t even do
t = t/2
if (x2 even) x2 = x2/2 else x2 = (x2+p)/2
3.3. if (s>=t) s = s-t, x1 = x1-x2
else t= t-s, x2 = x2-x1
4. if (s==1) return x1 mod p else return x2 mod p
Etiketter:
algorithm,
binary algorithm,
inverse,
modular,
modular inverse,
pseudo code
Subscribe to:
Posts (Atom)