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