예제 #1
0
        public static void ContextBuild(EcmultGenContext ctx, EventHandler <Callback> cb)
        {
            Ge[] r1   = new Ge[1024];
            GeJ  r2   = new GeJ();
            GeJ  geJ1 = new GeJ();

            if (ctx.Prec != null)
            {
                return;
            }
            ctx.PrecInit();
            Group.secp256k1_gej_set_ge(r2, Group.Secp256K1GeConstG);
            byte[] bytes = Encoding.UTF8.GetBytes("The scalar for this x is unknown");
            Fe     fe    = new Fe();
            Ge     ge    = new Ge();

            Field.SetB32(fe, bytes);
            Group.secp256k1_ge_set_xo_var(ge, fe, false);
            Group.secp256k1_gej_set_ge(geJ1, ge);
            Group.secp256k1_gej_add_ge_var(geJ1, geJ1, Group.Secp256K1GeConstG, (Fe)null);
            GeJ[] a = new GeJ[1024];
            for (int index = 0; index < a.Length; ++index)
            {
                a[index] = new GeJ();
            }
            GeJ geJ2 = r2.Clone();
            GeJ geJ3 = geJ1.Clone();

            for (int index1 = 0; index1 < 64; ++index1)
            {
                a[index1 * 16] = geJ3.Clone();
                for (int index2 = 1; index2 < 16; ++index2)
                {
                    Group.secp256k1_gej_add_var(a[index1 * 16 + index2], a[index1 * 16 + index2 - 1], geJ2, (Fe)null);
                }
                for (int index2 = 0; index2 < 4; ++index2)
                {
                    Group.secp256k1_gej_double_var(geJ2, geJ2, (Fe)null);
                }
                Group.secp256k1_gej_double_var(geJ3, geJ3, (Fe)null);
                if (index1 == 62)
                {
                    Group.secp256k1_gej_neg(geJ3, geJ3);
                    Group.secp256k1_gej_add_var(geJ3, geJ3, geJ1, (Fe)null);
                }
            }
            for (int index = 0; index < r1.Length; ++index)
            {
                r1[index] = new Ge();
            }
            Group.secp256k1_ge_set_all_gej_var(r1, a, 1024, cb);
            for (int index1 = 0; index1 < 64; ++index1)
            {
                for (int index2 = 0; index2 < 16; ++index2)
                {
                    Group.ToStorage(ctx.Prec[index1][index2], r1[index1 * 16 + index2]);
                }
            }
            EcMultGen.Blind(ctx, (byte[])null);
        }
예제 #2
0
        public static void ContextBuild(EcmultGenContext ctx, EventHandler <Callback> cb)
        {
#if !USE_ECMULT_STATIC_PRECOMPUTATION
            Ge[] prec = new Ge[1024];
            GeJ  gj = new GeJ();
            GeJ  numsGej = new GeJ();
            int  i, j;
#endif

            if (ctx.Prec != null)
            {
                return;
            }
#if !USE_ECMULT_STATIC_PRECOMPUTATION
            ctx.PrecInit();

            /* get the generator */
            Group.secp256k1_gej_set_ge(gj, Group.Secp256K1GeConstG);

            /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
            {
                var numsB32 = Encoding.UTF8.GetBytes("The scalar for this x is unknown");
                Fe  numsX   = new Fe();
                Ge  numsGe  = new Ge();
                var r       = Field.SetB32(numsX, numsB32);
                //(void)r;
                Util.VERIFY_CHECK(r);
                r = Group.secp256k1_ge_set_xo_var(numsGe, numsX, false);
                //(void)r;
                Util.VERIFY_CHECK(r);
                Group.secp256k1_gej_set_ge(numsGej, numsGe);
                /* Add G to make the bits in x uniformly distributed. */
                Group.secp256k1_gej_add_ge_var(numsGej, numsGej, Group.Secp256K1GeConstG, null);
            }

            /* compute prec. */
            {
                GeJ[] precj = new GeJ[1024]; /* Jacobian versions of prec. */
                for (int k = 0; k < precj.Length; k++)
                {
                    precj[k] = new GeJ();
                }
                GeJ gbase;
                GeJ numsbase;
                gbase    = gj.Clone();      /* 16^j * G */
                numsbase = numsGej.Clone(); /* 2^j * nums. */
                for (j = 0; j < 64; j++)
                {
                    /* Set precj[j*16 .. j*16+15] to (numsbase, numsbase + gbase, ..., numsbase + 15*gbase). */
                    precj[j * 16] = numsbase.Clone();
                    for (i = 1; i < 16; i++)
                    {
                        Group.secp256k1_gej_add_var(precj[j * 16 + i], precj[j * 16 + i - 1], gbase, null);
                    }
                    /* Multiply gbase by 16. */
                    for (i = 0; i < 4; i++)
                    {
                        Group.secp256k1_gej_double_var(gbase, gbase, null);
                    }
                    /* Multiply numbase by 2. */
                    Group.secp256k1_gej_double_var(numsbase, numsbase, null);
                    if (j == 62)
                    {
                        /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
                        Group.secp256k1_gej_neg(numsbase, numsbase);
                        Group.secp256k1_gej_add_var(numsbase, numsbase, numsGej, null);
                    }
                }
                for (int k = 0; k < prec.Length; k++)
                {
                    prec[k] = new Ge();
                }
                Group.secp256k1_ge_set_all_gej_var(prec, precj, 1024, cb);
            }
            for (j = 0; j < 64; j++)
            {
                for (i = 0; i < 16; i++)
                {
                    Group.ToStorage(ctx.Prec[j][i], prec[j * 16 + i]);
                }
            }
#else
            (void)cb;
            ctx.prec = (secp256k1_ge_storage(*)[64][16])secp256k1_ecmult_static_context;
예제 #3
0
 public static void secp256k1_gej_add_ge_var(GeJ r, GeJ a, Ge b, Fe rzr)
 {
     if (a.Infinity)
     {
         Group.secp256k1_gej_set_ge(r, b);
     }
     else if (b.Infinity)
     {
         if (rzr != null)
         {
             Field.SetInt(rzr, 1U);
         }
         r = a.Clone();
     }
     else
     {
         r.Infinity = false;
         Fe fe1 = new Fe();
         Field.Sqr(fe1, a.Z);
         Fe fe2 = a.X.Clone();
         Field.NormalizeWeak(fe2);
         Fe fe3 = new Fe();
         Field.Mul(fe3, b.X, fe1);
         Fe fe4 = a.Y.Clone();
         Field.NormalizeWeak(fe4);
         Fe fe5 = new Fe();
         Field.Mul(fe5, b.Y, fe1);
         Field.Mul(fe5, fe5, a.Z);
         Fe fe6 = new Fe();
         Field.Negate(fe6, fe2, 1U);
         Field.Add(fe6, fe3);
         Fe fe7 = new Fe();
         Field.Negate(fe7, fe4, 1U);
         Field.Add(fe7, fe5);
         if (Field.NormalizesToZeroVar(fe6))
         {
             if (Field.NormalizesToZeroVar(fe7))
             {
                 Group.secp256k1_gej_double_var(r, a, rzr);
             }
             else
             {
                 if (rzr != null)
                 {
                     Field.SetInt(rzr, 0U);
                 }
                 r.Infinity = true;
             }
         }
         else
         {
             Fe fe8 = new Fe();
             Field.Sqr(fe8, fe7);
             Fe fe9 = new Fe();
             Field.Sqr(fe9, fe6);
             Fe fe10 = new Fe();
             Field.Mul(fe10, fe6, fe9);
             if (rzr != null)
             {
                 rzr = fe6.Clone();
             }
             Field.Mul(r.Z, a.Z, fe6);
             Fe fe11 = new Fe();
             Field.Mul(fe11, fe2, fe9);
             r.X = fe11.Clone();
             Field.MulInt(r.X, 2U);
             Field.Add(r.X, fe10);
             Field.Negate(r.X, r.X, 3U);
             Field.Add(r.X, fe8);
             Field.Negate(r.Y, r.X, 5U);
             Field.Add(r.Y, fe11);
             Field.Mul(r.Y, r.Y, fe7);
             Field.Mul(fe10, fe10, fe4);
             Field.Negate(fe10, fe10, 1U);
             Field.Add(r.Y, fe10);
         }
     }
 }