Esempio n. 1
0
        Statement AssignmentStatement()
        {
            Statement re = null;

            if (vars.ContainsKey(currToken.sequence))
            {
                var result = vars[currToken.sequence];
                NextToken();
                if (Accept(Symbol.S_Assign))
                {
                    var exp = NumericExpression();
                    re = new SAssignment(result, exp);
                }
                Expect(Symbol.S_Semicolon);
            }
            else
            {
                Error("AssignmentStatement: variable is not declared");
            }
            return(re);
        }
Esempio n. 2
0
        private long RunTest(int commandNum, OperationType op, int keyLen, int numericBitLen, byte scaleBits, bool isOptimized, bool isOp1Enc, bool isOp2Enc)
        {
            Config.SetGlobalParameters(keyLen, numericBitLen, scaleBits, isOptimized);
            Program program = new Program();

            Numeric []   expectedResult = new Numeric [commandNum];
            Expression[] ret            = new Expression[commandNum];
            Numeric[]    operand1       = new Numeric[commandNum], operand2 = new Numeric[commandNum];


            for (int i = 0; i < commandNum; ++i)
            {
                Expression variable = new EVariable(program, "a" + i);
                if (op == OperationType.Inverse)
                {
                    //operand1[i] = new Numeric(1000, 0);
                    operand1[i] = Utility.NextUnsignedNumeric(0, 14);
                    operand2[i] = Utility.NextUnsignedNumeric(0, 14);
                }
                else if (op == OperationType.Multiplication)
                {
                    //operand1[i] = new Numeric(30 << 21, 21);
                    operand1[i] = Utility.NextSignedNumeric(scaleBits, numericBitLen / 2);
                    operand2[i] = Utility.NextSignedNumeric(scaleBits, numericBitLen / 2);
                }
                else if (op == OperationType.Sin)
                {
                    operand1[i] = Utility.NextSignedNumeric(scaleBits, numericBitLen);
                    operand2[i] = Utility.NextSignedNumeric(scaleBits, numericBitLen);
                }
                else if (op == OperationType.Switch)
                {
                    operand1[i] = i % 12;
                    operand2[i] = 1;
                }
                else if (op == OperationType.NOT)
                {
                    operand1[i] = Utility.NextUnsignedNumeric(0, 1);
                    operand2[i] = 1;
                }
                else
                {
                    //operand1[i] = new Numeric(30 << 21, 21);
                    operand1[i] = Utility.NextSignedNumeric(scaleBits, numericBitLen);
                    operand2[i] = Utility.NextSignedNumeric(scaleBits, numericBitLen);
                    //operand1[i] = new Numeric(-1350, fractionBitLength);
                }
                ENumericLiteral op1 = new ENumericLiteral(operand1[i]), op2 = new ENumericLiteral(operand2[i]);
                op1.needEnc = isOp1Enc;
                op2.needEnc = isOp2Enc;
                Expression expression = new EBinaryOperation(op1, op2, op);
                Statement  statement  = new SAssignment(variable, expression);
                program.AddStatement(statement);
                ret[i] = variable;
                switch (op)
                {
                case OperationType.Addition:
                    expectedResult[i] = operand1[i] + operand2[i];
                    break;

                case OperationType.Substraction:
                    expectedResult[i] = operand1[i] - operand2[i];
                    break;

                case OperationType.AND:
                    expectedResult[i] = operand1[i] & operand2[i];
                    break;

                case OperationType.XOR:
                    expectedResult[i] = operand1[i] ^ operand2[i];
                    break;

                case OperationType.Multiplication:
                    expectedResult[i] = operand1[i] * operand2[i];
                    break;

                case OperationType.OR:
                    expectedResult[i] = operand1[i] | operand2[i];
                    break;

                case OperationType.Sin:
                    expectedResult[i] = new Numeric((BigInteger)(Math.Sin(operand1[i].GetVal()) * Math.Pow(2, Config.ScaleBits)), Config.ScaleBits);
                    break;

                case OperationType.EqualZero:
                    expectedResult[i] = (operand1[i].GetVal() == 0) ? new Numeric(1, 0) : new Numeric(0, 0);
                    break;

                //case OperationType.FastEqualZero:
                //    expectedResult[i] = ((operand1[i].ModPow(FastEqualZero.GetKeySize())).GetVal() == 0) ? new Numeric(1, 0) : new Numeric(0, 0);
                //    break;
                case OperationType.LessZero:
                case OperationType.MultiLessZero:
                    expectedResult[i] = (operand1[i].GetVal() >= 0) ? new Numeric(0, 0) : new Numeric(1, 0);
                    break;

                case OperationType.None:
                    expectedResult[i] = operand1[i];
                    break;

                case OperationType.HammingDistance:
                    break;

                case OperationType.Inverse:
                    expectedResult[i] = operand1[i];
                    break;

                case OperationType.IndexMSB:

                case OperationType.Switch:
                    expectedResult[i] = operand1[i];
                    break;

                case OperationType.NOT:
                    expectedResult[i] = operand1[i] ^ new Numeric(1, 0);
                    break;

                default:
                    throw new ArgumentException();
                }
            }

            program.AddStatement(new SReturn(ret));
            program.Translate();

            var programEnc = program.EncProgram();

            if (op == OperationType.HammingDistance)
            {
                for (int i = 0; i < commandNum; ++i)
                {
                    expectedResult[i] = ((((ENumericLiteral)((ICAssignment)programEnc[0].icList[i]).operand1).GetValue()) ^ (((ENumericLiteral)((ICAssignment)programEnc[1].icList[i]).operand1).GetValue())).SumBits(Config.KeyBits);
                }
            }

            var totalTime = Party.RunAllParties(program, programEnc);

            int corrCount = 0;

            for (int i = 0; i < commandNum; ++i)
            {
                var computedRes = ((Numeric )program.vTable["a" + i]);
                var expRes      = expectedResult[i];

                if (op == OperationType.HammingDistance)
                {
                    if (computedRes.GetVal() == expRes.GetVal() || computedRes.GetVal() + (int)Math.Pow(2, Math.Ceiling(Math.Log(Config.KeyBits + 1, 2))) == expRes.GetVal())
                    {
                        corrCount++;
                    }
                }
                else if (op == OperationType.Inverse)
                {
                    if (Math.Abs(expRes.GetVal() - 1 / computedRes.GetVal()) < 10)
                    {
                        System.Diagnostics.Debug.WriteLine(i + ":");
                        System.Diagnostics.Debug.WriteLine("operand1: " + operand1[i]);
                        System.Diagnostics.Debug.WriteLine("operand2: " + operand2[i]);
                        System.Diagnostics.Debug.WriteLine("computed: " + program.vTable["a" + i]);
                        System.Diagnostics.Debug.WriteLine("expected: " + 1 / expRes.GetVal());

                        corrCount++;
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine(i + ":");
                        System.Diagnostics.Debug.WriteLine("operand1: " + operand1[i]);
                        System.Diagnostics.Debug.WriteLine("operand2: " + operand2[i]);
                        System.Diagnostics.Debug.WriteLine("computed: " + program.vTable["a" + i]);
                        System.Diagnostics.Debug.WriteLine("expected: " + 1 / expRes.GetVal());
                        Console.WriteLine("corr: " + 1 / expRes.GetVal() + ", computed: " + computedRes.GetVal());
                    }
                }
                else if (op == OperationType.NOT)
                {
                    if (computedRes.GetUnsignedBigInteger() == expRes.GetUnsignedBigInteger())
                    {
                        System.Diagnostics.Debug.WriteLine(i + ":");
                        System.Diagnostics.Debug.WriteLine("operand1: " + operand1[i]);
                        System.Diagnostics.Debug.WriteLine("operand2: " + operand2[i]);
                        System.Diagnostics.Debug.WriteLine("computed: " + program.vTable["a" + i]);
                        System.Diagnostics.Debug.WriteLine("expected: " + expectedResult[i]);
                        corrCount++;
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine(i + ":");
                        System.Diagnostics.Debug.WriteLine("operand1: " + operand1[i]);
                        System.Diagnostics.Debug.WriteLine("operand2: " + operand2[i]);
                        System.Diagnostics.Debug.WriteLine("computed: " + program.vTable["a" + i]);
                        System.Diagnostics.Debug.WriteLine("expected: " + expectedResult[i]);
                        Console.WriteLine("corr: " + expRes.GetVal() + ", computed: " + computedRes.GetVal());
                    }
                }
                else
                {
                    if (Math.Abs(computedRes.GetVal() - expRes.GetVal()) < 0.1)
                    {
                        System.Diagnostics.Debug.WriteLine(i + ":");
                        System.Diagnostics.Debug.WriteLine("operand1: " + operand1[i]);
                        System.Diagnostics.Debug.WriteLine("operand2: " + operand2[i]);
                        System.Diagnostics.Debug.WriteLine("computed: " + program.vTable["a" + i]);
                        System.Diagnostics.Debug.WriteLine("expected: " + expectedResult[i]);
                        corrCount++;
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine(i + ":");
                        System.Diagnostics.Debug.WriteLine("operand1: " + operand1[i]);
                        System.Diagnostics.Debug.WriteLine("operand2: " + operand2[i]);
                        System.Diagnostics.Debug.WriteLine("computed: " + program.vTable["a" + i]);
                        System.Diagnostics.Debug.WriteLine("expected: " + expectedResult[i]);
                        Console.WriteLine("corr: " + expRes.GetVal() + ", computed: " + computedRes.GetVal());
                    }
                }
            }
            Accuracy = (double)corrCount / commandNum;
            return(totalTime);
        }