public class H2cUtils
extends java.lang.Object
This implementation follows the straight-line, branch-free algorithmic structure required by RFC 9380, ensuring that all code paths perform the same sequence of mathematical operations regardless of input values. However, it relies on Java’s BigInteger arithmetic and standard JVM execution characteristics, neither of which provides strict guarantees of constant-time behavior at the microarchitectural level. Operations such as modular exponentiation, multiplication, inversion, and even conditional value selection (cmov) may execute in variable time depending on internal optimizations, operand size, and JIT behavior.
For most applications, this is sufficient to avoid the major side-channel pitfalls associated with probabilistic or data-dependent loops (e.g., try-and-increment). But if your threat model requires strong, formally constant-time guarantees, such as protection against local timing attacks or hostile co-tenant environments, you should consider using a lower-level language with fixed-limb field arithmetic and verifiable constant-time primitives. Java cannot practically provide such guarantees with BigInteger-based implementations.
| Constructor and Description |
|---|
H2cUtils() |
| Modifier and Type | Method and Description |
|---|---|
static java.math.BigInteger |
cmov(java.math.BigInteger a,
java.math.BigInteger b,
boolean condition)
Constant time implementation of selection of value based on condition
|
static byte[] |
i2osp(int val,
int len)
Convert an integer value to a byte array of a specified length.
|
static java.math.BigInteger |
inv0(java.math.BigInteger val,
java.math.BigInteger order)
Calculates the modular inverse of a BigInteger 'val' with respect to a given BigInteger 'order'.
|
static boolean |
isSquare(java.math.BigInteger val,
java.math.BigInteger order)
Test if a value is square in a prime field order
|
static java.math.BigInteger |
os2ip(byte[] val)
Converts a byte array to a BigInteger.
|
static int |
sgn0(java.math.BigInteger val,
ECCurve curve)
Returns the sign of the BigInteger 'val' using the given ECCurve 'curve'.
|
static java.math.BigInteger |
sqrt(java.math.BigInteger val,
java.math.BigInteger order)
Calculate the square root of val in a prime field order
|
static byte[] |
xor(byte[] arg1,
byte[] arg2)
Performs bitwise XOR operation on two byte arrays.
|
public static java.math.BigInteger cmov(java.math.BigInteger a,
java.math.BigInteger b,
boolean condition)
a - value selected on condition = falseb - value selected on condition = truecondition - conditionpublic static boolean isSquare(java.math.BigInteger val,
java.math.BigInteger order)
val - value to testorder - prime field orderpublic static java.math.BigInteger sqrt(java.math.BigInteger val,
java.math.BigInteger order)
val - valueorder - prime field orderpublic static int sgn0(java.math.BigInteger val,
ECCurve curve)
val - the BigInteger valuecurve - the EC curve specifying the curve fieldjava.lang.IllegalArgumentException - if curve.getField().getDimension() != 1public static java.math.BigInteger inv0(java.math.BigInteger val,
java.math.BigInteger order)
val - the BigInteger value to calculate the inverse fororder - the BigInteger representing the orderpublic static byte[] i2osp(int val,
int len)
val - the integer value to be convertedlen - the length of the resulting byte arrayjava.lang.IllegalArgumentException - if the value requires more bytes than the assigned length sizepublic static java.math.BigInteger os2ip(byte[] val)
val - the byte array to convertpublic static byte[] xor(byte[] arg1,
byte[] arg2)
arg1 - the first byte arrayarg2 - the second byte arrayjava.lang.NullPointerException - if either arg1 or arg2 is nulljava.lang.IllegalArgumentException - if arg1 and arg2 have different lengths