From 81c3bfd95bc2302f5bab43a9ad4d3f2c40ebd55a Mon Sep 17 00:00:00 2001 From: Christopher Patton Date: Fri, 7 Jun 2024 12:41:21 -0700 Subject: [PATCH] Have `is_prefix()` take in a level rather than a length --- poc/idpf.py | 9 ++++----- poc/tests/idpf.py | 4 ++-- poc/tests/test_idpf_poplar.py | 9 +++++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/poc/idpf.py b/poc/idpf.py index 0f4d83b6..3f55ec3d 100644 --- a/poc/idpf.py +++ b/poc/idpf.py @@ -106,13 +106,12 @@ def current_field(Idpf, level): else Idpf.FieldLeaf @classmethod - def is_prefix(Idpf, x: int, y: int, L: int) -> bool: + def is_prefix(Idpf, x: int, y: int, level: int) -> bool: """ - Returns `True` iff `x` is the prefix of `y` of length `L`. + Returns `True` iff `x` is the prefix of `y` at level `level`. Pre-conditions: - - `0 < L` and `L <= Idpf.BITS` + - `level` in `range(Idpf.BITS)` """ - assert 0 < L and L <= Idpf.BITS - return y >> (Idpf.BITS - L) == x + return y >> (Idpf.BITS - 1 - level) == x diff --git a/poc/tests/idpf.py b/poc/tests/idpf.py index e1aba0b4..06c54830 100644 --- a/poc/tests/idpf.py +++ b/poc/tests/idpf.py @@ -25,7 +25,7 @@ def test_idpf(Idpf, alpha, level, prefixes): out[i] = vec_add(out[i], out_share[i]) for (got, prefix) in zip(out, prefixes): - if Idpf.is_prefix(prefix, alpha, level+1): + if Idpf.is_prefix(prefix, alpha, level): if level < Idpf.BITS-1: want = beta_inner[level] else: @@ -68,7 +68,7 @@ def test_idpf_exhaustive(Idpf, alpha): got = reduce(lambda x, y: vec_add(x, y), map(lambda x: x[prefix], out_shares)) - if Idpf.is_prefix(prefix, alpha, level+1): + if Idpf.is_prefix(prefix, alpha, level): if level < Idpf.BITS-1: want = beta_inner[level] else: diff --git a/poc/tests/test_idpf_poplar.py b/poc/tests/test_idpf_poplar.py index 93e5b202..658301f6 100644 --- a/poc/tests/test_idpf_poplar.py +++ b/poc/tests/test_idpf_poplar.py @@ -81,3 +81,12 @@ def shard(s): 1, public_share, keys[1], level, (alpha,), binder) out = vec_add(out_share_0[0], out_share_1[0])[0] self.assertEqual(out.as_unsigned(), 1) + + def test_is_prefix(self): + cls = IdpfPoplar.with_value_len(1).with_bits(8) + self.assertTrue(cls.is_prefix(0b1, 0b11000001, 0)) + self.assertTrue(cls.is_prefix(0b11, 0b11000001, 1)) + self.assertTrue(cls.is_prefix(0b110, 0b11000001, 2)) + self.assertTrue(cls.is_prefix(0b1100, 0b11000001, 3)) + self.assertFalse(cls.is_prefix(0b111, 0b11000001, 2)) + self.assertFalse(cls.is_prefix(0b1101, 0b11000001, 3))