Skip to content

Commit

Permalink
Have is_prefix() take in a level rather than a length
Browse files Browse the repository at this point in the history
  • Loading branch information
cjpatton committed Jun 7, 2024
1 parent 3ac0362 commit 81c3bfd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
9 changes: 4 additions & 5 deletions poc/idpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions poc/tests/idpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions poc/tests/test_idpf_poplar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 81c3bfd

Please sign in to comment.