-
Notifications
You must be signed in to change notification settings - Fork 1
/
StatelessGrouping.h
121 lines (99 loc) · 2.71 KB
/
StatelessGrouping.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
///////////////////////////////////////////////////////////////////////////////
// Constant Time Stateless Shuffling and Grouping //
// Copyright (c) 2023 Electronic Arts Inc. All rights reserved. //
///////////////////////////////////////////////////////////////////////////////
#pragma once
#include <vector>
typedef unsigned int uint32;
class StatelessGrouping
{
public:
void SetSeed(uint32 seed)
{
m_seed = seed;
}
void SetIndexBits(uint32 bits)
{
m_halfIndexBits = bits / 2;
m_halfIndexBitsMask = (1 << m_halfIndexBits) - 1;
}
void SetRoundCount(uint32 count)
{
m_roundCount = count;
}
void SetGroupSize(uint32 size)
{
m_groupSize = size;
}
// Get a specific member of a group
uint32 GetMember(uint32 index, uint32 memberIndex)
{
// Bijection function
index = Encrypt(index);
// Involution
index = Involution(index, memberIndex);
// Inverse bijection function
return Decrypt(index);
}
// Get all members of a group
void GetGroup(uint32 index, std::vector<uint32>& members)
{
// Bijection function
index = Encrypt(index);
// Get all the members
members.resize(m_groupSize);
for (uint32 i = 0; i < m_groupSize; ++i)
members[i] = Decrypt(Involution(index, i));
}
private:
// Nth order involution, where N = m_groupSize
uint32 Involution(uint32 x, uint32 offset)
{
return m_groupSize * (x / m_groupSize) + ((x + offset) % m_groupSize);
}
uint32 Encrypt(uint32 index)
{
uint32 left = (index >> m_halfIndexBits);
uint32 right = index & m_halfIndexBitsMask;
for (uint32 i = 0; i < m_roundCount; ++i)
{
uint32 newLeft = right;
uint32 newRight = left ^ RoundFunction(right);
left = newLeft;
right = newRight;
}
return (left << m_halfIndexBits) | right;
}
uint32 Decrypt(uint32 index)
{
uint32 left = (index >> m_halfIndexBits);
uint32 right = index & m_halfIndexBitsMask;
for (uint32 i = 0; i < m_roundCount; ++i)
{
uint32 newRight = left;
uint32 newLeft = right ^ RoundFunction(left);
left = newLeft;
right = newRight;
}
return (left << m_halfIndexBits) | right;
}
uint32 RoundFunction(uint32 x)
{
// This is a function of both the input, as well as the key
return (pcg_hash(x ^ m_seed)) & m_halfIndexBitsMask;
}
// https://www.pcg-random.org/
// https://www.reedbeta.com/blog/hash-functions-for-gpu-rendering/
uint32 pcg_hash(uint32 input)
{
uint32 state = input * 747796405u + 2891336453u;
uint32 word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
private:
uint32 m_roundCount = 0;
uint32 m_groupSize = 0;
uint32 m_halfIndexBits = 0;
uint32 m_halfIndexBitsMask = 0;
uint32 m_seed = 0; // The "random seed" that determines ordering
};