Crypto/Role/SignVerify.pm

filter: Statementstmt 96.6% 58 / 60 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 1 totaltotal 91.4% 85 / 93 CC 8 SCAR 7.0
CC
8
Cov
95%
__ANON__
4.0
verify_message
3.1
sign_message
3.0
CRAP 2.0
« Crypto/PSBT/FieldType.pm ↑ Summary Crypto/Transaction/Digest/Config.pm »
1 package Bitcoin::Crypto::Role::SignVerify;
2 185 $Bitcoin::Crypto::Role::SignVerify::VERSION = '4.006';
3 43 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use Mooish::Base -standard, -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 43 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 43 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 43 ▶use Bitcoin::Crypto::Helpers qw(ecc make_strict_der_signature);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 43 ▶use Bitcoin::Crypto::Transaction::AutoSigner;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 43 ▶use Bitcoin::Crypto::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 43 ▶use Bitcoin::Crypto::Transaction::Flags;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 185 requires qw(
16 raw_key
17 taproot_output
18 _is_private
19 );
20  
21 my %algorithms = (
22 default => {
23 47 ▶ sign => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 47 my ($key, $digest) = @_;
25  
26 47 return ecc->sign_digest($key->raw_key, $digest);
27 },
28 159 ▶ verify => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
29 159 my ($key, $signature, $digest, $flags) = @_;
30  
31 159 ▶ if (!$flags->strict_signatures) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flags->strict_signatures) 5 154 159
32 5 $signature = make_strict_der_signature($signature);
33 }
34  
35 159 ▶ if ($flags->low_s_signatures) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->low_s_signatures) 8 151 159
36 8 my $normalized = ecc->normalize_signature($signature);
37 8 ▶ return !!0 if $normalized ne $signature;
Branch: 1 branch
BranchTrueFalseTotal
if $normalized ne $signature 2 6 8
38 }
39  
40 157 return ecc->verify_digest($key->raw_key('public'), $signature, $digest);
41 },
42 },
43 schnorr => {
44 21 ▶ sign => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 21 my ($key, $digest) = @_;
46  
47 21 return ecc->sign_digest_schnorr($key->raw_key, $digest);
48 },
49 0 ▶ verify => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my ($key, $signature, $digest) = @_;
51  
52 0 return ecc->verify_digest_schnorr($key->raw_key('public_xonly'), $signature, $digest);
53 },
54 },
55 185 );
56  
57 185 signature_for sign_message => (
58 method => !!1,
59 positional => [BitcoinDigest],
60 );
61  
62 sub sign_message
63 227 ▶{
Subroutine sign_message: called CC=3 SCAR=11.0
64 85 my ($self, $digest_result) = @_;
65 85 ▶ my $algorithm = $self->taproot_output ? 'schnorr' : 'default';
Branch: 1 branch
BranchTrueFalseTotal
$self->taproot_output ? : 21 206 227
66  
67 85 ▶ Bitcoin::Crypto::Exception::Sign->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_private 0 68 68
68 'cannot sign a message with a public key'
69 ) unless $self->_is_private;
70  
71 return Bitcoin::Crypto::Exception::Sign->trap_into(
72 68 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 85 return $algorithms{$algorithm}{sign}->($self, $digest_result->hash);
74 }
75 85 );
76 }
77  
78 sub sign_transaction
79 23 ▶{
Subroutine sign_transaction: called CC=1 SCAR=0.0
Pod undocumented
80 23 my ($self, $transaction, %args) = @_;
81  
82 23 $args{transaction} = $transaction;
83 23 $args{key} = $self;
84 23 my $signer = Bitcoin::Crypto::Transaction::AutoSigner->new(%args);
85 23 $signer->sign;
86  
87 23 return;
88 }
89  
90 185 signature_for verify_message => (
91 method => !!1,
92 head => [BitcoinDigest, ByteStr],
93 named => [
94 flags => Maybe [InstanceOf ['Bitcoin::Crypto::Transaction::Flags']],
95 {default => undef},
96 ],
97 bless => !!0,
98 );
99  
100 sub verify_message
101 {
102 17 my ($self, $digest_result, $signature, $args) = @_;
103 my $algorithm = $self->taproot_output ? 'schnorr' : 'default';
104 ▶ my $flags = $args->{flags} // Bitcoin::Crypto::Transaction::Flags->new;
Condition66%2/3
$args->{"flags"} // "Bitcoin::Crypto::Transaction::Flags"->new
l!l&&r!l&&!r
144150
MC/DC0%0/2
$args->{"flags"} // "Bitcoin::Crypto::Transaction::Flags"->new
$args->{"flags"} "Bitcoin::Crypto::Transaction::Flags"->new
Truth table66%2/3
A // B
A= $args->{"flags"}B= "Bitcoin::Crypto::Transaction::Flags"->new
ABresult
1X1
011
000
105  
106 try {
107 return $algorithms{$algorithm}{verify}->(
108 $self,
109 $signature,
110 $digest_result->hash,
111 $flags,
112 );
113 }
114 catch ($e) {
115 return !!0;
116 }
117 }
118  
119 39 1;
120  
« Crypto/PSBT/FieldType.pm ↑ Summary Crypto/Transaction/Digest/Config.pm »