private void UpdateTextBoxes() { LatticeND lattice = viewModel.Lattice; textReduced00.Text = Util.FormatBigInt(lattice.ReducedVectors[0].values[0]); textReduced11.Text = Util.FormatBigInt(lattice.ReducedVectors[1].values[1]); if (!lattice.UseRowVectors) { textReduced01.Text = Util.FormatBigInt(lattice.ReducedVectors[0].values[1]); textReduced10.Text = Util.FormatBigInt(lattice.ReducedVectors[1].values[0]); SepColumnRed.Width = new GridLength(10); SepRowRed.Height = new GridLength(0); } else { textReduced01.Text = Util.FormatBigInt(lattice.ReducedVectors[1].values[0]); textReduced10.Text = Util.FormatBigInt(lattice.ReducedVectors[0].values[1]); SepColumnRed.Width = new GridLength(0); SepRowRed.Height = new GridLength(10); } textReducedLength0.Text = Util.FormatDoubleGUI(lattice.ReducedVectors[0].Length); textReducedLength1.Text = Util.FormatDoubleGUI(lattice.ReducedVectors[1].Length); }
public LatticeManualInputView(int newN, int newM, LatticeND currentLattice, bool allowOnly2x2, bool notAllowDetZero, int newMod, List <BigInteger> allowedNumbers) { n = newN; m = newM; mod = newMod; this.allowOnly2x2 = allowOnly2x2; this.notAllowDetZero = notAllowDetZero; this.allowedNumbers = allowedNumbers; Initialized += delegate { viewModel = (LatticeManualInputViewModel)DataContext; viewModel.NewLattice(n, m); if (n == currentLattice.N && m == currentLattice.M) { oldLattice = currentLattice; CBRowVectors.IsChecked = oldLattice.UseRowVectors; BuildLatticeGrid(currentLattice, false); } else { oldLattice = null; BuildLatticeGrid(null, false); } }; InitializeComponent(); }
public void GenerateNewLattice(int n, int m, BigInteger codomainStart, BigInteger codomainEnd) { UiServices.SetBusyState(); LatticeND newLattice = new LatticeND(n, m, false); //Zur Generierung von kritischen Gittern //while (Math.Round(newLattice.AngleReducedVectors, 0) != 60) //{ newLattice.GenerateRandomVectors(ReductionMethod == ReductionMethods.reduceGauss, codomainStart, codomainEnd); switch (ReductionMethod) { case ReductionMethods.reduceGauss: newLattice.GaussianReduce(); break; default: newLattice.LLLReduce(); break; } //} Lattice = newLattice; WriteHistoryForNewLattice(Languages.buttonGenerateNewLattice); NotifyPropertyChanged("Lattice"); }
private void ButtonTranspose_Click(object sender, RoutedEventArgs e) { LatticeND tempLattice = viewModel.SetLattice(latticeGrid, useRowVectors); tempLattice.Transpose(); int tmp = n; n = m; m = tmp; BuildLatticeGrid(tempLattice, false); }
private void Button_ClipboardInput(object sender, RoutedEventArgs e) { try { string str = Clipboard.GetText(); LatticeND tmp = Util.ConvertStringToLatticeND(str); if (allowOnly2x2 && (tmp.N != 2 || tmp.M != 2)) { throw new Exception(); } viewModel.Lattice = tmp; n = viewModel.Lattice.N; m = viewModel.Lattice.M; BuildLatticeGrid(viewModel.Lattice, false); } catch (Exception) { MessageBox.Show(Languages.errorParsingLattice, Languages.error, MessageBoxButton.OK, MessageBoxImage.Error); } }
public void ChangeVectorToSelectedPoint(Point point) { BigInteger tempVectorX = new BigInteger(Math.Round((point.X - y_line.X1) / PixelsPerPoint / scalingFactorX)); BigInteger tempVectorY = new BigInteger(Math.Round((x_line.Y1 - point.Y) / PixelsPerPoint / scalingFactorY)); VectorND tempNewVector = new VectorND(new[] { tempVectorX, tempVectorY }); //Bevor geändert werden kann, muss auf lineare Unabhängigkeit geprüft werden LatticeND tempLattice = selectedPointTag == 1 ? new LatticeND(new[] { tempNewVector, Lattice.Vectors[1] }, false) : new LatticeND(new[] { Lattice.Vectors[0], tempNewVector }, false); if (tempLattice.Determinant == 0) { return; } Lattice = tempLattice; Lattice.GaussianReduce(); NotifyPropertyChanged("Lattice"); AddCoordinateLinesAndBasisVectorsToCanvas(); UpdateCanvas(); }
public void SetLatticeManually(LatticeND newLattice) { UiServices.SetBusyState(); Lattice = new LatticeND(newLattice.Vectors, newLattice.UseRowVectors); switch (ReductionMethod) { case ReductionMethods.reduceGauss: Lattice.GaussianReduce(); break; default: Lattice.LLLReduce(); break; } WriteHistoryForNewLattice(Languages.buttonDefineNewLattice); NotifyPropertyChanged("Lattice"); }
public void GGHTest() { //Private key, public key and error vector taken from the book: //Hoffstein, J.; Pipher, J. & Silverman, J. H. //Axler, S. & Ribet, K. A. (Eds.) //'An Introduction to Mathematical Cryptography' //Springer, 2008 //Chapter 'The GGH public key cryptosystem' //Pages 384ff VectorND[] privateVectors = { new VectorND(new BigInteger[] { -97, 19, 19 }), new VectorND(new BigInteger[] { -36, 30, 86 }), new VectorND(new BigInteger[] { -184, -64, 78 }) }; LatticeND privateKey = new LatticeND(privateVectors, false); VectorND[] publicVectors = { new VectorND(new BigInteger[] { -4179163, -1882253, 583183 }), new VectorND(new BigInteger[] { -3184353, -1434201, 444361 }), new VectorND(new BigInteger[] { -5277320, -2376852, 736426 }) }; LatticeND publicKey = new LatticeND(publicVectors, false); VectorND errorVector = new VectorND(new BigInteger[] { -4, -3, -2 }); GGHModel ggh = new GGHModel(3, privateKey.ToMatrixND(), publicKey.ToMatrixND(), errorVector); for (int i = 0; i < 1000; i++) { string message = GenerateMessage(1, 100); VectorND cipher = ggh.Encrypt(message); string decryptedMessage = ggh.Decrypt(cipher); Assert.AreEqual(message, decryptedMessage); } }
public void NewLattice(int n, int m) { Lattice = new LatticeND(n, m, false); NotifyPropertyChanged("Lattice"); }
public LatticeManualInputViewModel() { Lattice = new LatticeND(2, 2, false); NotifyPropertyChanged("Lattice"); }
private void BuildLatticeGrid(LatticeND lattice, bool justUpdate) { int cols = !useRowVectors ? n : m; int rows = !useRowVectors ? m : n; if (!justUpdate) { latticeGrid.RowDefinitions.Clear(); latticeGrid.ColumnDefinitions.Clear(); latticeGrid.Children.Clear(); for (int i = 0; i < cols; i++) { latticeGrid.ColumnDefinitions.Add(new ColumnDefinition()); //Zusatzspalten für Zwischenräume if (i < cols - 1 && !useRowVectors) { latticeGrid.ColumnDefinitions.Add(new ColumnDefinition()); } } for (int i = 0; i < rows; i++) { latticeGrid.RowDefinitions.Add(new RowDefinition()); //Zusatzzeilen für Zwischenräume if (i < rows - 1 && useRowVectors) { latticeGrid.RowDefinitions.Add(new RowDefinition()); } } //Leere Labels für den Zwischenraum for (int i = 1; i < cols * 2 - 1; i = i + 2) { for (int j = 0; j < rows; j++) { Label label = new Label(); if (!useRowVectors) { label.MinWidth = 10; } else { label.MinHeight = 10; } Grid.SetColumn(label, !useRowVectors ? i : j); Grid.SetRow(label, !useRowVectors ? j : i); latticeGrid.Children.Add(label); } } for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { TextBox textBox = new TextBox { MinHeight = 25, MinWidth = 50 }; textBox.GotKeyboardFocus += (sender, args) => ((TextBox)sender).SelectAll(); textBox.TabIndex = !useRowVectors ? n * i + j : m * j + i; if (lattice != null) { textBox.Text = lattice.Vectors[!useRowVectors ? i : j].values[!useRowVectors ? j : i].ToString(CultureInfo.InvariantCulture); } textBox.TextChanged += ValidateLattice; Grid.SetColumn(textBox, !useRowVectors ? 2 * i : i); Grid.SetRow(textBox, !useRowVectors ? j : 2 * j); latticeGrid.Children.Add(textBox); } } } else { //Beim Transponieren muss nicht die komplette Grid neu gebaut werden, es reicht ein Update foreach (TextBox textBox in latticeGrid.Children.OfType <TextBox>()) { textBox.TextChanged -= ValidateLattice; int col = Grid.GetColumn(textBox); int row = Grid.GetRow(textBox); textBox.Text = lattice.Vectors[!useRowVectors ? col / 2 : row / 2].values[!useRowVectors ? row : col].ToString(CultureInfo.InvariantCulture); textBox.TextChanged += ValidateLattice; } } foreach (TextBox textBox in latticeGrid.Children.OfType <TextBox>().Where(textBox => textBox.TabIndex == 0)) { textBox.Focus(); break; } }
private void ButtonOK_Click(object sender, RoutedEventArgs e) { returnLattice = viewModel.SetLattice(latticeGrid, useRowVectors); DialogResult = true; }
private void ValidateLattice(object sender, TextChangedEventArgs e) { errorText.Text = ""; buttonOK.IsEnabled = true; buttonCopy.IsEnabled = true; foreach (TextBox textBox in latticeGrid.Children.Cast <Control>().Where(control => control is TextBox && !((TextBox)control).Text.Equals("")).Cast <TextBox>()) { textBox.Background = System.Windows.Media.Brushes.White; BigInteger tryParse; if (!BigInteger.TryParse(textBox.Text, out tryParse)) { errorText.Text = Languages.errorOnlyIntegersAllowed; buttonOK.IsEnabled = false; buttonCopy.IsEnabled = false; textBox.Background = System.Windows.Media.Brushes.Red; return; } if (mod != 0 && tryParse >= mod) { errorText.Text = string.Format("Es sind nur Zahlen bis {0} erlaubt", mod); buttonOK.IsEnabled = false; buttonCopy.IsEnabled = false; textBox.Background = System.Windows.Media.Brushes.Red; return; } if (allowedNumbers != null && !allowedNumbers.Exists(x => x == tryParse)) { string numbers = ""; for (int i = 0; i < allowedNumbers.Count; i++) { numbers += allowedNumbers[i]; if (i < allowedNumbers.Count - 1) { numbers += ", "; } } errorText.Text = string.Format("Es sind nur die Zahlen {0} erlaubt", numbers); buttonOK.IsEnabled = false; buttonCopy.IsEnabled = false; textBox.Background = System.Windows.Media.Brushes.Red; return; } } if (latticeGrid.Children.Cast <Control>().Any(control => control is TextBox && ((TextBox)control).Text.Equals(""))) { errorText.Text = Languages.errorNoLatticeEntered; buttonOK.IsEnabled = false; buttonCopy.IsEnabled = false; return; } LatticeND newLattice = viewModel.SetLattice(latticeGrid, CBRowVectors.IsChecked != null && (bool)CBRowVectors.IsChecked); if (oldLattice != null && newLattice.Equals(oldLattice)) { errorText.Text = Languages.errorSameLattice; return; } if (n == m && notAllowDetZero && newLattice.Determinant == 0) { errorText.Text = Languages.errorVectorsDependent; buttonOK.IsEnabled = false; buttonCopy.IsEnabled = false; } if (n > 1 && m > 1 && ((newLattice.Vectors[0].Length > newLattice.Vectors[1].Length && newLattice.Vectors[0].Length > 1000 * newLattice.Vectors[1].Length) || newLattice.Vectors[1].Length > newLattice.Vectors[0].Length && newLattice.Vectors[1].Length > 1000 * newLattice.Vectors[0].Length)) { errorText.Text = Languages.errorBadLattice; //buttonOK.IsEnabled = false; } }
public void LWETest() { //Test for single bit encryption //Example taken from my own master thesis (sadly in the absence of an alternative example) VectorND[] SVectors = { new VectorND(new BigInteger[] { 7, 7 }) }; LatticeND S = new LatticeND(SVectors, false); VectorND[] AVectors = { new VectorND(new BigInteger[] { 4, 3, 4, 0, 5 }), new VectorND(new BigInteger[] { 3, 3, 0, 7, 1 }) }; LatticeND A = new LatticeND(AVectors, false); VectorND[] eVectors = { new VectorND(new BigInteger[] { 7, 2, 4, 5, 2 }) }; LatticeND e = new LatticeND(eVectors, false); const int q = 9; LWEModel lwe = new LWEModel(S.ToMatrixND(), A.ToMatrixND(), e.ToMatrixND(), q, 1); VectorND[] rVectors = { new VectorND(new BigInteger[] { 1, 1, 0, 0, 0 }) }; LatticeND r = new LatticeND(rVectors, false); r.Transpose(); lwe.SetRandomVector(r.ToMatrixND()); //Cryptosystem correct? VectorND[] BVectors = { new VectorND(new BigInteger[] { 2, 8, 5, 0, 8 }) }; LatticeND B = new LatticeND(BVectors, false); Assert.AreEqual(B.ToMatrixND().ToString(), lwe.B.ToString()); //Encryption and decryption correct? //Message = 0 MatrixND messageMatrix = new MatrixND(1, 1); messageMatrix[0, 0] = 0; MatrixND cipher = lwe.Encrypt(messageMatrix); MatrixND expectedCipherMatrix = new MatrixND(1, 1); expectedCipherMatrix[0, 0] = 1; Assert.AreEqual(cipher.ToString(), expectedCipherMatrix.ToString()); MatrixND decryptedMessage = lwe.Decrypt(cipher); Assert.AreEqual(messageMatrix.ToString(), decryptedMessage.ToString()); //Message = 1 messageMatrix[0, 0] = 1; cipher = lwe.Encrypt(messageMatrix); expectedCipherMatrix[0, 0] = 5; Assert.AreEqual(cipher.ToString(), expectedCipherMatrix.ToString()); decryptedMessage = lwe.Decrypt(cipher); Assert.AreEqual(messageMatrix.ToString(), decryptedMessage.ToString()); }