Beispiel #1
0
        protected override void OnEVH()
        {
            switch (step)
            {
            case 1:
                if (!ReferenceEquals(code, null))
                {
                    TransformEncType(program.GetValue(code.operand1));
                }
                else
                {
                    Run();
                }
                break;

            case 2:
                if (!ReferenceEquals(code, null))
                {
                    if (encVal[0].GetEncType() == EncryptionType.None)
                    {
                        encType = EncryptionType.None;
                        var re = new Numeric((BigInteger.One << (Config.ScaleBits + encVal[0].GetScaleBits())) / encVal[0].GetUnsignedBigInteger(), Config.ScaleBits);
                        eInva = new NumericArray(re);
                        // jump to round 13
                        step = 12;
                        Run();
                        break;
                    }
                }
                parallism = encVal.Length;
                length    = Config.NumericBits;
                new IndexMSBOnEVH(party, line, this, encVal, ePow2msb, length).Run();
                break;

            case 3:
                var eRevPow = Inverse.ReverseBits(ePow2msb, length);
                // encKpRevPow is in {1, 2, 4, ..., 2^(Config.NumericBitLength - 1)}
                new XORToAddModOnEVH(party, line, this, eRevPow, encKpRevPow).Run();
                break;

            case 4:
                var MulOperands = new NumericArray(2 * parallism);
                for (int p = 0; p < parallism; ++p)
                {
                    MulOperands[2 * p] = encVal[p]; MulOperands[2 * p + 1] = encKpRevPow[p];
                }
                // a' is in [ 2 ^ (NumericBitLength - 1), 2 ^ (NumericBitLength) )
                new MultiplicationOnEVH(party, line, this, MulOperands, encKppap).Run();
                break;

            case 5:
                et  = new Numeric[parallism][];
                et1 = new NumericArray(parallism);
                for (int p = 0; p < parallism; ++p)
                {
                    System.Diagnostics.Debug.Assert(encKppap[p].GetScaleBits() == 0);
                    //EncKppap[p].ResetScaleBits();
                    et[p]    = new Numeric[Inverse.nt + 1];
                    et[p][0] = new Numeric(1, 0);
                    // a' - at > 0
                    et1[p] = encKppap[p] - Inverse.at;
                }
                // AddModToAdd makes sure (a' + k" - at) > 0 and k" > 0
                new AddModToAddOnEVH(party, line, this, et1, et1).Run();
                break;

            // case 6~8 computes the (a' - at) ^ 2, (a' - at) ^ 3, ... , (a' - at) ^ 7
            case 6:
                var mulOperand2 = new NumericArray(2 * parallism);
                for (int p = 0; p < parallism; ++p)
                {
                    et[p][1]               = et1[p] >> Inverse.reductionBits;
                    mulOperand2[2 * p]     = et[p][1];
                    mulOperand2[2 * p + 1] = et[p][1];
                }
                new MultiplicationOnEVH(party, line, this, mulOperand2, et2).Run();
                break;

            case 7:
                var mulOperand34 = new NumericArray(parallism * 4);
                for (int p = 0; p < parallism; ++p)
                {
                    et[p][2]                = et2[p];
                    mulOperand34[4 * p]     = et[p][1];
                    mulOperand34[4 * p + 1] = et[p][2];
                    mulOperand34[4 * p + 2] = et[p][2];
                    mulOperand34[4 * p + 3] = et[p][2];
                }
                new MultiplicationOnEVH(party, line, this, mulOperand34, et3et4).Run();
                break;

            case 8:
                var mulOperand567 = new NumericArray(parallism * 6);
                for (int p = 0; p < parallism; ++p)
                {
                    et[p][3]                 = et3et4[2 * p];
                    et[p][4]                 = et3et4[2 * p + 1];
                    mulOperand567[6 * p]     = et[p][1];
                    mulOperand567[6 * p + 1] = et[p][4];
                    mulOperand567[6 * p + 2] = et[p][2];
                    mulOperand567[6 * p + 3] = et[p][4];
                    mulOperand567[6 * p + 4] = et[p][3];
                    mulOperand567[6 * p + 5] = et[p][4];
                }
                new MultiplicationOnEVH(party, line, this, mulOperand567, et5et6et7).Run();
                break;

            case 9:
                for (int p = 0; p < parallism; ++p)
                {
                    et[p][5] = et5et6et7[3 * p];
                    et[p][6] = et5et6et7[3 * p + 1];
                    et[p][7] = et5et6et7[3 * p + 2];
                }

                et2ToEt7 = new NumericArray(parallism * 6);
                for (int p = 0; p < parallism; ++p)
                {
                    int offset = p * 6;
                    for (int i = 0; i < 6; ++i)
                    {
                        et2ToEt7[offset + i] = et[p][i + 2];
                    }
                }
                // convert encrption for (a' - at) ^ 2, (a' - at) ^ 3, ... , (a' - at) ^ 7 from AddMod to Addition,
                // such that when computing taylor sum, module will affect the result
                new AddModToAddOnEVH(party, line, this, et2ToEt7, et2ToEt7).Run();
                break;

            // compute taylor sum
            case 10:
                for (int p = 0; p < parallism; ++p)
                {
                    int offset = p * 6;
                    for (int i = 0; i < 6; ++i)
                    {
                        et[p][i + 2] = et2ToEt7[offset + i];
                    }
                }
                eInva = new NumericArray(parallism);
                var MulOperandsScale = new NumericArray(2 * parallism);
                // since 1/a' is too small(even after mutilpied by scaling factor), it will always be 0 if converted to fixed pointer integer
                // such that we need to magnify it with a factor. Here we choose 2 ^ (Config.NumericBitLength - 1)
                BigInteger magnificationFactor = BigInteger.One << (Config.NumericBits - 1);
                for (int p = 0; p < parallism; ++p)
                {
                    BigInteger
                        taylorSum = Inverse.scaleFactor * magnificationFactor / Inverse.dReciprocal[0],
                        redFactor = Inverse.reductionFactor;
                    for (int i = 1; i <= Inverse.nt; ++i)
                    {
                        taylorSum += redFactor * Inverse.scaleFactor * magnificationFactor / Inverse.dReciprocal[i];
                        redFactor *= Inverse.reductionFactor;
                        // taylorSum += Inverse.d[i] * et[p][i].GetVal();
                    }

                    eInva[p] = new Numeric(taylorSum, 0);
                    //System.Diagnostics.Debug.WriteLine("e: taylorSum * magnificationFactor: " + taylorSum);
                    //System.Diagnostics.Debug.WriteLine("e: val: " + taylorSum * Math.Pow(2, Config.NumericBitLength - 1));
                    //System.Diagnostics.Debug.WriteLine("e: eInva befor: " + eInva[0]);
                    MulOperandsScale[2 * p]     = eInva[p];
                    MulOperandsScale[2 * p + 1] = encKpRevPow[p];
                }
                // 1 / a' * encKpRevPow is the result
                new MultiplicationOnEVH(party, line, this, MulOperandsScale, eInva).Run();
                break;

            case 11:
                new AddModToAddOnEVH(party, line, this, eInva, eInva).Run();
                break;

            case 12:
                for (int p = 0; p < parallism; ++p)
                {
                    // scale down the result by the factor 2 ^ (Config.NumericBitLength - 1)
                    eInva[p] >>= (Config.NumericBits - 1);
                    eInva[p].SetScaleBits(Config.ScaleBits);
                }
                //System.Diagnostics.Debug.WriteLine("e: eInva: " + eInva[0]);
                Run();
                break;

            case 13:
                SetResult(encType, eInva.GetArray());
                break;

            case 14:
                InvokeCaller();
                break;

            default:
                throw new Exception();
            }
        }
Beispiel #2
0
        protected override void OnEVH()
        {
            switch (step)
            {
            case 1:
                parallelism = encVal.Length;
                encType     = resultEncType;
                if (!ReferenceEquals(code, null))
                {
                    Numeric enckaa = program.GetValue(code.operand1);
                    encVal = new NumericArray(enckaa);
                    length = Config.NumericBits;
                    if (encVal[0].GetEncType() == EncryptionType.None)
                    {
                        encType = EncryptionType.None;
                        if (encVal[0].GetUnsignedBigInteger() == 0)
                        {
                            encH = new NumericArray(new Numeric(1, 0));
                        }
                        else
                        {
                            encH = new NumericArray(new Numeric(0, 0));
                        }
                        // jump to round 4
                        step = 3;
                        Run();
                        break;
                    }
                }
                new HammingDistanceOnEVH(party, line, this, encVal, encH, length).Run();
                break;

            case 2:
                new FastEqualZeroOnEVH(party, line, this, encH, encH, (int)Math.Ceiling(Math.Log(length + 1, 2))).Run();
                break;

            case 3:
                // as subfunction, the result should be 0 if secret == 0
                // however the result from FastEqualZero if 1 if secret == 0
                if (ReferenceEquals(code, null))
                {
                    for (int p = 0; p < parallelism; p++)
                    {
                        encH[p] ^= new Numeric(1, 0);
                    }
                }
                Run();
                break;

            case 4:
                SetResult(encType, encH.GetArray());
                break;

            case 5:
                InvokeCaller();
                break;

            default:
                throw new Exception();
                ////System.Diagnostics.Debug.Assert(le >= 2 || le == 0);
                //if (le > 2)
                //{
                //    le = (int)Math.Ceiling(Math.Log(le + 1, 2));
                //    new HammingDistanceOnEVH(party, line, this, encH, encH, le).Run();
                //}
                //else if (le == 2)
                //{
                //    var OROperand = new Numeric[2 * parallelism];
                //    for (int p = 0; p < parallelism; ++p)
                //    {
                //        OROperand[2 * p] = encH[p] >> 1;
                //        OROperand[2 * p + 1] = encH[p].ModPow(1);
                //    }
                //    le = 0;
                //    new OROnEVH(party, line, this, new NumericArray(OROperand), encH).Run();
                //}
                //else
                //{
                //    Numeric[] enckf = new Numeric[parallelism];
                //    for (int p = 0; p < parallelism; ++p)
                //    {
                //        enckf[p] = encH[p].ModPow(1);
                //        //enckf[p].SetEncType(EncryptionType.XOR);
                //    }
                //    if(!ReferenceEquals(code, null))
                //    {
                //        // not as a subfunction
                //        enckf[0] ^= new Numeric(1, 0);
                //    }
                //    SetResult(enckf);
                //}
                //break;
            }
        }
Beispiel #3
0
        protected override void OnEVH()
        {
            switch (step)
            {
            case 1:
                if (!ReferenceEquals(code, null))
                {
                    Numeric enckaa = program.GetValue(code.operand1);
                    TransformEncType(enckaa);
                }
                else
                {
                    Run();
                }
                break;

            case 2:
                encType = resultEncType;
                // input value is not encrypted
                if (!ReferenceEquals(code, null) && encVal[0].GetEncType() == EncryptionType.None)
                {
                    encType = EncryptionType.None;
                    enckf   = new NumericArray(1);
                    if (encVal[0].GetUnsignedBigInteger() < 0)
                    {
                        enckf[0] = new Numeric(1, 0);
                    }
                    else
                    {
                        enckf[0] = new Numeric(0, 0);
                    }
                    // jump to round 6
                    step = 5;
                    Run();
                    break;
                }
                parallelism     = encVal.Length;
                KInversePlusKip = new NumericArray();
                party.receiver.ReceiveFrom(PartyType.KH, line, this, KInversePlusKip);
                break;

            case 3:
                var Enckipa = new NumericArray(parallelism * ne);
                for (int p = 0; p < parallelism; ++p)
                {
                    int offset = p * ne;
                    for (int i = 0; i < ne; i++)
                    {
                        Enckipa[offset + i] = encVal[p] + KInversePlusKip[offset + i];
                    }
                }
                elei = new NumericArray();
                new LessZeroOnEVH(party, line, this, Enckipa, elei, length).Run();
                break;

            case 4:
                new XORToAddModOnEVH(party, line, this, elei, elei).Run();
                break;

            case 5:
                var sele = new NumericArray(parallelism);
                for (int p = 0; p < parallelism; ++p)
                {
                    sele[p] = new Numeric(ne / 2, 0);
                    int offset = p * ne;
                    for (int i = 0; i < ne; i++)
                    {
                        sele[p] -= elei[offset + i];
                    }
                    //System.Diagnostics.Debug.WriteLine("EVH SUM: " + sele[p].ToUnsignedBigInteger());
                }
                enckf = new NumericArray();
                new LessZeroOnEVH(party, line, this, sele, enckf, length).Run();
                break;

            case 6:
                SetResult(encType, enckf.GetArray());
                break;

            case 7:
                InvokeCaller();
                break;

            default:
                throw new Exception();
            }
        }
Beispiel #4
0
        protected override void OnKH()
        {
            switch (step)
            {
            case 1:
                encType = resultEncType;
                if (!ReferenceEquals(code, null))
                {
                    Numeric ka = program.GetValue(code.operand1);
                    key    = new NumericArray(ka);
                    length = Config.NumericBits;
                    if (key[0].GetEncType() == EncryptionType.None)
                    {
                        encType = EncryptionType.None;
                        if (key[0].GetEncType() == EncryptionType.None)
                        {
                            encType = EncryptionType.None;
                            if (key[0].GetUnsignedBigInteger() == 0)
                            {
                                keyH = new NumericArray(new Numeric(1, 0));
                            }
                            else
                            {
                                keyH = new NumericArray(new Numeric(0, 0));
                            }
                            // jump to round 3
                            step = 2;
                            Run();
                            break;
                        }
                    }
                }
                parallelism = key.Length;
                //Console.WriteLine("KH: " + key[0]);
                new HammingDistanceOnKH(party, line, this, key, keyH, length).Run();
                break;

            case 2:
                new FastEqualZeroOnKH(party, line, this, keyH, keyH, (int)Math.Ceiling(Math.Log(length + 1, 2))).Run();
                break;

            case 3:
                SetResult(encType, keyH.GetArray());
                break;

            case 4:
                InvokeCaller();
                break;

            default:
                throw new Exception();
                ////System.Diagnostics.Debug.Assert(le >= 2 || le == 0);
                //if (le > 2)
                //{
                //    le = (int)Math.Ceiling(Math.Log(le + 1, 2));
                //    new HammingDistanceOnKH(party, line, this, keyH, keyH, le).Run();
                //}
                //else if (le == 2)
                //{
                //    var OROperand = new Numeric[2 * parallelism];
                //    for (int p = 0; p < parallelism; ++p)
                //    {
                //        OROperand[2 * p] = keyH[p] >> 1;
                //        OROperand[2 * p + 1] = keyH[p].ModPow(1);
                //    }
                //    le = 0;
                //    new OROnKH(party, line, this, new NumericArray(OROperand), keyH).Run();
                //}
                //else
                //{
                //    Numeric[] kf = new Numeric[parallelism];
                //    for (int p = 0; p < parallelism; ++p)
                //    {
                //        kf[p] = keyH[p].ModPow(1);
                //        //kf[p].SetEncType(EncryptionType.XOR);
                //    }
                //    SetResult(kf);
                //}
                //break;
            }
        }
Beispiel #5
0
        protected override void OnKH()
        {
            switch (step)
            {
            case 1:
                if (!ReferenceEquals(code, null))
                {
                    Numeric ka = program.GetValue(code.operand1);
                    TransformEncType(ka);
                }
                else
                {
                    Run();
                }
                break;

            case 2:
                encType = resultEncType;
                // input value is not encrypted
                if (!ReferenceEquals(code, null) && key[0].GetEncType() == EncryptionType.None)
                {
                    encType = EncryptionType.None;
                    kf      = new NumericArray(1);
                    if (key[0].GetUnsignedBigInteger() < 0)
                    {
                        kf[0] = new Numeric(1, 0);
                    }
                    else
                    {
                        kf[0] = new Numeric(0, 0);
                    }
                    // jump to round 5
                    step = 4;
                    Run();
                    break;
                }
                parallelism = key.Length;
                var Kip             = new NumericArray(parallelism * ne);
                var KInversePlusKip = new Numeric[parallelism * ne];
                for (int p = 0; p < parallelism; ++p)
                {
                    int offset = p * ne;
                    for (int i = 0; i < ne; ++i)
                    {
                        Kip[offset + i]             = Utility.NextUnsignedNumeric(key[p].GetScaleBits());
                        KInversePlusKip[offset + i] = Kip[offset + i] - key[p];
                    }
                }
                var toEVH = Message.AssembleMessage(line, opType, false, KInversePlusKip);
                party.sender.SendTo(PartyType.EVH, toEVH);
                klei = new NumericArray();
                new LessZeroOnKH(party, line, this, Kip, klei, length).Run();
                break;

            case 3:
                new XORToAddModOnKH(party, line, this, klei, klei).Run();
                break;

            case 4:
                var skle = new NumericArray(parallelism);
                for (int p = 0; p < parallelism; ++p)
                {
                    skle[p] = new Numeric(0, 0);
                    int offset = p * ne;
                    for (int i = 0; i < ne; i++)
                    {
                        skle[p] -= klei[offset + i];
                    }
                    //System.Diagnostics.Debug.WriteLine("KH  SUM: " + skle[p].ToUnsignedBigInteger());
                }
                kf = new NumericArray();
                new LessZeroOnKH(party, line, this, skle, kf, length).Run();
                break;

            case 5:
                SetResult(encType, kf.GetArray());
                break;

            case 6:
                InvokeCaller();
                break;

            default:
                throw new Exception();
            }
        }