Class SM9P256V1Field
SM9P256V1Curve.
Unlike SM2P256V1Field (whose sparse prime admits a fast Solinas
reduction and stores elements in the ordinary residue representation), the SM9
BN prime is a general 256-bit prime, so this field keeps elements in
Montgomery form (a·R mod q, R = 2^256), always fully reduced, below q, and
multiplies by Koc, Acar and Kaliski's CIOS method.
Addition, subtraction, negation, multiplication, squaring and inversion run the same
instructions whatever values they are given: no branch, early exit or array index depends on
them, a result is brought below q by a subtraction of q that is always made, its difference kept
or discarded through a mask, and the inverse is BouncyCastle's constant-time
Mod.modOddInverse(int[], int[], int[]). This is the arithmetic of the F_q the SM9 pairing computes in,
org.bouncycastle.math.ec.sm9.Fp, whose elements have the same form.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidadd(int[] x, int[] y, int[] z) static int[]static voidinv(int[] x, int[] z) z = x^-1.static booleanisZero(int[] x) Deprecated.Nothing in the library calls this method.static voidmultiply(int[] x, int[] y, int[] z) z = x y R^-1 mod q, the Montgomery product, which for x and y in Montgomery form is their product in that form: for each limb x_i of x, a running sum takes x_i y, then the multiple of q that makes its low limb 0, and is shifted down a limb.static voidnegate(int[] x, int[] z) static voidrandom(SecureRandom r, int[] z) A uniformly random element, drawn as its Montgomery-form limbs: the form is a bijection on [0, q), so a uniform representation is a uniform element.static voidrandomMult(SecureRandom r, int[] z) A uniformly random non-zero element, asrandom(SecureRandom, int[])draws them; if none of a thousand of them is other than 0, it throws IllegalStateException.static voidsquare(int[] x, int[] z) static voidsquareN(int[] x, int n, int[] z) Deprecated.Nothing in the library calls this method.static voidsubtract(int[] x, int[] y, int[] z) static BigIntegertoBigInteger(int[] xMont) static voidtwice(int[] x, int[] z) Deprecated.Nothing in the library calls this method.
-
Method Details
-
add
public static void add(int[] x, int[] y, int[] z) -
subtract
public static void subtract(int[] x, int[] y, int[] z) -
negate
public static void negate(int[] x, int[] z) -
twice
Deprecated.Nothing in the library calls this method. Useadd(int[], int[], int[])with x as both operands instead. -
multiply
public static void multiply(int[] x, int[] y, int[] z) z = x y R^-1 mod q, the Montgomery product, which for x and y in Montgomery form is their product in that form: for each limb x_i of x, a running sum takes x_i y, then the multiple of q that makes its low limb 0, and is shifted down a limb. The sum stays below 2q, so one subtraction of q leaves the result below q. z may be x or y. -
square
public static void square(int[] x, int[] z) -
squareN
Deprecated.Nothing in the library calls this method. Usesquare(int[], int[])n times instead. -
fromBigInteger
-
toBigInteger
-
inv
public static void inv(int[] x, int[] z) z = x^-1. For x in Montgomery form, x R,Mod.checkedModOddInverse(int[], int[], int[])gives the integer (x R)^-1 = x^-1 R^-1 mod q, and its Montgomery product with R^3 is x^-1 R. Zero has no inverse, and is refused with an ArithmeticException, as the other fields refuse it. -
isZero
Deprecated.Nothing in the library calls this method. UseNat256.isZero(int[])instead: an element is 0 exactly when its Montgomery form is. -
random
A uniformly random element, drawn as its Montgomery-form limbs: the form is a bijection on [0, q), so a uniform representation is a uniform element. The same rejection samplingSM2P256V1Field.random(SecureRandom, int[])uses; q is a little over 2^255.5, so under 1.5 draws on average, and if none of a thousand draws is below q it throws IllegalStateException. -
randomMult
A uniformly random non-zero element, asrandom(SecureRandom, int[])draws them; if none of a thousand of them is other than 0, it throws IllegalStateException.
-