From 2d0e4825c0b88cde99946c8b60ff22056520fb96 Mon Sep 17 00:00:00 2001 From: Christopher Patton Date: Mon, 14 Oct 2024 13:44:16 -0700 Subject: [PATCH] Rename `Field.as_unsigned()` to `int()` --- draft-irtf-cfrg-vdaf.md | 34 ++++++++++++++++----------------- poc/gen_test_vec.py | 4 ++-- poc/tests/test_daf.py | 2 +- poc/tests/test_field.py | 6 +++--- poc/tests/test_flp.py | 4 ++-- poc/tests/test_flp_bbcggi19.py | 2 +- poc/tests/test_idpf_bbcggi21.py | 2 +- poc/vdaf_poc/field.py | 4 ++-- poc/vdaf_poc/flp_bbcggi19.py | 16 ++++++++-------- poc/vdaf_poc/idpf_bbcggi21.py | 12 ++++++------ poc/vdaf_poc/test_utils.py | 2 +- poc/vdaf_poc/vdaf_poplar1.py | 2 +- 12 files changed, 45 insertions(+), 45 deletions(-) diff --git a/draft-irtf-cfrg-vdaf.md b/draft-irtf-cfrg-vdaf.md index b165ff1c..16513867 100644 --- a/draft-irtf-cfrg-vdaf.md +++ b/draft-irtf-cfrg-vdaf.md @@ -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 @@ -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]: @@ -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 @@ -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 @@ -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 @@ -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} @@ -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] ~~~ @@ -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') @@ -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 @@ -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] ~~~ @@ -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} @@ -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) ~~~ @@ -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: @@ -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] @@ -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] @@ -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] diff --git a/poc/gen_test_vec.py b/poc/gen_test_vec.py index 02913de4..cea2a698 100755 --- a/poc/gen_test_vec.py +++ b/poc/gen_test_vec.py @@ -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), diff --git a/poc/tests/test_daf.py b/poc/tests/test_daf.py index f519e12a..303a9934 100644 --- a/poc/tests/test_daf.py +++ b/poc/tests/test_daf.py @@ -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") diff --git a/poc/tests/test_field.py b/poc/tests/test_field.py index 8b17f2bc..e4590940 100644 --- a/poc/tests/test_field.py +++ b/poc/tests/test_field.py @@ -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) @@ -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)) diff --git a/poc/tests/test_flp.py b/poc/tests/test_flp.py index d3999f75..5125cb8e 100644 --- a/poc/tests/test_flp.py +++ b/poc/tests/test_flp.py @@ -44,7 +44,7 @@ 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 @@ -52,7 +52,7 @@ 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): diff --git a/poc/tests/test_flp_bbcggi19.py b/poc/tests/test_flp_bbcggi19.py index fd100bab..dc37c301 100644 --- a/poc/tests/test_flp_bbcggi19.py +++ b/poc/tests/test_flp_bbcggi19.py @@ -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): diff --git a/poc/tests/test_idpf_bbcggi21.py b/poc/tests/test_idpf_bbcggi21.py index 2020aa3d..2d815693 100644 --- a/poc/tests/test_idpf_bbcggi21.py +++ b/poc/tests/test_idpf_bbcggi21.py @@ -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) diff --git a/poc/vdaf_poc/field.py b/poc/vdaf_poc/field.py index bd8765c0..27f7d9b2 100644 --- a/poc/vdaf_poc/field.py +++ b/poc/vdaf_poc/field.py @@ -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 @@ -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 diff --git a/poc/vdaf_poc/flp_bbcggi19.py b/poc/vdaf_poc/flp_bbcggi19.py index 2a4b9d3e..2d0a2565 100644 --- a/poc/vdaf_poc/flp_bbcggi19.py +++ b/poc/vdaf_poc/flp_bbcggi19.py @@ -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 @@ -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]: @@ -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') @@ -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 @@ -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]: @@ -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 @@ -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 @@ -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) diff --git a/poc/vdaf_poc/idpf_bbcggi21.py b/poc/vdaf_poc/idpf_bbcggi21.py index c7430514..18e1493b 100644 --- a/poc/vdaf_poc/idpf_bbcggi21.py +++ b/poc/vdaf_poc/idpf_bbcggi21.py @@ -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: @@ -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] @@ -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] @@ -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] @@ -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 diff --git a/poc/vdaf_poc/test_utils.py b/poc/vdaf_poc/test_utils.py index 3cd9aeb0..e1f48602 100644 --- a/poc/vdaf_poc/test_utils.py +++ b/poc/vdaf_poc/test_utils.py @@ -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) diff --git a/poc/vdaf_poc/vdaf_poplar1.py b/poc/vdaf_poc/vdaf_poplar1.py index 8b05653a..ad6febfb 100644 --- a/poc/vdaf_poc/vdaf_poplar1.py +++ b/poc/vdaf_poc/vdaf_poplar1.py @@ -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