Further work, Intfield INFINITY

master
Harald Wolff 2017-10-23 12:11:40 +02:00
parent e287225eaf
commit dd4715d55b
6 changed files with 89 additions and 34 deletions

View File

@ -93,14 +93,21 @@ namespace BigInt
**/
public static UInt32[] reduceSigned(UInt32[] value){
int n = value.Length;
//Console.WriteLine("reduceSigned(): < {0}", value.getBytes().Reverse().toHexString());
for (; n > 1; n--){
if (
(value[n-1] != 0) || ((value[n-2] & 1<<31)!=0)
((value[n-1] != 0) || ((value[n-2] & (1<<31))!=0)) && ((value[n-1] != 0xFFFFFFFF) || ((value[n-2] & (1<<31))==0))
){
break;
}
}
return value.Segment(0,n);
value = value.Segment(0, n);
//Console.WriteLine("reduceSigned(): > {0}", value.getBytes().Reverse().toHexString());
return value;
}
/**

View File

@ -1,4 +1,4 @@
using System;
using System;
using sharp.extensions;
using BigInt;
using Crypto.EC;
@ -14,6 +14,34 @@ namespace BigIntegerTest
testIntegerConversion();
testUInteger();
testEC();
Console.WriteLine();
// Mögliche Punkt G: 1 / 59 , 38 / 55
EllipticCurve curve61 = new EllipticCurve(61, 0, 3, 38, 55, 13, 1);
Console.WriteLine("Curve61:");
Console.WriteLine(curve61);
CurvePoint p = curve61.G * 2;
for (int n = 1; (n < 3) || (p != curve61.G);n++){
p = curve61.G * n;
Console.WriteLine("{0,2} * G = {1}", n, p);
}
ProjectiveCurvePoint pg = curve61.G;
ProjectiveCurvePoint pg2 = pg + pg;
ProjectiveCurvePoint pgb = pg2 - pg;
Console.WriteLine("Projektiv: G = {0} => {1}",pg,pg.toCurvePoint());
Console.WriteLine("Projektiv: G + G = {0} => {1}",pg2,pg2.toCurvePoint());
Console.WriteLine("Projektiv: (G + G) - G = {0} => {1}",pgb,pgb.toCurvePoint());
for (int n = 1; (n< 3) || (pgb != ProjectiveCurvePoint.INFINITY);n++){
pgb = pg * n;
Console.WriteLine("{0,2} * G = {1} => {2}", n, pgb, pgb.toCurvePoint() );
}
}
public static void testEC()
@ -22,23 +50,26 @@ namespace BigIntegerTest
EllipticCurve ec = EllipticCurve.createSecp256k1();
Console.WriteLine("{0}", ec);
UInteger yy = yG.Pow(2) % ec.Fp.FieldModulo; //ec.G.Y.Pow(2);
Console.WriteLine("Gy^2 = {0}", yy.toHexString());
Console.WriteLine("Y2(Gx) = {0}", ec.Y2(ec.G.X));
Console.WriteLine();
CurvePoint G2 = ec.G + ec.G;
Console.WriteLine("G + G = {0}", G2.toHexString());
Console.WriteLine("on Curve? {0}", ec.isOnCurve(G2));
Console.WriteLine();
/*
for (int i = 1; i < 6; i++){
CurvePoint p = ec.G * i;
Console.WriteLine("G({0}) = {1}",i,p.toHexString())
CurvePoint p = ec.G;
for (int i = 2; i < 16; i++){
p = ec.G + p;
Console.WriteLine("{0,3} * G = {1}", i, p.toHexString());
Console.WriteLine("{0,3} * G = {1}", i, (ec.G * i).toHexString());
}
*/
CurvePoint p3 = ec.G * 3;
Console.WriteLine("3 * G = {0}",p3.ToString());
Console.WriteLine("(3 * G) - G = {0}",(p3 - ec.G).ToString());
Console.WriteLine("(3 * G) - G == 2 * G ? {0}",(p3 - ec.G) == (ec.G * 2));
}
//public static void testUInt256()

View File

@ -40,6 +40,7 @@ namespace BigInt
act.r - (q * next.r),
act.t - (q * next.t)
);
//Console.WriteLine("EUCLID: q = {0}",q);
//Console.WriteLine("EUCLID: act: r = {0} / t = {1}",act.r,act.t);
//Console.WriteLine("EUCLID: next: r = {0} / t = {1}",next.r,next.t);

View File

@ -5,22 +5,47 @@ namespace BigInt
public class IntField
{
public static IntField Default { get; set; } = null; // new IntField(UBigInteger.ZERO.Resize(256) - 1);
public static IntField INFINITY { get; private set; } = new IntField();
public UInteger FieldModulo { get; private set; }
public int FieldWidth { get; private set; }
public Integer FieldModulo { get; private set; }
public IntField(UInteger p,int width){
this.FieldWidth = width;
this.FieldModulo = p;
}
public IntField(UInteger p){
this.FieldModulo = p;
this.FieldWidth = p.RawValue.Length << 5;
}
public IntField(Integer p){
this.FieldModulo = p;
}
public UInteger Fit(UInteger value){
return value % FieldModulo;
}
private IntField(){
this.FieldModulo = 0;
}
public Integer Fit(Integer value)
{
if (FieldModulo.isZero()){
return value;
}
value %= FieldModulo;
if (value.Sign())
{
value += FieldModulo;
}
return value;
}
public UInteger Fit(UInteger value)
{
if (FieldModulo.isZero()){
return value;
}
value %= (UInteger)FieldModulo;
return value;
}
public Integer AdditiveInverse(Integer value){
return FieldModulo - value;
}
public override string ToString(){
return String.Format("[IntField p={0}]",this.FieldModulo);

View File

@ -120,7 +120,8 @@ namespace BigInt
private Integer __op_mod(Integer b)
{
return null;
UInt32[] result = BigIntMath.smod(rawValue, b.RawValue);
return new Integer(result);
}
private Integer __op_mul(Integer b)
@ -171,11 +172,6 @@ namespace BigInt
{
return a.__op_mul(b);
}
public static Integer operator *(Integer a, UInt32 b)
{
Integer bb = new Integer(new UInt32[] { b });
return a.__op_mul(bb);
}
public static Integer operator /(Integer a, Integer b)
{

View File

@ -175,11 +175,6 @@ namespace BigInt
{
return a.__op_mul(b);
}
public static UInteger operator *(UInteger a, UInt32 b)
{
UInteger bb = a.__op_new(new UInt32[] { b });
return a.__op_mul(bb);
}
public static UInteger operator /(UInteger a, UInteger b)
{