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); }
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); }
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); }
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); }
private static bool IsTargetInGates(IList <BigGate> gates, BigWire outputWire) { foreach (BigGate gate in gates) { if (outputWire.TargetGate == gate) { return(true); } } return(false); }
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); }
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)); }
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)))); } }
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; }
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!"); } }
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))); }
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 )"); }
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); }
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!"); }
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; }
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; }
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; }
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; }
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; }
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); }
public void AddInputWire(BigWire wire) { InputWires.Add(wire); }
public BigGate(IList <BigWire> inputWires, BigWire outputWire, Operation operation, BigInteger prime) : this(inputWires, new List <BigWire>() { outputWire }, operation, prime) { }
private static bool IsTargetInGates(IList<BigGate> gates, BigWire outputWire) { foreach (BigGate gate in gates) { if (outputWire.TargetGate == gate) return true; } return false; }
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) { }
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; }
public void AddOutputWire(BigWire wire) { OutputWires.Add(wire); }
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; }