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

Subversion Repositories pairing

[/] [pairing/] [trunk/] [rtl/] [f32m.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 3 homer.xing
// out = (v0 & l0) | (v1 & l1) | (v2 & l2) | ... | (v5 & l5)
24
module f32m_mux6(v0, v1, v2, v3, v4, v5, l0, l1, l2, l3, l4, l5, out);
25
    input l0, l1, l2, l3, l4, l5;
26
    input [`W2:0] v0, v1, v2, v3, v4, v5;
27
    output [`W2:0] out;
28
    f3m_mux6
29
        ins1 (v0[`WIDTH:0], v1[`WIDTH:0], v2[`WIDTH:0],
30
              v3[`WIDTH:0], v4[`WIDTH:0], v5[`WIDTH:0],
31
              l0, l1, l2, l3, l4, l5,
32
              out[`WIDTH:0]),
33
        ins2 (v0[`W2:`WIDTH+1], v1[`W2:`WIDTH+1], v2[`W2:`WIDTH+1],
34
              v3[`W2:`WIDTH+1], v4[`W2:`WIDTH+1], v5[`W2:`WIDTH+1],
35
              l0, l1, l2, l3, l4, l5,
36
              out[`W2:`WIDTH+1]);
37
endmodule
38
 
39 2 homer.xing
// C == A+B in GF(3^{2M})
40
module f32m_add(a, b, c);
41
    input [`W2:0] a, b;
42
    output [`W2:0] c;
43 3 homer.xing
    f3m_add
44
        a1 (a[`W2:`WIDTH+1], b[`W2:`WIDTH+1], c[`W2:`WIDTH+1]),
45
        a2 (a[`WIDTH:0], b[`WIDTH:0], c[`WIDTH:0]);
46 2 homer.xing
endmodule
47
 
48 3 homer.xing
// C = a0 + a1 + a2 in GF(3^{2M})
49
module f32m_add3(a0, a1, a2, c);
50
    input [`W2:0] a0, a1, a2;
51
    output [`W2:0] c;
52
    wire [`W2:0] t;
53
    f32m_add
54
        ins1 (a0, a1, t), // t == a0+a1
55
        ins2 (t, a2, c);  // c == t+a2 == a0+a1+a2
56
endmodule
57
 
58
// C = a0 + a1 + a2 + a3 in GF(3^{2M})
59
module f32m_add4(a0, a1, a2, a3, c);
60
    input [`W2:0] a0, a1, a2, a3;
61
    output [`W2:0] c;
62
    wire [`W2:0] t1, t2;
63
    f32m_add
64
        ins1 (a0, a1, t1), // t1 == a0+a1
65
        ins2 (a2, a3, t2), // t2 == a2+a3
66
        ins3 (t1, t2, c);  // c == t1+t2 == a0+a1+a2+a3
67
endmodule
68
 
69
// c == -a in GF(3^{2M})
70
module f32m_neg(a, c);
71
    input [`W2:0] a;
72
    output [`W2:0] c;
73
    f3m_neg
74
        n1 (a[`W2:`WIDTH+1], c[`W2:`WIDTH+1]),
75
        n2 (a[`WIDTH:0], c[`WIDTH:0]);
76
endmodule
77
 
78 2 homer.xing
// C == A-B in GF(3^{2M})
79
module f32m_sub(a, b, c);
80
    input [`W2:0] a, b;
81
    output [`W2:0] c;
82 3 homer.xing
    f3m_sub
83
        s1 (a[`W2:`WIDTH+1], b[`W2:`WIDTH+1], c[`W2:`WIDTH+1]),
84
        s2 (a[`WIDTH:0], b[`WIDTH:0], c[`WIDTH:0]);
85 2 homer.xing
endmodule
86
 
87
// C == A*B in GF(3^{2M})
88
module f32m_mult(clk, reset, a, b, c, done);
89
    input reset, clk;
90
    input [`W2:0] a, b;
91
    output reg [`W2:0] c;
92
    output reg done;
93 7 homer.xing
    wire [`WIDTH:0] a0,a1,b0,b1,c0,c1,
94
                    v1,v2,v3,v4,v5,v6;
95 2 homer.xing
    reg mult_reset;
96 7 homer.xing
    wire mult_done, p;
97 2 homer.xing
 
98
    assign {a1,a0} = a;
99
    assign {b1,b0} = b;
100
 
101
    f3m_add
102
        ins1 (a0, a1, v1), // v1 == a0 + a1
103
        ins2 (b0, b1, v2), // v2 == b0 + b1
104
        ins3 (v3, v4, v6); // v6 == v3 + v4 = a0*b0 + a1*b1
105
    f3m_sub
106
        ins7 (v5, v6, c1), // c1 == v5 - v6 = (a0+a1) * (b0+b1) - (a0*b0 + a1*b1)
107
        ins8 (v3, v4, c0); // c0 == a0*b0 - a1*b1
108
    // v3 == a0 * b0
109
    // v4 == a1 * b1
110
    // v5 == v1 * v2 = (a0+a1) * (b0+b1)
111 7 homer.xing
    f3m_mult3
112
        ins9 (clk, mult_reset, a0, b0, v3, a1, b1, v4, v1, v2, v5, mult_done);
113 2 homer.xing
    func6
114 8 homer.xing
        ins10 (clk, reset, mult_done, p);
115 2 homer.xing
 
116
    always @ (posedge clk)
117 7 homer.xing
        mult_reset <= reset;
118 2 homer.xing
 
119
    always @ (posedge clk)
120 7 homer.xing
        if (reset)
121
            done <= 0;
122
        else if (p)
123
          begin
124
            done <= 1; c <= {c1, c0};
125
          end
126 2 homer.xing
endmodule
127
 
128
// C == A^3 in GF(3^{2m})
129
module f32m_cubic(clk, a, c);
130
    input clk;
131
    input [`W2:0] a;
132
    output reg [`W2:0] c;
133
    wire [`WIDTH:0] a0,a1,c0,c1,v;
134
    assign {a1,a0} = a;
135
    f3m_cubic
136
        ins1 (a0, c0), // c0 == a0^3
137
        ins2 (a1, v);  // v == a1^3
138
    f3m_neg
139
        ins3 (v, c1);  // c1 == -v == - a1^3
140
    always @ (posedge clk)
141
        c <= {c1,c0};
142
endmodule

powered by: WebSVN 2.1.0

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