hack(crypto): mock up a hash_to_curve implementation

This commit is contained in:
David Rusu 2024-05-28 23:27:55 +04:00
parent 7405b31378
commit 5044486f36
2 changed files with 36 additions and 0 deletions

View File

@ -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):

View File

@ -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