OpenCores
URL https://opencores.org/ocsvn/mod_mult_exp/mod_mult_exp/trunk

Subversion Repositories mod_mult_exp

[/] [mod_mult_exp/] [trunk/] [sw/] [SerialCommExponentiator/] [src/] [webexponentiator/] [serialcomm/] [Window.java] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 gajos
package webexponentiator.serialcomm;
2
 
3
import java.math.BigInteger;
4
import java.util.Date;
5
import java.util.Random;
6
 
7
import org.apache.commons.lang3.ArrayUtils;
8
import org.eclipse.swt.SWT;
9
import org.eclipse.swt.events.MouseAdapter;
10
import org.eclipse.swt.events.MouseEvent;
11
import org.eclipse.swt.widgets.Button;
12
import org.eclipse.swt.widgets.Combo;
13
import org.eclipse.swt.widgets.Composite;
14
import org.eclipse.swt.widgets.Display;
15
import org.eclipse.swt.widgets.Label;
16
import org.eclipse.swt.widgets.Shell;
17
import org.eclipse.swt.widgets.Text;
18
 
19
import webexponentiator.serialcomm.Command;
20
 
21
import org.eclipse.swt.custom.CLabel;
22
import org.eclipse.swt.events.DisposeListener;
23
import org.eclipse.swt.events.DisposeEvent;
24
import org.eclipse.swt.graphics.Point;
25
 
26
public class Window {
27
 
28
        //Communicator object
29
    Communication communication = null;
30
        public Display display;
31
        protected Shell shlPcExponentiator;
32
        public Text text;
33
        private Text text_2;
34
        public Combo combo;
35
        private Button btnConnect;
36
        private Button btnDisconnect;
37
        private Button btnSendAutomated;
38
        private Button btnSendResult;
39
        private Button btnSendPrepare;
40
        private Button btnBase;
41
        private Button btnModulus;
42
        private Button btnExponent;
43
        private Button btnResiduum;
44
        private Button btnPowerMod;
45
 
46
        public void toggleControls()
47
    {
48
        if (communication.getConnected() == true)
49
        {
50
                btnDisconnect.setEnabled(true);
51
            btnConnect.setEnabled(false);
52
            btnSendAutomated.setEnabled(true);
53
            btnSendResult.setEnabled(true);
54
            btnSendPrepare.setEnabled(true);
55
            btnBase.setEnabled(true);
56
            btnModulus.setEnabled(true);
57
            btnExponent.setEnabled(true);
58
            btnResiduum.setEnabled(true);
59
            btnPowerMod.setEnabled(true);
60
        }
61
        else
62
        {
63
                btnDisconnect.setEnabled(false);
64
            btnConnect.setEnabled(true);
65
            btnSendAutomated.setEnabled(false);
66
            btnSendResult.setEnabled(false);
67
            btnSendPrepare.setEnabled(false);
68
            btnBase.setEnabled(false);
69
            btnModulus.setEnabled(false);
70
            btnExponent.setEnabled(false);
71
            btnResiduum.setEnabled(false);
72
            btnPowerMod.setEnabled(false);
73
        }
74
    }
75
 
76
        /**
77
         * Launch the application.
78
         *
79
         * @param args
80
         */
81
        public static void main(String[] args) {
82
                try {
83
                        Window window = new Window();
84
                        window.open();
85
                } catch (Exception e) {
86
                        e.printStackTrace();
87
                }
88
        }
89
 
90
        /**
91
         * Open the window.
92
         */
93
        public void open() {
94
                display = Display.getDefault();
95
                createContents();
96
                communication = new Communication(this);
97
                communication.searchForPorts();
98
                toggleControls();
99
                shlPcExponentiator.open();
100
                shlPcExponentiator.layout();
101
                while (!shlPcExponentiator.isDisposed()) {
102
                        if (!display.readAndDispatch()) {
103
                                display.sleep();
104
                        }
105
                }
106
        }
107
 
108
        /**
109
         * Create contents of the window.
110
         */
111
        protected void createContents() {
112
                shlPcExponentiator = new Shell(SWT.CLOSE | SWT.MIN | SWT.TITLE);
113
                shlPcExponentiator.setSize(new Point(483, 369));
114
                shlPcExponentiator.addDisposeListener(new DisposeListener() {
115
                        public void widgetDisposed(DisposeEvent arg0) {
116
                                if (communication.getConnected()) {
117
                                        communication.disconnect();
118
                                }
119
                        }
120
                });
121
                shlPcExponentiator.setSize(493, 399);
122
                shlPcExponentiator.setText("PC Exponentiator");
123
                shlPcExponentiator.setLayout(null);
124
 
125
                Composite composite = new Composite(shlPcExponentiator, SWT.NONE);
126
                composite.setBounds(0, 0, 483, 369);
127
 
128
                text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
129
                this.text.setBounds(10, 171, 463, 188);
130
 
131
                combo = new Combo(composite, SWT.NONE);
132
                combo.setBounds(10, 10, 124, 23);
133
 
134
                btnConnect = new Button(composite, SWT.NONE);
135
                btnConnect.addMouseListener(new MouseAdapter() {
136
                        @Override
137
                        public void mouseUp(MouseEvent arg0) {
138
                                communication.connect();
139
                        if (communication.getConnected() == true)
140
                        {
141
                            if (communication.initIOStream() == true)
142
                            {
143
                                communication.initListener();
144
                            }
145
                        }
146
                        }
147
                });
148
                btnConnect.setBounds(140, 10, 75, 25);
149
                btnConnect.setText("Connect");
150
 
151
                btnDisconnect = new Button(composite, SWT.NONE);
152
                btnDisconnect.addMouseListener(new MouseAdapter() {
153
                        @Override
154
                        public void mouseUp(MouseEvent arg0) {
155
                                communication.disconnect();
156
                        }
157
                });
158
                btnDisconnect.setBounds(221, 10, 75, 25);
159
                btnDisconnect.setText("Disconnect");
160
 
161
                Label lblLog = new Label(composite, SWT.NONE);
162
                lblLog.setBounds(10, 150, 55, 15);
163
                lblLog.setText("Log");
164
 
165
                btnSendAutomated = new Button(composite, SWT.NONE);
166
                btnSendAutomated.addMouseListener(new MouseAdapter() {
167
                        @Override
168
                        public void mouseUp(MouseEvent arg0) {
169
                                        Random r = new Random(new Date().getTime());
170
                                        BigInteger modulus = BigInteger.probablePrime(512, r);
171
                                        BigInteger base = new BigInteger(512, r).mod(modulus);
172
                                        BigInteger exponent = new BigInteger(512, r).mod(modulus);
173
                                        BigInteger residuum = new BigInteger("2").modPow(new BigInteger("1024"), modulus);
174
                                        BigInteger expectedResult = base.modPow(exponent, modulus);
175
                                        /*
176
                                         * result.add(sendBase(
177
                                         * "10831972010009692284864743082963908985928244572237504978567815597954452424901701848115907348099319027887255346705501542390228546770547307022309796259930536"
178
                                         * )); result.add(sendModulus(
179
                                         * "11639194216848075599002265489360912001411488135138961225285267565441921553320210324625995654671521634712013831000392536053201786146999373798311679376312847"
180
                                         * )); result.add(sendExponent(
181
                                         * "1164213079911476522452523716613118512153792329806743382289257300977572318091588414675225325908322428116294194315992613761814533537627230020523566408522775"
182
                                         * )); result.add(sendResiduum(
183
                                         * "1710026381007983649390259627245755642172838934666512596966326197048317423109472713444486555154343967450576033188072022772979735585191761951832684734601532"
184
                                         * ));
185
                                         */
186
                                        appendText("Send " + sendBase(base.toString(10)) + "\n");
187
                                        appendText("Send " + sendModulus(modulus.toString(10)) + "\n");
188
                                        appendText("Send " + sendExponent(exponent.toString(10)) + "\n");
189
                                        appendText("Send " + sendResiduum(residuum.toString(10)) + "\n");
190
                                        appendText(sendPower() + "\n");
191
                                        String result = sendResult();
192
                                        if (null != result) {
193
                                                appendText("Result = \n" + result + "\n");
194
                                        } else {
195
                                                appendText("Result is NULL!!! \n");
196
                                        }
197
                                        appendText("Send prepare\n" + sendPrepare() + "\n");
198
                                        appendText("Expected result " + expectedResult.toString(16) + "\n");
199
                                        if (expectedResult.toString(16).compareTo(result) == 0) {
200
                                                appendText("Expected result = calculated result");
201
                                        } else {
202
                                                appendText("Calculated result ERROR!\nExpected result is \n" + expectedResult + "\nCalculated result is\n" + result + "\n");
203
                                        }
204
 
205
 
206
//                              for (int i = 0; i < dataProtocol.length; i++) {
207
//                                      String [] el = dataProtocol[i].split(" ");
208
//                                      ArrayUtils.reverse(el);
209
//                                      for (int j = 0; j < el.length; j++) {
210
//                                              communication.writeData(el[j]);
211
//                                      }
212
//                              }
213
                        }
214
                });
215
                btnSendAutomated.setBounds(256, 119, 91, 25);
216
                btnSendAutomated.setText("Send automated");
217
 
218
                text_2 = new Text(composite, SWT.BORDER);
219
                text_2.setBounds(10, 60, 463, 21);
220
 
221
                btnSendResult = new Button(composite, SWT.NONE);
222
                btnSendResult.addMouseListener(new MouseAdapter() {
223
                        @Override
224
                        public void mouseUp(MouseEvent arg0) {
225
                                String result = sendResult();
226
                                if (null != result) {
227
                                        appendText("Result = " + result + "\n");
228
                                } else {
229
                                        appendText("Result is NULL!!! \n");
230
                                }
231
                        }
232
                });
233
                btnSendResult.setBounds(91, 119, 75, 25);
234
                btnSendResult.setText("Send result");
235
 
236
                btnSendPrepare = new Button(composite, SWT.NONE);
237
                btnSendPrepare.addMouseListener(new MouseAdapter() {
238
                        @Override
239
                        public void mouseUp(MouseEvent arg0) {
240
                                appendText("Send prepare:\n" + sendPrepare() + "\n");
241
                        }
242
                });
243
                btnSendPrepare.setBounds(172, 119, 75, 25);
244
                btnSendPrepare.setText("Send prepare");
245
 
246
                btnBase = new Button(composite, SWT.NONE);
247
                btnBase.addMouseListener(new MouseAdapter() {
248
                        @Override
249
                        public void mouseUp(MouseEvent arg0) {
250
                                String text = text_2.getText();
251
                                try {
252
                                BigInteger bi = new BigInteger(text, 10);
253
                                appendText("Send base:\n" + sendBase(text) + "\n");
254
                                } catch (NumberFormatException e) {
255
                                        appendText("Wrong number format to send base \n");
256
                                }
257
                        }
258
                });
259
                btnBase.setBounds(10, 85, 75, 25);
260
                btnBase.setText("Base");
261
 
262
                btnModulus = new Button(composite, SWT.NONE);
263
                btnModulus.addMouseListener(new MouseAdapter() {
264
                        @Override
265
                        public void mouseUp(MouseEvent arg0) {
266
                                String text = text_2.getText();
267
                                try {
268
                                BigInteger bi = new BigInteger(text, 10);
269
                                appendText("Send modulus:\n" + sendModulus(text) + "\n");
270
                                } catch (NumberFormatException e) {
271
                                        appendText("Wrong number format to send modulus");
272
                                }
273
                        }
274
                });
275
                btnModulus.setBounds(91, 85, 75, 25);
276
                btnModulus.setText("Modulus");
277
 
278
                btnExponent = new Button(composite, SWT.NONE);
279
                btnExponent.addMouseListener(new MouseAdapter() {
280
                        @Override
281
                        public void mouseUp(MouseEvent arg0) {
282
                                String text = text_2.getText();
283
                                try {
284
                                BigInteger bi = new BigInteger(text, 10);
285
                                appendText("Send exponent:\n" + sendExponent(text) + "\n");
286
                                } catch (NumberFormatException e) {
287
                                        appendText("Wrong number format to send exponent");
288
                                }
289
                        }
290
                });
291
                btnExponent.setBounds(172, 85, 75, 25);
292
                btnExponent.setText("Exponent");
293
 
294
                btnResiduum = new Button(composite, SWT.NONE);
295
                btnResiduum.addMouseListener(new MouseAdapter() {
296
                        @Override
297
                        public void mouseUp(MouseEvent arg0) {
298
                                String text = text_2.getText();
299
                                try {
300
                                BigInteger bi = new BigInteger(text, 10);
301
                                appendText("Send residuum:\n" + sendResiduum(text) + "\n");
302
                                } catch (NumberFormatException e) {
303
                                        appendText("Wrong number format to send residuum");
304
                                }
305
                        }
306
                });
307
                btnResiduum.setBounds(256, 85, 75, 25);
308
                btnResiduum.setText("Residuum");
309
 
310
                btnPowerMod = new Button(composite, SWT.NONE);
311
                btnPowerMod.addMouseListener(new MouseAdapter() {
312
                        @Override
313
                        public void mouseUp(MouseEvent arg0) {
314
                                appendText("Send power:\n" + sendPower() + "\n");
315
                        }
316
                });
317
                btnPowerMod.setBounds(10, 119, 75, 25);
318
                btnPowerMod.setText("Power Mod");
319
 
320
                CLabel lblWriteHexadecimalValue = new CLabel(composite, SWT.NONE);
321
                lblWriteHexadecimalValue.setBottomMargin(1);
322
                lblWriteHexadecimalValue.setTopMargin(1);
323
                lblWriteHexadecimalValue.setBounds(10, 39, 172, 21);
324
                lblWriteHexadecimalValue.setText("Write DECIMAL value:");
325
        }
326
 
327
        private String sendPrepare() {
328
                sendData(new String[] { Command.mn_prepare_for_data });
329
                waitMicron("prepare");
330
                return "prepare";
331
        }
332
 
333
        private String sendResult() {
334
                sendData(new String[] { Command.mn_show_result});
335
                waitMicron("show result");
336
                return communication.readData();
337
        }
338
 
339
        private String sendPower() {
340
                sendData(new String[] { Command.mn_count_power });
341
                waitMicron("count power");
342
                return "count power \n" + communication.readData();
343
        }
344
 
345
        private String sendResiduum(String string) {
346
                String residuum = parseDataToSend(string, 10);
347
                String[] send = { Command.mn_read_residuum, residuum };
348
                sendData(send);
349
                waitMicron("residuum");
350
                return "residuum - " + string + "\n" + communication.readData();
351
        }
352
 
353
        private String sendExponent(String string) {
354
                String exponent = parseDataToSend(string, 10);
355
                String[] send = { Command.mn_read_exponent, exponent };
356
                sendData(send);
357
                waitMicron("exponent");
358
                return "exponent - " + string + "\n" + communication.readData();
359
        }
360
 
361
        private String sendModulus(String string) {
362
                String modulus = parseDataToSend(string, 10);
363
                String[] send = { Command.mn_read_modulus, modulus };
364
                sendData(send);
365
                waitMicron("modulus");
366
                return "modulus - " + string + "\n" + communication.readData();
367
        }
368
 
369
        private String sendBase(String data) {
370
                String base = parseDataToSend(data, 10);
371
                String[] send = { Command.mn_read_base, base };
372
                sendData(send);
373
                waitMicron("base");
374
                return "base - " + data + "\n" + communication.readData();
375
        }
376
 
377
 
378
        private String parseDataToSend(String string, int radix) {
379
                BigInteger strBi = new BigInteger(string, radix);
380
                String result = new String("");
381
                for (int i = Command.MAX_WORD - 1; i >= strBi.bitLength(); i--) {
382
                        result = result.concat("0");
383
 
384
                        if (i % 8 == 0) {
385
                                result = result.concat(" ");
386
                        }
387
                }
388
 
389
                for (int i = strBi.bitLength() - 1; i >= 0; i--) {
390
                        if (strBi.testBit(i)) {
391
                                result = result.concat("1");
392
                        } else {
393
                                result = result.concat("0");
394
                        }
395
 
396
                        if (i % 8 == 0) {
397
                                result = result.concat(" ");
398
                        }
399
                }
400
 
401
                return result;
402
        }
403
 
404
        private void waitMicron(String string) {
405
                try {
406
                        Thread.sleep(1000);
407
                } catch (InterruptedException e) {
408
                        appendText("Wait for data happen -" + string);
409
                        e.printStackTrace();
410
                }
411
 
412
        }
413
 
414
        private void sendData(String [] data) {
415
                for (int i = 0; i < data.length; i++) {
416
                        String [] el = data[i].split(" ");
417
                        ArrayUtils.reverse(el);
418
                        for (int j = 0; j < el.length; j++) {
419
                                communication.writeData(el[j]);
420
                        }
421
                }
422
        }
423
 
424
        public void appendText(final String s) {
425
                display.syncExec(new Runnable() {
426
                        public void run() {
427
                                text.append(s);
428
                        }
429
                });
430
 
431
        }
432
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.