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

Subversion Repositories pairing

[/] [pairing/] [trunk/] [rtl/] [f3m.v] - Blame information for rev 5

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

Line No. Rev Author Line
1 2 homer.xing
`include "inc.v"
2
`define MOST 2*`M+1:2*`M
3
 
4 3 homer.xing
// out = (v1 & l1) | (v2 & l2)
5
module f3m_mux2(v1, l1, v2, l2, out);
6
    input [`WIDTH:0] v1, v2;
7
    input l1, l2;
8
    output [`WIDTH:0] out;
9
    genvar i;
10
    generate
11
        for(i=0;i<=`WIDTH;i=i+1)
12
          begin : label
13
            assign out[i] = (v1[i] & l1) | (v2[i] & l2);
14
          end
15
    endgenerate
16
endmodule
17
 
18
// out = (v1 & l1) | (v2 & l2) | (v3 & l3)
19
module f3m_mux3(v1, l1, v2, l2, v3, l3, out);
20
    input [`WIDTH:0] v1, v2, v3;
21
    input l1, l2, l3;
22
    output [`WIDTH:0] out;
23
    genvar i;
24
    generate
25
        for(i=0;i<=`WIDTH;i=i+1)
26
          begin : label
27
            assign out[i] = (v1[i] & l1) | (v2[i] & l2) | (v3[i] & l3);
28
          end
29
    endgenerate
30
endmodule
31
 
32
// out = (v0 & l0) | (v1 & l1) | (v2 & l2) | ... | (v5 & l5)
33
module f3m_mux6(v0, v1, v2, v3, v4, v5, l0, l1, l2, l3, l4, l5, out);
34
    input l0, l1, l2, l3, l4, l5;
35
    input [`WIDTH:0] v0, v1, v2, v3, v4, v5;
36
    output reg [`WIDTH:0] out;
37
    always @ (l0,l1,l2,l3,l4,l5,v0,v1,v2,v3,v4,v5)
38
      case ({l0,l1,l2,l3,l4,l5})
39
        6'b100000: out = v0;
40
        6'b010000: out = v1;
41
        6'b001000: out = v2;
42
        6'b000100: out = v3;
43
        6'b000010: out = v4;
44
        6'b000001: out = v5;
45
        default: out = 0;
46
      endcase
47
endmodule
48
 
49 2 homer.xing
// f3m_add: C = A + B, in field F_{3^M}
50
module f3m_add(A, B, C);
51
    input [`WIDTH : 0] A, B;
52
    output [`WIDTH : 0] C;
53
    genvar i;
54
    generate
55
        for(i=0; i<`M; i=i+1) begin: aa
56
            f3_add aa(A[(2*i+1) : 2*i], B[(2*i+1) : 2*i], C[(2*i+1) : 2*i]);
57
        end
58
    endgenerate
59
endmodule
60
 
61 3 homer.xing
// f3m_add3: c == a0+a1+a2, in field GF(3^M)
62
module f3m_add3(a0, a1, a2, c);
63
    input [`WIDTH:0] a0,a1,a2;
64
    output [`WIDTH:0] c;
65
    wire [`WIDTH:0] v;
66
    f3m_add
67
        ins1 (a0,a1,v), // v == a0+a1
68
        ins2 (v,a2,c);  // c == v+a2 == a0+a1+a2
69
endmodule
70
 
71
// f3m_add4: c == a0+a1+a2+a3, in field GF(3^M)
72
module f3m_add4(a0, a1, a2, a3, c);
73
    input [`WIDTH:0] a0,a1,a2,a3;
74
    output [`WIDTH:0] c;
75
    wire [`WIDTH:0] v1,v2;
76
    f3m_add
77
        ins1 (a0,a1,v1), // v1 == a0+a1
78
        ins2 (a2,a3,v2), // v2 == a2+a3
79
        ins3 (v1,v2,c);  // c == v1+v2 == a0+a1+a2+a3
80
endmodule
81
 
82 2 homer.xing
// f3m_neg: c == -a in GF(3^M)
83
module f3m_neg(a, c);
84
    input [`WIDTH:0] a;
85
    output [`WIDTH:0] c;
86
    genvar i;
87
    generate
88
        for(i=0;i<=`WIDTH;i=i+2)
89
          begin:label
90
            assign c[i+1:i] = {a[i],a[i+1]};
91
          end
92
    endgenerate
93
endmodule
94
 
95
// f3m_sub: C = A - B, in field F_{3^M}
96
module f3m_sub(A, B, C);
97
    input [`WIDTH : 0] A, B;
98
    output [`WIDTH : 0] C;
99
    genvar i;
100
    generate
101
        for(i=0; i<`M; i=i+1) begin: aa
102
            f3_sub aa(A[(2*i+1) : 2*i], B[(2*i+1) : 2*i], C[(2*i+1) : 2*i]);
103
        end
104
    endgenerate
105
endmodule
106
 
107
// f3m_mult: C = A * B, in field GF(3^M)
108
module f3m_mult(clk, reset, A, B, C, done);
109
    input [`WIDTH : 0] A, B;
110
    input clk;
111
    input reset;
112
    output reg [`WIDTH : 0] C;
113
    output reg done;
114
    reg [`WIDTH : 0] x, y, z;
115
    wire [`WIDTH : 0] z1, z2, z4;
116
    wire [`WIDTH+2 : 0] z3;
117
    reg [`M+1 : 0] i;
118
    wire [`M+1 : 0] i1;
119
    wire done1;
120
    wire [1:0] dummy;
121
 
122
    func4
123
        ins1 ({2'b0,x}, y[1:0], {dummy,z1}); // z1 == A * B[0]
124
    f3m_add
125
        ins2 (z1, z, z2); // z2 == z1 + z == A*B[0] + z
126
    assign z4 = {2'd0, y[`WIDTH:2]}; // z4 == y >> 2
127
    func3
128
        ins3 ({2'd0,x}, z3); // z3 == X*x mod p(x)
129
    assign i1 = {1'b0, i[`M+1:1]}; // i1 == i >> 1
130
    assign done1 = (i1[1:0] == 2'b01);
131
 
132
    always @ (posedge clk)
133
        if (done1)
134
          begin
135
            C <= z;
136
          end
137
 
138
    always @ (posedge clk)
139
        if (reset)
140
            done <= 0;
141
        else if (done1)
142
            done <= 1;
143
 
144
    always @ (posedge clk)
145
      begin
146
        if (reset)
147
          begin
148
            x <= A; y <= B; z <= 0; i <= ~0;
149
          end
150
        else
151
          begin
152
            x <= z3[`WIDTH:0]; y <= z4; z <= z2; i <= i1;
153
          end
154
      end
155
endmodule
156
 
157
/* out == in^3 mod p(x) */
158
/* p(x) == x^97 + x^12 + 2 */
159
module f3m_cubic(input [193:0] in, output [193:0] out);
160
wire [1:0] w0; f3_add a0(in[131:130], in[139:138], w0);
161
wire [1:0] w1; f3_add a1(in[133:132], in[141:140], w1);
162
wire [1:0] w2; f3_add a2(in[135:134], in[143:142], w2);
163
wire [1:0] w3; f3_add a3(in[137:136], in[145:144], w3);
164
wire [1:0] w4; f3_add a4(in[147:146], in[155:154], w4);
165
wire [1:0] w5; f3_add a5(in[149:148], in[157:156], w5);
166
wire [1:0] w6; f3_add a6(in[151:150], in[159:158], w6);
167
wire [1:0] w7; f3_add a7(in[153:152], in[161:160], w7);
168
wire [1:0] w8; f3_add a8(in[163:162], in[171:170], w8);
169
wire [1:0] w9; f3_add a9(in[165:164], in[173:172], w9);
170
wire [1:0] w10; f3_add a10(in[167:166], in[175:174], w10);
171
wire [1:0] w11; f3_add a11(in[169:168], in[177:176], w11);
172
wire [1:0] w12; f3_add a12(in[179:178], in[187:186], w12);
173
wire [1:0] w13; f3_add a13(in[181:180], in[189:188], w13);
174
wire [1:0] w14; f3_add a14(in[183:182], in[191:190], w14);
175
wire [1:0] w15; f3_add a15(in[185:184], in[193:192], w15);
176
wire [1:0] w16;
177
f3_add a16(in[1:0], w12, w16);
178
assign out[1:0] = w16;
179
wire [1:0] w17;
180
f3_add a17({in[122],in[123]}, in[131:130], w17);
181
assign out[3:2] = w17;
182
assign out[5:4] = in[67:66];
183
wire [1:0] w18;
184
f3_add a18(in[3:2], w13, w18);
185
assign out[7:6] = w18;
186
wire [1:0] w19;
187
f3_add a19({in[124],in[125]}, in[133:132], w19);
188
assign out[9:8] = w19;
189
assign out[11:10] = in[69:68];
190
wire [1:0] w20;
191
f3_add a20(in[5:4], w14, w20);
192
assign out[13:12] = w20;
193
wire [1:0] w21;
194
f3_add a21({in[126],in[127]}, in[135:134], w21);
195
assign out[15:14] = w21;
196
assign out[17:16] = in[71:70];
197
wire [1:0] w22;
198
f3_add a22(in[7:6], w15, w22);
199
assign out[19:18] = w22;
200
wire [1:0] w23;
201
f3_add a23({in[128],in[129]}, in[137:136], w23);
202
assign out[21:20] = w23;
203
assign out[23:22] = in[73:72];
204
wire [1:0] w24;
205
f3_add a24(in[9:8], {in[178],in[179]}, w24);
206
assign out[25:24] = w24;
207
wire [1:0] w25;
208
f3_add a25(in[123:122], w0, w25);
209
assign out[27:26] = w25;
210
wire [1:0] w26;
211
f3_add a26({in[66],in[67]}, in[75:74], w26);
212
assign out[29:28] = w26;
213
wire [1:0] w27;
214
f3_add a27(in[11:10], {in[180],in[181]}, w27);
215
assign out[31:30] = w27;
216
wire [1:0] w28;
217
f3_add a28(in[125:124], w1, w28);
218
assign out[33:32] = w28;
219
wire [1:0] w29;
220
f3_add a29({in[68],in[69]}, in[77:76], w29);
221
assign out[35:34] = w29;
222
wire [1:0] w30;
223
f3_add a30(in[13:12], {in[182],in[183]}, w30);
224
assign out[37:36] = w30;
225
wire [1:0] w31;
226
f3_add a31(in[127:126], w2, w31);
227
assign out[39:38] = w31;
228
wire [1:0] w32;
229
f3_add a32({in[70],in[71]}, in[79:78], w32);
230
assign out[41:40] = w32;
231
wire [1:0] w33;
232
f3_add a33(in[15:14], {in[184],in[185]}, w33);
233
assign out[43:42] = w33;
234
wire [1:0] w34;
235
f3_add a34(in[129:128], w3, w34);
236
assign out[45:44] = w34;
237
wire [1:0] w35;
238
f3_add a35({in[72],in[73]}, in[81:80], w35);
239
assign out[47:46] = w35;
240
wire [1:0] w36;
241
f3_add a36(in[17:16], {in[186],in[187]}, w36);
242
assign out[49:48] = w36;
243
wire [1:0] w37;
244
f3_add a37(in[147:146], w0, w37);
245
assign out[51:50] = w37;
246
wire [1:0] w38;
247
f3_add a38({in[74],in[75]}, in[83:82], w38);
248
assign out[53:52] = w38;
249
wire [1:0] w39;
250
f3_add a39(in[19:18], {in[188],in[189]}, w39);
251
assign out[55:54] = w39;
252
wire [1:0] w40;
253
f3_add a40(in[149:148], w1, w40);
254
assign out[57:56] = w40;
255
wire [1:0] w41;
256
f3_add a41({in[76],in[77]}, in[85:84], w41);
257
assign out[59:58] = w41;
258
wire [1:0] w42;
259
f3_add a42(in[21:20], {in[190],in[191]}, w42);
260
assign out[61:60] = w42;
261
wire [1:0] w43;
262
f3_add a43(in[151:150], w2, w43);
263
assign out[63:62] = w43;
264
wire [1:0] w44;
265
f3_add a44({in[78],in[79]}, in[87:86], w44);
266
assign out[65:64] = w44;
267
wire [1:0] w45;
268
f3_add a45(in[23:22], {in[192],in[193]}, w45);
269
assign out[67:66] = w45;
270
wire [1:0] w46;
271
f3_add a46(in[153:152], w3, w46);
272
assign out[69:68] = w46;
273
wire [1:0] w47;
274
f3_add a47({in[80],in[81]}, in[89:88], w47);
275
assign out[71:70] = w47;
276
assign out[73:72] = in[25:24];
277
wire [1:0] w48;
278
f3_add a48(in[139:138], w4, w48);
279
assign out[75:74] = w48;
280
wire [1:0] w49;
281
f3_add a49({in[82],in[83]}, in[91:90], w49);
282
assign out[77:76] = w49;
283
assign out[79:78] = in[27:26];
284
wire [1:0] w50;
285
f3_add a50(in[141:140], w5, w50);
286
assign out[81:80] = w50;
287
wire [1:0] w51;
288
f3_add a51({in[84],in[85]}, in[93:92], w51);
289
assign out[83:82] = w51;
290
assign out[85:84] = in[29:28];
291
wire [1:0] w52;
292
f3_add a52(in[143:142], w6, w52);
293
assign out[87:86] = w52;
294
wire [1:0] w53;
295
f3_add a53({in[86],in[87]}, in[95:94], w53);
296
assign out[89:88] = w53;
297
assign out[91:90] = in[31:30];
298
wire [1:0] w54;
299
f3_add a54(in[145:144], w7, w54);
300
assign out[93:92] = w54;
301
wire [1:0] w55;
302
f3_add a55({in[88],in[89]}, in[97:96], w55);
303
assign out[95:94] = w55;
304
assign out[97:96] = in[33:32];
305
wire [1:0] w56;
306
f3_add a56(in[163:162], w4, w56);
307
assign out[99:98] = w56;
308
wire [1:0] w57;
309
f3_add a57({in[90],in[91]}, in[99:98], w57);
310
assign out[101:100] = w57;
311
assign out[103:102] = in[35:34];
312
wire [1:0] w58;
313
f3_add a58(in[165:164], w5, w58);
314
assign out[105:104] = w58;
315
wire [1:0] w59;
316
f3_add a59({in[92],in[93]}, in[101:100], w59);
317
assign out[107:106] = w59;
318
assign out[109:108] = in[37:36];
319
wire [1:0] w60;
320
f3_add a60(in[167:166], w6, w60);
321
assign out[111:110] = w60;
322
wire [1:0] w61;
323
f3_add a61({in[94],in[95]}, in[103:102], w61);
324
assign out[113:112] = w61;
325
assign out[115:114] = in[39:38];
326
wire [1:0] w62;
327
f3_add a62(in[169:168], w7, w62);
328
assign out[117:116] = w62;
329
wire [1:0] w63;
330
f3_add a63({in[96],in[97]}, in[105:104], w63);
331
assign out[119:118] = w63;
332
assign out[121:120] = in[41:40];
333
wire [1:0] w64;
334
f3_add a64(in[155:154], w8, w64);
335
assign out[123:122] = w64;
336
wire [1:0] w65;
337
f3_add a65({in[98],in[99]}, in[107:106], w65);
338
assign out[125:124] = w65;
339
assign out[127:126] = in[43:42];
340
wire [1:0] w66;
341
f3_add a66(in[157:156], w9, w66);
342
assign out[129:128] = w66;
343
wire [1:0] w67;
344
f3_add a67({in[100],in[101]}, in[109:108], w67);
345
assign out[131:130] = w67;
346
assign out[133:132] = in[45:44];
347
wire [1:0] w68;
348
f3_add a68(in[159:158], w10, w68);
349
assign out[135:134] = w68;
350
wire [1:0] w69;
351
f3_add a69({in[102],in[103]}, in[111:110], w69);
352
assign out[137:136] = w69;
353
assign out[139:138] = in[47:46];
354
wire [1:0] w70;
355
f3_add a70(in[161:160], w11, w70);
356
assign out[141:140] = w70;
357
wire [1:0] w71;
358
f3_add a71({in[104],in[105]}, in[113:112], w71);
359
assign out[143:142] = w71;
360
assign out[145:144] = in[49:48];
361
wire [1:0] w72;
362
f3_add a72(in[179:178], w8, w72);
363
assign out[147:146] = w72;
364
wire [1:0] w73;
365
f3_add a73({in[106],in[107]}, in[115:114], w73);
366
assign out[149:148] = w73;
367
assign out[151:150] = in[51:50];
368
wire [1:0] w74;
369
f3_add a74(in[181:180], w9, w74);
370
assign out[153:152] = w74;
371
wire [1:0] w75;
372
f3_add a75({in[108],in[109]}, in[117:116], w75);
373
assign out[155:154] = w75;
374
assign out[157:156] = in[53:52];
375
wire [1:0] w76;
376
f3_add a76(in[183:182], w10, w76);
377
assign out[159:158] = w76;
378
wire [1:0] w77;
379
f3_add a77({in[110],in[111]}, in[119:118], w77);
380
assign out[161:160] = w77;
381
assign out[163:162] = in[55:54];
382
wire [1:0] w78;
383
f3_add a78(in[185:184], w11, w78);
384
assign out[165:164] = w78;
385
wire [1:0] w79;
386
f3_add a79({in[112],in[113]}, in[121:120], w79);
387
assign out[167:166] = w79;
388
assign out[169:168] = in[57:56];
389
wire [1:0] w80;
390
f3_add a80(in[171:170], w12, w80);
391
assign out[171:170] = w80;
392
wire [1:0] w81;
393
f3_add a81({in[114],in[115]}, in[123:122], w81);
394
assign out[173:172] = w81;
395
assign out[175:174] = in[59:58];
396
wire [1:0] w82;
397
f3_add a82(in[173:172], w13, w82);
398
assign out[177:176] = w82;
399
wire [1:0] w83;
400
f3_add a83({in[116],in[117]}, in[125:124], w83);
401
assign out[179:178] = w83;
402
assign out[181:180] = in[61:60];
403
wire [1:0] w84;
404
f3_add a84(in[175:174], w14, w84);
405
assign out[183:182] = w84;
406
wire [1:0] w85;
407
f3_add a85({in[118],in[119]}, in[127:126], w85);
408
assign out[185:184] = w85;
409
assign out[187:186] = in[63:62];
410
wire [1:0] w86;
411
f3_add a86(in[177:176], w15, w86);
412
assign out[189:188] = w86;
413
wire [1:0] w87;
414
f3_add a87({in[120],in[121]}, in[129:128], w87);
415
assign out[191:190] = w87;
416
assign out[193:192] = in[65:64];
417
endmodule
418
 
419
/* nine square in GF(3^m), out = in^9 mod p(x) */
420
/* p(x) == x^97 + x^12 + 2 */
421
module f3m_nine(clk, in, out);
422
    input clk;
423
    input [`WIDTH:0] in;
424
    output reg [`WIDTH:0] out;
425
    wire [`WIDTH:0] a,b;
426
    f3m_cubic
427
        ins1 (in, a), // a == in^3
428
        ins2 (a, b);  // b == a^3 == in^9
429
    always @ (posedge clk)
430
        out <= b;
431
endmodule
432
 
433
// inversion in GF(3^m). C = A^(-1)
434
module f3m_inv(clk, reset, A, C);
435
        input [`WIDTH:0] A;
436
        input clk;
437
        input reset;
438
        output reg [`WIDTH:0] C;
439
 
440
        reg [`WIDTH+2:0] S, R, U, V, d, i;
441
        wire [1:0] q;
442
        wire [`WIDTH+2:0] S1, S2,
443
                          R1,
444
                          U1, U2, U3,
445
                          V1, V2,
446
                          d1, d2,
447
                      i1;
448
        wire done;
449
 
450
        assign d1 = {d[`WIDTH+1:0], 1'b1}; // d1 == d+1
451
        assign d2 = {1'b0, d[`WIDTH+2:1]}; // d2 == d-1
452
    assign i1 = {1'b0, i[`WIDTH+2:1]}; // i1 == i-1
453
        assign done = (i[2:1] == 2'b01);
454
 
455
        always @ (posedge clk)
456
            if (done)
457
          begin
458
                C <= U2[`WIDTH:0];
459
          end
460
 
461
        f3_mult
462
            q1(S[`MOST], R[`MOST], q); // q = s_m / r_m
463
        func1
464
            ins1(S, R, q, S1), // S1 = S - q*R
465
            ins2(V, U, q, V1); // V1 = V - q*U
466
        func2
467
            ins3(S1, S2), // S2 = x*S1 = x*(S-q*R)
468
            ins4(R, R1); // R1 = x*R
469
        func3
470
            ins5(U, U1), // U1 = x*U mod p
471
            ins6(V1, V2); // V2 = x*V1 mod p = x*(V-qU) mod p
472
    func4
473
        ins7(U, R[`MOST], U2); // U2 = U/r_m
474
    func5
475
        ins8(U, U3); // U3 = (U/x) mod p
476
 
477
    always @ (posedge clk)
478
      begin
479
        if (reset)
480
            i <= ~0;
481
        else
482
          begin
483
            i <= i1;
484
          end
485
      end
486
 
487
    always @ (posedge clk)
488
      begin
489
        if (reset)
490
          begin
491
            S<=`PX; R<=A; U<=1; V<=0; d<=0;
492
          end
493
        else if (R[`MOST] == 2'b0)
494
          begin
495
            R<=R1; U<=U1; d<=d1;
496
          end
497
        else if (d[0] == 1'b0) // d == 0
498
          begin
499
            R<=S2; S<=R; U<=V2; V<=U; d<=d1;
500
          end
501
        else // d != 0
502
          begin
503
            S<=S2; V<=V1; U<=U3; d<=d2;
504
          end
505
      end
506
endmodule
507
 
508 3 homer.xing
// put func1~5 here for breaking circular dependency in "f3m", "fun"
509
 
510
// out = S - q*R
511
module func1(S, R, q, out);
512
    input [`WIDTH+2:0] S, R;
513
    input [1:0] q;
514
    output [`WIDTH+2:0] out;
515
    wire [`WIDTH+2:0] t;
516
    func4 f(R, q, t); // t == q*R
517
    genvar i;
518
    generate for(i=0; i<=`WIDTH+2; i=i+2) begin: label
519
        f3_sub s1(S[i+1:i], t[i+1:i], out[i+1:i]); // out == S - t
520
    end endgenerate
521
endmodule
522
 
523
// out = x*A
524
module func2(A, out);
525
    input [`WIDTH+2:0] A;
526
    output [`WIDTH+2:0] out;
527
    assign out = {A[`WIDTH:0], 2'd0};
528
endmodule
529
 
530
// C = (x*B mod p(x))
531
module func3(B, C);
532
    input [`WIDTH+2:0] B;
533
    output [`WIDTH+2:0] C;
534
    wire [`WIDTH+2:0] A;
535
    assign A = {B[`WIDTH:0], 2'd0}; // A == B*x
536
    wire [1:0] w0;
537
    f3_mult m0 (A[195:194], 2'd2, w0);
538
    f3_sub s0 (A[1:0], w0, C[1:0]);
539
    assign C[23:2] = A[23:2];
540
    wire [1:0] w12;
541
    f3_mult m12 (A[195:194], 2'd1, w12);
542
    f3_sub s12 (A[25:24], w12, C[25:24]);
543
    assign C[193:26] = A[193:26];
544
    assign C[195:194] = 0;
545
endmodule
546
 
547
// C = a * A; A,C \in GF(3^m); a \in GF(3)
548
module func4(A, aa, C);
549
    input [`WIDTH+2:0] A;
550
    input [1:0] aa;
551
    output [`WIDTH+2:0] C;
552
    genvar i;
553
    generate
554
      for(i=0; i<=`WIDTH+2; i=i+2)
555
      begin: label
556
        f3_mult m(A[i+1:i], aa, C[i+1:i]);
557
      end
558
    endgenerate
559
endmodule
560
 
561
// C = (A/x) mod p, \in GF(3^m)
562
module func5(A, C);
563
    input [`WIDTH+2:0] A;
564
    output [`WIDTH+2:0] C;
565
    assign C[195:194] = 0;
566
    assign C[193:192] = A[1:0];
567
    assign C[191:24] = A[193:26];
568
    f3_add a11 (A[25:24], A[1:0], C[23:22]);
569
    assign C[21:0] = A[23:2];
570
endmodule

powered by: WebSVN 2.1.0

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