Skip to content

Commit

Permalink
Adds examples of the OPRF protocol execution.
Browse files Browse the repository at this point in the history
  • Loading branch information
armfazh committed May 28, 2022
1 parent f19f9e5 commit 4d84f0d
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
7 changes: 6 additions & 1 deletion poc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,4 +39,4 @@ clean:

.PHONY: distclean
distclean: clean
rm -rf vectors ascii
rm -rf vectors ascii
53 changes: 53 additions & 0 deletions poc/example_oprf.sage
Original file line number Diff line number Diff line change
@@ -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))
55 changes: 55 additions & 0 deletions poc/example_poprf.sage
Original file line number Diff line number Diff line change
@@ -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))
52 changes: 52 additions & 0 deletions poc/example_voprf.sage
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 4d84f0d

Please sign in to comment.