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

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

added index-calculus to discrete logarithm plugin (do not try it yet, because it doesn't seem to work efficient at the moment).

File size: 9.1 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;
29using DiscreteLogarithm;
30
31namespace Cryptool.Plugins.DiscreteLogarithm
32{
33    [Author("Sven Rech", null, "Uni Duisburg-Essen", "http://www.uni-due.de")]
34    [PluginInfo(false, "Discrete Logarithm", "Calculates the discrete logarithm", null, "DiscreteLogarithm/icon.png")]
35    /// <summary>
36    /// This plugin calculates the discrete logarithm of the input.
37    /// The input contains of a the BigInteger value and base and the modulo value to determine the residue class
38    /// </summary>
39    class DiscreteLogarithm : IThroughput
40    {
41        #region private members
42
43        private DiscreteLogarithmSettings settings = new DiscreteLogarithmSettings();
44        private BigInteger inputValue;
45        private BigInteger inputBase;
46        private BigInteger inputMod;
47        private BigInteger outputLogarithm;
48
49        #endregion
50
51        #region events
52
53        public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
54        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
55        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
56        public event StatusChangedEventHandler OnPluginStatusChanged;
57
58        #endregion
59
60        #region public
61       
62        /// <summary>
63        /// Notify that a property changed
64        /// </summary>
65        /// <param name="name">property name</param>
66        public void OnPropertyChanged(string name)
67        {
68            EventsHelper.PropertyChanged(PropertyChanged, this, new PropertyChangedEventArgs(name));
69        }
70
71        /// <summary>
72        /// Gets/Sets the Settings of this plugin
73        /// </summary>
74        public ISettings Settings
75        {
76            get { return this.settings; }
77            set { this.settings = (DiscreteLogarithmSettings)value; }
78        }
79
80        /// <summary>
81        /// Get the Presentation of this plugin
82        /// </summary>
83        public System.Windows.Controls.UserControl Presentation
84        {
85            get { return null; }
86        }
87
88        /// <summary>
89        /// Get the QuickWatchRepresentation of this plugin
90        /// </summary>
91        public System.Windows.Controls.UserControl QuickWatchPresentation
92        {
93            get { return null; }
94        }
95
96        /// <summary>
97        /// Called by the environment before execution
98        /// </summary>
99        public void PreExecution()
100        {
101        }
102
103        /// <summary>
104        /// Called by the environment to execute this plugin
105        /// </summary>
106        public void Execute()
107        {
108            if (inputMod.IsZero || inputMod.IsOne)
109            {
110                GuiLogMessage("Input modulo not valid!", NotificationLevel.Error);
111                return;
112            }
113
114            inputBase %= inputMod;
115            if (inputBase.IsZero || inputBase.IsOne)
116            {
117                GuiLogMessage("Input base not valid!", NotificationLevel.Error);
118                return;
119            }
120
121            inputValue %= inputMod;
122            if (inputValue.IsZero)
123            {
124                GuiLogMessage("Input value not valid!", NotificationLevel.Error);
125                return;
126            }
127
128            switch (settings.Algorithm)
129            {
130                case 0:
131                    Enumeration();
132                    break;
133                case 1:
134                    IndexCalculus();
135                    break;
136            }
137        }
138
139        private void IndexCalculus()
140        {
141            IndexCalculusMethod ic = new IndexCalculusMethod();
142            try
143            {
144                BigInteger res = ic.DiscreteLog(inputValue, inputBase, inputMod);
145                OutputLogarithm = res;
146            }
147            catch (Exception ex)
148            {
149                GuiLogMessage("Index-Calculus error: " + ex.Message, NotificationLevel.Error);
150            }
151        }
152
153        private void Enumeration()
154        {
155            BigInteger t = inputBase;
156            BigInteger counter = 1;
157            while (t != 1 && t != inputValue)
158            {
159                t = (t * inputBase) % inputMod;
160                counter++;
161            }
162            if (t == inputValue)
163                OutputLogarithm = counter;
164            else
165                GuiLogMessage("Input base not a generator of given residue class", NotificationLevel.Error);
166        }
167
168        /// <summary>
169        /// Called by the environment after execution of this plugin
170        /// </summary>
171        public void PostExecution()
172        {
173        }
174
175        /// <summary>
176        /// Called by the environment to pause this plugin
177        /// </summary>
178        public void Pause()
179        {
180        }
181
182        /// <summary>
183        /// Called by the environment to stop this plugin
184        /// </summary>
185        public void Stop()
186        {
187        }
188
189        /// <summary>
190        /// Called by the environment to initialize this plugin
191        /// </summary>
192        public void Initialize()
193        {
194        }
195
196        /// <summary>
197        /// Called by the environment to Dispose this plugin
198        /// </summary>
199        public void Dispose()
200        {
201
202        }
203
204        /// <summary>
205        /// Gets/Sets the value x in b^log_b(x) = x
206        /// </summary>
207        [PropertyInfo(Direction.InputData, "Input value", "Enter your input value here", "", DisplayLevel.Beginner)]
208        public BigInteger InputValue
209        {
210            get
211            {
212                return inputValue;
213            }
214            set
215            {
216                inputValue = value;
217                OnPropertyChanged("InputValue");
218            }
219        }
220
221        /// <summary>
222        /// Gets/Sets the base b in b^log_b(x) = x
223        /// </summary>
224        [PropertyInfo(Direction.InputData, "Input base/generator", "Enter your base/generator here", "", DisplayLevel.Beginner)]
225        public BigInteger InputBase
226        {
227            get
228            {
229                return inputBase;
230            }
231            set
232            {
233                inputBase = value;
234                OnPropertyChanged("InputBase");
235            }
236        }
237
238        /// <summary>
239        /// Gets/Sets the modulo value for the used residue class
240        /// </summary>
241        [PropertyInfo(Direction.InputData, "Input modulo", "Enter your modulo here", "", DisplayLevel.Beginner)]
242        public BigInteger InputMod
243        {
244            get
245            {
246                return inputMod;
247            }
248            set
249            {
250                inputMod = value;
251                OnPropertyChanged("InputMod");
252            }
253        }
254
255        /// <summary>
256        /// Gets/Sets the calculated discrete logarithm
257        /// </summary>
258        [PropertyInfo(Direction.OutputData, "discrete logarithm output", "Get the result here!", "", DisplayLevel.Beginner)]
259        public BigInteger OutputLogarithm
260        {
261            get
262            {
263                return outputLogarithm;
264            }
265            set
266            {
267                outputLogarithm = value;
268                OnPropertyChanged("OutputLogarithm");
269            }
270        }
271       
272
273        #endregion
274
275        #region private
276
277        /// <summary>
278        /// Change the progress of this plugin
279        /// </summary>
280        /// <param name="value">value</param>
281        /// <param name="max">max</param>
282        private void ProgressChanged(double value, double max)
283        {
284            EventsHelper.ProgressChanged(OnPluginProgressChanged, this, new PluginProgressEventArgs(value, max));
285        }
286
287        /// <summary>
288        /// Logg a message to cryptool
289        /// </summary>
290        /// <param name="p">p</param>
291        /// <param name="notificationLevel">notificationLevel</param>
292        private void GuiLogMessage(string p, NotificationLevel notificationLevel)
293        {
294            EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this, new GuiLogEventArgs(p, this, notificationLevel));
295        }
296
297        #endregion
298
299    }//end DiscreteLogarithm
300
301}//end Cryptool.Plugins.DiscreteLogarithm
Note: See TracBrowser for help on using the repository browser.