public class Zn extends java.lang.Object implements Ring
| Modifier and Type | Class and Description |
|---|---|
class |
Zn.ZnElement
An equivalence class of integers with the same remainder when divided by n.
|
| Modifier and Type | Field and Description |
|---|---|
protected int |
maxByteLength
Maximum value (over all elements elem) of
elem.getInteger().toByteArray().length;. |
protected java.math.BigInteger |
n
The modulus.
|
protected java.lang.Boolean |
nIsPrime
Whether the modulus
n is prime. |
protected Zn.ZnElement |
ONE
The neutral element of this ring's unit group (the one element).
|
protected Zn.ZnElement |
ZERO
The neutral element of this ring's additive group (the zero element).
|
| Constructor and Description |
|---|
Zn(java.math.BigInteger n)
Constructs the ring.
|
Zn(Representation repr)
Constructs the ring from a
Representation. |
| Modifier and Type | Method and Description |
|---|---|
Zn.ZnElement |
createZnElement(java.math.BigInteger v)
Creates an element from a
BigInteger (formally, the projection of \(v\) from \(\mathbb{Z}\)
to \(\mathbb{Z}_n\)). |
protected Zn.ZnElement |
createZnElementUnsafe(java.math.BigInteger vBetween0andN)
Instantiates a
ZnElement without checking that the given representative is within the proper range. |
boolean |
equals(java.lang.Object obj) |
double |
estimateCostInvPerOp()
Estimates the number of inversions that can be done per group operation for the same cost.
|
double |
estimateCostNegPerOp()
Estimates the number of negations (additive inversions) that can be done per group operation for the same cost.
|
java.math.BigInteger |
getCharacteristic()
Returns the characteristic of the ring.
|
Zn.ZnElement |
getElement(java.math.BigInteger i)
Maps the integer i into this ring \(R\), such that this map is a
ring homomorphism \(\mathbb{Z}_{\text{getCharacteristic()}} \rightarrow R\).
|
Zn.ZnElement |
getOneElement()
Returns the multiplicative neutral element of this ring.
|
Representation |
getRepresentation()
The representation of this object.
|
Zn.ZnElement |
getUniformlyRandomElement()
Returns an element of this structure that is drawn uniformly at random
using a cryptographically strong RNG.
|
Zn.ZnElement |
getUniformlyRandomNonzeroElement()
Generates a nonzero element from this ring uniformly at random using a cryptographically strong RNG.
|
Zn.ZnElement |
getUniformlyRandomUnit()
Generates an invertible element from this ring uniformly at random using a cryptographically strong RNG.
|
java.util.Optional<java.lang.Integer> |
getUniqueByteLength()
Returns the number of bytes returned by this structure's
UniqueByteRepresentable.getUniqueByteRepresentation(),
or an empty Optional if this structure's elements do not guarantee a fixed length. |
Zn.ZnElement |
getZeroElement()
Returns the additive neutral element of this ring.
|
int |
hashCode() |
boolean |
hasPrimeSize()
Checks if the structure has prime size.
|
Zn.ZnElement |
injectiveValueOf(byte[] bytes)
For all
k < floor((n.bitLength()-1)/8), this is an injective map byte^k -> Zn. |
boolean |
isCommutative()
Returns true if this ring is known to be commutative.
|
Zn.ZnElement |
restoreElement(Representation repr)
Creates an element of this structure from its representation.
|
java.math.BigInteger |
size()
Returns the number of elements in this structure (the size).
|
java.math.BigInteger |
sizeUnitGroup()
Returns the number of units in this ring.
|
java.lang.String |
toString() |
int |
upperBoundForUniqueRepresentation()
Returns the length of the representation of the largest element in this ring in terms of number of bytes.
|
Zn.ZnElement |
valueOf(java.math.BigInteger representative)
Creates the corresponding element mod n.
|
static Zn.ZnElement |
valueOf(java.math.BigInteger representative,
java.math.BigInteger modulus)
Create the Zn element "representative mod modulus" (convenience method)
|
Zn.ZnElement |
valueOf(byte[] bytes)
Interprets given bytes as an integer and projects that number into Zn.
|
Zn.ZnElement |
valueOf(long representative)
Creates the corresponding element mod n.
|
static Zn.ZnElement |
valueOf(long representative,
java.math.BigInteger modulus)
Create the Zn element "representative mod modulus"
|
static Zn.ZnElement |
valueOf(long representative,
long modulus)
Create the element "representative mod modulus"
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitasAdditiveGroup, asUnitGroup, extendedEuclideanAlgorithm, extendedEuclideanAlgorithm, getElement, getUniformlyRandomElements, getUniformlyRandomNonzeroElements, getUniformlyRandomUnits, restoreFromRepresentation, restoreVectorprotected final Zn.ZnElement ONE
protected final Zn.ZnElement ZERO
protected final java.math.BigInteger n
protected java.lang.Boolean nIsPrime
n is prime.protected final int maxByteLength
elem.getInteger().toByteArray().length;.
The value is exactly the number of bytes needed to represent n.public Zn(java.math.BigInteger n)
n - number of elements in the ringpublic Zn(Representation repr)
Representation.public java.math.BigInteger size()
Structurepublic boolean hasPrimeSize()
throws java.lang.UnsupportedOperationException
StructurehasPrimeSize in interface Structurejava.lang.UnsupportedOperationException - if the primality of the size cannot be determinedpublic java.math.BigInteger sizeUnitGroup()
RingsizeUnitGroup in interface Ringpublic Zn.ZnElement getZeroElement()
RinggetZeroElement in interface Ringpublic Zn.ZnElement getOneElement()
RinggetOneElement in interface Ringpublic Zn.ZnElement getUniformlyRandomElement() throws java.lang.UnsupportedOperationException
StructuregetUniformlyRandomElement in interface RinggetUniformlyRandomElement in interface Structurejava.lang.UnsupportedOperationException - if the operation is not supportedpublic Zn.ZnElement getUniformlyRandomUnit() throws java.lang.UnsupportedOperationException
RingThe default implementation generates random ring elements until it hits a unit. Implementors should override if this is not feasible or if there is a better way!
getUniformlyRandomUnit in interface Ringjava.lang.UnsupportedOperationException - if the ring does not support this methodpublic Zn.ZnElement getUniformlyRandomNonzeroElement()
RinggetUniformlyRandomNonzeroElement in interface Ringpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic final int upperBoundForUniqueRepresentation()
public Representation getRepresentation()
RepresentableReprUtilgetRepresentation in interface RepresentableRepresentationpublic Zn.ZnElement restoreElement(Representation repr)
StructurerestoreElement in interface RingrestoreElement in interface Structurerepr - the Representation returned by Representable.getRepresentation()public static Zn.ZnElement valueOf(java.math.BigInteger representative, java.math.BigInteger modulus)
representative - the integer representative of the elementmodulus - the ring sizepublic static Zn.ZnElement valueOf(long representative, java.math.BigInteger modulus)
representative - the integer representative of the elementmodulus - the ring sizepublic static Zn.ZnElement valueOf(long representative, long modulus)
representative - the integer representative of the elementmodulus - the ring sizepublic Zn.ZnElement valueOf(long representative)
representative - the integer representative of the elementpublic Zn.ZnElement valueOf(java.math.BigInteger representative)
representative - the integer representative of the elementpublic Zn.ZnElement injectiveValueOf(byte[] bytes) throws java.lang.IllegalArgumentException
k < floor((n.bitLength()-1)/8), this is an injective map byte^k -> Zn.
Note that there may be collisions between injectiveValueOf(bytes1) and injectiveValueOf(bytes2)
if bytes1.length != bytes2.length.
bytes - the bytes to map injectively into Zn.java.lang.IllegalArgumentException - if the byte array is too longpublic Zn.ZnElement valueOf(byte[] bytes)
public java.math.BigInteger getCharacteristic()
Ring
The characteristic of a ring is defined to be the number n
such that there is a ring homomorphism from Zn to the ring.
getCharacteristic in interface Ringpublic Zn.ZnElement createZnElement(java.math.BigInteger v)
BigInteger (formally, the projection of \(v\) from \(\mathbb{Z}\)
to \(\mathbb{Z}_n\)).
Implementation detail: This factory method allows the subclass Zp to use its own kind of elements
while reusing the Zn implementation.
protected Zn.ZnElement createZnElementUnsafe(java.math.BigInteger vBetween0andN)
ZnElement without checking that the given representative is within the proper range.vBetween0andN - the representative of the element to instantiate.
Must be between 0 (inclusive) and n (exclusive).public java.lang.String toString()
toString in class java.lang.Objectpublic java.util.Optional<java.lang.Integer> getUniqueByteLength()
StructureUniqueByteRepresentable.getUniqueByteRepresentation(),
or an empty Optional if this structure's elements do not guarantee a fixed length.
For example, elements of Zp will always be represented by ceil(ceil(log(p))/8) bytes,
hence getUniqueByteLength() would return ceil(ceil(log(p))/8).
A polynomial ring would return an empty Optional since a polynomial's unique byte representation length
depends on its degree.
getUniqueByteLength in interface StructuregetUniqueByteRepresentation(),
or an empty Optional, if no guaranteepublic Zn.ZnElement getElement(java.math.BigInteger i)
RinggetElement in interface Ringi - the integer to mappublic double estimateCostInvPerOp()
Ring2 would mean that an inversion costs half as much as a group operation, on average.estimateCostInvPerOp in interface Ringpublic double estimateCostNegPerOp()
Ring2 would mean that a negation costs half as much as a group operation, on average.estimateCostNegPerOp in interface Ringpublic boolean isCommutative()
RingisCommutative in interface Ring