xref: /aosp_15_r20/external/rappor/tests/fastrand.py (revision 2abb31345f6c95944768b5222a9a5ed3fc68cc00)
1# Copyright 2014 Google Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""fastrand.py - Python wrapper for _fastrand."""
16
17# NOTE: We could retire this module in favor of the C++ client?  One reason to
18# keep it is if it supports a wider range of params (e.g. more than 32 or 64
19# bits.)
20
21import random
22
23import _fastrand
24
25
26class FastIrrRand(object):
27  """Fast insecure version of rappor.SecureIrrRand."""
28
29  def __init__(self, params):
30    randbits = _fastrand.randbits  # accelerated function
31    num_bits = params.num_bloombits
32
33    # IRR probabilities
34    self.p_gen = lambda: randbits(params.prob_p, num_bits)
35    self.q_gen = lambda: randbits(params.prob_q, num_bits)
36