ConstantTimeUtils Class — netty Architecture
Architecture documentation for the ConstantTimeUtils class in ConstantTimeUtils.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD 5238cea0_9d09_4635_e266_4c7ad434e578["ConstantTimeUtils"] 2368f230_0f66_20b8_7761_53c1a4bfb940["ConstantTimeUtils.java"] 5238cea0_9d09_4635_e266_4c7ad434e578 -->|defined in| 2368f230_0f66_20b8_7761_53c1a4bfb940 19495969_2f23_e9e2_cd0d_42c6be903360["ConstantTimeUtils()"] 5238cea0_9d09_4635_e266_4c7ad434e578 -->|method| 19495969_2f23_e9e2_cd0d_42c6be903360 c088cd8e_1849_f25c_77b3_2ec81363bd76["equalsConstantTime()"] 5238cea0_9d09_4635_e266_4c7ad434e578 -->|method| c088cd8e_1849_f25c_77b3_2ec81363bd76
Relationship Graph
Source Code
common/src/main/java/io/netty/util/internal/ConstantTimeUtils.java lines 18–131
public final class ConstantTimeUtils {
private ConstantTimeUtils() { }
/**
* Compare two {@code int}s without leaking timing information.
* <p>
* The {@code int} return type is intentional and is designed to allow cascading of constant time operations:
* <pre>
* int l1 = 1;
* int l2 = 1;
* int l3 = 1;
* int l4 = 500;
* boolean equals = (equalsConstantTime(l1, l2) & equalsConstantTime(l3, l4)) != 0;
* </pre>
* @param x the first value.
* @param y the second value.
* @return {@code 0} if not equal. {@code 1} if equal.
*/
public static int equalsConstantTime(int x, int y) {
int z = ~(x ^ y);
z &= z >> 16;
z &= z >> 8;
z &= z >> 4;
z &= z >> 2;
z &= z >> 1;
return z & 1;
}
/**
* Compare two {@code longs}s without leaking timing information.
* <p>
* The {@code int} return type is intentional and is designed to allow cascading of constant time operations:
* <pre>
* long l1 = 1;
* long l2 = 1;
* long l3 = 1;
* long l4 = 500;
* boolean equals = (equalsConstantTime(l1, l2) & equalsConstantTime(l3, l4)) != 0;
* </pre>
* @param x the first value.
* @param y the second value.
* @return {@code 0} if not equal. {@code 1} if equal.
*/
public static int equalsConstantTime(long x, long y) {
long z = ~(x ^ y);
z &= z >> 32;
z &= z >> 16;
z &= z >> 8;
z &= z >> 4;
z &= z >> 2;
z &= z >> 1;
return (int) (z & 1);
}
/**
* Compare two {@code byte} arrays for equality without leaking timing information.
* For performance reasons no bounds checking on the parameters is performed.
* <p>
* The {@code int} return type is intentional and is designed to allow cascading of constant time operations:
* <pre>
* byte[] s1 = new {1, 2, 3};
* byte[] s2 = new {1, 2, 3};
* byte[] s3 = new {1, 2, 3};
* byte[] s4 = new {4, 5, 6};
* boolean equals = (equalsConstantTime(s1, 0, s2, 0, s1.length) &
* equalsConstantTime(s3, 0, s4, 0, s3.length)) != 0;
* </pre>
* @param bytes1 the first byte array.
* @param startPos1 the position (inclusive) to start comparing in {@code bytes1}.
* @param bytes2 the second byte array.
* @param startPos2 the position (inclusive) to start comparing in {@code bytes2}.
* @param length the amount of bytes to compare. This is assumed to be validated as not going out of bounds
* by the caller.
* @return {@code 0} if not equal. {@code 1} if equal.
*/
public static int equalsConstantTime(byte[] bytes1, int startPos1,
byte[] bytes2, int startPos2, int length) {
// Benchmarking demonstrates that using an int to accumulate is faster than other data types.
int b = 0;
final int end = startPos1 + length;
for (; startPos1 < end; ++startPos1, ++startPos2) {
Source
Frequently Asked Questions
What is the ConstantTimeUtils class?
ConstantTimeUtils is a class in the netty codebase, defined in common/src/main/java/io/netty/util/internal/ConstantTimeUtils.java.
Where is ConstantTimeUtils defined?
ConstantTimeUtils is defined in common/src/main/java/io/netty/util/internal/ConstantTimeUtils.java at line 18.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free