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

Subversion Repositories pairing

[/] [pairing/] [trunk/] [rtl/] [f36m.v] - Blame information for rev 24

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

Line No. Rev Author Line
1 24 homer.xing
/*
2
    Copyright 2011, City University of Hong Kong
3
    Author is Homer (Dongsheng) Xing.
4
 
5
    This file is part of Tate Bilinear Pairing Core.
6
 
7
    Tate Bilinear Pairing 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
    Tate Bilinear Pairing 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 Foobar.  If not, see http://www.gnu.org/licenses/lgpl.txt
19
*/
20
 
21 2 homer.xing
`include "inc.v"
22
 
23
// c == a*b in GF(3^{6M})
24
module f36m_mult(clk, reset, a, b, c, done);
25
    input clk, reset;
26
    input [`W6:0] a, b;
27
    output reg [`W6:0] c;
28
    output reg done;
29
 
30
    reg [`W2:0] x0, x1, x2, x3, x4, x5;
31
    wire [`W2:0] a0, a1, a2,
32
                 b0, b1, b2,
33
                 c0, c1, c2,
34
                 v1, v2, v3, v4, v5, v6,
35
                 nx0, nx2, nx5,
36
                 d0, d1, d2, d3, d4;
37
    reg [6:0] K;
38
    wire e0, e1, e2,
39
         e3, e4, e5,
40
         mult_done, p, rst;
41
    wire [`W2:0] in0, in1;
42
    wire [`W2:0] o;
43
    reg mult_reset, delay1, delay2;
44 10 homer.xing
    reg [`W2:0] in0d,in1d;
45 2 homer.xing
 
46
    assign {e0,e1,e2,e3,e4,e5} = K[6:1];
47
    assign {a2,a1,a0} = a;
48
    assign {b2,b1,b0} = b;
49
    assign d4 = x0;
50
    assign d0 = x5;
51
    assign rst = delay2;
52
 
53 3 homer.xing
    f32m_mux6
54 2 homer.xing
        ins1 (a2,v1,a1,v3,v5,a0,e0,e1,e2,e3,e4,e5,in0), // $in0$ is the first input
55
        ins2 (b2,v2,b1,v4,v6,b0,e0,e1,e2,e3,e4,e5,in1); // $in1$ is the second input
56
    f32m_mult
57 10 homer.xing
        ins3 (clk, mult_reset, in0d, in1d, o, mult_done); // o == in0 * in1
58 2 homer.xing
    func6
59 8 homer.xing
        ins4 (clk, reset, mult_done, p);
60 2 homer.xing
    f32m_add
61
        ins5 (a1, a2, v1), // v1 == a1+a2
62
        ins6 (b1, b2, v2), // v2 == b1+b2
63
        ins7 (a0, a2, v3), // v3 == a0+a2
64
        ins8 (b0, b2, v4), // v4 == b0+b2
65
        ins9 (a0, a1, v5), // v5 == a0+a1
66
        ins10 (b0, b1, v6), // v6 == b0+b1
67
        ins11 (d0, d3, c0), // c0 == d0+d3
68
        ins12 (d2, d4, c2); // c2 == d2+d4
69 3 homer.xing
    f32m_neg
70 2 homer.xing
        ins13 (x0, nx0), // nx0 == -x0
71
        ins14 (x2, nx2), // nx2 == -x2
72
        ins15 (x5, nx5); // nx5 == -x5
73 3 homer.xing
    f32m_add3
74 2 homer.xing
        ins16 (x1, nx0, nx2, d3), // d3 == x1-x0-x2
75
        ins17 (x4, nx2, nx5, d1), // d1 == x4-x2-x5
76
        ins18 (d1, d3, d4, c1); // c1 == d1+d3+d4
77 3 homer.xing
    f32m_add4
78 2 homer.xing
        ins19 (x3, x2, nx0, nx5, d2); // d2 == x3+x2-x0-x5
79
 
80
    always @ (posedge clk)
81
      begin
82 10 homer.xing
        in0d <= in0; in1d <= in1;
83
      end
84
 
85
    always @ (posedge clk)
86
      begin
87 2 homer.xing
        if (reset) K <= 7'b1000000;
88 8 homer.xing
        else if (p | K[0]) K <= {1'b0,K[6:1]};
89 2 homer.xing
      end
90
 
91
    always @ (posedge clk)
92
      begin
93
        if (e0) x0 <= o; // x0 == a2*b2
94
        if (e1) x1 <= o; // x1 == (a2+a1)*(b2+b1)
95
        if (e2) x2 <= o; // x2 == a1*b1
96
        if (e3) x3 <= o; // x3 == (a2+a0)*(b2+b0)
97
        if (e4) x4 <= o; // x4 == (a1+a0)*(b1+b0)
98
        if (e5) x5 <= o; // x5 == a0*b0
99
      end
100
 
101
    always @ (posedge clk)
102
      begin
103
        if (reset) done <= 0;
104
        else if (K[0])
105
          begin
106
            done <= 1; c <= {c2,c1,c0};
107
          end
108
      end
109
 
110
    always @ (posedge clk)
111
      begin
112
        if (rst) mult_reset <= 1;
113
        else if (mult_done) mult_reset <= 1;
114
        else mult_reset <= 0;
115
      end
116
 
117
    always @ (posedge clk)
118
      begin
119
        delay2 <= delay1; delay1 <= reset;
120
      end
121
endmodule
122
 
123
// c == a^3 in GF(3^{6M})
124
module f36m_cubic(clk, a, c);
125
    input clk;
126
    input [`W6:0] a;
127
    output reg [`W6:0] c;
128
    wire [`W2:0] a0,a1,a2,v0,v1,v2,v3,c0,c1,c2;
129
 
130
    assign {a2,a1,a0} = a;
131
    assign c2 = v2; // c2 == a2^3
132
 
133
    f32m_cubic
134
        ins1 (clk, a0, v0), // v0 == a0^3
135
        ins2 (clk, a1, v1), // v0 == a1^3
136
        ins3 (clk, a2, v2); // v0 == a2^3
137
    f32m_add
138
        ins4 (v0, v1, v3), // v3 == v0+v1 = a0^3 + a1^3
139
        ins5 (v2, v3, c0); // c0 == a0^3 + a1^3 + a2^3
140
    f32m_sub
141
        ins6 (v1, v2, c1); // c1 == a1^3 - a2^3
142
 
143
    always @ (posedge clk)
144
        c <= {c2,c1,c0};
145
endmodule
146 8 homer.xing
 
147
// c == a ^ { 3^{3*M} - 1 } in GF(3^{6M})
148
module second_part(clk, reset, a, c, done);
149
    input clk, reset;
150
    input [`W6:0] a;
151
    output reg [`W6:0] c;
152
    output reg done;
153
 
154
    reg [3:0] K;
155
    wire [`WIDTH:0] d0,d1,d2,d3,d4,d5,
156
                    c0,c1,c2,c3,c4,c5;
157
    wire [`W3:0] a0,a1,b0,b1,
158
                 v1,v2,v3,v4,v5,v6,v7,v8,nv6;
159
    wire [1:0] v9,v10;
160
    wire rst1, rst2, rst3, done1, done2, done3;
161
 
162
    assign {d5,d4,d3,d2,d1,d0} = a;
163
    assign {a1,a0} = {d5,d3,d1,d4,d2,d0}; // change basis
164
    assign {b1,b0} = {{v8[`W3:2],v10}, {v7[`W3:2],v9}};
165
    assign {c5,c3,c1,c4,c2,c0} = {b1,b0}; // change basis back
166
    assign rst1 = reset;
167
 
168
    f33m_mult2
169
        ins1 (clk, rst1,
170
              a0, a0, v1, // v1 == a0^2
171
              a1, a1, v2, // v2 == a1^2
172
              done1);
173
    f33m_add
174
        ins2 (v1, v2, v3), // v3 == v1+v2 == a0^2+a1^2
175
        ins3 (a0, a1, v5); // v5 == a0+a1
176
    f33m_inv
177
        ins4 (clk, rst2, v3, v4, done2); // v4 == v3^{-1} == (a0^2+a1^2)^{-1}
178
    f33m_neg
179
        ins5 (v6, nv6); // nv6 == -v6 == -(a0+a1)^2
180
    f33m_mult3 // ****** $v8$ depends on $v6$ ******
181
        ins6 (clk, rst3,
182
              v5, v5, v6, // v6 == v5^2 == (a0+a1)^2
183
              v2, v4, v7, // v7 == v2*v4 == (a1^2)*{(a0^2+a1^2)^{-1}}
184
              nv6, v4, v8, // v8 == -v6*v4
185
              done3);
186
    f3_add1
187
        ins7 (v7[1:0], v9), // v9 == v7[1:0]+1
188
        ins8 (v8[1:0], v10); // v10 == v8[1:0]+1
189
    func6
190
        ins9  (clk, reset, done1, rst2),
191
        ins10 (clk, reset, done2, rst3);
192
 
193
    always @ (posedge clk)
194
        if (reset) K <= 4'b1000;
195
        else if ((K[3]&rst2)|(K[2]&rst3)|(K[1]&done3)|K[0])
196
            K <= K >> 1;
197
 
198
    always @ (posedge clk)
199
        if (reset) done <= 0;
200
        else if (K[0])
201
          begin
202
            done <= 1; c <= {c5,c4,c3,c2,c1,c0};
203
          end
204
endmodule

powered by: WebSVN 2.1.0

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