Example #1
0
        private BigWire CreateEqualsGate(string line, BigWire firstWire)
        {
            int equalsIndex = line.IndexOf("==");

            Debug.Assert(equalsIndex != -1);
            if (firstWire == null)
            {
                firstWire = GetWireFromExpression(line.Substring(0, equalsIndex));
            }

            var secondWire  = GetWireFromExpression(line.Substring(equalsIndex + 2));
            var compareWire = new BigWire();

            SetGate(new List <BigWire>()
            {
                firstWire, secondWire
            }, compareWire, Operation.Sub);
            var normilizedWire = new BigWire();

            SetGate(new List <BigWire>()
            {
                compareWire, compareWire
            }, normilizedWire, Operation.Div);
            var resultWire = new BigWire();

            SetGate(new List <BigWire>()
            {
                new BigWire(new BigZp(prime, 1)), normilizedWire
            }, resultWire, Operation.Sub);
            return(resultWire);
        }
Example #2
0
        private BigWire CreateMulGates(BigWire firstWireOrNull, Operation firstOpOrNull, string line)
        {
            var tokens     = new List <string>();
            var operations = new List <Operation>();

            SplitMul(line, tokens, operations);
            Debug.Assert(tokens.Count >= 2);
            var firstWire = firstWireOrNull == null?GetWireFromString(tokens[0]) : firstWireOrNull;

            Operation currOperation = firstOpOrNull;

            if (currOperation == Operation.None)
            {
                currOperation = operations[0];
            }

            for (int i = firstWireOrNull == null ? 1 : 0; i < tokens.Count; i++)
            {
                var secondWire = GetWireFromString(tokens[i]);
                var outputWire = new BigWire();
                SetGate(new List <BigWire>()
                {
                    firstWire, secondWire
                }, outputWire, currOperation);
                firstWire = outputWire;
                if (operations.Count <= i)
                {
                    break;
                }

                currOperation = operations[i];
            }
            return(firstWire);
        }
Example #3
0
        private BigWire GetWireFromCondition(string line, BigWire conditionWire)
        {
            int index1 = line.IndexOf("?");
            int index2 = line.IndexOf(":");

            Debug.Assert(index1 != -1 && index2 != -1 && index1 < index2);

            // get all 3 expression wires
            if (conditionWire == null)
            {
                conditionWire = GetWireFromExpression(line.Substring(0, index1));
            }

            var firstWire  = GetWireFromExpression(line.Substring(index1 + 1, index2 - (index1 + 1)));
            var secondWire = GetWireFromExpression(line.Substring(index2 + 1));

            // normalizedConditionWire = conditionWire/conditionWire
            var normalizedConditionWire = new BigWire();

            SetGate(new List <BigWire>()
            {
                conditionWire, conditionWire
            }, normalizedConditionWire, Operation.Div);

            // reverseCondition = 1 - normalizedConditionWire
            var reverseCondition = new BigWire();

            SetGate(new List <BigWire>()
            {
                new BigWire(new BigZp(prime, 1)), normalizedConditionWire
            }, reverseCondition, Operation.Sub);

            // firstWireMultiplied = firstWire * normalizedConditionWire
            var firstWireMultiplied = new BigWire();

            SetGate(new List <BigWire>()
            {
                firstWire, normalizedConditionWire
            }, firstWireMultiplied, Operation.Mul);

            // secondWireMultiplied = secondWire * reverseCondition
            var secondWireMultiplied = new BigWire();

            SetGate(new List <BigWire>()
            {
                secondWire, reverseCondition
            }, secondWireMultiplied, Operation.Mul);

            // result = firstWireMultiplied + secondWireMultiplied
            var resultWire = new BigWire();

            SetGate(new List <BigWire>()
            {
                firstWireMultiplied, secondWireMultiplied
            }, resultWire, Operation.Add);
            return(resultWire);
        }
Example #4
0
        private BigWire CreateAddGates(string line, BigWire firstWire)
        {
            var op = GetOperation(line.Substring(0, 1));

            line = line.Substring(1);

            while (op == Operation.Sub)
            {
                int firstIndex = GetFirstParamterIndex(line);
                if (firstIndex == -1)
                {
                    break;
                }

                if (firstIndex == 0 && !line.StartsWith("(") || firstIndex == -1)
                {
                    break;
                }

                if (getIndexOfDivOrMul(line, 0) == firstIndex)
                {
                    break;
                }

                bool    isPar      = line.StartsWith("(");
                BigWire secondWire = isPar ? CreateParenthesesWire(line, false) : GetWireFromExpression(line.Substring(0, firstIndex));
                BigWire outputWire = new BigWire();

                SetGate(new List <BigWire>()
                {
                    firstWire, secondWire
                }, outputWire, op);
                line = line.Substring(isPar ? FindEndingParantheses(line) + 1 : firstIndex);

                if (line.Length == 0)
                {
                    return(outputWire);
                }

                op        = GetOperation(line.Substring(0, 1));
                line      = line.Substring(1);
                firstWire = outputWire;
            }
            if (op != Operation.Add && op != Operation.Sub)
            {
                return(CreateMulGate(firstWire, op, line));
            }

            var sndWire = GetWireFromExpression(line);
            var outWire = new BigWire();

            SetGate(new List <BigWire>()
            {
                firstWire, sndWire
            }, outWire, op);
            return(outWire);
        }
Example #5
0
 private static bool IsTargetInGates(IList <BigGate> gates, BigWire outputWire)
 {
     foreach (BigGate gate in gates)
     {
         if (outputWire.TargetGate == gate)
         {
             return(true);
         }
     }
     return(false);
 }
Example #6
0
        public BigWire Clone()
        {
            var wire = new BigWire(InputIndex, IsOutput, ConstValue);

            wire.SourceGate = sourceGate;
            wire.TargetGate = targetGate;
            if (wire.SourceGate != null)
            {
                wire.SourceGate.AddOutputWire(wire);
            }
            return(wire);
        }
Example #7
0
        private BigWire CreateMulGate(BigWire firstWireOrNull, Operation firstOpOrNull, string line)
        {
            int addIndex = FindCorrectIndex(line, SearchFor.AddMin);

            if (addIndex == -1)
            {
                return(CreateMulGates(firstWireOrNull, firstOpOrNull, line));
            }

            var firstWire = CreateMulGates(firstWireOrNull, firstOpOrNull, line.Substring(0, addIndex));

            return(CreateAddGates(line.Substring(addIndex), firstWire));
        }
Example #8
0
        private BigWire GetWireFromExpression(string line)
        {
            int mulIndex     = FindCorrectIndex(line, SearchFor.DivMul);
            int addIndex     = FindCorrectIndex(line, SearchFor.AddMin);
            int equalsIndex  = FindCorrectIndex(line, SearchFor.Equal);
            int questionMark = FindCorrectIndex(line, SearchFor.QuestionMark);
            int parIndex     = line.IndexOf("(");

            if (questionMark != -1 &&
                (new Regex(conditionalTerm)).IsMatch(line) &&
                parIndex != 0 &&
                (addIndex == -1 || addIndex > questionMark ||
                 addIndex > equalsIndex && addIndex < questionMark &&
                 !line.Substring(equalsIndex, questionMark - equalsIndex).Contains("(")) &&
                (mulIndex == -1 || mulIndex > questionMark || mulIndex > equalsIndex &&
                 mulIndex < questionMark && !line.Substring(equalsIndex, questionMark - equalsIndex).Contains("(")))
            {
                return(GetWireFromCondition(line));
            }

            if (mulIndex == -1 && addIndex == -1 && equalsIndex == -1 && parIndex != 0)
            {
                return(GetWireFromString(line));
            }

            if (addIndex > 0 && parIndex != 0 && equalsIndex == -1 && (mulIndex == -1 || addIndex < mulIndex))             //handle add gate
            {
                BigWire firstWire = GetWireFromString(line.Substring(0, addIndex));
                return(CreateAddGates(line.Substring(addIndex), firstWire));
            }

            if (parIndex == 0)
            {
                return(CreateParenthesesWire(line, true));
            }

            if (equalsIndex != -1)
            {
                return(CreateEqualsGate(line));
            }

            if (addIndex == -1)
            {
                return(CreateMulGate(null, 0, line));
            }
            else
            {
                return(CreateAddGates(line.Substring(addIndex),
                                      CreateMulGate(null, 0, line.Substring(0, addIndex))));
            }
        }
Example #9
0
        private void SetGate(IList <BigWire> inputWires, BigWire outputWire, Operation op)
        {
            BigGate gate = new BigGate(inputWires, new List <BigWire>()
            {
                outputWire
            }, op, prime);

            foreach (BigWire wire in inputWires)
            {
                wire.TargetGate = gate;
            }

            outputWire.SourceGate = gate;
        }
Example #10
0
        public virtual void Parse()
        {
            StreamReader reader;

            if (File.Exists(pathOrString))
            {
                reader = new StreamReader(pathOrString);
            }
            else
            {
                reader = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(pathOrString)));
            }

            varsToWires = new Dictionary <string, BigWire>();
            string line       = null;
            int    lineNumber = 0;

            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }

                int commentIndex = line.IndexOf("//");
                if (commentIndex != -1)
                {
                    line = line.Substring(0, commentIndex);
                }

                try
                {
                    ReadLine(line.Trim().Replace("\t", ""));                            // remove white spaces
                    lineNumber++;
                }
                catch (Exception e)
                {
                    throw new Exception("Error in line " + lineNumber + "\n" + e.Message);
                }
            }
            outputWire          = GetWireFromString(outputVar);
            outputWire.IsOutput = true;
            circuit             = CreateCircuit(prime, inputs);
            if (circuit == null)
            {
                throw new Exception("Error in parsing the circuit!");
            }
        }
Example #11
0
        private BigWire GetWireFromString(string st)
        {
            if (st.StartsWith("("))
            {
                return(CreateParenthesesWire(st, false));
            }

            BigWire wire = null;

            if (varsToWires.ContainsKey(st))
            {
                wire = varsToWires[st];
                if (wire.IsOutput && wire.SourceGate == null)
                {
                    throw new Exception("Variable " + st + " was not initialized");
                }
            }

            if (wire != null)
            {
                if (wire.TargetGate != null)
                {
                    return(wire.Clone());
                }
                else
                {
                    return(wire);
                }
            }

            int value;

            try
            {
                value = Convert.ToInt32(st);
            }
            catch (Exception)
            {
                if (st.Contains("("))
                {
                    throw new Exception("No operation before opening ( in expression: " + st);
                }
                throw new Exception("variable '" + st + "' is not defined");
            }
            return(new BigWire(new BigZp(prime, value)));
        }
Example #12
0
        private BigWire CreateParenthesesWire(string line, bool keepCalculating)
        {
            int     parEndIndex = FindEndingParantheses(line);
            BigWire firstWire   = GetWireFromExpression(line.Substring(1, parEndIndex - 1));

            line = line.Substring(parEndIndex + 1);

            if (!keepCalculating)
            {
                return(firstWire);
            }

            if (line.StartsWith("+") || line.StartsWith("-"))
            {
                return(CreateAddGates(line, firstWire));
            }

            if (line.StartsWith("*"))
            {
                return(CreateMulGate(firstWire, Operation.Mul, line.Substring(1)));
            }

            if (line.StartsWith("/"))
            {
                return(CreateMulGate(firstWire, Operation.Div, line.Substring(1)));
            }

            if (line.Equals(""))
            {
                return(firstWire);
            }

            if (line.StartsWith("=="))
            {
                return(CreateEqualsGate(line, firstWire));
            }

            if (line.StartsWith("?"))
            {
                return(GetWireFromCondition(line, firstWire));
            }

            throw new Exception("No operation after closing )");
        }
Example #13
0
        private bool ReadInputOutputDecleration(string line, bool isInput)
        {
            var index = line.IndexOf("=");

            Debug.Assert(index != -1);
            var tokens = line.Substring(index + 1).Split(new char[] { ',' });

            for (int i = 0; i < tokens.Length; i++)
            {
                var token = tokens[i].Trim();
                CheckVariableSynthax(token);
                var wire = new BigWire(i, isInput);

                if (varsToWires.ContainsKey(token))
                {
                    if (isInput)
                    {
                        varsToWires[token].InputIndex = index;
                    }
                    else
                    {
                        varsToWires[token].IsOutput = true;
                    }
                }
                else
                {
                    varsToWires[token] = wire;
                }

                if (!isInput)
                {
                    Debug.Assert(outputVar == null);
                    outputVar = token;
                }
                else
                {
                    inputs.Add(token);
                }
            }
            return(true);
        }
Example #14
0
        public virtual void Parse()
        {
            StreamReader reader;
            if (File.Exists(pathOrString))
                reader = new StreamReader(pathOrString);
            else
                reader = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(pathOrString)));

            varsToWires = new Dictionary<string, BigWire>();
            string line = null;
            int lineNumber = 0;
            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                if (line == null)
                    break;

                int commentIndex = line.IndexOf("//");
                if (commentIndex != -1)
                    line = line.Substring(0, commentIndex);

                try
                {
                    ReadLine(line.Trim().Replace("\t", ""));	// remove white spaces
                    lineNumber++;
                }
                catch (Exception e)
                {
                    throw new Exception("Error in line " + lineNumber + "\n" + e.Message);
                }
            }
            outputWire = GetWireFromString(outputVar);
            outputWire.IsOutput = true;
            circuit = CreateCircuit(prime, inputs);
            if (circuit == null)
                throw new Exception("Error in parsing the circuit!");
        }
Example #15
0
        private BigWire CreateEqualsGate(string line, BigWire firstWire)
        {
            int equalsIndex = line.IndexOf("==");
            Debug.Assert(equalsIndex != -1);
            if (firstWire == null)
                firstWire = GetWireFromExpression(line.Substring(0, equalsIndex));

            var secondWire = GetWireFromExpression(line.Substring(equalsIndex + 2));
            var compareWire = new BigWire();
            SetGate(new List<BigWire>() { firstWire, secondWire }, compareWire, Operation.Sub);
            var normilizedWire = new BigWire();
            SetGate(new List<BigWire>() { compareWire, compareWire }, normilizedWire, Operation.Div);
            var resultWire = new BigWire();
            SetGate(new List<BigWire>() { new BigWire(new BigZp(prime, 1)), normilizedWire }, resultWire, Operation.Sub);
            return resultWire;
        }
Example #16
0
        private void SetGate(IList<BigWire> inputWires, BigWire outputWire, Operation op)
        {
            BigGate gate = new BigGate(inputWires, new List<BigWire>() { outputWire }, op, prime);
            foreach (BigWire wire in inputWires)
                wire.TargetGate = gate;

            outputWire.SourceGate = gate;
        }
Example #17
0
        private bool ReadInputOutputDecleration(string line, bool isInput)
        {
            var index = line.IndexOf("=");
            Debug.Assert(index != -1);
            var tokens = line.Substring(index + 1).Split(new char[] { ',' });

            for (int i = 0; i < tokens.Length; i++)
            {
                var token = tokens[i].Trim();
                CheckVariableSynthax(token);
                var wire = new BigWire(i, isInput);

                if (varsToWires.ContainsKey(token))
                {
                    if (isInput)
                        varsToWires[token].InputIndex = index;
                    else
                        varsToWires[token].IsOutput = true;
                }
                else
                    varsToWires[token] = wire;

                if (!isInput)
                {
                    Debug.Assert(outputVar == null);
                    outputVar = token;
                }
                else
                    inputs.Add(token);
            }
            return true;
        }
Example #18
0
        private BigWire GetWireFromCondition(string line, BigWire conditionWire)
        {
            int index1 = line.IndexOf("?");
            int index2 = line.IndexOf(":");
            Debug.Assert(index1 != -1 && index2 != -1 && index1 < index2);

            // get all 3 expression wires
            if (conditionWire == null)
                conditionWire = GetWireFromExpression(line.Substring(0, index1));

            var firstWire = GetWireFromExpression(line.Substring(index1 + 1, index2 - (index1 + 1)));
            var secondWire = GetWireFromExpression(line.Substring(index2 + 1));

            // normalizedConditionWire = conditionWire/conditionWire
            var normalizedConditionWire = new BigWire();
            SetGate(new List<BigWire>() { conditionWire, conditionWire }, normalizedConditionWire, Operation.Div);

            // reverseCondition = 1 - normalizedConditionWire
            var reverseCondition = new BigWire();
            SetGate(new List<BigWire>() { new BigWire(new BigZp(prime, 1)), normalizedConditionWire }, reverseCondition, Operation.Sub);

            // firstWireMultiplied = firstWire * normalizedConditionWire
            var firstWireMultiplied = new BigWire();
            SetGate(new List<BigWire>() { firstWire, normalizedConditionWire }, firstWireMultiplied, Operation.Mul);

            // secondWireMultiplied = secondWire * reverseCondition
            var secondWireMultiplied = new BigWire();
            SetGate(new List<BigWire>() { secondWire, reverseCondition }, secondWireMultiplied, Operation.Mul);

            // result = firstWireMultiplied + secondWireMultiplied
            var resultWire = new BigWire();
            SetGate(new List<BigWire>() { firstWireMultiplied, secondWireMultiplied }, resultWire, Operation.Add);
            return resultWire;
        }
Example #19
0
        private BigWire CreateMulGates(BigWire firstWireOrNull, Operation firstOpOrNull, string line)
        {
            var tokens = new List<string>();
            var operations = new List<Operation>();

            SplitMul(line, tokens, operations);
            Debug.Assert(tokens.Count >= 2);
            var firstWire = firstWireOrNull == null ? GetWireFromString(tokens[0]) : firstWireOrNull;
            Operation currOperation = firstOpOrNull;

            if (currOperation == Operation.None)
                currOperation = operations[0];

            for (int i = firstWireOrNull == null ? 1 : 0; i < tokens.Count; i++)
            {
                var secondWire = GetWireFromString(tokens[i]);
                var outputWire = new BigWire();
                SetGate(new List<BigWire>() { firstWire, secondWire }, outputWire, currOperation);
                firstWire = outputWire;
                if (operations.Count <= i)
                    break;

                currOperation = operations[i];
            }
            return firstWire;
        }
Example #20
0
        private BigWire CreateMulGate(BigWire firstWireOrNull, Operation firstOpOrNull, string line)
        {
            int addIndex = FindCorrectIndex(line, SearchFor.AddMin);
            if (addIndex == -1)
                return CreateMulGates(firstWireOrNull, firstOpOrNull, line);

            var firstWire = CreateMulGates(firstWireOrNull, firstOpOrNull, line.Substring(0, addIndex));
            return CreateAddGates(line.Substring(addIndex), firstWire);
        }
Example #21
0
 public void AddInputWire(BigWire wire)
 {
     InputWires.Add(wire);
 }
Example #22
0
 public BigGate(IList <BigWire> inputWires, BigWire outputWire, Operation operation, BigInteger prime)
     : this(inputWires, new List <BigWire>() { outputWire }, operation, prime)
 {
 }
Example #23
0
 private static bool IsTargetInGates(IList<BigGate> gates, BigWire outputWire)
 {
     foreach (BigGate gate in gates)
     {
         if (outputWire.TargetGate == gate)
             return true;
     }
     return false;
 }
Example #24
0
        private readonly BigInteger prime; // the prime number

        #endregion Fields

        #region Constructors

        public BigGate(IList<BigWire> inputWires, BigWire outputWire, Operation operation, BigInteger prime)
            : this(inputWires, new List<BigWire>() { outputWire }, operation, prime)
        {
        }
Example #25
0
        private BigWire CreateAddGates(string line, BigWire firstWire)
        {
            var op = GetOperation(line.Substring(0, 1));
            line = line.Substring(1);

            while (op == Operation.Sub)
            {
                int firstIndex = GetFirstParamterIndex(line);
                if (firstIndex == -1)
                    break;

                if (firstIndex == 0 && !line.StartsWith("(") || firstIndex == -1)
                    break;

                if (getIndexOfDivOrMul(line, 0) == firstIndex)
                    break;

                bool isPar = line.StartsWith("(");
                BigWire secondWire = isPar ? CreateParenthesesWire(line, false) : GetWireFromExpression(line.Substring(0, firstIndex));
                BigWire outputWire = new BigWire();

                SetGate(new List<BigWire>() { firstWire, secondWire }, outputWire, op);
                line = line.Substring(isPar ? FindEndingParantheses(line) + 1 : firstIndex);

                if (line.Length == 0)
                    return outputWire;

                op = GetOperation(line.Substring(0, 1));
                line = line.Substring(1);
                firstWire = outputWire;
            }
            if (op != Operation.Add && op != Operation.Sub)
                return CreateMulGate(firstWire, op, line);

            var sndWire = GetWireFromExpression(line);
            var outWire = new BigWire();
            SetGate(new List<BigWire>() { firstWire, sndWire }, outWire, op);
            return outWire;
        }
Example #26
0
 public void AddInputWire(BigWire wire)
 {
     InputWires.Add(wire);
 }
Example #27
0
 public void AddOutputWire(BigWire wire)
 {
     OutputWires.Add(wire);
 }
Example #28
0
 public BigWire Clone()
 {
     var wire = new BigWire(InputIndex, IsOutput, ConstValue);
     wire.SourceGate = sourceGate;
     wire.TargetGate = targetGate;
     if (wire.SourceGate != null)
         wire.SourceGate.AddOutputWire(wire);
     return wire;
 }
Example #29
0
 public void AddOutputWire(BigWire wire)
 {
     OutputWires.Add(wire);
 }