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

Subversion Repositories mod_mult_exp

[/] [mod_mult_exp/] [trunk/] [sw/] [WebExponentiator/] [src/] [main/] [java/] [webexponentiator/] [controller/] [ExponentiationController.java] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 gajos
package webexponentiator.controller;
2
 
3
import java.math.BigInteger;
4
import java.util.ArrayList;
5
import java.util.Date;
6
import java.util.Random;
7
 
8
import org.apache.commons.lang3.ArrayUtils;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.springframework.stereotype.Controller;
12
import org.springframework.ui.Model;
13
import org.springframework.web.bind.annotation.RequestMapping;
14
 
15
import webexponentiator.util.Command;
16
import webexponentiator.util.Communication;
17
 
18
@Controller
19
public class ExponentiationController {
20
 
21
        /** Logger for this class and subclasses */
22
        protected final Log logger = LogFactory.getLog(getClass());
23
 
24
        private static Communication communication;
25
 
26
        @RequestMapping("/exponentiation")
27
        public String showExponentiation(Model model) {
28
                String ret = getPort();
29
                ArrayList<String> exponentiation = modularExponentiation();
30
            logger.info("Starting exponentiation");
31
                if (null == exponentiation) {
32
                        exponentiation = new ArrayList<String>();
33
                        exponentiation.add(ret);
34
                }
35
                logger.info("Ending exponentiation");
36
                model.addAttribute("exponentiation", exponentiation);
37
                return "exponentiate";
38
        }
39
 
40
        private synchronized ArrayList<String> modularExponentiation() {
41
                ArrayList<String> result = new ArrayList<String>();
42
                if (communication.getConnected()) {
43
                        Random r = new Random(new Date().getTime());
44
                        BigInteger modulus = BigInteger.probablePrime(512, r);
45
                        BigInteger base = new BigInteger(512, r).mod(modulus);
46
                        BigInteger exponent = new BigInteger(512, r).mod(modulus);
47
                        BigInteger residuum = new BigInteger("2").modPow(new BigInteger("1024"), modulus);
48
                        BigInteger expectedResult = base.modPow(exponent, modulus);
49
                        /*
50
                         * result.add(sendBase(
51
                         * "10831972010009692284864743082963908985928244572237504978567815597954452424901701848115907348099319027887255346705501542390228546770547307022309796259930536"
52
                         * )); result.add(sendModulus(
53
                         * "11639194216848075599002265489360912001411488135138961225285267565441921553320210324625995654671521634712013831000392536053201786146999373798311679376312847"
54
                         * )); result.add(sendExponent(
55
                         * "1164213079911476522452523716613118512153792329806743382289257300977572318091588414675225325908322428116294194315992613761814533537627230020523566408522775"
56
                         * )); result.add(sendResiduum(
57
                         * "1710026381007983649390259627245755642172838934666512596966326197048317423109472713444486555154343967450576033188072022772979735585191761951832684734601532"
58
                         * ));
59
                         */
60
                        logger.info("Send base");
61
                        result.add(sendBase(base.toString(10)));
62
                        logger.info("Send modulus");
63
                        result.add(sendModulus(modulus.toString(10)));
64
                        logger.info("Send exponent");
65
                        result.add(sendExponent(exponent.toString(10)));
66
                        logger.info("Send residuum");
67
                        result.add(sendResiduum(residuum.toString(10)));
68
                        logger.info("Send power");
69
                        result.add(sendPower());
70
                        logger.info("Send result");
71
                        result.add(sendResult());
72
                        logger.info("Send prepare");
73
                        result.add(sendPrepare());
74
                        result.add("Expected result " + expectedResult.toString(16));
75
                        result.add("Equal = 0? " + expectedResult.toString(16).compareTo(result.get(5)));
76
                        communication.disconnect();
77
                        return result;
78
                }
79
                return null;
80
        }
81
 
82
        private String sendPrepare() {
83
                sendData(new String[] { Command.mn_prepare_for_data });
84
                waitMicron("prepare");
85
                return "prepare";
86
        }
87
 
88
        private String sendResult() {
89
                sendData(new String[] { Command.mn_show_result});
90
                waitMicron("show result");
91
                return communication.readData();
92
        }
93
 
94
        private String sendPower() {
95
                sendData(new String[] { Command.mn_count_power });
96
                waitMicron("count power");
97
                return "count power - " + communication.readData();
98
        }
99
 
100
        private String sendResiduum(String string) {
101
                String residuum = parseDataToSend(string, 10);
102
                String[] send = { Command.mn_read_residuum, residuum };
103
                sendData(send);
104
                waitMicron("residuum");
105
                return "residuum - " + string + " " + communication.readData();
106
        }
107
 
108
        private String sendExponent(String string) {
109
                String exponent = parseDataToSend(string, 10);
110
                String[] send = { Command.mn_read_exponent, exponent };
111
                sendData(send);
112
                waitMicron("exponent");
113
                return "exponent - " + string + " " + communication.readData();
114
        }
115
 
116
        private String sendModulus(String string) {
117
                String modulus = parseDataToSend(string, 10);
118
                String[] send = { Command.mn_read_modulus, modulus };
119
                sendData(send);
120
                waitMicron("modulus");
121
                return "modulus - " + string + " " + communication.readData();
122
        }
123
 
124
        private String sendBase(String data) {
125
                String base = parseDataToSend(data, 10);
126
                String[] send = { Command.mn_read_base, base };
127
                sendData(send);
128
                waitMicron("base");
129
                return "base - " + data + " " + communication.readData();
130
        }
131
 
132
 
133
        private String parseDataToSend(String string, int radix) {
134
                BigInteger strBi = new BigInteger(string, radix);
135
                String result = new String("");
136
                for (int i = Command.MAX_WORD - 1; i >= strBi.bitLength(); i--) {
137
                        result = result.concat("0");
138
 
139
                        if (i % 8 == 0) {
140
                                result = result.concat(" ");
141
                        }
142
                }
143
 
144
                for (int i = strBi.bitLength() - 1; i >= 0; i--) {
145
                        if (strBi.testBit(i)) {
146
                                result = result.concat("1");
147
                        } else {
148
                                result = result.concat("0");
149
                        }
150
 
151
                        if (i % 8 == 0) {
152
                                result = result.concat(" ");
153
                        }
154
                }
155
 
156
                return result;
157
        }
158
 
159
        private void sendData(String[] data) {
160
                for (int i = 0; i < data.length; i++) {
161
                        String[] el = data[i].split(" ");
162
                        ArrayUtils.reverse(el);
163
                        for (int j = 0; j < el.length; j++) {
164
                                communication.writeData(el[j]);
165
                        }
166
                }
167
        }
168
 
169
        private void waitMicron(String string) {
170
                try {
171
                        Thread.sleep(1000);
172
                } catch (InterruptedException e) {
173
                        logger.error("Wait for data happen -" + string);
174
                        e.printStackTrace();
175
                }
176
 
177
        }
178
 
179
        private synchronized String getPort() {
180
                logger.info("getPort() start");
181
                if (communication == null) {
182
                        communication = new Communication();
183
                }
184
                ArrayList<String> ports = communication.searchForPorts();
185
                if (communication.getConnected()) {
186
                        return "COM3";
187
                }
188
                for (String port : ports) {
189
                        if (port.contains("COM3")) {
190
                                communication.connect();
191
                                if (communication.getConnected() == true) {
192
                                        if (communication.initIOStream() == true) {
193
                                                return "COM3";
194
                                        }
195
                                        return "Something happens - initIOStream";
196
                                }
197
                                return "Something happens - getConnected";
198
                        }
199
                }
200
                logger.info("getPort() end");
201
                return "Something happens - COM3 not found";
202
        }
203
}

powered by: WebSVN 2.1.0

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