-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathmain.ts
More file actions
181 lines (169 loc) · 4.84 KB
/
main.ts
File metadata and controls
181 lines (169 loc) · 4.84 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { parse } from 'ts-command-line-args'
import { readFileSync } from 'fs'
import { Issue } from '../header-validator/context'
import { Maybe } from '../header-validator/maybe'
import { validateSource } from '../header-validator/validate-source'
import { AttributionScopes } from '../header-validator/source'
import { SourceType, parseSourceType } from '../source-type'
import * as vsv from '../vendor-specific-values'
import { Config, PerTriggerDataConfig } from './privacy'
// Workaround for `parse` not handling top-level array types without `multiple`
// `OptionDef` configuration.
type Wrapped<T> = { value: T }
function commaSeparatedInts(str: string): Wrapped<number[]> {
return { value: str.split(',').map((v) => Number(v)) }
}
interface Arguments {
max_event_level_reports: number
attribution_scope_limit?: number
max_event_states?: number
epsilon: number
source_type: SourceType
windows?: Wrapped<number[]>
buckets?: Wrapped<number[]>
json_file?: string
help: boolean
}
const options = parse<Arguments>(
{
max_event_level_reports: {
alias: 'm',
type: Number,
defaultValue: 20,
},
attribution_scope_limit: {
alias: 'a',
type: Number,
optional: true,
},
max_event_states: {
alias: 's',
type: Number,
optional: true,
},
epsilon: {
alias: 'e',
type: Number,
defaultValue: 14,
},
source_type: {
alias: 't',
type: parseSourceType,
defaultValue: SourceType.navigation,
},
windows: {
alias: 'w',
type: commaSeparatedInts,
optional: true,
},
buckets: {
alias: 'b',
type: commaSeparatedInts,
optional: true,
},
json_file: {
alias: 'f',
type: String,
optional: true,
},
help: {
alias: 'h',
type: Boolean,
description: 'Prints this usage guide.',
},
},
{
helpArg: 'help',
headerContentSections: [
{
header: 'Attribution Reporting Flexible Event',
content:
'Computes privacy-related information for an attribution source.',
},
],
}
)
function logIssue(prefix: string, i: Issue): void {
console.log(
`${prefix}: ${i.path !== undefined ? i.path.join('/') : 'root'}: ${i.msg}`
)
}
let config: Maybe<Config> = Maybe.None
if (options.json_file !== undefined) {
const json = readFileSync(options.json_file, { encoding: 'utf8' })
const [{ errors, warnings }, source] = validateSource(json, {
vsv: vsv.Chromium,
sourceType: options.source_type,
})
warnings.forEach((i) => logIssue('W', i))
if (errors.length > 0) {
errors.forEach((i) => logIssue('E', i))
process.exit(1)
}
config = source.map(
(source) =>
new Config(
source.maxEventLevelReports,
source.attributionScopes,
new Array<PerTriggerDataConfig>(source.triggerData.size).fill(
new PerTriggerDataConfig(
source.eventReportWindows.endTimes.length,
source.maxEventLevelReports
)
)
)
)
} else if (options.windows === undefined || options.buckets === undefined) {
throw new Error('windows and buckets must be specified if json_file is not')
} else {
if (options.windows.value.length !== options.buckets.value.length) {
throw new Error('windows and buckets must have same length')
}
if (
(options.attribution_scope_limit === undefined) !==
(options.max_event_states === undefined)
) {
throw new Error(
'attribution_scope_limit and max_event_states must be set / unset at the same time'
)
}
const attributionScopes: AttributionScopes | null =
options.attribution_scope_limit === undefined ||
options.max_event_states === undefined
? null
: {
limit: options.attribution_scope_limit,
values: new Set<string>(),
maxEventStates: options.max_event_states,
}
config = Maybe.some(
new Config(
options.max_event_level_reports,
attributionScopes,
options.windows.value.map(
(w: number, i: number) =>
new PerTriggerDataConfig(w, options.buckets!.value[i]!)
)
)
)
}
config.peek((config) => {
const infoGainMax =
vsv.Chromium.maxEventLevelChannelCapacityPerSource[options.source_type]
const out = config.computeConfigData(options.epsilon, infoGainMax)
console.log(`Number of possible different output states: ${out.numStates}`)
console.log(`Information gain: ${out.infoGain.toFixed(2)} bits`)
console.log(`Randomized trigger rate: ${out.flipProb.toFixed(7)}`)
if (out.excessive) {
const e = out.excessive
console.log(
`WARNING: info gain > ${infoGainMax.toFixed(2)} for ${
options.source_type
} sources. Would require a ${e.newFlipProb.toFixed(
7
)} randomized trigger rate (effective epsilon = ${e.newEps.toFixed(
3
)}) to resolve.`
)
}
})