From 5044486f36dda41fd4880833485129db15dbf5ab Mon Sep 17 00:00:00 2001 From: David Rusu Date: Tue, 28 May 2024 23:27:55 +0400 Subject: [PATCH] hack(crypto): mock up a hash_to_curve implementation --- coordination-layer/crypto.py | 15 +++++++++++++++ coordination-layer/test_crypto.py | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 coordination-layer/test_crypto.py diff --git a/coordination-layer/crypto.py b/coordination-layer/crypto.py index c642c4b..06cc32b 100644 --- a/coordination-layer/crypto.py +++ b/coordination-layer/crypto.py @@ -55,6 +55,21 @@ def prf(domain, *elements) -> Field: return Field(int(POSEIDON([*_str_to_vec(domain), *elements]))) +def hash_to_curve(domain, *elements) -> Point: + # HACK: we don't currently have a proper hash_to_curve implementation + # so we hack the Point.random() function. + # + # Point.random() calls into the global `random` module to generate a + # point. We will seed the random module with the result of hashing the + # elements and then call Point.random() to retreive the point + # corresponding to the mentioned elements. + + r = prf(f"HASH_TO_CURVE_{domain}", *elements) + + import random + + random.seed(r.v) + return Point.random() def comm(*elements): diff --git a/coordination-layer/test_crypto.py b/coordination-layer/test_crypto.py new file mode 100644 index 0000000..f7ce7b1 --- /dev/null +++ b/coordination-layer/test_crypto.py @@ -0,0 +1,21 @@ +""" +This module tests that all the hacks we introduced in our crypto mocks give us +the basic behaviour that we need. +""" + +from unittest import TestCase + + +from crypto import hash_to_curve, Field + + +class TestCrypto(TestCase): + def test_hash_to_curve(self): + p1 = hash_to_curve(Field(0), Field(1), Field(2)) + p2 = hash_to_curve(Field(0), Field(1), Field(2)) + + assert p1 == p2 + + p3 = hash_to_curve(Field(0), Field(1), Field(3)) + + assert p1 != p3