Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Retrieve a database record without revealing its index using SimplePIR

Use SimplePIR when a client knows the zero-based index of a record and wants to read it privately from a table of equal-length records. The client downloads a table-dependent hint once and reuses it for successive lookups. This suits a table that changes infrequently.

Build the server's table and hint

uses
  System.SysUtils, stDrbgAesCtr,
  StreamSec.PIR.Scan, StreamSec.PIR.Simple;
function BuildPirTable(const aRecords: array of TBytes): tSimplePirServer;
var
  lParams: tPirParams;
  lSeed: TBytes;
  I: Integer;
begin
  if Length(aRecords) = 0 then
    raise EArgumentException.Create('Supply at least one record');
  lParams := tPirParams.Resolve(Length(aRecords), Length(aRecords[0]));
  SetLength(lSeed, cPirSeedLen);
  NewPirRandomSource(nil).NextOctets(lSeed[0], Length(lSeed));
  Result := tSimplePirServer.Create(lParams, lSeed);
  try
    for I := 0 to High(aRecords) do
      Result.PutRecord(I, aRecords[I]);
    Result.BuildHint;
  except
    Result.Free;
    raise;
  end;
end;

Supply the records as byte arrays. For example, three four-byte records have indices 0, 1 and 2. Call BuildPirTable on the server. Resolve selects the scheme parameters for this table size. PutRecord checks the length of each record. BuildHint computes the hint for the populated table.

Initialize the client

function NewPirReader(aRecordCount, aRecordLen: Integer; const aSeed: TBytes;
  const aHint: tPirWordArray; aHintVersion: UInt32): tSimplePirClient;
begin
  Result := tSimplePirClient.Create(tPirParams.Resolve(aRecordCount, aRecordLen), aSeed);
  try
    Result.SetHint(aHint, aHintVersion);
  except
    Result.Free;
    raise;
  end;
end;

Publish the record count, record length, public seed, hint and hint version from the same server snapshot. Pass these to NewPirReader on the client. The seed lets each side generate the same public matrix. The client derives the hint digest when it needs it. The stDrbgAesCtr unit registers the random generator used for the public seed and the client's fresh query randomness.

Request one record

type
  TPirMessage = record
    Words: tPirWordArray;
    HintVersion: UInt32;
    HintDigest: TBytes;
  end;
function AnswerPirQuery(aServer: tSimplePirServer; const aQuery: TPirMessage): TPirMessage;
begin
  aServer.Answer(aQuery.Words, aQuery.HintVersion, aQuery.HintDigest, Result.Words);
  Result.HintVersion := aServer.HintVersion;
  Result.HintDigest := aServer.HintDigest;
end;
function ReadPrivateRecord(aClient: tSimplePirClient; aServer: tSimplePirServer;
  aIndex: Integer): TBytes;
var
  lQuery, lReply: TPirMessage;
begin
  aClient.Query(aIndex, lQuery.Words);
  lQuery.HintVersion := aClient.HintVersion;
  lQuery.HintDigest := aClient.HintDigest;
  lReply := AnswerPirQuery(aServer, lQuery);
  Result := aClient.Recover(lReply.Words, lReply.HintVersion, lReply.HintDigest);
end;

For the second record, pass 1 to ReadPrivateRecord. The sample calls the server in memory. Across a transport, send the three fields of lQuery and return the three fields of lReply. Keep the index and the client object on the client. The server sees a randomized query and scans the entire table. Privacy of the requested index relies on the learning with errors assumption.

Recover returns the original record bytes. Finish recovery before submitting another query on that client object because it holds the private state for one outstanding query. Free the client and server when finished with them.

Refresh the hint after a table change

After PutRecord, rebuild the hint before serving from the updated table. Serialize updates with answering a query and reading its version and digest, so each reply describes one snapshot. A client with the previous hint receives ePirHintVersionMismatch. Fetch the current hint and version, call SetHint and make a fresh query. A digest mismatch raises ePirHintMismatch.

The version and digest check that both sides used the same hint. Applications that need proof of the returned record's origin can verify a publisher's signature over the record and its index. For hint sizes, incremental updates and the scheme's trust assumptions, see the private information retrieval notes.

Reference

StreamSec.PIR.Simple · StreamSec.PIR.Scan · stDrbgAesCtr

Related tasks