Skip to content

Commit

Permalink
Rename Field.as_unsigned() to int()
Browse files Browse the repository at this point in the history
  • Loading branch information
cjpatton committed Oct 15, 2024
1 parent 931a4fe commit 2d0e482
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 45 deletions.
34 changes: 17 additions & 17 deletions draft-irtf-cfrg-vdaf.md
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,7 @@ subtraction, multiplication, division, negation, and inversion are denoted,
respectively, `x + y`, `x - y`, `x * y`, `x / y`, `-x`, and `x.inv()`.

We sometimes need to convert a field element to an `int`, which we denote by
`x.as_unsigned()`. Likewise, each concrete `Field` implements a constructor for
`x.int()`. Likewise, each concrete `Field` implements a constructor for
converting an unsigned integer into a field element:

* `Field(integer: int)` returns `integer` represented as a field element. The
Expand All @@ -1995,7 +1995,7 @@ def encode_vec(cls, vec: list[Self]) -> bytes:
"""
encoded = bytes()
for x in vec:
encoded += to_le_bytes(x.as_unsigned(), cls.ENCODED_SIZE)
encoded += to_le_bytes(x.int(), cls.ENCODED_SIZE)
return encoded

def decode_vec(cls, encoded: bytes) -> list[Self]:
Expand Down Expand Up @@ -3838,7 +3838,7 @@ class Count(Valid[int, int, F]):
return meas

def decode(self, output: list[F], _num_measurements: int) -> int:
return output[0].as_unsigned()
return output[0].int()
~~~

### Prio3Sum
Expand Down Expand Up @@ -3921,7 +3921,7 @@ class Sum(Valid[int, int, F]):
self.bits
)
encoded += self.field.encode_into_bit_vector(
measurement + self.offset.as_unsigned(),
measurement + self.offset.int(),
self.bits
)
return encoded
Expand All @@ -3930,7 +3930,7 @@ class Sum(Valid[int, int, F]):
return [self.field.decode_from_bit_vector(meas[:self.bits])]

def decode(self, output: list[F], _num_measurements: int) -> int:
return output[0].as_unsigned()
return output[0].int()
~~~

### Prio3SumVec
Expand Down Expand Up @@ -4059,7 +4059,7 @@ class SumVec(Valid[list[int], list[int], F]):
self,
output: list[F],
_num_measurements: int) -> list[int]:
return [x.as_unsigned() for x in output]
return [x.int() for x in output]
~~~

#### Selection of `ParallelSum` Chunk Length {#parallel-sum-chunk-length}
Expand Down Expand Up @@ -4195,7 +4195,7 @@ class Histogram(Valid[int, list[int], F]):
self,
output: list[F],
_num_measurements: int) -> list[int]:
return [bucket_count.as_unsigned()
return [bucket_count.int()
for bucket_count in output]
~~~

Expand Down Expand Up @@ -4266,7 +4266,7 @@ class MultihotCountVec(Valid[list[int], list[int], F]):
# Make sure `offset + length` doesn't overflow the field
# modulus. Otherwise we may not correctly compute the sum
# measurement vector entries during circuit evaluation.
if self.field.MODULUS - self.offset.as_unsigned() <= length:
if self.field.MODULUS - self.offset.int() <= length:
raise ValueError('length and max_weight are too large '
'for the current field size')

Expand Down Expand Up @@ -4338,7 +4338,7 @@ class MultihotCountVec(Valid[list[int], list[int], F]):
encoded = []
encoded += count_vec
encoded += self.field.encode_into_bit_vector(
(self.offset + weight_reported).as_unsigned(),
(self.offset + weight_reported).int(),
self.bits_for_weight)
return encoded

Expand All @@ -4349,7 +4349,7 @@ class MultihotCountVec(Valid[list[int], list[int], F]):
self,
output: list[F],
_num_measurements: int) -> list[int]:
return [bucket_count.as_unsigned() for
return [bucket_count.int() for
bucket_count in output]
~~~

Expand Down Expand Up @@ -4999,7 +4999,7 @@ def unshard(
agg_shares: list[FieldVec],
_num_measurements: int) -> list[int]:
agg = self.merge(agg_param, agg_shares)
return [x.as_unsigned() for x in agg]
return [x.int() for x in agg]
~~~

### Message Serialization {#poplar1-encode}
Expand Down Expand Up @@ -5056,7 +5056,7 @@ Field `packed_control_bits` is encoded with the following function:
~~~ python
packed_control_buf = [int(0)] * packed_len
for i, bit in enumerate(control_bits):
packed_control_buf[i // 8] |= bit.as_unsigned() << (i % 8)
packed_control_buf[i // 8] |= bit.int() << (i % 8)
packed_control_bits = bytes(packed_control_buf)
~~~

Expand Down Expand Up @@ -5354,13 +5354,13 @@ def gen(
# input-dependent array indices should be replaced with
# constant-time selects in practice in order to reduce
# leakage via timing side channels.
if ctrl[0].as_unsigned():
if ctrl[0].int():
x0 = xor(s0[keep], seed_cw)
ctrl[0] = t0[keep] + ctrl_cw[keep]
else:
x0 = s0[keep]
ctrl[0] = t0[keep]
if ctrl[1].as_unsigned():
if ctrl[1].int():
x1 = xor(s1[keep], seed_cw)
ctrl[1] = t1[keep] + ctrl_cw[keep]
else:
Expand All @@ -5383,7 +5383,7 @@ def gen(
# replaced with a constant time select or a constant time
# multiplication in practice in order to reduce leakage via
# timing side channels.
if ctrl[1].as_unsigned():
if ctrl[1].int():
for i in range(len(w_cw)):
w_cw[i] = -w_cw[i]

Expand Down Expand Up @@ -5486,7 +5486,7 @@ def eval_next(
# input-dependent array indices should be replaced with
# constant-time selects in practice in order to reduce leakage
# via timing side channels.
if prev_ctrl.as_unsigned():
if prev_ctrl.int():
s[0] = xor(s[0], seed_cw)
s[1] = xor(s[1], seed_cw)
t[0] += ctrl_cw[0]
Expand All @@ -5499,7 +5499,7 @@ def eval_next(
# Implementation note: this conditional addition should be
# replaced with a constant-time select in practice in order to
# reduce leakage via timing side channels.
if next_ctrl.as_unsigned():
if next_ctrl.int():
for i in range(len(y)):
y[i] += w_cw[i]

Expand Down
4 changes: 2 additions & 2 deletions poc/gen_test_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def gen_test_vec_for_idpf(idpf: Idpf,
)

printable_beta_inner = [
[str(elem.as_unsigned()) for elem in value] for value in beta_inner
[str(elem.int()) for elem in value] for value in beta_inner
]
printable_beta_leaf = [str(elem.as_unsigned()) for elem in beta_leaf]
printable_beta_leaf = [str(elem.int()) for elem in beta_leaf]
printable_keys = [key.hex() for key in keys]
test_vec = {
'bits': int(idpf.BITS),
Expand Down
2 changes: 1 addition & 1 deletion poc/tests/test_daf.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def unshard(
_agg_param: None,
agg_shares: list[Field128],
_num_measurements: int) -> int:
return reduce(lambda x, y: x + y, agg_shares).as_unsigned()
return reduce(lambda x, y: x + y, agg_shares).int()


Measurement = TypeVar("Measurement")
Expand Down
6 changes: 3 additions & 3 deletions poc/tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def run_field_test(self, cls: type[Field]) -> None:
for val in vals:
encoded = cls.encode_into_bit_vector(val, bits)
self.assertTrue(cls.decode_from_bit_vector(
encoded).as_unsigned() == val)
encoded).int() == val)

def run_ntt_field_test(self, cls: type[NttField]) -> None:
self.run_field_test(cls)
Expand All @@ -62,8 +62,8 @@ def test_field255(self) -> None:

def test_field2(self) -> None:
# Test GF(2).
self.assertEqual(Field2(1).as_unsigned(), 1)
self.assertEqual(Field2(0).as_unsigned(), 0)
self.assertEqual(Field2(1).int(), 1)
self.assertEqual(Field2(0).int(), 0)
self.assertEqual(Field2(1) + Field2(1), Field2(0))
self.assertEqual(Field2(1) * Field2(1), Field2(1))
self.assertEqual(-Field2(1), Field2(1))
Expand Down
4 changes: 2 additions & 2 deletions poc/tests/test_flp.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ def decide(self, verifier: list[F]) -> bool:
measurement."""
if len(verifier) != 2 or \
verifier[0] != verifier[1] or \
verifier[0].as_unsigned() not in self.meas_range:
verifier[0].int() not in self.meas_range:
return False
return True

def truncate(self, meas: list[F]) -> list[F]:
return [meas[0]]

def decode(self, output: list[F], _num_measurements: int) -> int:
return output[0].as_unsigned()
return output[0].int()


class TestFlp(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion poc/tests/test_flp_bbcggi19.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def truncate(self, meas: list[Field64]) -> list[Field64]:
return meas

def decode(self, output: list[Field64], _num_measurements: int) -> int:
return output[0].as_unsigned()
return output[0].int()


class TestAverage(Sum):
Expand Down
2 changes: 1 addition & 1 deletion poc/tests/test_idpf_bbcggi21.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def shard(s: bytes) -> tuple[list[CorrectionWord], list[bytes]]:
out_share_1 = cast(list[list[Field]], idpf.eval(
1, public_share, keys[1], level, (prefix,), ctx, nonce))
out = vec_add(out_share_0[0], out_share_1[0])[0]
self.assertEqual(out.as_unsigned(), 1)
self.assertEqual(out.int(), 1)

def test_is_prefix(self) -> None:
idpf = IdpfBBCGGI21(1, 8)
Expand Down
4 changes: 2 additions & 2 deletions poc/vdaf_poc/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def encode_vec(cls, vec: list[Self]) -> bytes:
"""
encoded = bytes()
for x in vec:
encoded += to_le_bytes(x.as_unsigned(), cls.ENCODED_SIZE)
encoded += to_le_bytes(x.int(), cls.ENCODED_SIZE)
return encoded

@classmethod
Expand Down Expand Up @@ -145,7 +145,7 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return str(self.val)

def as_unsigned(self) -> int:
def int(self) -> int:
return self.val


Expand Down
16 changes: 8 additions & 8 deletions poc/vdaf_poc/flp_bbcggi19.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def truncate(self, meas: list[F]) -> list[F]:
return meas

def decode(self, output: list[F], _num_measurements: int) -> int:
return output[0].as_unsigned()
return output[0].int()


# NOTE: This class is excerpted in the document. Its width should be
Expand Down Expand Up @@ -726,7 +726,7 @@ def decode(
self,
output: list[F],
_num_measurements: int) -> list[int]:
return [bucket_count.as_unsigned()
return [bucket_count.int()
for bucket_count in output]

def test_vec_set_type_param(self, test_vec: dict[str, Any]) -> list[str]:
Expand Down Expand Up @@ -800,7 +800,7 @@ def __init__(self,
# Make sure `offset + length` doesn't overflow the field
# modulus. Otherwise we may not correctly compute the sum
# measurement vector entries during circuit evaluation.
if self.field.MODULUS - self.offset.as_unsigned() <= length:
if self.field.MODULUS - self.offset.int() <= length:
raise ValueError('length and max_weight are too large '
'for the current field size')

Expand Down Expand Up @@ -874,7 +874,7 @@ def encode(self, measurement: list[int]) -> list[F]:
encoded = []
encoded += count_vec
encoded += self.field.encode_into_bit_vector(
(self.offset + weight_reported).as_unsigned(),
(self.offset + weight_reported).int(),
self.bits_for_weight)
return encoded

Expand All @@ -885,7 +885,7 @@ def decode(
self,
output: list[F],
_num_measurements: int) -> list[int]:
return [bucket_count.as_unsigned() for
return [bucket_count.int() for
bucket_count in output]

def test_vec_set_type_param(self, test_vec: dict[str, Any]) -> list[str]:
Expand Down Expand Up @@ -998,7 +998,7 @@ def decode(
self,
output: list[F],
_num_measurements: int) -> list[int]:
return [x.as_unsigned() for x in output]
return [x.int() for x in output]

def test_vec_set_type_param(self, test_vec: dict[str, Any]) -> list[str]:
test_vec['length'] = self.length
Expand Down Expand Up @@ -1082,7 +1082,7 @@ def encode(self, measurement: int) -> list[F]:
self.bits
)
encoded += self.field.encode_into_bit_vector(
measurement + self.offset.as_unsigned(),
measurement + self.offset.int(),
self.bits
)
return encoded
Expand All @@ -1091,7 +1091,7 @@ def truncate(self, meas: list[F]) -> list[F]:
return [self.field.decode_from_bit_vector(meas[:self.bits])]

def decode(self, output: list[F], _num_measurements: int) -> int:
return output[0].as_unsigned()
return output[0].int()

def test_vec_set_type_param(self, test_vec: dict[str, Any]) -> list[str]:
test_vec['max_measurement'] = int(self.max_measurement)
Expand Down
12 changes: 6 additions & 6 deletions poc/vdaf_poc/idpf_bbcggi21.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ def gen(
# input-dependent array indices should be replaced with
# constant-time selects in practice in order to reduce
# leakage via timing side channels.
if ctrl[0].as_unsigned():
if ctrl[0].int():
x0 = xor(s0[keep], seed_cw)
ctrl[0] = t0[keep] + ctrl_cw[keep]
else:
x0 = s0[keep]
ctrl[0] = t0[keep]
if ctrl[1].as_unsigned():
if ctrl[1].int():
x1 = xor(s1[keep], seed_cw)
ctrl[1] = t1[keep] + ctrl_cw[keep]
else:
Expand All @@ -137,7 +137,7 @@ def gen(
# replaced with a constant time select or a constant time
# multiplication in practice in order to reduce leakage via
# timing side channels.
if ctrl[1].as_unsigned():
if ctrl[1].int():
for i in range(len(w_cw)):
w_cw[i] = -w_cw[i]

Expand Down Expand Up @@ -238,7 +238,7 @@ def eval_next(
# input-dependent array indices should be replaced with
# constant-time selects in practice in order to reduce leakage
# via timing side channels.
if prev_ctrl.as_unsigned():
if prev_ctrl.int():
s[0] = xor(s[0], seed_cw)
s[1] = xor(s[1], seed_cw)
t[0] += ctrl_cw[0]
Expand All @@ -251,7 +251,7 @@ def eval_next(
# Implementation note: this conditional addition should be
# replaced with a constant-time select in practice in order to
# reduce leakage via timing side channels.
if next_ctrl.as_unsigned():
if next_ctrl.int():
for i in range(len(y)):
y[i] += w_cw[i]

Expand Down Expand Up @@ -364,7 +364,7 @@ def pack_bits(control_bits: list[Field2]) -> bytes:
# ===================================================================
packed_control_buf = [int(0)] * packed_len
for i, bit in enumerate(control_bits):
packed_control_buf[i // 8] |= bit.as_unsigned() << (i % 8)
packed_control_buf[i // 8] |= bit.int() << (i % 8)
packed_control_bits = bytes(packed_control_buf)
# NOTE: End of exerpt.
return packed_control_bits
Expand Down
2 changes: 1 addition & 1 deletion poc/vdaf_poc/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def gen_test_vec_for_vdaf(

for out_share in outbound_out_shares:
prep_test_vec['out_shares'].append([
to_le_bytes(x.as_unsigned(), x.ENCODED_SIZE).hex()
to_le_bytes(x.int(), x.ENCODED_SIZE).hex()
for x in out_share
])
test_vec['prep'].append(prep_test_vec)
Expand Down
2 changes: 1 addition & 1 deletion poc/vdaf_poc/vdaf_poplar1.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def unshard(
agg_shares: list[FieldVec],
_num_measurements: int) -> list[int]:
agg = self.merge(agg_param, agg_shares)
return [x.as_unsigned() for x in agg]
return [x.int() for x in agg]

def encode_agg_param(self, agg_param: Poplar1AggParam) -> bytes:
level, prefixes = agg_param
Expand Down

0 comments on commit 2d0e482

Please sign in to comment.