コード例 #1
0
        Expression BooleanExpression()
        {
            var result = BooleanTerm();

            while (Accept(Symbol.S_OR))
            {
                result = new EBinaryOperation(result, BooleanTerm(), OperationType.OR);
            }
            return(result);
        }
コード例 #2
0
        Expression BooleanTerm()
        {
            var result = BooleanFactor();

            while (Accept(Symbol.S_AND))
            {
                result = new EBinaryOperation(result, BooleanFactor(), OperationType.AND);
            }
            return(result);
        }
コード例 #3
0
        Expression NumericExpression()
        {
            Expression exp = new ENumericLiteral(0, 0);

            ((ENumericLiteral)exp).needEnc = false;
            if (currToken.sym == Symbol.S_Plus)
            {
                NextToken();
            }
            else if (currToken.sym == Symbol.S_Minus)
            {
                NextToken();
                ENumericLiteral temp;
                if (IsNumeric(out temp))
                {
                    Numeric val = temp.GetValue();
                    exp = new ENumericLiteral(new Numeric(0, val.GetScaleBits()) - val);
                }
                else
                {
                    exp = new EBinaryOperation(exp, NumericTerm(), OperationType.Substraction);
                }
            }
            else
            {
                exp = NumericTerm();
            }
            while (currToken.sym == Symbol.S_Plus || currToken.sym == Symbol.S_Minus || currToken.sym == Symbol.S_BXOR)
            {
                if (currToken.sym == Symbol.S_Plus)
                {
                    NextToken();
                    exp = new EBinaryOperation(exp, NumericTerm(), OperationType.Addition);
                }
                else if (currToken.sym == Symbol.S_Minus)
                {
                    NextToken();
                    exp = new EBinaryOperation(exp, NumericTerm(), OperationType.Substraction);
                }
                else
                {
                    NextToken();
                    exp = new EBinaryOperation(exp, NumericTerm(), OperationType.XOR);
                }
            }
            return(exp);
        }
コード例 #4
0
        Expression NumericTerm()
        {
            Expression term = NumericFactor();

            while (currToken.sym == Symbol.S_Times || currToken.sym == Symbol.S_Divide || currToken.sym == Symbol.S_BAND)
            {
                if (currToken.sym == Symbol.S_Times)
                {
                    NextToken();
                    term = new EBinaryOperation(term, NumericFactor(), OperationType.Multiplication);
                }
                else if (currToken.sym == Symbol.S_Divide)
                {
                    NextToken();
                    term = new EBinaryOperation(term, new EUnaryOperation(NumericFactor(), OperationType.Inverse), OperationType.Multiplication);
                }
                else
                {
                    NextToken();
                    term = new EBinaryOperation(term, NumericFactor(), OperationType.AND);
                }
            }
            return(term);
        }
コード例 #5
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);
        }