Example #1
0
        // tranform intermediate code at runtime
        public static ICSequence TransformIntermediateCode(ETerminal condition, IntermediateCode code, Program program)
        {
            ICSequence re = new ICSequence();

            if (code is ICAssignment)
            {
                ICAssignment ica = (ICAssignment)code;
                if (ReferenceEquals(condition, null))
                {
                    re.Add(code);
                }
                else
                {
                    EVariable
                        result = ica.result,
                        temp1  = new EVariable(program, "$ICA_1_" + code.index),
                        temp2  = new EVariable(program, "$ICA_2_" + code.index),
                        temp3  = new EVariable(program, "$ICA_3_" + code.index),
                        temp4  = new EVariable(program, "$ICA_4_" + code.index);
                    ICAssignment
                    // condition * newResult + !condition * oldResult
                    // Command index is the same as the old command
                    // such that EVH and KH can receive the correct message.
                    // Commands generated at runtime cannot be run in parallel.
                        newCode      = new ICAssignment(ica.op, temp1, ica.operand1, ica.operand2, code.index),
                        notCondition = new ICAssignment(OperationType.NOT, temp2, condition, null, code.index),
                        term1Exp     = new ICAssignment(OperationType.Multiplication, temp3, condition, temp1, code.index),
                        term2Exp     = new ICAssignment(OperationType.Multiplication, temp4, temp2, result, code.index),
                        term3Exp     = new ICAssignment(OperationType.Addition, result, temp3, temp4, code.index);

                    re.AddRange(new ICAssignment[] { newCode, notCondition, term1Exp, term2Exp, term3Exp });
                }
            }
            else if (code is ICIfElse)
            {
                // for if-else statement, simply set the outerCondition
                ICIfElse icie = (ICIfElse)code;
                icie.outerCondition = condition;
                re.Add(icie);
            }
            else if (code is ICWhile)
            {
                ICWhile icw = (ICWhile)code;
                // outerCondition && while condition. This command will be executed very time condition codes are evaluated
                if (!ReferenceEquals(condition, null))
                {
                    EVariable temp1 = new EVariable(program, "&ICW_1_" + code.index);
                    icw.conditionCodes.Add(new ICAssignment(OperationType.AND, temp1, icw.condition, condition, icw.conditionCodes[icw.conditionCodes.Count - 1].index));
                    icw.condition = temp1;
                }
                ICSequence icwCodes = new ICSequence();
                foreach (var entry in icw.codes.GetCodes())
                {
                    // if statement is Assignment and result is a temporarily variable, no need to transform
                    if (entry is ICAssignment && ((ICAssignment)entry).result.isTemporary)
                    {
                        icwCodes.Add(entry);
                        continue;
                    }
                    icwCodes.AddRange(TransformIntermediateCode(icw.condition, entry, program));
                }
                icw.codes = icwCodes;
                re.Add(icw);
            }
            return(re);
        }
Example #2
0
        private ICSequence[] EncBlock(ICSequence codes)
        {
            var toKH  = new ICSequence();
            var toEVH = new ICSequence();

            foreach (var ic in codes.GetCodes())
            {
                if (ic is ICAssignment)
                {
                    ICAssignment ica = (ICAssignment)ic;
                    Numeric      key1 = null, key2 = null;
                    toKH.Add(new ICAssignment(ica, ref key1, ref key2, PartyType.KH));
                    toEVH.Add(new ICAssignment(ica, ref key1, ref key2, PartyType.EVH));
                }
                else if (ic is ICWhile)
                {
                    ICWhile icw = (ICWhile)ic, icwEVH = new ICWhile(), icwKH = new ICWhile();
                    icwEVH.index = icw.index;
                    icwKH.index  = icw.index;
                    if (icw.condition is ENumericLiteral)
                    {
                        var cond = (ENumericLiteral)icw.condition;
                        if (!cond.needEnc)
                        {
                            var val = new Numeric(cond.GetValue());
                            val.SetEncType(EncryptionType.None);
                            icwEVH.condition = new ENumericLiteral(val);
                            ((ENumericLiteral)icwEVH.condition).needEnc = false;
                            icwKH.condition = new ENumericLiteral(val);
                            ((ENumericLiteral)icwKH.condition).needEnc = false;
                        }
                        else
                        {
                            var keyTemp = Utility.NextUnsignedNumeric(0, 1);
                            icwEVH.condition = new ENumericLiteral(GetValue(icw.condition) ^ keyTemp);
                            icwKH.condition  = new ENumericLiteral(keyTemp);
                        }
                    }
                    else
                    {
                        icwEVH.condition = icw.condition;
                        icwKH.condition  = icw.condition;
                    }
                    var encCondCodes = EncBlock(icw.conditionCodes);
                    icwEVH.conditionCodes.AddRange(encCondCodes[0]);
                    icwKH.conditionCodes.AddRange(encCondCodes[1]);
                    var encCodes = EncBlock(icw.codes);
                    icwEVH.codes.AddRange(encCodes[0]);
                    icwKH.codes.AddRange(encCodes[1]);
                    toEVH.Add(icwEVH);
                    toKH.Add(icwKH);
                }
                else
                {
                    ICIfElse icie = (ICIfElse)ic, icieEVH = new ICIfElse(), icieKH = new ICIfElse();
                    icieEVH.revealCond = icie.revealCond;
                    icieKH.revealCond  = icie.revealCond;
                    icieEVH.prob       = icie.prob;
                    icieKH.prob        = icie.prob;
                    icieEVH.index      = icie.index;
                    icieKH.index       = icie.index;
                    if (icie.condition is ENumericLiteral)
                    {
                        var cond = (ENumericLiteral)icie.condition;
                        if (!cond.needEnc)
                        {
                            var val = new Numeric(cond.GetValue());
                            val.SetEncType(EncryptionType.None);
                            icieEVH.condition = new ENumericLiteral(val);
                            ((ENumericLiteral)icieEVH.condition).needEnc = false;
                            icieKH.condition = new ENumericLiteral(val);
                            ((ENumericLiteral)icieKH.condition).needEnc = false;
                        }
                        else
                        {
                            var keyTemp = Utility.NextUnsignedNumeric(0, 1);
                            icieEVH.condition = new ENumericLiteral(GetValue(icie.condition) ^ keyTemp);
                            icieKH.condition  = new ENumericLiteral(keyTemp);
                        }
                    }
                    else
                    {
                        icieEVH.condition = icie.condition;
                        icieKH.condition  = icie.condition;
                    }
                    var encCondCodes = EncBlock(icie.conditionCodes);
                    icieEVH.conditionCodes.AddRange(encCondCodes[0]);
                    icieKH.conditionCodes.AddRange(encCondCodes[1]);
                    var encIfCodes = EncBlock(icie.codesIf);
                    icieEVH.codesIf = encIfCodes[0];
                    icieKH.codesIf  = encIfCodes[1];
                    var encElseCodes = EncBlock(icie.codesElse);
                    icieEVH.codesElse = encElseCodes[0];
                    icieKH.codesElse  = encElseCodes[1];
                    toEVH.Add(icieEVH);
                    toKH.Add(icieKH);
                }
            }
            return(new ICSequence[] { toEVH, toKH });
        }
Example #3
0
        public static ICSequence TranslateExpression(Expression exp, EVariable place, Program program)
        {
            System.Diagnostics.Debug.Assert(!(exp is ETerminal));
            ICSequence re = new ICSequence();

            if (exp is EBinaryOperation)
            {
                EBinaryOperation ebo = (EBinaryOperation)exp;
                ETerminal        place1, place2;
                ICSequence       code1 = null, code2 = null;
                if (!(ebo.Operand1 is ETerminal))
                {
                    place1 = new EVariable(program);
                    code1  = TranslateExpression(ebo.Operand1, (EVariable)place1, program);
                }
                else
                {
                    place1 = (ETerminal)ebo.Operand1;
                }
                if (!(ebo.Operand2 is ETerminal))
                {
                    place2 = new EVariable(program);
                    code2  = TranslateExpression(ebo.Operand2, (EVariable)place2, program);
                }
                else
                {
                    place2 = (ETerminal)ebo.Operand2;
                }
                IntermediateCode lastIC = new ICAssignment(ebo.Operation, place, place1, place2);
                if (!ReferenceEquals(code1, null))
                {
                    re.AddRange(code1);
                    //addDependency(code1[code1.Count - 1], lastIC, DependencyType.Flow);
                }
                if (!ReferenceEquals(code2, null))
                {
                    re.AddRange(code2);
                    //addDependency(code2[code2.Count - 1], lastIC, DependencyType.Flow);
                }
                re.Add(lastIC);
            }
            else if (exp is EUnaryOperation)
            {
                EUnaryOperation euo = (EUnaryOperation)exp;
                ETerminal       place1;
                ICSequence      code1 = null;
                if (!(euo.Operand is ETerminal))
                {
                    place1 = new EVariable(program);
                    code1  = TranslateExpression(euo.Operand, (EVariable)place1, program);
                }
                else
                {
                    place1 = (ETerminal)euo.Operand;
                }
                IntermediateCode lastIC = new ICAssignment(euo.Operation, place, place1, null);
                if (!ReferenceEquals(code1, null))
                {
                    re.AddRange(code1);
                    //addDependency(code1[code1.Count - 1], lastIC, DependencyType.Flow);
                }
                re.Add(lastIC);
            }
            return(re);
        }
Example #4
0
        // used when encrypting the program
        public ICAssignment(ICAssignment ica, ref Numeric key1, ref Numeric key2, PartyType party)
        {
            index  = ica.index;
            op     = ica.op;
            result = ica.result;
            if (ica.operand1 is ENumericLiteral)
            {
                var opr1 = (ENumericLiteral)ica.operand1;
                switch (party)
                {
                case PartyType.EVH:
                    if (!opr1.needEnc)
                    {
                        var val = new Numeric(opr1.GetValue());
                        val.SetEncType(EncryptionType.None);
                        operand1 = new ENumericLiteral(val);
                        ((ENumericLiteral)operand1).needEnc = false;
                    }
                    else
                    {
                        switch (Config.DefaultEnc)
                        {
                        case EncryptionType.AddMod:
                            operand1 = new ENumericLiteral(opr1.GetValue() + key1);
                            break;

                        case EncryptionType.XOR:
                            operand1 = new ENumericLiteral(opr1.GetValue() ^ key1);
                            break;

                        default:
                            operand1 = new ENumericLiteral(opr1.GetValue());
                            break;
                        }
                    }
                    break;

                case PartyType.KH:
                    if (!opr1.needEnc)
                    {
                        var val = new Numeric(opr1.GetValue());
                        val.SetEncType(EncryptionType.None);
                        operand1 = new ENumericLiteral(val);
                        ((ENumericLiteral)operand1).needEnc = false;
                    }
                    else
                    {
                        key1     = Utility.NextUnsignedNumeric(opr1.GetValue().GetScaleBits());
                        operand1 = new ENumericLiteral(key1);
                    }
                    break;

                default:
                    throw new ArgumentException();
                }
            }
            else
            {
                operand1 = ica.operand1;
            }

            if (ica.operand2 is ENumericLiteral)
            {
                var opr2 = (ENumericLiteral)ica.operand2;
                switch (party)
                {
                case PartyType.EVH:
                    if (!opr2.needEnc)
                    {
                        var val = new Numeric(opr2.GetValue());
                        val.SetEncType(EncryptionType.None);
                        operand2 = new ENumericLiteral(val);
                        ((ENumericLiteral)operand2).needEnc = false;
                    }
                    else
                    {
                        switch (Config.DefaultEnc)
                        {
                        case EncryptionType.AddMod:
                            operand2 = new ENumericLiteral(opr2.GetValue() + key2);
                            break;

                        case EncryptionType.XOR:
                            operand2 = new ENumericLiteral(opr2.GetValue() ^ key2);
                            break;

                        default:
                            operand2 = new ENumericLiteral(opr2.GetValue());
                            break;
                        }
                    }

                    break;

                case PartyType.KH:
                    if (!opr2.needEnc)
                    {
                        var val = new Numeric(opr2.GetValue());
                        val.SetEncType(EncryptionType.None);
                        operand2 = new ENumericLiteral(val);
                        ((ENumericLiteral)operand2).needEnc = false;
                    }
                    else
                    {
                        key2     = Utility.NextUnsignedNumeric(opr2.GetValue().GetScaleBits());
                        operand2 = new ENumericLiteral(key2);
                    }
                    break;

                default:
                    throw new ArgumentException();
                }
            }
            else
            {
                operand2 = ica.operand2;
            }
        }