]> Repositorios git - scryer-prolog.git/commitdiff
ENHANCED: library(crypto): Faster conversion to bytes when the integer is known.
authorMarkus Triska <[email protected]>
Thu, 8 Sep 2022 17:45:43 +0000 (19:45 +0200)
committerMark Thom <[email protected]>
Thu, 27 Oct 2022 05:36:07 +0000 (23:36 -0600)
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

index 42ce8f5d79f47c42208779c9823ec068f06c00e8..458283b751798967c66bbc356d511a582ddbc224 100644 (file)
@@ -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