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

Subversion Repositories ecg

[/] [ecg/] [trunk/] [rtl/] [ecg.v] - Blame information for rev 6

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 homer.xing
/*
2
    Copyright 2011, City University of Hong Kong
3
    Author is Homer (Dongsheng) Xing.
4
 
5
    This file is part of Elliptic Curve Group Core.
6
 
7
    Elliptic Curve Group Core is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU Lesser General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
 
12
    Elliptic Curve Group Core is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU Lesser General Public License for more details.
16
 
17
    You should have received a copy of the GNU General Public License
18
    along with Elliptic Curve Group Core.  If not, see http://www.gnu.org/licenses/lgpl.txt
19
*/
20
 
21 2 homer.xing
`include "inc.v"
22
 
23 5 homer.xing
/* point scalar multiplication on the elliptic curve $y^2=x^3-x+1$ over a Galois field GF(3^M)
24
 * whose irreducible polynomial is $x^97 + x^12 + 2$. */
25
/* $P3(x3,y3) == c \cdot P1(x1,y1)$ */
26
module point_scalar_mult(clk, reset, x1, y1, zero1, c, done, x3, y3, zero3);
27
    input clk, reset;
28
    input [`WIDTH:0] x1, y1;
29
    input zero1;
30
    input [`SCALAR_WIDTH:0] c;
31
    output reg done;
32
    output reg [`WIDTH:0] x3, y3;
33
    output reg zero3;
34
 
35
    reg [`WIDTH:0] x2, y2; reg zero2; // the result
36
    wire [`WIDTH:0] x4, y4; wire zero4;
37
    wire [`WIDTH:0] x5, y5; wire zero5;
38
    reg [`SCALAR_WIDTH   : 0] k; // the scalar value
39
    reg [`SCALAR_WIDTH+1 : 0] i; // the counter
40
    reg op;
41
    wire p, p2, rst, done1;
42
 
43
    assign x4    = (~op) ? x2    : (k[`SCALAR_WIDTH]?x1:0);
44
    assign y4    = (~op) ? y2    : (k[`SCALAR_WIDTH]?y1:0);
45
    assign zero4 = (~op) ? zero2 : (k[`SCALAR_WIDTH]?zero1:1);
46
    assign rst   = reset | p2 ;
47
 
48
    point_add
49
        ins1 (clk, rst, x2, y2, zero2, x4, y4, zero4, done1, x5, y5, zero5);
50
    func6
51
        ins2 (clk, reset, done1, p),
52
        ins3 (clk, reset, p, p2);
53
 
54
    always @ (posedge clk)
55
        if (reset) i <= 1;
56
        else if ((op & p) | i[`SCALAR_WIDTH+1]) i <= i << 1;
57
 
58
    always @ (posedge clk)
59
        if (reset) k <= c;
60
        else if (op & p) k <= k << 1;
61
 
62
    always @ (posedge clk)
63
        if (reset) op <= 0;
64
        else if (p) op <= ~op;
65
 
66
    always @ (posedge clk)
67
        if (reset)  begin x2 <= 0; y2 <= 0; zero2 <= 1; end
68
        else if (p) begin x2 <= x5; y2 <= y5; zero2 <= zero5; end
69
 
70
    always @ (posedge clk)
71
        if (reset)  begin x3 <= 0; y3 <= 0; zero3 <= 1; done <= 0; end
72
        else if (i[`SCALAR_WIDTH+1])
73
          begin x3 <= x2; y3 <= y2; zero3 <= zero2; done <= 1; end
74
endmodule
75
 
76 2 homer.xing
/* add two points on the elliptic curve $y^2=x^3-x+1$ over a Galois field GF(3^M)
77
 * whose irreducible polynomial is $x^97 + x^12 + 2$. */
78
/* $P3(x3,y3) == P1 + P2$ for any points $P1(x1,y1),P2(x2,y2)$ */
79
module point_add(clk, reset, x1, y1, zero1, x2, y2, zero2, done, x3, y3, zero3);
80
    input clk, reset;
81 3 homer.xing
    input [`WIDTH:0] x1, y1; // this guy is $P1$
82 2 homer.xing
    input zero1; // asserted if P1 == 0
83 3 homer.xing
    input [`WIDTH:0] x2, y2; // and this guy is $P2$
84 2 homer.xing
    input zero2; // asserted if P2 == 0
85
    output reg done;
86 3 homer.xing
    output reg [`WIDTH:0] x3, y3; // ha ha, this guy is $P3$
87 2 homer.xing
    output reg zero3; // asserted if P3 == 0
88 3 homer.xing
    wire [`WIDTH:0] x3a, x3b, x3c,
89
                    y3a, y3b, y3c,
90
                    ny2;
91 2 homer.xing
    wire zero3a,
92 3 homer.xing
         use1,  // asserted if $ins9$ did the work
93
         done10, // asserted if $ins10$ finished
94
         done11,
95
         cond1,
96
         cond2,
97
         cond3,
98
         cond4,
99
         cond5;
100 2 homer.xing
 
101
    assign use1 = zero1 | zero2;
102 3 homer.xing
    assign cond1 = (~use1) && cond2 && cond4; // asserted if $P1 == -P2$
103
    assign cond2 = (x1 == x2);
104
    assign cond3 = (y1 == y2);
105
    assign cond4 = (y1 == ny2);
106
    assign cond5 = (~use1) && cond2 && cond3; // asserted if $P1 == P2$
107 2 homer.xing
 
108 3 homer.xing
    f3m_neg
109
        ins1 (y2, ny2); // ny2 == -y2
110 2 homer.xing
    func9
111
        ins9 (x1, y1, zero1, x2, y2, zero2, x3a, y3a, zero3a);
112
    func10
113 3 homer.xing
        ins10 (clk, reset, x1, y1, done10, x3b, y3b);
114
    func11
115
        ins11 (clk, reset, x1, y1, x2, y2, done11, x3c, y3c);
116 2 homer.xing
 
117
    always @ (posedge clk)
118 3 homer.xing
        if (reset)
119
            zero3 <= 0;
120
        else
121
            zero3 <= (use1 & zero3a) | cond1; // if both of $P1$ and $P2$ are inf point, or $P1 == -P2$, then $P3$ is inf point
122 2 homer.xing
 
123
    always @ (posedge clk)
124
        if (reset)
125
            done <= 0;
126
        else
127 3 homer.xing
            done <= (use1 | cond1) ? 1 : (cond5 ? done10 : done11);
128 2 homer.xing
 
129
    always @ (posedge clk)
130
        if (reset)
131
          begin
132
            x3 <= 0; y3 <= 0;
133
          end
134
        else
135
          begin
136 3 homer.xing
            x3 <= use1 ? x3a : (cond5 ? x3b : x3c);
137
            y3 <= use1 ? y3a : (cond5 ? y3b : y3c);
138 2 homer.xing
          end
139
endmodule
140
 
141 3 homer.xing
/* $P3 == P1+P2$ */
142 2 homer.xing
/* $P1$ and/or $P2$ is the infinite point */
143
module func9(x1, y1, zero1, x2, y2, zero2, x3, y3, zero3);
144
    input [`WIDTH:0] x1, y1, x2, y2;
145
    input zero1; // asserted if P1 == 0
146
    input zero2; // asserted if P2 == 0
147
    output [`WIDTH:0] x3, y3;
148
    output zero3; // asserted if P3 == 0
149
 
150
    assign zero3 = zero1 & zero2;
151
 
152
    genvar i;
153
    generate
154
        for (i=0; i<=`WIDTH; i=i+1)
155
          begin:label
156 3 homer.xing
            assign x3[i] = (x2[i] & zero1) | (x1[i] & zero2);
157
            assign y3[i] = (y2[i] & zero1) | (y1[i] & zero2);
158 2 homer.xing
          end
159
    endgenerate
160
endmodule
161
 
162 3 homer.xing
/* $P3 == P1+P2$ */
163
/* $P1$ or $P2$ is not the infinite point. $P1 == P2$ */
164
module func10(clk, reset, x1, y1, done, x3, y3);
165 2 homer.xing
    input clk, reset;
166 3 homer.xing
    input [`WIDTH:0] x1, y1;
167
    output reg done;
168
    output reg [`WIDTH:0] x3, y3;
169
    wire [`WIDTH:0] v1, v2, v3, v4, v5, v6;
170
    wire rst2, done1, done2;
171
    reg [2:0] K;
172 2 homer.xing
 
173 3 homer.xing
    f3m_inv
174
        ins1 (clk, reset, y1, v1, done1); // v1 == inv y1
175
    f3m_mult
176
        ins2 (clk, rst2, v1, v1, v2, done2); // v2 == v1^2
177
    f3m_cubic
178
        ins3 (v1, v3); // v3 == v1^3
179
    f3m_add
180
        ins4 (x1, v2, v4), // v4 == x1+v2 == x1 + (inv y1)^2
181
        ins5 (y1, v3, v5); // v5 == y1+v3 == y1 + (inv y1)^3
182
    f3m_neg
183
        ins6 (v5, v6); // v6 == -[y1 + (inv y1)^3]
184
    func6
185
        ins7 (clk, reset, done1, rst2);
186
 
187
    always @ (posedge clk)
188
        if (reset)
189
            K <= 3'b100;
190
        else if ((K[2]&rst2)|(K[1]&done2)|K[0])
191
            K <= K >> 1;
192
 
193
    always @ (posedge clk)
194
        if (reset)
195
          begin
196
            done <= 0; x3 <= 0; y3 <= 0;
197
          end
198
        else if (K[0])
199
          begin
200
            done <= 1; x3 <= v4; y3 <= v6;
201
          end
202 2 homer.xing
endmodule
203 3 homer.xing
 
204
/* $P3 == P1+P2$ */
205
/* $P1$ or $P2$ is not the infinite point. $P1 != P2, and P1 != -P2$ */
206
module func11(clk, reset, x1, y1, x2, y2, done, x3, y3);
207
    input clk, reset;
208
    input [`WIDTH:0] x1, y1, x2, y2;
209
    output reg done;
210
    output reg [`WIDTH:0] x3, y3;
211
    wire [`WIDTH:0] v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
212
    wire rst2, rst3, done1, done2, done3;
213
    reg [3:0] K;
214
 
215
    f3m_sub
216
        ins1 (x2, x1, v1), // v1 == x2-x1
217
        ins2 (y2, y1, v2); // v2 == y2-y1
218
    f3m_inv
219
        ins3 (clk, reset, v1, v3, done1); // v3 == inv v1 == inv(x2-x1)
220
    f3m_mult
221
        ins4 (clk, rst2, v2, v3, v4, done2), // v4 == v2*v3 == (y2-y1)/(x2-x1)
222
        ins5 (clk, rst3, v4, v4, v5, done3); // v5 == v4^2
223
    f3m_cubic
224
        ins6 (v4, v6); // v6 == v4^3
225
    f3m_add
226
        ins7 (x1, x2, v7), // v7 == x1+x2
227
        ins8 (y1, y2, v8); // v8 == y1+y2
228
    f3m_sub
229
        ins9 (v5, v7, v9), // v9 == v5-v7 == v4^2 - (x1+x2)
230
        ins10 (v8, v6, v10); // v10 == (y1+y2) - v4^3
231
    func6
232
        ins11 (clk, reset, done1, rst2),
233
        ins12 (clk, reset, done2, rst3);
234
 
235
    always @ (posedge clk)
236
        if (reset)
237
            K <= 4'b1000;
238
        else if ((K[3]&rst2)|(K[2]&rst3)|(K[1]&done3)|K[0])
239
            K <= K >> 1;
240
 
241
    always @ (posedge clk)
242
        if (reset)
243
          begin
244
            done <= 0; x3 <= 0; y3 <= 0;
245
          end
246
        else if (K[0])
247
          begin
248
            done <= 1; x3 <= v9; y3 <= v10;
249
          end
250
endmodule

powered by: WebSVN 2.1.0

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