Jump to content

Elliptic Curve Digital Signature Algorithm: Difference between revisions

m
→‎{{header|Perl 6}}: Thanks to Thundergnat for the advice ; use the correct lib ; remove unnecessary int/str round trip ; echo the Julia entry by showing a failure and suppress naughty hyper
(→‎{{header|Perl 6}}: Update: use another lib for SHA; try bigger data set ; borrow point on curve check routine from reference entries ; more verbose output ; get rid of pack , etc)
m (→‎{{header|Perl 6}}: Thanks to Thundergnat for the advice ; use the correct lib ; remove unnecessary int/str round trip ; echo the Julia entry by showing a failure and suppress naughty hyper)
Line 1,115:
Reference: Many routines are translated from this [https://github.com/sblackstone/toy-ecdsa Ruby repository], by Stephen Blackstone. The rest are taken here and there from RC.
<lang perl6>#!/usr/bin/env perl6
 
use Digest::SHASHA256::Native;
 
# Following data taken from the C entry
Line 1,122 ⟶ 1,123:
#`{ Following data taken from the Julia entry; 256-bit; tested
our (\A,\B,\P,\O,\Gx,\Gy) = (0, 7, # https://en.bitcoin.it/wiki/Secp256k1
:10("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"),
:10("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"),
:10("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"),
:10("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8")); # }
 
role Horizon { method gist { 'EC Point at horizon' } }
Line 1,171 ⟶ 1,172:
 
method generate_signature(Int \private_key, Str \msg) {
my \z = :16((sha256-hex msg) % $.n; # self ref: Blob.list>>.&{fmt("%02s02X".sprintf(.base(16,''))}.join) % $.n;
loop ( my $k = my $s = my $r = 0 ; $s == 0 ; ) {
loop ( $r = $s = 0 ; $r == 0 ; ) {
$r = (( $k = (1..^$.n).roll ) ⊠ $.G).x % $.n;
}
Line 1,182 ⟶ 1,183:
 
method verify_signature(\msg, \r, \s, \public_key) {
my \z = :16((sha256-hex msg).list>>.&{"%02s".sprintf(.base(16))}.join) % $.n;
my \w = mult_inv s, :modulo($.n);
my (\u1,\u2) = (z*w, r*w)>>.&map: { $_ % $.n }
my \p = (u1 ⊠ $.G ) ⊞ (u2 ⊠ public_key);
return (p.x % $.n) == (r % $.n)
Line 1,201 ⟶ 1,202:
say "The public key Qa is : ", Qa;
say "Is Qa ∈ E ? : ", Qa.isOn;
say "Is signature valid? : ", $ec.verify_signature(message, $r, $s, Qa);
say "Message (Tampered) : ", my \altered = "Show me the money";
</lang>
say "Is signature valid? : ", $ec.verify_signature(altered, $r, $s, Qa)</lang>
{{out}}
<pre>The Curve E is : 𝑦² = 𝑥³ + 355 𝑥 + 671 (mod 1073741789)
Line 1,209 ⟶ 1,211:
Is G ∈ E ? : True
Message : Show me the monKey
The private key dA is : 31462688384652035
The public key Qa is : EC Point at x=105863728919494857, y=65404307118030536
Is Qa ∈ E ? : True
Is signature valid? : True
Message (Tampered) : Show me the money
Is signature valid? : False
</pre>
 
351

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.