Skip to content

Commit

Permalink
Fixed a few id variable to rawUid/firstLevelHash to make it clear. Fi…
Browse files Browse the repository at this point in the history
…xed the UserIdentity/FirstLevelHashIdentity class uses to HashedDiiIdentity instead in IdentityMapBenchmark and TokenEndecBenchmark
  • Loading branch information
sunnywu committed Oct 15, 2024
1 parent 7f4ebf5 commit 72f7be4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private RefreshTokenInput decodeRefreshTokenV3(Buffer b, byte[] bytes) {
final Instant establishedAt = Instant.ofEpochMilli(b2.getLong(49));
final IdentityScope identityScope = decodeIdentityScopeV3(b2.getByte(57));
final IdentityType identityType = decodeIdentityTypeV3(b2.getByte(57));
final byte[] id = b2.getBytes(58, 90);
final byte[] firstLevelHash = b2.getBytes(58, 90);

if (identityScope != decodeIdentityScopeV3(b.getByte(0))) {
throw new ClientInputValidationException("Failed to decode refreshTokenV3: Identity scope mismatch");
Expand All @@ -160,7 +160,7 @@ private RefreshTokenInput decodeRefreshTokenV3(Buffer b, byte[] bytes) {

return new RefreshTokenInput(
TokenVersion.V3, createdAt, expiresAt, operatorIdentity, sourcePublisher,
new FirstLevelHashIdentity(identityScope, identityType, id, privacyBits, establishedAt, null));
new FirstLevelHashIdentity(identityScope, identityType, firstLevelHash, privacyBits, establishedAt, null));
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/uid2/operator/service/TokenUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public static byte[] getRawUidV2FromIdentityHash(String identityString, String f

public static byte[] getRawUidV3(IdentityScope scope, IdentityType type, byte[] firstLevelHash, String rotatingSalt) {
final byte[] sha = EncodingUtils.getSha256Bytes(EncodingUtils.toBase64String(firstLevelHash), rotatingSalt);
final byte[] id = new byte[33];
id[0] = (byte)(encodeIdentityScope(scope) | encodeIdentityType(type));
System.arraycopy(sha, 0, id, 1, 32);
return id;
final byte[] rawUid = new byte[33];
rawUid[0] = (byte)(encodeIdentityScope(scope) | encodeIdentityType(type));
System.arraycopy(sha, 0, rawUid, 1, 32);
return rawUid;
}

public static byte[] getRawUidV3FromIdentity(IdentityScope scope, IdentityType type, String identityString, String firstLevelSalt, String rotatingSalt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ static ICloudStorage make1mOptOutEntryStorage(String salt, List<String> out_gene
return storage;
}

static HashedDiiIdentity[] createUserIdentities() {
static HashedDiiIdentity[] createHashedDiiIdentities() {
HashedDiiIdentity[] arr = new HashedDiiIdentity[65536];
for (int i = 0; i < 65536; i++) {
final byte[] id = new byte[33];
new Random().nextBytes(id);
arr[i] = new HashedDiiIdentity(IdentityScope.UID2, IdentityType.Email, id, 0,
final byte[] diiHash = new byte[33];
new Random().nextBytes(diiHash);
arr[i] = new HashedDiiIdentity(IdentityScope.UID2, IdentityType.Email, diiHash, 0,
Instant.now().minusSeconds(120), Instant.now().minusSeconds(60));
}
return arr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

public class IdentityMapBenchmark {
private static final IUIDOperatorService uidService;
private static final HashedDiiIdentity[] firstLevelHashIdentities;
private static final HashedDiiIdentity[] hashedDiiIdentities;
private static int idx = 0;

static {
try {
uidService = BenchmarkCommon.createUidOperatorService();
firstLevelHashIdentities = BenchmarkCommon.createUserIdentities();
hashedDiiIdentities = BenchmarkCommon.createHashedDiiIdentities();
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -26,12 +26,12 @@ public class IdentityMapBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
public RawUidResponse IdentityMapRawThroughput() {
return uidService.map(firstLevelHashIdentities[(idx++) & 65535], Instant.now());
return uidService.map(hashedDiiIdentities[(idx++) & 65535], Instant.now());
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
public RawUidResponse IdentityMapWithOptOutThroughput() {
return uidService.mapIdentity(new MapRequest(firstLevelHashIdentities[(idx++) & 65535], OptoutCheckPolicy.RespectOptOut, Instant.now()));
return uidService.mapIdentity(new MapRequest(hashedDiiIdentities[(idx++) & 65535], OptoutCheckPolicy.RespectOptOut, Instant.now()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class TokenEndecBenchmark {

private static final IUIDOperatorService uidService;
private static final HashedDiiIdentity[] userIdentities;
private static final HashedDiiIdentity[] hashedDiiIdentities;
private static final SourcePublisher publisher;
private static final EncryptedTokenEncoder encoder;
private static final IdentityResponse[] generatedTokens;
Expand All @@ -23,11 +23,11 @@ public class TokenEndecBenchmark {
static {
try {
uidService = BenchmarkCommon.createUidOperatorService();
userIdentities = BenchmarkCommon.createUserIdentities();
hashedDiiIdentities = BenchmarkCommon.createHashedDiiIdentities();
publisher = BenchmarkCommon.createSourcePublisher();
encoder = BenchmarkCommon.createTokenEncoder();
generatedTokens = createAdvertisingTokens();
if (generatedTokens.length < 65536 || userIdentities.length < 65536) {
if (generatedTokens.length < 65536 || hashedDiiIdentities.length < 65536) {
throw new IllegalStateException("must create more than 65535 test candidates.");
}
} catch (Exception e) {
Expand All @@ -37,11 +37,11 @@ public class TokenEndecBenchmark {

static IdentityResponse[] createAdvertisingTokens() {
List<IdentityResponse> tokens = new ArrayList<>();
for (int i = 0; i < userIdentities.length; i++) {
for (int i = 0; i < hashedDiiIdentities.length; i++) {
tokens.add(
uidService.generateIdentity(new IdentityRequest(
publisher,
userIdentities[i],
hashedDiiIdentities[i],
OptoutCheckPolicy.DoNotRespect)));
}
return tokens.toArray(new IdentityResponse[tokens.size()]);
Expand All @@ -52,7 +52,7 @@ static IdentityResponse[] createAdvertisingTokens() {
public IdentityResponse TokenGenerationBenchmark() {
return uidService.generateIdentity(new IdentityRequest(
publisher,
userIdentities[(idx++) & 65535],
hashedDiiIdentities[(idx++) & 65535],
OptoutCheckPolicy.DoNotRespect));
}

Expand Down

0 comments on commit 72f7be4

Please sign in to comment.