source: trunk/CrypPlugins/DiscreteLogarithm/DiscreteLogarithm.cs @ 1785

Last change on this file since 1785 was 1785, checked in by Sven Rech, 11 years ago

added discrete logarithm plugin
very, very simple enumeration algorithm.

File size: 8.3 KB
Line 
1/*                             
2   Copyright 2010 Team CrypTool (Sven Rech), Uni Duisburg-Essen
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using Cryptool.PluginBase;
22using Cryptool.PluginBase.Miscellaneous;
23using System.ComponentModel;
24using System.Security.Cryptography;
25using System.Threading;
26using System.Collections;
27using System.Numerics;
28using Cryptool.PluginBase.IO;
29
30namespace Cryptool.Plugins.DiscreteLogarithm
31{
32    [Author("Sven Rech", null, "Uni Duisburg-Essen", "http://www.uni-due.de")]
33    [PluginInfo(false, "Discrete Logarithm", "Calculates the discrete logarithm", null, "DiscreteLogarithm/icon.png")]
34    /// <summary>
35    /// This plugin calculates the discrete logarithm of the input.
36    /// The input contains of a the BigInteger value and base and the modulo value to determine the residue class
37    /// </summary>
38    class DiscreteLogarithm : IThroughput
39    {
40        #region private members
41
42        private DiscreteLogarithmSettings settings = new DiscreteLogarithmSettings();
43        private BigInteger inputValue;
44        private BigInteger inputBase;
45        private BigInteger inputMod;
46        private BigInteger outputLogarithm;
47
48        #endregion
49
50        #region events
51
52        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
53        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
54        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
55        public event StatusChangedEventHandler OnPluginStatusChanged;
56
57        #endregion
58
59        #region public
60       
61        /// <summary>
62        /// Notify that a property changed
63        /// </summary>
64        /// <param name="name">property name</param>
65        public void OnPropertyChanged(string name)
66        {
67            EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
68        }
69
70        /// <summary>
71        /// Gets/Sets the Settings of this plugin
72        /// </summary>
73        public ISettings Settings
74        {
75            get { return this.settings; }
76            set { this.settings = (DiscreteLogarithmSettings)value; }
77        }
78
79        /// <summary>
80        /// Get the Presentation of this plugin
81        /// </summary>
82        public System.Windows.Controls.UserControl Presentation
83        {
84            get { return null; }
85        }
86
87        /// <summary>
88        /// Get the QuickWatchRepresentation of this plugin
89        /// </summary>
90        public System.Windows.Controls.UserControl QuickWatchPresentation
91        {
92            get { return null; }
93        }
94
95        /// <summary>
96        /// Called by the environment before execution
97        /// </summary>
98        public void PreExecution()
99        {
100        }
101
102        /// <summary>
103        /// Called by the environment to execute this plugin
104        /// </summary>
105        public void Execute()
106        {
107            if (inputMod.IsZero || inputMod.IsOne)
108            {
109                GuiLogMessage("Input modulo not valid!", NotificationLevel.Error);
110                return;
111            }
112
113            inputBase %= inputMod;
114            if (inputBase.IsZero || inputBase.IsOne)
115            {
116                GuiLogMessage("Input base not valid!", NotificationLevel.Error);
117                return;
118            }
119
120            inputValue %= inputMod;
121            if (inputValue.IsZero)
122            {
123                GuiLogMessage("Input value not valid!", NotificationLevel.Error);
124                return;
125            }
126
127            BigInteger t = inputBase;
128            BigInteger counter = 1;
129            while (t != 1 && t != inputValue)
130            {
131                t = (t * inputBase) % inputMod;
132                counter++;
133            }
134            if (t == inputValue)
135                OutputLogarithm = counter;
136            else
137                GuiLogMessage("Input base not a generator of given residue class", NotificationLevel.Error);
138        }//end Execute
139
140        /// <summary>
141        /// Called by the environment after execution of this plugin
142        /// </summary>
143        public void PostExecution()
144        {
145        }
146
147        /// <summary>
148        /// Called by the environment to pause this plugin
149        /// </summary>
150        public void Pause()
151        {
152        }
153
154        /// <summary>
155        /// Called by the environment to stop this plugin
156        /// </summary>
157        public void Stop()
158        {
159        }
160
161        /// <summary>
162        /// Called by the environment to initialize this plugin
163        /// </summary>
164        public void Initialize()
165        {
166        }
167
168        /// <summary>
169        /// Called by the environment to Dispose this plugin
170        /// </summary>
171        public void Dispose()
172        {
173
174        }
175
176        /// <summary>
177        /// Gets/Sets the value x in b^log_b(x) = x
178        /// </summary>
179        [PropertyInfo(Direction.InputData, "Input value", "Enter your input value here", "", DisplayLevel.Beginner)]
180        public BigInteger InputValue
181        {
182            get
183            {
184                return inputValue;
185            }
186            set
187            {
188                inputValue = value;
189                OnPropertyChanged("InputValue");
190            }
191        }
192
193        /// <summary>
194        /// Gets/Sets the base b in b^log_b(x) = x
195        /// </summary>
196        [PropertyInfo(Direction.InputData, "Input base/generator", "Enter your base/generator here", "", DisplayLevel.Beginner)]
197        public BigInteger InputBase
198        {
199            get
200            {
201                return inputBase;
202            }
203            set
204            {
205                inputBase = value;
206                OnPropertyChanged("InputBase");
207            }
208        }
209
210        /// <summary>
211        /// Gets/Sets the modulo value for the used residue class
212        /// </summary>
213        [PropertyInfo(Direction.InputData, "Input modulo", "Enter your modulo here", "", DisplayLevel.Beginner)]
214        public BigInteger InputMod
215        {
216            get
217            {
218                return inputMod;
219            }
220            set
221            {
222                inputMod = value;
223                OnPropertyChanged("InputMod");
224            }
225        }
226
227        /// <summary>
228        /// Gets/Sets the calculated discrete logarithm
229        /// </summary>
230        [PropertyInfo(Direction.OutputData, "discrete logarithm output", "Get the result here!", "", DisplayLevel.Beginner)]
231        public BigInteger OutputLogarithm
232        {
233            get
234            {
235                return outputLogarithm;
236            }
237            set
238            {
239                outputLogarithm = value;
240                OnPropertyChanged("OutputLogarithm");
241            }
242        }
243       
244
245        #endregion
246
247        #region private
248
249        /// <summary>
250        /// Change the progress of this plugin
251        /// </summary>
252        /// <param name="value">value</param>
253        /// <param name="max">max</param>
254        private void ProgressChanged(double value, double max)
255        {
256            EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
257        }
258
259        /// <summary>
260        /// Logg a message to cryptool
261        /// </summary>
262        /// <param name="p">p</param>
263        /// <param name="notificationLevel">notificationLevel</param>
264        private void GuiLogMessage(string p, NotificationLevel notificationLevel)
265        {
266            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
267        }
268
269        #endregion
270
271    }//end DiscreteLogarithm
272
273}//end Cryptool.Plugins.DiscreteLogarithm
Note: See TracBrowser for help on using the repository browser.