Change the hash algorithm from SHA1 to a 6-round KeccakF1600 with a rate

of 160.  This uses about 1/3rd fewer CPU cycles.

FossilOrigin-Name: 96c7f47a8f59e5078bd296979421c1b57fbcb7be261f8a7a0b1d22a4b5914db0
This commit is contained in:
drh 2024-09-13 12:28:11 +00:00
parent 5c6dc45a10
commit d0d3182fa8
6 changed files with 506 additions and 17 deletions

View File

@ -702,7 +702,6 @@ dbhash$(TEXE): $(TOP)/tool/dbhash.c sqlite3.lo sqlite3.h
RSYNC_SRC = \
$(TOP)/tool/sqlite3-rsync.c \
$(TOP)/ext/misc/sha1.c \
sqlite3.c
RSYNC_OPT = \

View File

@ -1869,7 +1869,6 @@ dbhash.exe: $(TOP)\tool\dbhash.c $(SQLITE3C) $(SQLITE3H)
RSYNC_SRC = \
$(TOP)\tool\sqlite3-rsync.c \
$(TOP)\ext\misc\sha1.c \
$(SQLITE3C)
RSYNC_OPT = \

View File

@ -570,7 +570,6 @@ dbhash$(EXE): $(TOP)/tool/dbhash.c sqlite3.c sqlite3.h
RSYNC_SRC = \
$(TOP)/tool/sqlite3-rsync.c \
$(TOP)/ext/misc/sha1.c \
sqlite3.c
RSYNC_OPT = \

View File

@ -1,11 +1,11 @@
C Better\sinitialization\sof\snew\sdatabase\sfiles\sin\ssqlite3-rsync.
D 2024-09-12T23:30:29.682
C Change\sthe\shash\salgorithm\sfrom\sSHA1\sto\sa\s6-round\sKeccakF1600\swith\sa\srate\nof\s160.\s\sThis\suses\sabout\s1/3rd\sfewer\sCPU\scycles.
D 2024-09-13T12:28:11.247
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F Makefile.in 086a7ca4e5223da3990e1a6f0cdc082af74c3ca0e175b0af955a44115832ba85
F Makefile.in 31368ad3e1800bb5f311adede543ee456ca7d2595403c7f131797ae65a7d415c
F Makefile.linux-gcc f3842a0b1efbfbb74ac0ef60e56b301836d05b4d867d014f714fa750048f1ab6
F Makefile.msc f25c476f22a25f23af8dc7260e9f95896250e7fceb621388ca8ae4f096bd3fda
F Makefile.msc 62ace0005c53b52f189c20c1d6d8fa4dbd2a37c90d9c1362b60f4fb4c841fa15
F README.md c3c0f19532ce28f6297a71870f3c7b424729f0e6d9ab889616d3587dd2332159
F VERSION 0db40f92c04378404eb45bff93e9e42c148c7e54fd3da99469ed21e22411f5a6
F aclocal.m4 a5c22d164aff7ed549d53a90fa56d56955281f50
@ -687,7 +687,7 @@ F ext/wasm/wasmfs.make 8a4955882aaa0783b3f60a9484a1f0f3d8b6f775c0fcd17c082f31966
F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x
F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8
F magic.txt 5ade0bc977aa135e79e3faaea894d5671b26107cc91e70783aa7dc83f22f3ba0
F main.mk 91d391457000574faaaa34a833564b98f4a3dae6c4a5681e8e6b5ff1cf5c4b57
F main.mk 8b9c0252aef57b5b2a10f34b8b46e89f9ed06bdccef1df98673a12f34e9b3e79
F mptest/config01.test 3c6adcbc50b991866855f1977ff172eb6d901271
F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504
F mptest/crash01.test 61e61469e257df0850df4293d7d4d6c2af301421
@ -2174,7 +2174,7 @@ F tool/speedtest8inst1.c 7ce07da76b5e745783e703a834417d725b7d45fd
F tool/spellsift.tcl 52b4b04dc4333c7ab024f09d9d66ed6b6f7c6eb00b38497a09f338fa55d40618 x
F tool/split-sqlite3c.tcl 5aa60643afca558bc732b1444ae81a522326f91e1dc5665b369c54f09e20de60
F tool/sqldiff.c 847fc8fcfddf5ce4797b7394cad6372f2f5dc17d8186e2ef8fb44d50fae4f44a
F tool/sqlite3-rsync.c 4a2bfb77e633b6a6edbd88d7bfc3e6e03a0c8662d0c3ae815e6c1ff7eb7df94e
F tool/sqlite3-rsync.c 10fa0e40cc637195dc86c84390f1d7bae3be0830a0e50990df9289234097889c
F tool/sqlite3_analyzer.c.in 8da2b08f56eeac331a715036cf707cc20f879f231362be0c22efd682e2b89b4f
F tool/sqltclsh.c.in 1bcc2e9da58fadf17b0bf6a50e68c1159e602ce057210b655d50bad5aaaef898
F tool/sqltclsh.tcl 862f4cf1418df5e1315b5db3b5ebe88969e2a784525af5fbf9596592f14ed848
@ -2213,8 +2213,8 @@ F vsixtest/vsixtest.tcl 6195aba1f12a5e10efc2b8c0009532167be5e301abe5b31385638080
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P b2a3497e5525dd33faf70961107a0529f476735fef756953c66e105747271c6d
R 3def4dc608d40602e340858854fc17d3
P 75d5a8eb3d4ece06900109ad4022ba2a3e82de2f0acb012e3a02bfb4326bfa6d
R 6d0c1116cd9e3a065842817267acb222
U drh
Z 3eea4133291b7d423d6d8c3c9c75fcb1
Z 4ed0549ad93aad61732b14753f0892c6
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
75d5a8eb3d4ece06900109ad4022ba2a3e82de2f0acb012e3a02bfb4326bfa6d
96c7f47a8f59e5078bd296979421c1b57fbcb7be261f8a7a0b1d22a4b5914db0

View File

@ -506,6 +506,498 @@ int append_escaped_arg(sqlite3_str *pStr, const char *zIn, int isFilename){
** End of the append_escaped_arg() routine, adapted from the Fossil **
*****************************************************************************/
/*****************************************************************************
** The Hash Engine
**
** This is basically SHA3, though with a 160-bit hash, and reducing the
** number of rounds in the KeccakF1600 step function from 24 to 6.
*/
/*
** Macros to determine whether the machine is big or little endian,
** and whether or not that determination is run-time or compile-time.
**
** For best performance, an attempt is made to guess at the byte-order
** using C-preprocessor macros. If that is unsuccessful, or if
** -DHash_BYTEORDER=0 is set, then byte-order is determined
** at run-time.
*/
#ifndef Hash_BYTEORDER
# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
defined(__arm__)
# define Hash_BYTEORDER 1234
# elif defined(sparc) || defined(__ppc__)
# define Hash_BYTEORDER 4321
# else
# define Hash_BYTEORDER 0
# endif
#endif
typedef sqlite3_uint64 u64;
/*
** State structure for a Hash hash in progress
*/
typedef struct HashContext HashContext;
struct HashContext {
union {
u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
unsigned char x[1600]; /* ... or 1600 bytes */
} u;
unsigned nRate; /* Bytes of input accepted per Keccak iteration */
unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
unsigned iSize; /* 224, 256, 358, or 512 */
};
/*
** A single step of the Keccak mixing function for a 1600-bit state
*/
static void KeccakF1600Step(HashContext *p){
int i;
u64 b0, b1, b2, b3, b4;
u64 c0, c1, c2, c3, c4;
u64 d0, d1, d2, d3, d4;
static const u64 RC[] = {
0x0000000000000001ULL, 0x0000000000008082ULL,
0x800000000000808aULL, 0x8000000080008000ULL,
0x000000000000808bULL, 0x0000000080000001ULL,
0x8000000080008081ULL, 0x8000000000008009ULL,
0x000000000000008aULL, 0x0000000000000088ULL,
0x0000000080008009ULL, 0x000000008000000aULL,
0x000000008000808bULL, 0x800000000000008bULL,
0x8000000000008089ULL, 0x8000000000008003ULL,
0x8000000000008002ULL, 0x8000000000000080ULL,
0x000000000000800aULL, 0x800000008000000aULL,
0x8000000080008081ULL, 0x8000000000008080ULL,
0x0000000080000001ULL, 0x8000000080008008ULL
};
# define a00 (p->u.s[0])
# define a01 (p->u.s[1])
# define a02 (p->u.s[2])
# define a03 (p->u.s[3])
# define a04 (p->u.s[4])
# define a10 (p->u.s[5])
# define a11 (p->u.s[6])
# define a12 (p->u.s[7])
# define a13 (p->u.s[8])
# define a14 (p->u.s[9])
# define a20 (p->u.s[10])
# define a21 (p->u.s[11])
# define a22 (p->u.s[12])
# define a23 (p->u.s[13])
# define a24 (p->u.s[14])
# define a30 (p->u.s[15])
# define a31 (p->u.s[16])
# define a32 (p->u.s[17])
# define a33 (p->u.s[18])
# define a34 (p->u.s[19])
# define a40 (p->u.s[20])
# define a41 (p->u.s[21])
# define a42 (p->u.s[22])
# define a43 (p->u.s[23])
# define a44 (p->u.s[24])
# define ROL64(a,x) ((a<<x)|(a>>(64-x)))
/* v---- Number of rounds. SHA3 has 24 here. */
for(i=0; i<6; i+=4){
c0 = a00^a10^a20^a30^a40;
c1 = a01^a11^a21^a31^a41;
c2 = a02^a12^a22^a32^a42;
c3 = a03^a13^a23^a33^a43;
c4 = a04^a14^a24^a34^a44;
d0 = c4^ROL64(c1, 1);
d1 = c0^ROL64(c2, 1);
d2 = c1^ROL64(c3, 1);
d3 = c2^ROL64(c4, 1);
d4 = c3^ROL64(c0, 1);
b0 = (a00^d0);
b1 = ROL64((a11^d1), 44);
b2 = ROL64((a22^d2), 43);
b3 = ROL64((a33^d3), 21);
b4 = ROL64((a44^d4), 14);
a00 = b0 ^((~b1)& b2 );
a00 ^= RC[i];
a11 = b1 ^((~b2)& b3 );
a22 = b2 ^((~b3)& b4 );
a33 = b3 ^((~b4)& b0 );
a44 = b4 ^((~b0)& b1 );
b2 = ROL64((a20^d0), 3);
b3 = ROL64((a31^d1), 45);
b4 = ROL64((a42^d2), 61);
b0 = ROL64((a03^d3), 28);
b1 = ROL64((a14^d4), 20);
a20 = b0 ^((~b1)& b2 );
a31 = b1 ^((~b2)& b3 );
a42 = b2 ^((~b3)& b4 );
a03 = b3 ^((~b4)& b0 );
a14 = b4 ^((~b0)& b1 );
b4 = ROL64((a40^d0), 18);
b0 = ROL64((a01^d1), 1);
b1 = ROL64((a12^d2), 6);
b2 = ROL64((a23^d3), 25);
b3 = ROL64((a34^d4), 8);
a40 = b0 ^((~b1)& b2 );
a01 = b1 ^((~b2)& b3 );
a12 = b2 ^((~b3)& b4 );
a23 = b3 ^((~b4)& b0 );
a34 = b4 ^((~b0)& b1 );
b1 = ROL64((a10^d0), 36);
b2 = ROL64((a21^d1), 10);
b3 = ROL64((a32^d2), 15);
b4 = ROL64((a43^d3), 56);
b0 = ROL64((a04^d4), 27);
a10 = b0 ^((~b1)& b2 );
a21 = b1 ^((~b2)& b3 );
a32 = b2 ^((~b3)& b4 );
a43 = b3 ^((~b4)& b0 );
a04 = b4 ^((~b0)& b1 );
b3 = ROL64((a30^d0), 41);
b4 = ROL64((a41^d1), 2);
b0 = ROL64((a02^d2), 62);
b1 = ROL64((a13^d3), 55);
b2 = ROL64((a24^d4), 39);
a30 = b0 ^((~b1)& b2 );
a41 = b1 ^((~b2)& b3 );
a02 = b2 ^((~b3)& b4 );
a13 = b3 ^((~b4)& b0 );
a24 = b4 ^((~b0)& b1 );
c0 = a00^a20^a40^a10^a30;
c1 = a11^a31^a01^a21^a41;
c2 = a22^a42^a12^a32^a02;
c3 = a33^a03^a23^a43^a13;
c4 = a44^a14^a34^a04^a24;
d0 = c4^ROL64(c1, 1);
d1 = c0^ROL64(c2, 1);
d2 = c1^ROL64(c3, 1);
d3 = c2^ROL64(c4, 1);
d4 = c3^ROL64(c0, 1);
b0 = (a00^d0);
b1 = ROL64((a31^d1), 44);
b2 = ROL64((a12^d2), 43);
b3 = ROL64((a43^d3), 21);
b4 = ROL64((a24^d4), 14);
a00 = b0 ^((~b1)& b2 );
a00 ^= RC[i+1];
a31 = b1 ^((~b2)& b3 );
a12 = b2 ^((~b3)& b4 );
a43 = b3 ^((~b4)& b0 );
a24 = b4 ^((~b0)& b1 );
b2 = ROL64((a40^d0), 3);
b3 = ROL64((a21^d1), 45);
b4 = ROL64((a02^d2), 61);
b0 = ROL64((a33^d3), 28);
b1 = ROL64((a14^d4), 20);
a40 = b0 ^((~b1)& b2 );
a21 = b1 ^((~b2)& b3 );
a02 = b2 ^((~b3)& b4 );
a33 = b3 ^((~b4)& b0 );
a14 = b4 ^((~b0)& b1 );
b4 = ROL64((a30^d0), 18);
b0 = ROL64((a11^d1), 1);
b1 = ROL64((a42^d2), 6);
b2 = ROL64((a23^d3), 25);
b3 = ROL64((a04^d4), 8);
a30 = b0 ^((~b1)& b2 );
a11 = b1 ^((~b2)& b3 );
a42 = b2 ^((~b3)& b4 );
a23 = b3 ^((~b4)& b0 );
a04 = b4 ^((~b0)& b1 );
b1 = ROL64((a20^d0), 36);
b2 = ROL64((a01^d1), 10);
b3 = ROL64((a32^d2), 15);
b4 = ROL64((a13^d3), 56);
b0 = ROL64((a44^d4), 27);
a20 = b0 ^((~b1)& b2 );
a01 = b1 ^((~b2)& b3 );
a32 = b2 ^((~b3)& b4 );
a13 = b3 ^((~b4)& b0 );
a44 = b4 ^((~b0)& b1 );
b3 = ROL64((a10^d0), 41);
b4 = ROL64((a41^d1), 2);
b0 = ROL64((a22^d2), 62);
b1 = ROL64((a03^d3), 55);
b2 = ROL64((a34^d4), 39);
a10 = b0 ^((~b1)& b2 );
a41 = b1 ^((~b2)& b3 );
a22 = b2 ^((~b3)& b4 );
a03 = b3 ^((~b4)& b0 );
a34 = b4 ^((~b0)& b1 );
c0 = a00^a40^a30^a20^a10;
c1 = a31^a21^a11^a01^a41;
c2 = a12^a02^a42^a32^a22;
c3 = a43^a33^a23^a13^a03;
c4 = a24^a14^a04^a44^a34;
d0 = c4^ROL64(c1, 1);
d1 = c0^ROL64(c2, 1);
d2 = c1^ROL64(c3, 1);
d3 = c2^ROL64(c4, 1);
d4 = c3^ROL64(c0, 1);
b0 = (a00^d0);
b1 = ROL64((a21^d1), 44);
b2 = ROL64((a42^d2), 43);
b3 = ROL64((a13^d3), 21);
b4 = ROL64((a34^d4), 14);
a00 = b0 ^((~b1)& b2 );
a00 ^= RC[i+2];
a21 = b1 ^((~b2)& b3 );
a42 = b2 ^((~b3)& b4 );
a13 = b3 ^((~b4)& b0 );
a34 = b4 ^((~b0)& b1 );
b2 = ROL64((a30^d0), 3);
b3 = ROL64((a01^d1), 45);
b4 = ROL64((a22^d2), 61);
b0 = ROL64((a43^d3), 28);
b1 = ROL64((a14^d4), 20);
a30 = b0 ^((~b1)& b2 );
a01 = b1 ^((~b2)& b3 );
a22 = b2 ^((~b3)& b4 );
a43 = b3 ^((~b4)& b0 );
a14 = b4 ^((~b0)& b1 );
b4 = ROL64((a10^d0), 18);
b0 = ROL64((a31^d1), 1);
b1 = ROL64((a02^d2), 6);
b2 = ROL64((a23^d3), 25);
b3 = ROL64((a44^d4), 8);
a10 = b0 ^((~b1)& b2 );
a31 = b1 ^((~b2)& b3 );
a02 = b2 ^((~b3)& b4 );
a23 = b3 ^((~b4)& b0 );
a44 = b4 ^((~b0)& b1 );
b1 = ROL64((a40^d0), 36);
b2 = ROL64((a11^d1), 10);
b3 = ROL64((a32^d2), 15);
b4 = ROL64((a03^d3), 56);
b0 = ROL64((a24^d4), 27);
a40 = b0 ^((~b1)& b2 );
a11 = b1 ^((~b2)& b3 );
a32 = b2 ^((~b3)& b4 );
a03 = b3 ^((~b4)& b0 );
a24 = b4 ^((~b0)& b1 );
b3 = ROL64((a20^d0), 41);
b4 = ROL64((a41^d1), 2);
b0 = ROL64((a12^d2), 62);
b1 = ROL64((a33^d3), 55);
b2 = ROL64((a04^d4), 39);
a20 = b0 ^((~b1)& b2 );
a41 = b1 ^((~b2)& b3 );
a12 = b2 ^((~b3)& b4 );
a33 = b3 ^((~b4)& b0 );
a04 = b4 ^((~b0)& b1 );
c0 = a00^a30^a10^a40^a20;
c1 = a21^a01^a31^a11^a41;
c2 = a42^a22^a02^a32^a12;
c3 = a13^a43^a23^a03^a33;
c4 = a34^a14^a44^a24^a04;
d0 = c4^ROL64(c1, 1);
d1 = c0^ROL64(c2, 1);
d2 = c1^ROL64(c3, 1);
d3 = c2^ROL64(c4, 1);
d4 = c3^ROL64(c0, 1);
b0 = (a00^d0);
b1 = ROL64((a01^d1), 44);
b2 = ROL64((a02^d2), 43);
b3 = ROL64((a03^d3), 21);
b4 = ROL64((a04^d4), 14);
a00 = b0 ^((~b1)& b2 );
a00 ^= RC[i+3];
a01 = b1 ^((~b2)& b3 );
a02 = b2 ^((~b3)& b4 );
a03 = b3 ^((~b4)& b0 );
a04 = b4 ^((~b0)& b1 );
b2 = ROL64((a10^d0), 3);
b3 = ROL64((a11^d1), 45);
b4 = ROL64((a12^d2), 61);
b0 = ROL64((a13^d3), 28);
b1 = ROL64((a14^d4), 20);
a10 = b0 ^((~b1)& b2 );
a11 = b1 ^((~b2)& b3 );
a12 = b2 ^((~b3)& b4 );
a13 = b3 ^((~b4)& b0 );
a14 = b4 ^((~b0)& b1 );
b4 = ROL64((a20^d0), 18);
b0 = ROL64((a21^d1), 1);
b1 = ROL64((a22^d2), 6);
b2 = ROL64((a23^d3), 25);
b3 = ROL64((a24^d4), 8);
a20 = b0 ^((~b1)& b2 );
a21 = b1 ^((~b2)& b3 );
a22 = b2 ^((~b3)& b4 );
a23 = b3 ^((~b4)& b0 );
a24 = b4 ^((~b0)& b1 );
b1 = ROL64((a30^d0), 36);
b2 = ROL64((a31^d1), 10);
b3 = ROL64((a32^d2), 15);
b4 = ROL64((a33^d3), 56);
b0 = ROL64((a34^d4), 27);
a30 = b0 ^((~b1)& b2 );
a31 = b1 ^((~b2)& b3 );
a32 = b2 ^((~b3)& b4 );
a33 = b3 ^((~b4)& b0 );
a34 = b4 ^((~b0)& b1 );
b3 = ROL64((a40^d0), 41);
b4 = ROL64((a41^d1), 2);
b0 = ROL64((a42^d2), 62);
b1 = ROL64((a43^d3), 55);
b2 = ROL64((a44^d4), 39);
a40 = b0 ^((~b1)& b2 );
a41 = b1 ^((~b2)& b3 );
a42 = b2 ^((~b3)& b4 );
a43 = b3 ^((~b4)& b0 );
a44 = b4 ^((~b0)& b1 );
}
}
/*
** Initialize a new hash. iSize determines the size of the hash
** in bits and should be one of 224, 256, 384, or 512. Or iSize
** can be zero to use the default hash size of 256 bits.
*/
static void HashInit(HashContext *p, int iSize){
memset(p, 0, sizeof(*p));
p->iSize = iSize;
if( iSize>=128 && iSize<=512 ){
p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
}else{
p->nRate = (1600 - 2*256)/8;
}
#if Hash_BYTEORDER==1234
/* Known to be little-endian at compile-time. No-op */
#elif Hash_BYTEORDER==4321
p->ixMask = 7; /* Big-endian */
#else
{
static unsigned int one = 1;
if( 1==*(unsigned char*)&one ){
/* Little endian. No byte swapping. */
p->ixMask = 0;
}else{
/* Big endian. Byte swap. */
p->ixMask = 7;
}
}
#endif
}
/*
** Make consecutive calls to the HashUpdate function to add new content
** to the hash
*/
static void HashUpdate(
HashContext *p,
const unsigned char *aData,
unsigned int nData
){
unsigned int i = 0;
if( aData==0 ) return;
#if Hash_BYTEORDER==1234
if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
for(; i+7<nData; i+=8){
p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
p->nLoaded += 8;
if( p->nLoaded>=p->nRate ){
KeccakF1600Step(p);
p->nLoaded = 0;
}
}
}
#endif
for(; i<nData; i++){
#if Hash_BYTEORDER==1234
p->u.x[p->nLoaded] ^= aData[i];
#elif Hash_BYTEORDER==4321
p->u.x[p->nLoaded^0x07] ^= aData[i];
#else
p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
#endif
p->nLoaded++;
if( p->nLoaded==p->nRate ){
KeccakF1600Step(p);
p->nLoaded = 0;
}
}
}
/*
** After all content has been added, invoke HashFinal() to compute
** the final hash. The function returns a pointer to the binary
** hash value.
*/
static unsigned char *HashFinal(HashContext *p){
unsigned int i;
if( p->nLoaded==p->nRate-1 ){
const unsigned char c1 = 0x86;
HashUpdate(p, &c1, 1);
}else{
const unsigned char c2 = 0x06;
const unsigned char c3 = 0x80;
HashUpdate(p, &c2, 1);
p->nLoaded = p->nRate - 1;
HashUpdate(p, &c3, 1);
}
for(i=0; i<p->nRate; i++){
p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
}
return &p->u.x[p->nRate];
}
/*
** Implementation of the hash(X) function.
**
** Return a 160-bit BLOB which is the hash of X.
*/
static void hashFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
HashContext cx;
int eType = sqlite3_value_type(argv[0]);
int nByte = sqlite3_value_bytes(argv[0]);
if( eType==SQLITE_NULL ) return;
HashInit(&cx, 160);
if( eType==SQLITE_BLOB ){
HashUpdate(&cx, sqlite3_value_blob(argv[0]), nByte);
}else{
HashUpdate(&cx, sqlite3_value_text(argv[0]), nByte);
}
sqlite3_result_blob(context, HashFinal(&cx), 160/8, SQLITE_TRANSIENT);
}
/* Register the hash function */
static int hashRegister(sqlite3 *db){
return sqlite3_create_function(db, "hash", 1,
SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
0, hashFunc, 0, 0);
}
/* End of the hashing logic
*****************************************************************************/
/*
** Return the tail of a file pathname. The tail is the last component
** of the path. For example, the tail of "/a/b/c.d" is "c.d".
@ -897,7 +1389,7 @@ static void originSide(SQLiteRsync *p){
closeDb(p);
return;
}
sqlite3_sha_init(p->db, 0, 0);
hashRegister(p->db);
runSql(p, "BEGIN");
runSqlReturnText(p, buf, "PRAGMA journal_mode");
if( sqlite3_stricmp(buf,"wal")!=0 ){
@ -944,7 +1436,7 @@ static void originSide(SQLiteRsync *p){
runSql(p, "CREATE TEMP TABLE badHash(pgno INTEGER PRIMARY KEY)");
pCkHash = prepareStmt(p,
"INSERT INTO badHash SELECT pgno FROM sqlite_dbpage('main')"
" WHERE pgno=?1 AND sha1b(data)!=?2"
" WHERE pgno=?1 AND hash(data)!=?2"
);
if( pCkHash==0 ) break;
}
@ -1081,7 +1573,7 @@ static void replicaSide(SQLiteRsync *p){
closeDb(p);
break;
}
sqlite3_sha_init(p->db, 0, 0);
hashRegister(p->db);
if( runSqlReturnUInt(p, &nRPage, "PRAGMA page_count") ){
break;
}
@ -1105,7 +1597,7 @@ static void replicaSide(SQLiteRsync *p){
}
pStmt = prepareStmt(p,
"SELECT sha1b(data) FROM sqlite_dbpage"
"SELECT hash(data) FROM sqlite_dbpage"
" WHERE pgno<=min(%d,%d)"
" ORDER BY pgno", nRPage, nOPage);
while( sqlite3_step(pStmt)==SQLITE_ROW && p->nErr==0 ){