@@ -14,7 +14,7 @@ use Try::Tiny;
14
14
use Scalar::Util qw( blessed) ;
15
15
use Types::Common -sigs, -types;
16
16
17
- use Bitcoin::Crypto::Helpers qw( parse_formatdesc) ;
17
+ use Bitcoin::Crypto::Helpers qw( parse_formatdesc ecc ) ;
18
18
use Bitcoin::Crypto::Constants;
19
19
use Bitcoin::Crypto::Types -types;
20
20
use Bitcoin::Crypto::Exception;
@@ -38,6 +38,8 @@ our @EXPORT_OK = qw(
38
38
merkle_root
39
39
taproot_merkle_root
40
40
tagged_hash
41
+ lift_x
42
+ has_even_y
41
43
) ;
42
44
43
45
our %EXPORT_TAGS = (all => [@EXPORT_OK ]);
@@ -490,6 +492,38 @@ sub tagged_hash
490
492
return sha256($partial . $partial . $message );
491
493
}
492
494
495
+ signature_for lift_x => (
496
+ positional => [ByteStr],
497
+ );
498
+
499
+ sub lift_x
500
+ {
501
+ my ($x ) = @_ ;
502
+
503
+ my $key = " \x02 " . $x ;
504
+ Bitcoin::Crypto::Exception::KeyCreate-> raise(
505
+ ' invalid xonly public key'
506
+ ) unless ecc-> verify_public_key($key );
507
+
508
+ return $key ;
509
+ }
510
+
511
+ signature_for has_even_y => (
512
+ positional => [ByteStr],
513
+ );
514
+
515
+ sub has_even_y
516
+ {
517
+ my ($key ) = @_ ;
518
+
519
+ return Bitcoin::Crypto::Exception-> trap_into(
520
+ sub {
521
+ $key = ecc-> compress_public_key($key );
522
+ return substr ($key , 0, 1) eq " \x02 " ;
523
+ }
524
+ );
525
+ }
526
+
493
527
1;
494
528
495
529
__END__
@@ -518,6 +552,8 @@ Bitcoin::Crypto::Util - General Bitcoin utilities
518
552
merkle_root
519
553
taproot_merkle_root
520
554
tagged_hash
555
+ lift_x
556
+ has_even_y
521
557
);
522
558
523
559
=head1 DESCRIPTION
@@ -779,6 +815,23 @@ strings for C<$tag>, but can't detect whether it got it or not. This will only
779
815
become a problem if you use non-ascii tag. If there's a possibility of
780
816
non-ascii, always use utf8 and set binmodes to get decoded (wide) characters.
781
817
818
+ =head2 lift_x
819
+
820
+ $public_key = lift_x $xonly_public_key;
821
+
822
+ This implements C<lift_x > function defined in BIP340. Returns a compressed ECC
823
+ public key with even Y coordinate as a bytestring for a given 32-byte bytestring
824
+ C<$xonly_public_key > . Throws an exception if the result is not a valid public
825
+ key.
826
+
827
+ =head2 has_even_y
828
+
829
+ $even_y = has_even_y $public_key;
830
+
831
+ This implements C<has_even_y > function defined in BIP340. Returns a boolean for
832
+ a given serialized C<$public_key > - a bytestring. Throws an exception if the
833
+ argument is not a valid public key.
834
+
782
835
=head1 SEE ALSO
783
836
784
837
L<https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki>
0 commit comments