The PIR protocol (Private Information Retrieval) applies to scenarios where you obtain the attached values of matching items from the other party's dataset without exposing the query conditions, for example, querying configuration information by identifier or querying entitlement information by credential.
Private information retrieval through the PIR protocol involves three steps: generating a privacy target genPrivacyTarget, performing a privacy search privacySearch, and obtaining the search result getSearchResult. The PIR protocol returns the attached value of the matching element upon a hit.
Import the required modules.
import { privacyComputation } from '@kit.DataProtectionKit'; The initiator generates a privacy target.
Construct TargetElement (the original data of the target element and the hash algorithm) and PrivacyProtocol (set the protocol type to PIR_PROTOCOL), and call genPrivacyTarget to generate the privacy target.
const targetElement: privacyComputation.TargetElement = {
elemData: new Uint8Array([0x01, 0x02, 0x03]),
hashAlg: privacyComputation.HashAlg.SHA256
};
const privacyProtocol: privacyComputation.PrivacyProtocol = {
dataSetSize: privacyComputation.DataSetSize.SIZE_256,
protocolType: privacyComputation.ProtocolType.PIR_PROTOCOL
};
const privacyTarget: Uint8Array = await privacyComputation.genPrivacyTarget(targetElement, privacyProtocol); The responder performs a privacy search.
Construct an array of Element (dataset elements, including the key, hash algorithm, and attached value elemValue), and call privacySearch together with the privacy target and protocol configuration to perform the privacy search. elemValue is the attached value returned upon a hit and is used only in the PIR protocol.
const elements: privacyComputation.Element[] = [
{
elemKey: new Uint8Array([0x01, 0x02, 0x03]),
hashAlg: privacyComputation.HashAlg.SHA256,
elemValue: new Uint8Array([0xA1, 0xA2])
},
{
elemKey: new Uint8Array([0x07, 0x08, 0x09]),
hashAlg: privacyComputation.HashAlg.SHA256,
elemValue: new Uint8Array([0xB1, 0xB2])
}
];
const searchResult: privacyComputation.PrivacySearchResult =
await privacyComputation.privacySearch(privacyTarget, elements, privacyProtocol); The initiator obtains the final result.
Pass the result returned by privacySearch to getSearchResult and decrypt it to obtain SearchResult. matchedResult indicates whether there is a hit; upon a hit, attachedValues returns the attached value of the matching element.
const result: privacyComputation.SearchResult =
await privacyComputation.getSearchResult(searchResult, privacyProtocol);
console.info('matched: ' + result.matchedResult);
if (result.matchedResult && result.attachedValues) {
console.info('attachedValues count: ' + result.attachedValues.length);
}