forked from jettero/crypt--pbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PBC.pm
766 lines (599 loc) · 19.4 KB
/
PBC.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
package Crypt::PBC::Element;
use strict;
use Carp;
use MIME::Base64;
use Math::BigInt lib => 'GMP';
our %tt; # This maps our element types and our pairings. Arguably this should be
# done in the element references themselves, but those are scalar refs, not
# hash refs.
use overload
'""' => sub { my $this = shift; "Crypt::PBC::Element-$tt{$$this}{t}#$$this" },
'nomethod' => sub { my $this = shift; my $that = pop; croak "arithmetic operation '$that' not defined for $this" };
1;
# DESTROY {{{
sub DESTROY {
my $this = shift;
my $i = $$this;
Crypt::PBC::element_clear( $this );
delete $tt{$i};
}
# }}}
# clone {{{
sub clone {
my $this = shift;
my ($type, $pair) = @{ $tt{$$this} }{qw(t p)};
my $that = eval "\$pair->init_$type";
if( $@ ) {
# Can't call method "init_G1" on an undefined value at (eval 2) line 1.
# at t/13_pow_arith.t line 28
chomp $@; $@ =~ s/at \(eval \d+\) line \d+/during Crypt::PBC::Element::clone()/;
croak $@;
}
return $that->set( $this );
}
*copy = *clone;
# }}}
#### exporters
# as_bytes {{{
sub as_bytes {
my $this = shift;
return Crypt::PBC::export_element( $this );
}
# }}}
# as_hex {{{
sub as_hex {
my $this = shift;
return unpack("H*", $this->as_bytes);
}
*as_str = *as_hex;
# }}}
# as_base64 {{{
sub as_base64 {
my $this = shift;
my $arg = shift || "";
my $that = encode_base64($this->as_bytes, $arg);
$that =~ s/\n$//sg;
return $that;
}
# }}}
# as_bigint {{{
sub as_bigint {
my $this = shift;
my $that = Crypt::PBC::element_to_mpz($this);
my $int = new Math::BigInt;
$int->{value} = $that;
$int->{sign} = '+';
# I wanted to do something like thits, but I think
# the mpz_t's returned from element_to_mpz are always going to be positive...
# $int->{sign} = $this->is_neg ? "-" : "+";
return $int;
}
# }}}
# stddump {{{
sub stddump {
my $this = shift;
Crypt::PBC::element_fprintf(*STDOUT, '%B', $this );
}
# }}}
# errdump {{{
sub errdump {
my $this = shift;
return Crypt::PBC::element_fprintf(*STDERR, '%B', $this );
}
# }}}
#### initializers and set routines
# random {{{
sub random {
my $this = shift;
Crypt::PBC::element_random( $this );
return $this;
}
# }}}
# set_to_bytes {{{
sub set_to_bytes {
my $this = shift;
my $data = shift;
croak "provide something to set the element to" unless defined $data and length $data > 0;
Crypt::PBC::element_from_bytes($this, $data);
$this;
}
# }}}
# set_to_hash {{{
sub set_to_hash {
my $this = shift;
my $hash = shift;
croak "provide something to set the element to" unless defined $hash and length $hash > 0;
#my $type = $tt{$$this}{t};
#warn " >type=$type; hash=$hash...@_...<\n";
Crypt::PBC::element_from_hash($this, $hash);
#warn " <type=$type; hash=$hash...@_...>\n";
$this;
}
# }}}
# set_to_int {{{
sub set_to_int {
my $this = shift;
my $int = shift;
croak "int provided ($int) is not acceptable" unless $int =~ m/^\-?[0-9]+\z/s;
Crypt::PBC::element_set_si($this, $int);
$this;
}
# }}}
# set_to_bigint {{{
sub set_to_bigint {
my $this = shift;
my $int = shift;
croak "int provided is not a bigint" unless ref $int and $int->isa("Math::BigInt");
Crypt::PBC::element_set_mpz($this, $int->{value});
$this;
}
# }}}
# set {{{
sub set {
my $this = shift;
my $that = shift;
croak "LHS and RHS must be algebraically similar ($tt{$$this}{c} vs $tt{$$that}{c})"
unless $tt{$$this}{c} eq $tt{$$that}{c};
Crypt::PBC::element_set($this, $that);
$this;
}
# }}}
# set0 {{{
sub set0 {
my $this = shift;
my $that = shift;
Crypt::PBC::element_set0($this);
$this;
}
# }}}
# set1 {{{
sub set1 {
my $this = shift;
my $that = shift;
Crypt::PBC::element_set1($this);
$this;
}
# }}}
#### comparisons
# is0 {{{
sub is0 {
my $this = shift;
croak "LHS should have a type" unless exists $tt{$$this};
return Crypt::PBC::element_is0( $this );
}
# }}}
# is1 {{{
sub is1 {
my $this = shift;
croak "LHS should have a type" unless exists $tt{$$this};
return Crypt::PBC::element_is1( $this );
}
# }}}
# is_eq {{{
sub is_eq {
my $this = shift;
my $that = shift;
croak "LHS should have a type" unless exists $tt{$$this};
croak "LHS and RHS must be algebraically similar ($tt{$$this}{c} vs $tt{$$that}{c}) "
unless $tt{$$this}{c} eq $tt{$$that}{c};
return not Crypt::PBC::element_cmp( $this, $that ); # returns 0 if they're algebraically similar
}
# }}}
# is_sqr {{{
sub is_sqr {
my $this = shift;
croak "LHS should have a type" unless exists $tt{$$this};
my $type = $tt{$$this}{t};
return 1 if $type eq "G1";
return 1 if $type eq "G2";
return 1 if $type eq "GT";
return Crypt::PBC::element_is_sqr( $this );
}
# }}}
#### exponentiation
# pow_zn {{{
sub pow_zn {
my $this = shift;
my $base = shift;
my $expo = shift;
if( defined $base and not defined $expo ) {
$expo = $base;
$base = $this;
}
croak "LHS should have a type" unless exists $tt{$$this};
croak "LHS and BASE must be algebraically similar ($tt{$$this}{c} vs $tt{$$base}{c})"
unless $tt{$$this}{c} eq $tt{$$base}{c};
croak "EXPO must be of type Zr (not $tt{$$expo}{t})" unless $tt{$$expo}{t} eq "Zr";
Crypt::PBC::element_pow_zn( $this, $base, $expo );
$this;
}
# }}}
# pow2_zn {{{
sub pow2_zn {
my $this = shift;
my $a1 = shift;
my $n1 = shift;
my $a2 = shift;
my $n2 = shift;
croak "LHS should have a type" unless exists $tt{$$this};
croak "LHS and a1 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a1}{c})" unless $tt{$$this}{c} eq $tt{$$a1}{c};
croak "LHS and a2 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a2}{c})" unless $tt{$$this}{c} eq $tt{$$a2}{c};
croak "n1 must be of type Zr (not $tt{$$n1}{t})" unless $tt{$$n1}{t} eq "Zr";
croak "n2 must be of type Zr (not $tt{$$n2}{t})" unless $tt{$$n2}{t} eq "Zr";
Crypt::PBC::element_pow2_zn( $this, $a1, $n1, $a2, $n2 );
$this;
}
# }}}
# pow3_zn {{{
sub pow3_zn {
my $this = shift;
my $a1 = shift;
my $n1 = shift;
my $a2 = shift;
my $n2 = shift;
my $a3 = shift;
my $n3 = shift;
croak "LHS should have a type" unless exists $tt{$$this};
croak "LHS and a1 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a1}{c})" unless $tt{$$this}{c} eq $tt{$$a1}{c};
croak "LHS and a2 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a2}{c})" unless $tt{$$this}{c} eq $tt{$$a2}{c};
croak "LHS and a3 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a3}{c})" unless $tt{$$this}{c} eq $tt{$$a3}{c};
croak "n1 must be of type Zr (not $tt{$$n1}{t})" unless $tt{$$n1}{t} eq "Zr";
croak "n2 must be of type Zr (not $tt{$$n2}{t})" unless $tt{$$n2}{t} eq "Zr";
croak "n3 must be of type Zr (not $tt{$$n3}{t})" unless $tt{$$n3}{t} eq "Zr";
Crypt::PBC::element_pow3_zn( $this, $a1, $n1, $a2, $n2, $a3, $n3 );
$this;
}
# }}}
# pow_bigint {{{
sub pow_bigint {
my $this = shift;
my $base = shift;
my $expo = shift;
if( defined $base and not defined $expo ) {
$expo = $base;
$base = $this;
}
croak "EXPO provided is not a bigint" unless ref $expo and $expo->isa("Math::BigInt");
croak "LHS should have a type" unless exists $tt{$$this};
croak "LHS and BASE must be algebraically similar ($tt{$$this}{c} vs $tt{$$base}{c})"
unless exists $tt{$$this} and $tt{$$this}{c} eq $tt{$$base}{c};
Crypt::PBC::element_pow_mpz( $this, $base, $expo->{value} );
$this;
}
# }}}
# pow2_bigint {{{
sub pow2_bigint {
my $this = shift;
my $a1 = shift;
my $n1 = shift;
my $a2 = shift;
my $n2 = shift;
croak "n1 provided is not a bigint" unless ref $n1 and $n1->isa("Math::BigInt");
croak "n2 provided is not a bigint" unless ref $n2 and $n2->isa("Math::BigInt");
croak "LHS should have a type" unless exists $tt{$$this};
croak "LHS and a1 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a1}{c})" unless $tt{$$this}{c} eq $tt{$$a1}{c};
croak "LHS and a2 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a2}{c})" unless $tt{$$this}{c} eq $tt{$$a2}{c};
Crypt::PBC::element_pow2_mpz( $this, $a1, $n1->{value}, $a2, $n2->{value} );
$this;
}
# }}}
# pow3_bigint {{{
sub pow3_bigint {
my $this = shift;
my $a1 = shift;
my $n1 = shift;
my $a2 = shift;
my $n2 = shift;
my $a3 = shift;
my $n3 = shift;
croak "n1 provided is not a bigint" unless ref $n1 and $n1->isa("Math::BigInt");
croak "n2 provided is not a bigint" unless ref $n2 and $n2->isa("Math::BigInt");
croak "n3 provided is not a bigint" unless ref $n3 and $n2->isa("Math::BigInt");
croak "LHS should have a type" unless exists $tt{$$this};
croak "LHS and a1 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a1}{c})" unless $tt{$$this}{c} eq $tt{$$a1}{c};
croak "LHS and a2 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a2}{c})" unless $tt{$$this}{c} eq $tt{$$a2}{c};
croak "LHS and a3 must be algebraically similar ($tt{$$this}{c} vs $tt{$$a3}{c})" unless $tt{$$this}{c} eq $tt{$$a3}{c};
Crypt::PBC::element_pow3_mpz( $this, $a1, $n1->{value}, $a2, $n2->{value}, $a3, $n3->{value} );
$this;
}
# }}}
#### arith
## 1op
# square {{{
sub square {
my $lhs = shift;
my $rhs = shift;
if( $rhs ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs}{c})" unless $tt{$$lhs}{c} eq $tt{$$rhs}{c};
} else {
$rhs = $lhs;
}
Crypt::PBC::element_square( $lhs, $rhs );
$lhs;
}
# }}}
# double {{{
sub double {
my $lhs = shift;
my $rhs = shift;
if( $rhs ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs}{c})" unless $tt{$$lhs}{c} eq $tt{$$rhs}{c};
} else {
$rhs = $lhs;
}
Crypt::PBC::element_double( $lhs, $rhs );
$lhs;
}
# }}}
# halve {{{
sub halve {
my $lhs = shift;
my $rhs = shift;
if( $rhs ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs}{c})" unless $tt{$$lhs}{c} eq $tt{$$rhs}{c};
} else {
$rhs = $lhs;
}
Crypt::PBC::element_halve( $lhs, $rhs );
$lhs;
}
# }}}
# neg {{{
sub neg {
my $lhs = shift;
my $rhs = shift;
if( $rhs ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs}{c})" unless $tt{$$lhs}{c} eq $tt{$$rhs}{c};
} else {
$rhs = $lhs;
}
Crypt::PBC::element_neg( $lhs, $rhs );
$lhs;
}
# }}}
# invert {{{
sub invert {
my $lhs = shift;
my $rhs = shift;
if( $rhs ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs}{c})" unless $tt{$$lhs}{c} eq $tt{$$rhs}{c};
} else {
$rhs = $lhs;
}
Crypt::PBC::element_invert( $lhs, $rhs );
$lhs;
}
# }}}
## 2op
# add {{{
sub add {
my $lhs = shift;
my $rhs1 = shift;
my $rhs2 = shift;
if( $rhs2 ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS, RHS1 and RHS2 must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c} vs $tt{$$rhs2}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c} and $tt{$$rhs1}{c} eq $tt{$$rhs2}{c};
Crypt::PBC::element_add( $lhs, $rhs1, $rhs2 );
} else {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS should be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c};
Crypt::PBC::element_add( $lhs, $lhs, $rhs1 );
}
$lhs;
}
# }}}
# Sub {{{
sub Sub {
my $lhs = shift;
my $rhs1 = shift;
my $rhs2 = shift;
if( $rhs2 ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS, RHS1 and RHS2 must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c} vs $tt{$$rhs2}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c} and $tt{$$rhs1}{c} eq $tt{$$rhs2}{c};
Crypt::PBC::element_sub( $lhs, $rhs1, $rhs2 );
} else {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS should be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c};
Crypt::PBC::element_sub( $lhs, $lhs, $rhs1 );
}
$lhs;
}
# }}}
# mul {{{
sub mul {
my $lhs = shift;
my $rhs1 = shift;
my $rhs2 = shift;
if( $rhs2 ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS, RHS1 and RHS2 must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c} vs $tt{$$rhs2}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c} and $tt{$$rhs1}{c} eq $tt{$$rhs2}{c};
Crypt::PBC::element_mul( $lhs, $rhs1, $rhs2 );
} else {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS should be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c};
Crypt::PBC::element_mul( $lhs, $lhs, $rhs1 );
}
$lhs;
}
# }}}
# div {{{
sub div {
my $lhs = shift;
my $rhs1 = shift;
my $rhs2 = shift;
if( $rhs2 ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS, RHS1 and RHS2 must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c} vs $tt{$$rhs2}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c} and $tt{$$rhs1}{c} eq $tt{$$rhs2}{c};
Crypt::PBC::element_div( $lhs, $rhs1, $rhs2 );
} else {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS and RHS should be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c};
Crypt::PBC::element_div( $lhs, $lhs, $rhs1 );
}
$lhs;
}
# }}}
# mul_zn {{{
sub mul_zn {
my $lhs = shift;
my $rhs1 = shift;
my $rhs2 = shift;
if( $rhs2 ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS, RHS1 must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c};
croak "RHS2 should be in Zr (not $tt{$$rhs2}{t})" unless $tt{$$rhs2}{t} eq "Zr";
Crypt::PBC::element_mul_zn( $lhs, $rhs1, $rhs2 );
} else {
croak "RHS should be in Zr (not $tt{$$rhs1}{t})"
unless $tt{$$rhs1}{t} eq "Zr";
Crypt::PBC::element_mul_zn( $lhs, $lhs, $rhs1 );
}
$lhs;
}
# }}}
# mul_int {{{
sub mul_int {
my $lhs = shift;
my $rhs1 = shift;
my $rhs2 = shift;
if( $rhs2 ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS, RHS1 must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c};
croak "int provided ($rhs2) is not acceptable" unless $rhs2 =~ m/^\-?[0-9]+\z/s;
Crypt::PBC::element_mul_si( $lhs, $rhs1, $rhs2 );
} else {
croak "int provided ($rhs1) is not acceptable" unless $rhs1 =~ m/^\-?[0-9]+\z/s;
Crypt::PBC::element_mul_si( $lhs, $lhs, $rhs1 );
}
$lhs;
}
# }}}
# mul_bigint {{{
sub mul_bigint {
my $lhs = shift;
my $rhs1 = shift;
my $rhs2 = shift;
if( $rhs2 ) {
croak "LHS should have a type" unless exists $tt{$$lhs};
croak "LHS, RHS1 must be algebraically similar ($tt{$$lhs}{c} vs $tt{$$rhs1}{c})"
unless $tt{$$lhs}{c} eq $tt{$$rhs1}{c};
croak "int provided is not a bigint" unless ref $rhs2 and $rhs2->isa("Math::BigInt");
Crypt::PBC::element_mul_si( $lhs, $rhs1, $rhs2 );
} else {
croak "int provided is not a bigint" unless ref $rhs1 and $rhs1->isa("Math::BigInt");
Crypt::PBC::element_mul_si( $lhs, $lhs, $rhs1 );
}
$lhs;
}
# }}}
# pairing_apply {{{
sub pairing_apply {
my $this = shift;
my $rhs1 = shift;
my $rhs2 = shift;
my $pair = $tt{$$this}{p};
my $c1 = $tt{$$rhs1}{c};
my $c2 = $tt{$$rhs2}{c};
croak "group type for LHS must be GT (not $tt{$$this}{t})" unless $tt{$$this}{t} eq "GT";
croak "group type for RHS1 must be G1 (not $c1)" unless $c1 eq "G1" or $c1 eq "G[12]";
croak "group type for RHS2 must be G2 (not $c2)" unless $c2 eq "G2" or $c2 eq "G[12]";
Crypt::PBC::pairing_apply( $this => ($rhs1, $rhs2) => $pair );
$this;
}
*ehat = *pairing_apply;
*e_hat = *pairing_apply;
*apply_pairing = *pairing_apply;
# }}}
#### package Crypt::PBC::Pairing {{{
package Crypt::PBC::Pairing;
use strict;
use Carp;
1;
sub _stype {
my $this = shift;
my $that = shift;
my $type = shift;
$Crypt::PBC::Element::tt{$$that} = {
t => $type,
p => $this,
c => $type,
};
if( $type =~ m/G[12]/ and Crypt::PBC::pairing_is_symmetric($this) ) {
$Crypt::PBC::Element::tt{$$that}{c} = "G[12]";
}
return;
}
sub init_G1 { my $this = shift; my $that = Crypt::PBC::element_init_G1( $this ); $this->_stype($that => "G1"); $that }
sub init_G2 { my $this = shift; my $that = Crypt::PBC::element_init_G2( $this ); $this->_stype($that => "G2"); $that }
sub init_GT { my $this = shift; my $that = Crypt::PBC::element_init_GT( $this ); $this->_stype($that => "GT"); $that }
sub init_Zr { my $this = shift; my $that = Crypt::PBC::element_init_Zr( $this ); $this->_stype($that => "Zr"); $that }
sub DESTROY { my $this = shift; my $that = Crypt::PBC::pairing_clear( $this ); }
# }}}
#### package Crypt::PBC {{{
package Crypt::PBC;
use strict;
use warnings;
use Carp;
our $VERSION = '0.9000';
# use base 'Exporter';
# our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
# our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
# our @EXPORT = qw( );
#
# sub AUTOLOAD {
# my $constname;
# our $AUTOLOAD; ($constname = $AUTOLOAD) =~ s/.*:://;
# croak "Crypt::PBC::constant wtf($AUTOLOAD, $constname) not defined" if $constname eq 'constant';
# my ($error, $val) = constant($constname);
# if( $error ) { croak $error }
# goto &$AUTOLOAD;
# }
require XSLoader;
XSLoader::load('Crypt::PBC', $VERSION);
1;
sub new {
my $class = shift;
my $that;
my $arg = shift;
TOP: {
if( ref($arg) eq "GLOB" ) {
my $contents = do { local $/; <$arg> };
$arg = $contents;
redo TOP;
} elsif( $arg !~ m/\n/ and -f $arg ) {
open my $in, $arg or croak "couldn't open param file ($arg): $!";
my $contents = do { local $/; <$in> }; close $in;
$arg = $contents;
redo TOP;
} elsif( $arg ) {
$arg =~ s/^\s*//s;
$arg =~ s/\s*$//s;
if( $arg =~ m/^(?s:type\s+[a-z]+\s*|[a-z0-9]+\s+[0-9]+\s*)+\z/s ) {
$that = Crypt::PBC::pairing_init_str($arg);
} else {
croak "either the filename doesn't exist or that param string is unparsable: $arg";
}
} else {
croak "you must pass a file, glob (stream), or init params to new()";
}
}
croak "something went wrong ... you must pass a file, glob (stream), or init params to new()" unless $$that>0;
return $that;
}
# }}}
1;