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_911_bits/] [rtl/] [pe.v] - Blame information for rev 15

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 homer.hsin
/*
2 15 homer.hsin
 * 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 11 homer.hsin
 
17
`define M     593         // 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 1187
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, /* output of MUX */
35
                    ad0, ad1, ad2, /* output of GF(3^m) adder */
36
                    cu0, cu1, cu2, /* 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
    assign mx2 = c6 ? ppg0 : cu0;
63
    assign mx3 = c6 ? ppg1 : cu1;
64
    assign mx4 = c6 ? mo1 : cu2;
65
    assign mx5 = c7 ? mo2 : R3;
66
    mod_p
67
        mod_p_0 (mx3, mo0),
68
        mod_p_1 (ppg2, t0),
69
        mod_p_2 (t0, mo1),
70
        mod_p_3 (R3, t1),
71
        mod_p_4 (t1, t2),
72
        mod_p_5 (t2, mo2);
73
    assign mx6 = c9 ? mo0 : mx3;
74
    f3m_add
75
        f3m_add_0 (mx2, mx6, ad0),
76
        f3m_add_1 (mx4, c8 ? mx5 : 0, ad1),
77
        f3m_add_2 (ad0, ad1, ad2);
78
    always @ (posedge clk)
79
        if (reset) R3 <= 0;
80
        else if (c10) R3 <= ad2;
81
        else R3 <= 0; /* change */
82
    assign out = R3;
83
endmodule
84
 
85
// C = (x*B mod p(x))
86
module mod_p(B, C);
87
    input [`WIDTH:0] B;
88
    output [`WIDTH:0] C;
89
    wire [`WIDTH+2:0] A;
90
    assign A = {B[`WIDTH:0], 2'd0}; // A == B*x
91
    wire [1:0] w0;
92
    f3_mult m0 (A[1187:1186], 2'd2, w0);
93
    f3_sub s0 (A[1:0], w0, C[1:0]);
94
    assign C[223:2] = A[223:2];
95
    wire [1:0] w112;
96
    f3_mult m112 (A[1187:1186], 2'd1, w112);
97
    f3_sub s112 (A[225:224], w112, C[225:224]);
98
    assign C[1185:226] = A[1185:226];
99
endmodule
100
 
101
// PPG: partial product generator, C == A*d in GF(3^m)
102
module PPG(d, A, C);
103
    input [1:0] d;
104
    input [`WIDTH:0] A;
105
    output [`WIDTH:0] C;
106
    genvar i;
107
    generate
108
        for (i=0; i < `M; i=i+1)
109
        begin: ppg0
110
            f3_mult f3_mult_0 (d, A[2*i+1:2*i], C[2*i+1:2*i]);
111
        end
112
    endgenerate
113
endmodule
114
 
115
// f3m_add: C = A + B, in field F_{3^M}
116
module f3m_add(A, B, C);
117
    input [`WIDTH : 0] A, B;
118
    output [`WIDTH : 0] C;
119
    genvar i;
120
    generate
121
        for(i=0; i<`M; i=i+1) begin: aa
122
            f3_add aa(A[(2*i+1) : 2*i], B[(2*i+1) : 2*i], C[(2*i+1) : 2*i]);
123
        end
124
    endgenerate
125
endmodule
126
 
127
// f3_add: C == A+B (mod 3)
128
module f3_add(A, B, C);
129
    input [1:0] A, B;
130
    output [1:0] C;
131
    wire a0, a1, b0, b1, c0, c1;
132
    assign {a1, a0} = A;
133
    assign {b1, b0} = B;
134
    assign C = {c1, c0};
135
    assign c0 = ( a0 & ~a1 & ~b0 & ~b1) |
136
                (~a0 & ~a1 &  b0 & ~b1) |
137
                (~a0 &  a1 & ~b0 &  b1) ;
138
    assign c1 = (~a0 &  a1 & ~b0 & ~b1) |
139
                ( a0 & ~a1 &  b0 & ~b1) |
140
                (~a0 & ~a1 & ~b0 &  b1) ;
141
endmodule
142
 
143
// f3_sub: C == A-B (mod 3)
144
module f3_sub(A, B, C);
145
    input [1:0] A, B;
146
    output [1:0] C;
147
    f3_add a0(A, {B[0],B[1]}, C);
148
endmodule
149
 
150
// f3_mult: C = A*B (mod 3)
151
module f3_mult(A, B, C);
152
    input [1:0] A;
153
    input [1:0] B;
154
    output [1:0] C;
155
    wire a0, a1, b0, b1;
156
    assign {a1, a0} = A;
157
    assign {b1, b0} = B;
158
    assign C[0] = (~a1 & a0 & ~b1 & b0) | (a1 & ~a0 & b1 & ~b0);
159
    assign C[1] = (~a1 & a0 & b1 & ~b0) | (a1 & ~a0 & ~b1 & b0);
160
endmodule

powered by: WebSVN 2.1.0

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