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

Subversion Repositories tiny_tate_bilinear_pairing

[/] [tiny_tate_bilinear_pairing/] [trunk/] [group_size_is_697_bits/] [rtl/] [pe.v] - Blame information for rev 18

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 18 homer.hsin
/*
2
 * Copyright 2012, Homer Hsing <homer.hsing@gmail.com>
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
`define M     503         // M is the degree of the irreducible polynomial
18
`define WIDTH (2*`M-1)    // width for a GF(3^M) element
19
`define WIDTH_D0 (1008-1)
20
 
21
/* PE: processing element */
22
module PE(clk, reset, ctrl, d0, d1, d2, out);
23
    input clk;
24
    input reset;
25
    input [10:0] ctrl;
26
    input [`WIDTH_D0:0] d0;
27
    input [`WIDTH:0] d1, d2;
28
    output [`WIDTH:0] out;
29
 
30
    reg [`WIDTH_D0:0] R0;
31
    reg [`WIDTH:0] R1, R2, R3;
32
    wire [1:0] e0, e1, e2; /* part of R0 */
33
    wire [`WIDTH:0] ppg0, ppg1, ppg2, /* output of PPG */
34
                    mx0, mx1, mx2, mx3, mx4, mx5, mx6, mx7, /* output of MUX */
35
                    ad0, ad1, ad2, /* output of GF(3^m) adder */
36
                    cu0, cu1, cu2, cu3, /* output of cubic */
37
                    mo0, mo1, mo2, /* output of mod_p */
38
                    t0, t1, t2;
39
    wire c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;
40
 
41
    assign {c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10} = ctrl;
42
    assign mx0 = c0 ? d1 : ad2;
43
    assign mx1 = c2 ? d2 : ad2;
44
    always @ (posedge clk)
45
        if(reset) R1 <= 0;
46
        else if (c1) R1 <= mx0;
47
    always @ (posedge clk)
48
        if(reset) R2 <= 0;
49
        else if (c3) R2 <= mx1;
50
    always @ (posedge clk)
51
        if(reset) R0 <= 0;
52
        else if (c4) R0 <= d0;
53
        else if (c5) R0 <= R0 << 6;
54
    assign {e2,e1,e0} = R0[`WIDTH_D0:(`WIDTH_D0-5)];
55
    PPG
56
        ppg_0 (e0, R1, ppg0),
57
        ppg_1 (e1, R2, ppg1),
58
        ppg_2 (e2, R1, ppg2);
59
    v0  v0_ (ppg0, cu0);
60
    v1  v1_ (ppg1, cu1);
61
    v2  v2_ (ppg2, cu2);
62
    v3  v3_ (R2,   cu3);
63
    assign mx2 = c6 ? ppg0 : cu0;
64
    assign mx3 = c6 ? ppg1 : cu1;
65
    assign mx4 = c6 ? mo1 : cu2;
66
    assign mx5 = c7 ? mo2 : R3;
67
    mod_p
68
        mod_p_0 (mx3, mo0),
69
        mod_p_1 (ppg2, t0),
70
        mod_p_2 (t0, mo1),
71
        mod_p_3 (R3, t1),
72
        mod_p_4 (t1, t2),
73
        mod_p_5 (t2, mo2);
74
    assign mx6 = c9 ? mo0 : mx3;
75
    assign mx7 = c6 ? (c8 ? mx5 : 0) : cu3;
76
    f3m_add
77
        f3m_add_0 (mx2, mx6, ad0),
78
        f3m_add_1 (mx4, mx7, ad1),
79
        f3m_add_2 (ad0, ad1, ad2);
80
    always @ (posedge clk)
81
        if (reset) R3 <= 0;
82
        else if (c10) R3 <= ad2;
83
        else R3 <= 0; /* change */
84
    assign out = R3;
85
endmodule
86
 
87
// C = (x*B mod p(x))
88
module mod_p(B, C);
89
    input [`WIDTH:0] B;
90
    output [`WIDTH:0] C;
91
    wire [`WIDTH+2:0] A;
92
    assign A = {B[`WIDTH:0], 2'd0}; // A == B*x
93
    wire [1:0] w0;
94
    f3_mult m0 (A[1007:1006], 2'd2, w0);
95
    f3_sub s0 (A[1:0], w0, C[1:0]);
96
    assign C[207:2] = A[207:2];
97
    wire [1:0] w104;
98
    f3_mult m104 (A[1007:1006], 2'd1, w104);
99
    f3_sub s104 (A[209:208], w104, C[209:208]);
100
    assign C[1005:210] = A[1005:210];
101
endmodule
102
 
103
// PPG: partial product generator, C == A*d in GF(3^m)
104
module PPG(d, A, C);
105
    input [1:0] d;
106
    input [`WIDTH:0] A;
107
    output [`WIDTH:0] C;
108
    genvar i;
109
    generate
110
        for (i=0; i < `M; i=i+1)
111
        begin: ppg0
112
            f3_mult f3_mult_0 (d, A[2*i+1:2*i], C[2*i+1:2*i]);
113
        end
114
    endgenerate
115
endmodule
116
 
117
// f3m_add: C = A + B, in field F_{3^M}
118
module f3m_add(A, B, C);
119
    input [`WIDTH : 0] A, B;
120
    output [`WIDTH : 0] C;
121
    genvar i;
122
    generate
123
        for(i=0; i<`M; i=i+1) begin: aa
124
            f3_add aa(A[(2*i+1) : 2*i], B[(2*i+1) : 2*i], C[(2*i+1) : 2*i]);
125
        end
126
    endgenerate
127
endmodule
128
 
129
// f3_add: C == A+B (mod 3)
130
module f3_add(A, B, C);
131
    input [1:0] A, B;
132
    output [1:0] C;
133
    wire a0, a1, b0, b1, c0, c1;
134
    assign {a1, a0} = A;
135
    assign {b1, b0} = B;
136
    assign C = {c1, c0};
137
    assign c0 = ( a0 & ~a1 & ~b0 & ~b1) |
138
                (~a0 & ~a1 &  b0 & ~b1) |
139
                (~a0 &  a1 & ~b0 &  b1) ;
140
    assign c1 = (~a0 &  a1 & ~b0 & ~b1) |
141
                ( a0 & ~a1 &  b0 & ~b1) |
142
                (~a0 & ~a1 & ~b0 &  b1) ;
143
endmodule
144
 
145
// f3_sub: C == A-B (mod 3)
146
module f3_sub(A, B, C);
147
    input [1:0] A, B;
148
    output [1:0] C;
149
    f3_add a0(A, {B[0],B[1]}, C);
150
endmodule
151
 
152
// f3_mult: C = A*B (mod 3)
153
module f3_mult(A, B, C);
154
    input [1:0] A;
155
    input [1:0] B;
156
    output [1:0] C;
157
    wire a0, a1, b0, b1;
158
    assign {a1, a0} = A;
159
    assign {b1, b0} = B;
160
    assign C[0] = (~a1 & a0 & ~b1 & b0) | (a1 & ~a0 & b1 & ~b0);
161
    assign C[1] = (~a1 & a0 & b1 & ~b0) | (a1 & ~a0 & ~b1 & b0);
162
endmodule

powered by: WebSVN 2.1.0

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