From b2cccab768ccd5b10a9bda8c404bd029c68dd32a Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Thu, 8 Sep 2022 19:45:43 +0200 Subject: [PATCH] ENHANCED: library(crypto): Faster conversion to bytes when the integer is known. These entailed constraints only affect performance, and can be removed without affecting the correctness of the code. They speed up scalar multiplication of a curve point, used for example in Bitcoinolog. --- src/lib/crypto.pl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lib/crypto.pl b/src/lib/crypto.pl index 42ce8f5d..458283b7 100644 --- a/src/lib/crypto.pl +++ b/src/lib/crypto.pl @@ -713,12 +713,17 @@ curve25519_scalar_mult(Scalar, Point, Result) :- '$curve25519_scalar_mult'(ScalarBytes, PointBytes, Result). bytes_integer(Bs, N) :- - foldl(pow, Bs, 0-0, N-_). + foldl(pow, Bs, t(0,0,N), t(N,_,_)). -pow(B, N0-I0, N-I) :- +pow(B, t(N0,P0,I0), t(N,P,I)) :- + ( integer(I0) -> + B #= I0 mod 256, + I #= I0 >> 8 + ; true + ), B in 0..255, - N #= N0 + B*256^I0, - I #= I0 + 1. + N #= N0 + B*256^P0, + P #= P0 + 1. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Operations on Elliptic Curves -- 2.54.0