From 4d84f0da3f710107dd1f64e354c72e0ff9426e1b Mon Sep 17 00:00:00 2001 From: armfazh Date: Fri, 27 May 2022 18:21:42 -0700 Subject: [PATCH] Adds examples of the OPRF protocol execution. --- poc/Makefile | 7 +++++- poc/example_oprf.sage | 53 ++++++++++++++++++++++++++++++++++++++++ poc/example_poprf.sage | 55 ++++++++++++++++++++++++++++++++++++++++++ poc/example_voprf.sage | 52 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 poc/example_oprf.sage create mode 100644 poc/example_poprf.sage create mode 100644 poc/example_voprf.sage diff --git a/poc/Makefile b/poc/Makefile index 35c52717..58a27886 100644 --- a/poc/Makefile +++ b/poc/Makefile @@ -21,6 +21,11 @@ setup: test: pyfiles sage test_oprf.sage +examples: pyfiles + sage example_oprf.sage + sage example_voprf.sage + sage example_poprf.sage + vectors: pyfiles @echo "Removing vectors folder, if present" @rm -rf vectors @@ -34,4 +39,4 @@ clean: .PHONY: distclean distclean: clean - rm -rf vectors ascii \ No newline at end of file + rm -rf vectors ascii diff --git a/poc/example_oprf.sage b/poc/example_oprf.sage new file mode 100644 index 00000000..50c1c295 --- /dev/null +++ b/poc/example_oprf.sage @@ -0,0 +1,53 @@ +#!/usr/bin/sage +# vim: syntax=python + +"""Exemplifies a run of the OPRF protocol""" + +import os +import sys + +try: + from sagelib.test_drng import TestDRNG + from sagelib.oprf \ + import DeriveKeyPair, SetupOPRFServer, SetupOPRFClient, MODE_OPRF, \ + oprf_ciphersuites, ciphersuite_p256_sha256 + +except ImportError as err: + sys.exit("Try running `make setup && make clean pyfiles`. Full error: " + err) + +to_hex = lambda x: "".join(["{:02x}".format(i) for i in x]) + +if __name__ == "__main__": + # Offline Setup + rng = TestDRNG('prng-seed'.encode('utf-8')) + suite = oprf_ciphersuites[ciphersuite_p256_sha256] + Ns = suite.group.scalar_byte_length() + info = b'info specific for this key' + seed = os.urandom(Ns) + skS, _ = DeriveKeyPair(MODE_OPRF, suite, seed, info) + + client = SetupOPRFClient(suite) + server = SetupOPRFServer(suite, skS) + + # Online Protocol + # + # Client Server(skS) + # ------------------------------------------------------------------- + # blind, blindedElement = Blind(input) + input = b'alice in wonderland' + blind, blinded_element = client.blind(input, rng) + # blindedElement + # ----------> + # + # evaluatedElement = Evaluate(blindedElement) + evaluated_element = server.evaluate(blinded_element, rng) + # + # evaluatedElement + # <---------- + # + # output = Finalize(input, blind, evaluatedElement) + output = client.finalize(input, blind, evaluated_element) + print("mode:", "OPRF") + print("suite:", suite.name) + print("input:", to_hex(input)) + print("output:", to_hex(output)) diff --git a/poc/example_poprf.sage b/poc/example_poprf.sage new file mode 100644 index 00000000..ebe5d258 --- /dev/null +++ b/poc/example_poprf.sage @@ -0,0 +1,55 @@ +#!/usr/bin/sage +# vim: syntax=python + +"""Exemplifies a run of the POPRF protocol""" + +import os +import sys + +try: + from sagelib.test_drng import TestDRNG + from sagelib.oprf \ + import DeriveKeyPair, SetupPOPRFServer, SetupPOPRFClient, MODE_POPRF, \ + oprf_ciphersuites, ciphersuite_p256_sha256 + +except ImportError as err: + sys.exit("Try running `make setup && make clean pyfiles`. Full error: " + err) + +to_hex = lambda x: "".join(["{:02x}".format(i) for i in x]) + +if __name__ == "__main__": + # Offline Setup + rng = TestDRNG('prng-seed'.encode('utf-8')) + suite = oprf_ciphersuites[ciphersuite_p256_sha256] + Ns = suite.group.scalar_byte_length() + info = b'info specific for this key' + seed = os.urandom(Ns) + skS, pkS = DeriveKeyPair(MODE_POPRF, suite, seed, info) + + client = SetupPOPRFClient(suite, pkS) + server = SetupPOPRFServer(suite, skS, pkS) + + # Online Protocol + # + # Client(pkS, info) <---- pkS ------ Server(skS, info) + # ------------------------------------------------------------------- + # blind, blindedElement, tweakedKey = Blind(input, info) + input = b'alice in wonderland' + blind, blinded_element, tweaked_key = client.blind(input, info, rng) + # + # blindedElement + # ----------> + # + # evaluatedElement, proof = Evaluate(blindedElement, info) + evaluated_element, proof, _ = server.evaluate(blinded_element, info, rng) + # + # evaluatedElement, proof + # <---------- + # + # output = Finalize(input, blind, evaluatedElement, + # blindedElement, proof, info, tweakedKey) + output = client.finalize(input, blind, evaluated_element, blinded_element, proof, info, tweaked_key) + print("mode:", "POPRF") + print("suite:", suite.name) + print("input:", to_hex(input)) + print("output:", to_hex(output)) diff --git a/poc/example_voprf.sage b/poc/example_voprf.sage new file mode 100644 index 00000000..a3060dd7 --- /dev/null +++ b/poc/example_voprf.sage @@ -0,0 +1,52 @@ +#!/usr/bin/sage +# vim: syntax=python + +"""Exemplifies a run of the VOPRF protocol""" + +import os +import sys + +try: + from sagelib.test_drng import TestDRNG + from sagelib.oprf \ + import DeriveKeyPair, SetupVOPRFServer, SetupVOPRFClient, MODE_VOPRF, \ + oprf_ciphersuites, ciphersuite_p256_sha256 + +except ImportError as err: + sys.exit("Try running `make setup && make clean pyfiles`. Full error: " + err) + +to_hex = lambda x: "".join(["{:02x}".format(i) for i in x]) + +if __name__ == "__main__": + # Offline Setup + rng = TestDRNG('prng-seed'.encode('utf-8')) + suite = oprf_ciphersuites[ciphersuite_p256_sha256] + Ns = suite.group.scalar_byte_length() + info = b'info specific for this key' + seed = os.urandom(Ns) + skS, pkS = DeriveKeyPair(MODE_VOPRF, suite, seed, info) + + client = SetupVOPRFClient(suite, pkS) + server = SetupVOPRFServer(suite, skS, pkS) + + # Online Protocol + # + # Client(pkS) Server(skS,pkS) + # ------------------------------------------------------------------- + # blind, blindedElement = Blind(input) + input = b'alice in wonderland' + blind, blinded_element = client.blind(input, rng) + # blindedElement + # ----------> + # + # evaluatedElement, proof = Evaluate(blindedElement) + evaluated_element, proof, _ = server.evaluate(blinded_element, rng) + # <---------- + # + # output = Finalize(input, blind, evaluatedElement, + # blindedElement, proof) + output = client.finalize(input, blind, evaluated_element, blinded_element, proof) + print("mode:", "VOPRF") + print("suite:", suite.name) + print("input:", to_hex(input)) + print("output:", to_hex(output))