private static bool IsSquare(uint[] x) { uint[] t1 = Nat224.Create(); uint[] t2 = Nat224.Create(); Nat224.Copy(x, t1); for (int i = 0; i < 7; ++i) { Nat224.Copy(t1, t2); SecP224R1Field.SquareN(t1, 1 << i, t1); SecP224R1Field.Multiply(t1, t2, t1); } SecP224R1Field.SquareN(t1, 95, t1); return(Nat224.IsOne(t1)); }
/** * return a sqrt root - the routine verifies that the calculation returns the right value - if * none exists it returns null. */ public override ECFieldElement Sqrt() { uint[] c = this.x; if (Nat224.IsZero(c) || Nat224.IsOne(c)) { return(this); } uint[] nc = Nat224.Create(); SecP224R1Field.Negate(c, nc); uint[] r = Mod.Random(SecP224R1Field.P); for (;;) { uint[] d1 = Nat224.Create(); Nat224.Copy(r, d1); uint[] e1 = Nat224.Create(); e1[0] = 1; uint[] f1 = Nat224.Create(); RP(nc, d1, e1, f1); uint[] d0 = Nat224.Create(); uint[] e0 = Nat224.Create(); for (int k = 1; k < 96; ++k) { Nat224.Copy(d1, d0); Nat224.Copy(e1, e0); RS(d1, e1, f1); if (Nat224.IsZero(d1)) { Mod.Invert(SecP224R1Field.P, e0, f1); SecP224R1Field.Multiply(f1, d0, f1); SecP224R1Field.Square(f1, d1); return(Nat224.Eq(c, d1) ? new SecP224R1FieldElement(f1) : null); } } // Avoid any possible infinite loop due to a bad random number generator SecP224R1Field.AddOne(r, r); } }
public override ECFieldElement Sqrt() { uint[] x = this.x; if (Nat224.IsZero(x) || Nat224.IsOne(x)) { return(this); } uint[] z = Nat224.Create(); SecP224R1Field.Negate(x, z); uint[] r = Mod.Random(SecP224R1Field.P); uint[] t = Nat224.Create(); if (!IsSquare(x)) { return(null); } while (!TrySqrt(z, r, t)) { SecP224R1Field.AddOne(r, r); } SecP224R1Field.Square(t, r); return(!Nat224.Eq(x, r) ? null : new SecP224R1FieldElement(t)); }
public override ECFieldElement Sqrt() { uint[] array = x; if (Nat224.IsZero(array) || Nat224.IsOne(array)) { return(this); } uint[] array2 = Nat224.Create(); SecP224R1Field.Negate(array, array2); uint[] array3 = Mod.Random(SecP224R1Field.P); uint[] t = Nat224.Create(); if (!IsSquare(array)) { return(null); } while (!TrySqrt(array2, array3, t)) { SecP224R1Field.AddOne(array3, array3); } SecP224R1Field.Square(t, array3); return((!Nat224.Eq(array, array3)) ? null : new SecP224R1FieldElement(t)); }
/** * return a sqrt root - the routine verifies that the calculation returns the right value - if * none exists it returns null. */ public override ECFieldElement Sqrt() { uint[] c = this.x; if (Nat224.IsZero(c) || Nat224.IsOne(c)) return this; uint[] nc = Nat224.Create(); SecP224R1Field.Negate(c, nc); uint[] r = Mod.Random(SecP224R1Field.P); uint[] t = Nat224.Create(); if (!IsSquare(c)) return null; while (!TrySqrt(nc, r, t)) { SecP224R1Field.AddOne(r, r); } SecP224R1Field.Square(t, r); return Nat224.Eq(c, r) ? new SecP224R1FieldElement(t) : null; }
/** * return a sqrt root - the routine verifies that the calculation returns the right value - if * none exists it returns null. */ public override ECFieldElement Sqrt() { /* * Q == 8m + 5, so we use Pocklington's method for this case. * * First, raise this element to the exponent 2^221 - 2^29 - 2^9 - 2^8 - 2^6 - 2^4 - 2^1 (i.e. m + 1) * * Breaking up the exponent's binary representation into "repunits", we get: * { 191 1s } { 1 0s } { 19 1s } { 2 0s } { 1 1s } { 1 0s } { 1 1s } { 1 0s } { 3 1s } { 1 0s } * * Therefore we need an addition chain containing 1, 3, 19, 191 (the lengths of the repunits) * We use: [1], 2, [3], 4, 8, 11, [19], 23, 42, 84, 107, [191] */ uint[] x1 = this.x; if (Nat224.IsZero(x1) || Nat224.IsOne(x1)) { return(this); } uint[] x2 = Nat224.Create(); SecP224K1Field.Square(x1, x2); SecP224K1Field.Multiply(x2, x1, x2); uint[] x3 = x2; SecP224K1Field.Square(x2, x3); SecP224K1Field.Multiply(x3, x1, x3); uint[] x4 = Nat224.Create(); SecP224K1Field.Square(x3, x4); SecP224K1Field.Multiply(x4, x1, x4); uint[] x8 = Nat224.Create(); SecP224K1Field.SquareN(x4, 4, x8); SecP224K1Field.Multiply(x8, x4, x8); uint[] x11 = Nat224.Create(); SecP224K1Field.SquareN(x8, 3, x11); SecP224K1Field.Multiply(x11, x3, x11); uint[] x19 = x11; SecP224K1Field.SquareN(x11, 8, x19); SecP224K1Field.Multiply(x19, x8, x19); uint[] x23 = x8; SecP224K1Field.SquareN(x19, 4, x23); SecP224K1Field.Multiply(x23, x4, x23); uint[] x42 = x4; SecP224K1Field.SquareN(x23, 19, x42); SecP224K1Field.Multiply(x42, x19, x42); uint[] x84 = Nat224.Create(); SecP224K1Field.SquareN(x42, 42, x84); SecP224K1Field.Multiply(x84, x42, x84); uint[] x107 = x42; SecP224K1Field.SquareN(x84, 23, x107); SecP224K1Field.Multiply(x107, x23, x107); uint[] x191 = x23; SecP224K1Field.SquareN(x107, 84, x191); SecP224K1Field.Multiply(x191, x84, x191); uint[] t1 = x191; SecP224K1Field.SquareN(t1, 20, t1); SecP224K1Field.Multiply(t1, x19, t1); SecP224K1Field.SquareN(t1, 3, t1); SecP224K1Field.Multiply(t1, x1, t1); SecP224K1Field.SquareN(t1, 2, t1); SecP224K1Field.Multiply(t1, x1, t1); SecP224K1Field.SquareN(t1, 4, t1); SecP224K1Field.Multiply(t1, x3, t1); SecP224K1Field.Square(t1, t1); uint[] t2 = x84; SecP224K1Field.Square(t1, t2); if (Nat224.Eq(x1, t2)) { return(new SecP224K1FieldElement(t1)); } /* * If the first guess is incorrect, we multiply by a precomputed power of 2 to get the second guess, * which is ((4x)^(m + 1))/2 mod Q */ SecP224K1Field.Multiply(t1, PRECOMP_POW2, t1); SecP224K1Field.Square(t1, t2); if (Nat224.Eq(x1, t2)) { return(new SecP224K1FieldElement(t1)); } return(null); }
public override ECFieldElement Sqrt() { uint[] y = this.x; if (Nat224.IsZero(y) || Nat224.IsOne(y)) { return(this); } uint[] array = Nat224.Create(); SecP224K1Field.Square(y, array); SecP224K1Field.Multiply(array, y, array); uint[] array2 = array; SecP224K1Field.Square(array, array2); SecP224K1Field.Multiply(array2, y, array2); uint[] array3 = Nat224.Create(); SecP224K1Field.Square(array2, array3); SecP224K1Field.Multiply(array3, y, array3); uint[] array4 = Nat224.Create(); SecP224K1Field.SquareN(array3, 4, array4); SecP224K1Field.Multiply(array4, array3, array4); uint[] array5 = Nat224.Create(); SecP224K1Field.SquareN(array4, 3, array5); SecP224K1Field.Multiply(array5, array2, array5); uint[] array6 = array5; SecP224K1Field.SquareN(array5, 8, array6); SecP224K1Field.Multiply(array6, array4, array6); uint[] array7 = array4; SecP224K1Field.SquareN(array6, 4, array7); SecP224K1Field.Multiply(array7, array3, array7); uint[] array8 = array3; SecP224K1Field.SquareN(array7, 19, array8); SecP224K1Field.Multiply(array8, array6, array8); uint[] array9 = Nat224.Create(); SecP224K1Field.SquareN(array8, 42, array9); SecP224K1Field.Multiply(array9, array8, array9); uint[] z = array8; SecP224K1Field.SquareN(array9, 23, z); SecP224K1Field.Multiply(z, array7, z); uint[] array10 = array7; SecP224K1Field.SquareN(z, 84, array10); SecP224K1Field.Multiply(array10, array9, array10); uint[] z2 = array10; SecP224K1Field.SquareN(z2, 20, z2); SecP224K1Field.Multiply(z2, array6, z2); SecP224K1Field.SquareN(z2, 3, z2); SecP224K1Field.Multiply(z2, y, z2); SecP224K1Field.SquareN(z2, 2, z2); SecP224K1Field.Multiply(z2, y, z2); SecP224K1Field.SquareN(z2, 4, z2); SecP224K1Field.Multiply(z2, array2, z2); SecP224K1Field.Square(z2, z2); uint[] array11 = array9; SecP224K1Field.Square(z2, array11); if (Nat224.Eq(y, array11)) { return(new SecP224K1FieldElement(z2)); } SecP224K1Field.Multiply(z2, SecP224K1FieldElement.PRECOMP_POW2, z2); SecP224K1Field.Square(z2, array11); if (Nat224.Eq(y, array11)) { return(new SecP224K1FieldElement(z2)); } return(null); }
public override ECFieldElement Sqrt() { uint[] x = this.x; if (Nat224.IsZero(x) || Nat224.IsOne(x)) { return(this); } uint[] z = Nat224.Create(); SecP224K1Field.Square(x, z); SecP224K1Field.Multiply(z, x, z); uint[] numArray3 = z; SecP224K1Field.Square(z, numArray3); SecP224K1Field.Multiply(numArray3, x, numArray3); uint[] numArray4 = Nat224.Create(); SecP224K1Field.Square(numArray3, numArray4); SecP224K1Field.Multiply(numArray4, x, numArray4); uint[] numArray5 = Nat224.Create(); SecP224K1Field.SquareN(numArray4, 4, numArray5); SecP224K1Field.Multiply(numArray5, numArray4, numArray5); uint[] numArray6 = Nat224.Create(); SecP224K1Field.SquareN(numArray5, 3, numArray6); SecP224K1Field.Multiply(numArray6, numArray3, numArray6); uint[] numArray7 = numArray6; SecP224K1Field.SquareN(numArray6, 8, numArray7); SecP224K1Field.Multiply(numArray7, numArray5, numArray7); uint[] numArray8 = numArray5; SecP224K1Field.SquareN(numArray7, 4, numArray8); SecP224K1Field.Multiply(numArray8, numArray4, numArray8); uint[] numArray9 = numArray4; SecP224K1Field.SquareN(numArray8, 0x13, numArray9); SecP224K1Field.Multiply(numArray9, numArray7, numArray9); uint[] numArray10 = Nat224.Create(); SecP224K1Field.SquareN(numArray9, 0x2a, numArray10); SecP224K1Field.Multiply(numArray10, numArray9, numArray10); uint[] numArray11 = numArray9; SecP224K1Field.SquareN(numArray10, 0x17, numArray11); SecP224K1Field.Multiply(numArray11, numArray8, numArray11); uint[] numArray12 = numArray8; SecP224K1Field.SquareN(numArray11, 0x54, numArray12); SecP224K1Field.Multiply(numArray12, numArray10, numArray12); uint[] numArray13 = numArray12; SecP224K1Field.SquareN(numArray13, 20, numArray13); SecP224K1Field.Multiply(numArray13, numArray7, numArray13); SecP224K1Field.SquareN(numArray13, 3, numArray13); SecP224K1Field.Multiply(numArray13, x, numArray13); SecP224K1Field.SquareN(numArray13, 2, numArray13); SecP224K1Field.Multiply(numArray13, x, numArray13); SecP224K1Field.SquareN(numArray13, 4, numArray13); SecP224K1Field.Multiply(numArray13, numArray3, numArray13); SecP224K1Field.Square(numArray13, numArray13); uint[] numArray14 = numArray10; SecP224K1Field.Square(numArray13, numArray14); if (Nat224.Eq(x, numArray14)) { return(new SecP224K1FieldElement(numArray13)); } SecP224K1Field.Multiply(numArray13, PRECOMP_POW2, numArray13); SecP224K1Field.Square(numArray13, numArray14); if (Nat224.Eq(x, numArray14)) { return(new SecP224K1FieldElement(numArray13)); } return(null); }