<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ZeroDB on Medium]]></title>
        <description><![CDATA[Stories by ZeroDB on Medium]]></description>
        <link>https://medium.com/@ZeroDB_?source=rss-a0aa5238b2d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*6EhcPMrg0HZnlO1p.png</url>
            <title>Stories by ZeroDB on Medium</title>
            <link>https://medium.com/@ZeroDB_?source=rss-a0aa5238b2d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 26 Nov 2026 03:50:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ZeroDB_/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Scalable full text search over encrypted data in ZeroDB]]></title>
            <link>https://medium.com/@ZeroDB_/scalable-full-text-search-over-encrypted-data-cb2b5dd5bce2?source=rss-a0aa5238b2d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb2b5dd5bce2</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <dc:creator><![CDATA[ZeroDB]]></dc:creator>
            <pubDate>Mon, 07 Mar 2016 15:42:34 GMT</pubDate>
            <atom:updated>2016-03-09T12:49:23.215Z</atom:updated>
            <content:encoded><![CDATA[<p>In ZeroDB, it’s easy to find encrypted data by the value of one field simply by traversing an index from the client. But what about more complex queries? In particular, full text search, where a user finds documents matching one or multiple keywords in a query, ordered by a relevancy score.</p><p>Originally we started with a standard <a href="https://en.wikipedia.org/wiki/Okapi_BM25">Okapi BM25</a> scoring function, which ZODB used for full text search, and tried optimizing the bucket sizes of the trees used in the indexes. We quickly discovered that this approach isn’t scalable. It worked well enough searching over an encrypted copy of the Linux Kernel Mailing List, but with larger datasets we ended up downloading far too much data to the client.</p><p>We’ve managed to fix that. And our fix may be equally applicable to making Elasticsearch more scalable. Briefly, we’ve reduced average bandwidth requirements for a multi-keyword search (with a limit) from <em>O(n_query_terms)*sum(O(n_docs_per_term) for all query_terms)</em> down to <em>O(n_query_terms)*constant</em>, in an incremental manner. Feel free to test zerodb &gt;= 0.97 / zerodb-server &gt;= 0.1.16 with this new full text search and give us your thoughts on <a href="http://slack.zerodb.io/">Slack</a>. It works with the new version of <em>fields.Text</em>, replacing <em>fields.TextOkapi</em>.</p><p>Here’s a more detailed description for those who are curious. Or you can read <a href="https://github.com/zero-db/zerodb/commit/524902638682f792a8d8c6ebe98028d47482b538#diff-70d3a97e00183a3caf2bfa7e3bb1824aR27">a less wordy version</a> in comments to the code.</p><h4>Okapi BM25</h4><p>What’s wrong with Okapi? Let’s first look at <a href="https://en.wikipedia.org/wiki/Okapi_BM25">how it works</a>. Document score is determined as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*cHlA7nMXEwwnMbIeywW2ag.png" /></figure><p>where <em>f(q, D)</em> is how many times we see term <em>q</em> in the document <em>D</em>, <em>|D|</em> is the length of document <em>D</em> in words and <em>avgdl</em> is an average document length in words, <em>b</em> and <em>k1</em> are constants and <em>IDF</em> is the inverse document frequency. For simplicity, let’s assume that there is only one keyword <em>q</em>, so <em>IDF</em> is just a constant multiplier.</p><p>What’s the problem with this formula? The way we work with large datasets in ZeroDB requires having references to documents pre-ordered in the index, so that limit queries are easy (e.g. just return first 10 <em>docids</em> in the index). But the problem here is that the score depends on both document length and <em>average</em> document length (<em>avgdl</em>) as well. This <em>avgdl</em> changes every time we add a new document to the dataset. And that changes the scores unpredictably, so we have to re-calculate them every time we query.</p><p>That means that we have to download document lengths and <em>f(q)</em> for all documents which contain term <em>q</em>. That’s not only not scalable (especially when there are many documents) but also very insecure. The size of the data we’ve just downloaded is proportional to the number of documents containing <em>q</em>, which, gives the server a good clue what the keyword <em>q</em> probably is, given natural language statistics.</p><p>So for our purposes, Okapi BM25 is not good. We need to try something else.</p><h4>Lucene’s practical scoring function</h4><p>Fortunately, there is a <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/practical-scoring-function.html">more practical way</a> of calculating document scores used by Lucene/Elasticsearch. It appears that in many cases it is just enough to introduce some non-linearity in the scoring function.</p><p>In Lucene’s scoring, for the simple case of one search term <em>q</em>, the score is just proportional to the square root of the number of occurrences of <em>q</em> in the document <em>D</em> divided by the square root of the total number of unique terms in document <em>D</em>.</p><p>In this case, the document score can be pre-calculated and <em>docids</em> per term can be ordered by this score. So, if we do a limit query, we just take first documents from the set pre-ordered by <em>-score</em>. The server could guess how many documents we’ve downloaded, but not how many documents contain this term. This is secure enough!</p><h4>Multi-keyword search and incremental weighted union</h4><p>Ok, that was easy enough. But what happens when there are multiple keywords in a search query? In this case, for each term <em>q_i</em> we effectively already have a list of docids and scores:</p><p><em>scores(q_i) = [(score_i(docid), docid), …]</em></p><p>ordered by -score_i. Documents in the result set will effectively have a total score:</p><p><em>score(docid) = sum(w_i * score_i(docid) for i in query_terms),</em></p><p>And these docids should be ordered by <em>-score</em>. Most queries are limit queries (e.g. top 20 docs with highest score). Can we do these without downloading all the <em>score_i</em> values?</p><p>In Lucene, it’s easier <a href="https://sbdevel.wordpress.com/2010/03/19/string-sorting-in-lucene-without-the-memory-penalty/">to actually download</a> all the <em>score_i</em>s into memory when a query is performed. But when we want everything to stay encrypted on the server, we don’t have such a luxury! So, how do we produce this list of <em>docid</em>s ordered by <em>score</em> without downloading <em>score_i</em> for all terms?</p><p>In order to do that, we first download a small number of document scores and <em>docid</em>s for every keyword <em>q_i</em>. After that, based on the scores and weights we know, and on the knowledge that we first read higher scores, we estimate a possible range (<em>score_min</em> and <em>score_max</em>) for the total score for every document we’ve seen so far. That is, if for a <em>docid</em>, we’ve already downloaded scores <em>i</em> in <em>i_known</em> with scores <em>i</em> in <em>i_unknown</em> still to be downloaded, the possible range of scores for this <em>docid</em> would be:</p><p><em>min_score(docid) = sum(score_i for i in i_known),</em></p><p><em>max_score(docid) = min_score(docid) + sum(min(known score_i) for i in i_unknown).</em></p><p>We calculate these scores for all documents we’ve seen so far, determine their order (sorting by <em>min_score</em>) and check if there could be any order violations (e.g. <em>min_score(docid1) &gt; min_score(docid2)</em> but <em>min_score(docid1) &lt; max_score(docid2)</em>). Also we check if <em>min_score(last_document) &gt; max_possible_score(documents_left_unread)</em>. These tests determine whether we need to read more scores and docids from the server or if we already have enough information to determine the relative ordering of docids.</p><h4>TODO</h4><p>There’s still some work left to be done. Our way of doing weighted unions could be equally applicable to weighted intersections. It opens up possibilities for doing logically complex text searches (with both <em>and</em> and <em>or</em> <em>conditions</em>).</p><p>There are also some optimizations possible regarding reducing the number of roundtrips to get more search results. These optimizations require an ability to read all the contents of a B-Tree in a specified range of values, while performing a number of roundtrips equal to the height of the tree.</p><p>What do you think? Ideas on how we can do this even better? Please tell us in our <a href="http://slack.zerodb.io/">Slack channel</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb2b5dd5bce2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[zerodb-js, ZeroDB in the Browser]]></title>
            <link>https://medium.com/@ZeroDB_/zerodb-js-zerodb-in-the-browser-ffe013dca6f0?source=rss-a0aa5238b2d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffe013dca6f0</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[ZeroDB]]></dc:creator>
            <pubDate>Sat, 13 Feb 2016 16:19:10 GMT</pubDate>
            <atom:updated>2016-02-13T16:19:10.529Z</atom:updated>
            <content:encoded><![CDATA[<p>You’ve asked about using ZeroDB in the browser, and we’ve been working on it. But we need your help.</p><p>ZeroDB is written in Python and to get it working in browsers we’ll need to port it to JavaScript. As we see it, there are two possible ways.</p><h4>pypyjs</h4><p>The first is to run <em>real Python</em> in JavaScript. As crazy as that sounds, it’s actually fast with <a href="http://pypyjs.org/">pypyjs</a>.</p><p>There are two compatibility issues here. One is the fact that JavaScript cannot have multiple processes or threads. However, it can have workers and Logan Perkins managed to <a href="https://github.com/pypyjs/pypyjs/issues/92">wrap those properly</a> (and sockets as well). With these and a few patches, ZODB/BTrees can work pretty well inside JavaScript. We’ve started that effort <a href="https://github.com/zero-db/zerodb-js/tree/master/pypyjs/website/js/pypy.js-0.2.0/lib/modules">here</a>.</p><p>In order for ZeroDB to work, we need to get ZODB working. And that seems relatively easy. <a href="https://pypyjs.zerodb.io/">Try for yourself</a> in the Python terminal:</p><pre>Welcome to PyPy.js!<br>&gt;&gt;&gt; from ZODB import DB, DemoStorage<br>&gt;&gt;&gt; from BTrees.IOBTree import BTree<br>&gt;&gt;&gt; import transaction<br>&gt;&gt;&gt; storage = DemoStorage.DemoStorage()<br>&gt;&gt;&gt; db = DB(storage)<br>&gt;&gt;&gt; conn = db.open()<br>&gt;&gt;&gt; root = conn.root()<br>&gt;&gt;&gt; root[&#39;test&#39;] = BTree()<br>&gt;&gt;&gt; for i in range(1000):<br>… root[&#39;test&#39;][i] = str(i+1)<br>… <br>&gt;&gt;&gt; transaction.commit()<br>&gt;&gt;&gt; root[&#39;test&#39;][10]<br>&#39;11&#39;<br>&gt;&gt;&gt; root[&#39;test&#39;].__getstate__()<br>((BTrees.IOBTree.IOBucket…</pre><p>So, we’ve got ZODB using DemoStorage and B-Trees in the browser. Great!</p><p>You’d think that running Python over JavaScript would be a bit too slow. Apparently, that’s not always the case! For example, we’d often need to convert lists of tuples to dictionaries:</p><pre>&gt;&gt;&gt; import time<br>&gt;&gt;&gt; def test(x): <br>… t0 = time.time() <br>… _ = dict([(i,i) for i in range(x)]) <br>… return time.time() — t0<br>… <br>&gt;&gt;&gt; test(100000)<br>0.03900003433227539<br>&gt;&gt;&gt; test(100000)<br>0.01900005340576172<br>&gt;&gt;&gt; test(100000)<br>0.021000146865844727</pre><p>And in CPython:</p><pre>&gt;&gt;&gt; test(100000)<br>0.038311004638671875<br>&gt;&gt;&gt; test(100000)<br>0.03164410591125488<br>&gt;&gt;&gt; test(100000)<br>0.02698993682861328</pre><p>Woah! Python over Javascript is faster! Sometimes :-)</p><p>However, we want to work with a database *server* which assumes communication over the network. Using the patches from @perkinsl, we can get the socket library emulated for websockets:</p><pre>&gt;&gt;&gt; import socket<br>&gt;&gt;&gt; sock = socket.socket()<br>&gt;&gt;&gt; sock.connect((&#39;<a href="http://echo.websocket.org/">echo.websocket.org</a>&#39;, 80))</pre><p>But even when wrapped as a socket object, we still work in an asynchronous fashion. We cannot wait for the server’s response and continue executing the code. But standard ZEO assumes that our connections work in both synchronous and asynchronous ways. In additon, standard ZEO uses some socket-specific things:</p><pre>&gt;&gt;&gt; import ZEO<br>&gt;&gt;&gt; cl = ZEO.client((&#39;localhost&#39;, 8000))<br>Traceback (most recent call last):<br> File &quot;&lt;console&gt;&quot;, line 1, in &lt;module&gt;<br> File &quot;/lib/pypyjs/lib_pypy/ZEO/__init__.py&quot;, line 26, in client<br> return ZEO.ClientStorage.ClientStorage(*args, **kw)<br> File &quot;/lib/pypyjs/lib_pypy/ZEO/ClientStorage.py&quot;, line 418, in __init__<br> tmax=max_disconnect_poll)<br> File &quot;/lib/pypyjs/lib_pypy/ZEO/zrpc/client.py&quot;, line 144, in __init__<br> self._start_asyncore_loop()<br> File &quot;/lib/pypyjs/lib_pypy/ZEO/zrpc/client.py&quot;, line 160, in _start_asyncore_loop<br> self.trigger = ZEO.zrpc.trigger.trigger(self.map)<br> File &quot;/lib/pypyjs/lib_pypy/ZEO/zrpc/trigger.py&quot;, line 188, in __init__<br> w.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)<br>TypeError: setsockopt() takes exactly 1 argument (4 given)</pre><p>So, ideally, we’d need to rewrite the ZEO client in an asynchronous fashion (e.g., with futures/callbacks). Jim Fulton, the creator of Zope, started <a href="https://github.com/zopefoundation/ZEO/tree/asyncio">porting ZEO to asyncio</a>, so that it will be written in this fashion. Perhaps, it’s possible to adapt that to use for the JavaScript port of ZeroDB.</p><p>Or as an easier alternative, maybe it’s possible to do synchronous network calls from a Worker? <a href="https://github.com/pypyjs/pypyjs/issues/132#issuecomment-109568935">Read more here</a>.</p><p>JavaScript ports of encryption primitives can be interfaced from Python in pypyjs as well (the module `js` in pypyjs makes the global JavaScript namespace accessible from Python).</p><h4>pyjs or Rapydscript</h4><p>Another way is to translate Python to JavaScript without the overhead of having the Python interpreter over emscripten. A good way is to use something like <a href="http://pyjs.org/">pyjs</a> or <a href="https://github.com/atsepkov/RapydScript">RapydScript</a>. The challenge here is that the code will significantly diverge from the original ZODB base. But the benefit is a huge increase in load speed and much smaller memory overhead.</p><p>Of course, in either case it would be preferable to have the same codebase for both the Python and JavaScript versions.</p><p>If you have ideas, let us know in our <a href="http://slack.zerodb.io/">Slack</a>. Or jump into the <a href="https://github.com/zero-db/zerodb-js">repo</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffe013dca6f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Symmetric Key Function Preserving Encryption Schemes]]></title>
            <link>https://medium.com/@ZeroDB_/symmetric-key-function-preserving-encryption-schemes-167afad7b2ac?source=rss-a0aa5238b2d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/167afad7b2ac</guid>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[ZeroDB]]></dc:creator>
            <pubDate>Tue, 02 Feb 2016 17:18:21 GMT</pubDate>
            <atom:updated>2016-03-07T13:53:40.371Z</atom:updated>
            <content:encoded><![CDATA[<p>Diving into the world of encryption and data security can be intimidating. While encryption traditionally renders data unusable, a new wave of function-preserving encryption algorithms have emerged that attempt to provide both security and usability. This guide is designed to help orient you so that you can make an informed decision about the security and usability trade-offs of these new encryption schemes.</p><h4>Unstructured Encryption</h4><p>The gold standard in confidentiality. Probabilistic encryption is semantically secure — given a ciphertext, nothing about the plaintext data can be determined. Encrypting the same plaintext multiple times will yield different ciphertexts, preventing statistical attacks.</p><p>Unfortunately, unstructured encryption renders data completely unusable, obfuscating it to both adversaries and legitimate users alike.</p><p>Let’s use pycryptodome (<a href="https://pypi.python.org/pypi/pycrypto/">pip install pycrypto</a>) to define a probabilistic encryption function:</p><pre>from Crypto.Cipher import AES<br>from Crypto.Random import get_random_bytes</pre><pre>key = get_random_bytes(32)</pre><pre># Probabilistic encryption<br>def encrypt(txt): <br> nonce = get_random_bytes(AES.block_size)<br> cipher = AES.new(key, AES.MODE_CFB, nonce)<br> return nonce + cipher.encrypt(txt)</pre><p>And testing it out:</p><pre>&gt; encrypt(&#39;hello world&#39;)<br>&#39;\x15]Xc\x19\x11\xe1\x13\x83\xbbNyo\xd9~T\x15G\x0f\x9fU\x16\xbc\xc5.\xd6\xb2&#39;</pre><pre>&gt; encrypt(&#39;hello world&#39;)<br>&#39;\xf2\xd4\xceJ\xbbo\x9b\xbf\xfaV\x0e\xd2`\x82\xa5In\x17y\xcd\xb5\xd6M\xf5\xbf\x93\n&#39;</pre><p>As you can see, we get a different ciphertext every time.</p><h4>Selective Encryption</h4><p>Selective encryption only encrypts the sensitive bits of some larger piece of data. This is often done for compliance reasons or when sharing personally identifiable information. The context of the selectively encrypted data is exposed to an adversary:</p><pre><strong>Plaintext:</strong>  The secret to life is 42<br><strong>Ciphertext:</strong> The secret to life is uV7sQO1PCW5o6Rs9v4+JH+nBQXzvlewOgzFeaaFNBFQ=</pre><h4>Searchable (Deterministic) Encryption/ Tokenization</h4><p>Encrypting a given plaintext with deterministic encryption always yields the same ciphertext token. While this makes it trivial to do keyword searches against this token, it also opens your data set to statistical attacks. By comparing the frequency of ciphertext tokens to the frequency of words in natural language, an attacker can infer the plaintext inputs.</p><p>Let’s return to the probabilistic encryption function we defined previously and tweak it a bit:</p><pre># Deterministic encryption<br>def encrypt(txt):            <br>    nonce = chr(0) * AES.block_size<br>    cipher = AES.new(key, AES.MODE_CFB, nonce)<br>    return cipher.encrypt(txt)</pre><p>Testing it out:</p><pre>&gt; encrypt(&#39;hello world&#39;)<br>&#39;\x1c\xd9\xb4G\xa5\xf4\x80\xd6\xdbH\xb3&#39;</pre><pre>&gt; encrypt(&#39;hello world&#39;)<br>&#39;\x1c\xd9\xb4G\xa5\xf4\x80\xd6\xdbH\xb3&#39;</pre><p>Encrypted, but the same result each time!</p><h4>Format Preserving Encryption (FPE)</h4><p>Ciphertext encrypted using an FPE scheme maintains the length and format of the corresponding plaintext. FPE is a special case of deterministic encryption and is particularly useful when a database schema requires fields to be stored according to some validation rules. For this reason dates, credit card numbers, phone numbers are often encrypted using FPE.</p><p>However, encrypting the same plaintext multiple times will yield the same ciphertext, so FPE does leak equality. In cases where a plaintext is not repeated (e.g., credit card or phone numbers) this might not be an issue.</p><pre><strong>Plaintext:</strong>  867-5309<br><strong>Ciphertext:</strong> 091-1399</pre><pre><strong>Plaintext:</strong>  Jan 23, 1932<br><strong>Ciphertext:</strong> Oct 15, 2044</pre><h4>Order Preserving Encryption (OPE)</h4><p>There are variants of searchable encryption in which ciphertexts retain the order of their corresponding plaintexts. OPE preserves sort and range query functionality. However, leaking the order also leaks the relative distance between plaintexts as well as their approximate value.</p><p>Let’s give it a try with <a href="https://pypi.python.org/pypi/pyope/">pip install pyope</a>:</p><pre>&gt; from pyope.ope import OPE<br>&gt; cipher = OPE(b&#39;secret key&#39;)</pre><pre>&gt; cipher.encrypt(100)<br>6569784</pre><pre>&gt; cipher.encrypt(1000)<br>63738157</pre><pre>&gt; cipher.encrypt(10000)<br>651165852</pre><h3>ZeroDB</h3><p>ZeroDB takes a different approach to encryption in-use. Rather than working at the encryption layer, as in the schemes above, ZeroDB makes unstructured encryption usable by creating encrypted indexes for the data whose functionality needs to be preserved. These encrypted indexes are then traversed remotely from a client machine with access to the encryption keys, in such a way that the plaintexts are never revealed to the database server.</p><p>If you’re interested in building highly secure and performant applications with ZeroDB, check out the <a href="http://docs.zerodb.io/">docs</a> and <a href="https://pypi.python.org/pypi/zerodb-server/">pip install zerodb-server</a>. You can find the GitHub repo here: <a href="https://github.com/zero-db/zerodb">https://github.com/zero-db</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=167afad7b2ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ZeroDB, an end-to-end encrypted database is open source!]]></title>
            <link>https://medium.com/@ZeroDB_/zerodb-an-end-to-end-encrypted-database-is-open-source-6f036688107a?source=rss-a0aa5238b2d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f036688107a</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[ZeroDB]]></dc:creator>
            <pubDate>Mon, 07 Dec 2015 14:13:34 GMT</pubDate>
            <atom:updated>2015-12-07T14:13:34.198Z</atom:updated>
            <content:encoded><![CDATA[<p>We’re excited to release ZeroDB, an end-to-end encrypted database, to the world. ZeroDB makes it easy to develop applications with strong security and privacy guarantees by enabling applications to query encrypted data.</p><p>zerodb repo: <a href="https://github.com/zero-db/zerodb/">https://github.com/zero-db/zerodb/</a><br>zerodb-server repo: <a href="https://github.com/zero-db/zerodb-server/">https://github.com/zero-db/zerodb-server/</a><br>Documentation: <a href="http://docs.zerodb.io/">http://docs.zerodb.io/</a></p><p>Now that it’s open source, we want your help to make it better. Try it, build awesome things with it, break it. Then tell us about it.</p><p>Today, we’re releasing a Python implementation. A JavaScript client will be following soon.</p><p>Questions? Ask us on <a href="http://slack.zerodb.io/">Slack</a> or <a href="https://groups.google.com/forum/#!forum/zerodb-dev">Google Groups</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f036688107a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>