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

Subversion Repositories pairing

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

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

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

powered by: WebSVN 2.1.0

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