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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 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 2 homer.hsin
 
17
`define M     97          // M is the degree of the irreducible polynomial
18
`define WIDTH (2*`M-1)    // width for a GF(3^M) element
19
 
20
/* PE: processing element */
21
module PE(clk, reset, ctrl, d0, d1, d2, out);
22
    input clk;
23
    input reset;
24
    input [10:0] ctrl;
25
    input [197:0] d0;
26
    input [`WIDTH:0] d1, d2;
27
    output [`WIDTH:0] out;
28
 
29
    reg [197:0] R0;
30
    reg [`WIDTH:0] R1, R2, R3;
31
    wire [1:0] e0, e1, e2; /* part of R0 */
32
    wire [`WIDTH:0] ppg0, ppg1, ppg2, /* output of PPG */
33
                    mx0, mx1, mx2, mx3, mx4, mx5, mx6, /* output of MUX */
34
                    ad0, ad1, ad2, /* output of GF(3^m) adder */
35
                    cu0, cu1, cu2, /* output of cubic */
36
                    mo0, mo1, mo2, /* output of mod_p */
37
                    t0, t1, t2;
38
    wire c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;
39
 
40
    assign {c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10} = ctrl;
41
    assign mx0 = c0 ? d1 : ad2;
42
    assign mx1 = c2 ? d2 : ad2;
43
    always @ (posedge clk)
44
        if(reset) R1 <= 0;
45
        else if (c1) R1 <= mx0;
46
    always @ (posedge clk)
47
        if(reset) R2 <= 0;
48
        else if (c3) R2 <= mx1;
49
    always @ (posedge clk)
50
        if(reset) R0 <= 0;
51
        else if (c4) R0 <= d0;
52
        else if (c5) R0 <= R0 << 6;
53
    assign {e2,e1,e0} = R0[197:192];
54
    PPG
55
        ppg_0 (e0, R1, ppg0),
56
        ppg_1 (e1, R2, ppg1),
57
        ppg_2 (e2, R1, ppg2);
58
    v0  v0_ (ppg0, cu0);
59
    v1  v1_ (ppg1, cu1);
60
    v2  v2_ (ppg2, cu2);
61
    assign mx2 = c6 ? ppg0 : cu0;
62
    assign mx3 = c6 ? ppg1 : cu1;
63
    assign mx4 = c6 ? mo1 : cu2;
64
    assign mx5 = c7 ? mo2 : R3;
65
    mod_p
66
        mod_p_0 (mx3, mo0),
67
        mod_p_1 (ppg2, t0),
68
        mod_p_2 (t0, mo1),
69
        mod_p_3 (R3, t1),
70
        mod_p_4 (t1, t2),
71
        mod_p_5 (t2, mo2);
72
    assign mx6 = c9 ? mo0 : mx3;
73
    f3m_add
74
        f3m_add_0 (mx2, mx6, ad0),
75
        f3m_add_1 (mx4, c8 ? mx5 : 0, ad1),
76
        f3m_add_2 (ad0, ad1, ad2);
77
    always @ (posedge clk)
78
        if (reset) R3 <= 0;
79
        else if (c10) R3 <= ad2;
80 3 homer.hsin
        else R3 <= 0; /* change */
81 2 homer.hsin
    assign out = R3;
82
endmodule
83
 
84
// C = (x*B mod p(x))
85
module mod_p(B, C);
86
    input [`WIDTH:0] B;
87
    output [`WIDTH:0] C;
88
    wire [`WIDTH+2:0] A;
89
    assign A = {B[`WIDTH:0], 2'd0}; // A == B*x
90
    wire [1:0] w0;
91
    f3_mult m0 (A[195:194], 2'd2, w0);
92
    f3_add s0 (A[1:0], {w0[0], w0[1]}, C[1:0]); //f3_sub s0 (A[1:0], w0, C[1:0]);
93
    assign C[23:2] = A[23:2];
94
    wire [1:0] w12;
95
    f3_mult m12 (A[195:194], 2'd1, w12);
96
    f3_add s12 (A[25:24], {w12[0], w12[1]}, C[25:24]); // f3_sub s12 (A[25:24], w12, C[25:24]);
97
    assign C[193:26] = A[193:26];
98
endmodule
99
 
100
// PPG: partial product generator, C == A*d in GF(3^m)
101
module PPG(d, A, C);
102
    input [1:0] d;
103
    input [`WIDTH:0] A;
104
    output [`WIDTH:0] C;
105
    genvar i;
106
    generate
107
        for (i=0; i < `M; i=i+1)
108
        begin: ppg0
109
            f3_mult f3_mult_0 (d, A[2*i+1:2*i], C[2*i+1:2*i]);
110
        end
111
    endgenerate
112
endmodule
113
 
114
// f3m_add: C = A + B, in field F_{3^M}
115
module f3m_add(A, B, C);
116
    input [`WIDTH : 0] A, B;
117
    output [`WIDTH : 0] C;
118
    genvar i;
119
    generate
120
        for(i=0; i<`M; i=i+1) begin: aa
121
            f3_add aa(A[(2*i+1) : 2*i], B[(2*i+1) : 2*i], C[(2*i+1) : 2*i]);
122
        end
123
    endgenerate
124
endmodule
125
 
126
// f3_add: C == A+B (mod 3)
127
module f3_add(A, B, C);
128
    input [1:0] A, B;
129
    output [1:0] C;
130
    wire a0, a1, b0, b1, c0, c1;
131
    assign {a1, a0} = A;
132
    assign {b1, b0} = B;
133
    assign C = {c1, c0};
134
    assign c0 = ( a0 & ~a1 & ~b0 & ~b1) |
135
                (~a0 & ~a1 &  b0 & ~b1) |
136
                (~a0 &  a1 & ~b0 &  b1) ;
137
    assign c1 = (~a0 &  a1 & ~b0 & ~b1) |
138
                ( a0 & ~a1 &  b0 & ~b1) |
139
                (~a0 & ~a1 & ~b0 &  b1) ;
140
endmodule
141
 
142
// f3_mult: C = A*B (mod 3)
143
module f3_mult(A, B, C);
144
    input [1:0] A;
145
    input [1:0] B;
146
    output [1:0] C;
147
    wire a0, a1, b0, b1;
148
    assign {a1, a0} = A;
149
    assign {b1, b0} = B;
150
    assign C[0] = (~a1 & a0 & ~b1 & b0) | (a1 & ~a0 & b1 & ~b0);
151
    assign C[1] = (~a1 & a0 & b1 & ~b0) | (a1 & ~a0 & ~b1 & b0);
152
endmodule

powered by: WebSVN 2.1.0

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