# grondilu/libbitcoin-perl6

making BC wrapper available via environment var "PERL6_EC_METHOD"

1 parent 08dd2d7 commit dcbefb0d87fb0edb6d3d9ce0e5ca6459958ef71a committed Jun 5, 2012
Showing with 22 additions and 3 deletions.
1. +22 −3 lib/Bitcoin/EC.pm6
25 lib/Bitcoin/EC.pm6
 @@ -73,9 +73,28 @@ multi infix:<*>(2, Point:D \$point) { } else { Point.bless: *, :\$x, :\$y } } -multi infix:<*>(Int \$n where \$n > 2, Point:D \$point) { - 2 * (\$n div 2 * \$point) + \$n % 2 * \$point; -} + +## Were Perl6 a bit faster, we'd do this: +#multi infix:<*>(Int \$n where \$n > 2, Point:D \$point) { +# 2 * (\$n div 2 * \$point) + \$n % 2 * \$point; +#} + +# But since it is still very slow, we need the following +# trick to make it possible to use the unix basic calculator +# for the elliptic curve exponentiation: +my &mult:(\$n, \$point) := +defined(%*ENV) && %*ENV.uc eq 'BC' ?? +sub (\$n, \$point) { + use Bitcoin::EC::BC; + return Point.new: + |Bitcoin::EC::BC::compute + "mult( \$n, {\$point.x} * p + {\$point.y} )"; +} !! +sub (\$n, \$point) { + \$n == 0|1|2 ?? \$n * \$point !! + 2 * &?ROUTINE(\$n div 2, \$point) + \$n % 2 * \$point +}; +multi infix:<*>(Int \$n where \$n > 2, Point:D \$point) { mult(\$n, \$point) } multi infix:<+>(Point:U, Point \$b) { \$b } multi infix:<+>(Point:D \$a, Point:U) { \$a }