Package com.horizen.librustsidechains
Class FieldElement
- java.lang.Object
-
- com.horizen.librustsidechains.FieldElement
-
- All Implemented Interfaces:
AutoCloseable
public class FieldElement extends Object implements AutoCloseable
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedFieldElement(long fieldElementPointer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()static FieldElementcreateFromLong(long value)Convert an integer value to a field elementstatic FieldElementcreateRandom()static FieldElementcreateRandom(long seed)static FieldElementcreateRandom(Random r)static FieldElementdeserialize(byte[] fieldElementBytes)Deserialize a FieldElement from "fieldElementBytes"booleanequals(Object o)voidfreeFieldElement()static FieldElementjoinAt(FieldElement fe1, int index1, FieldElement fe2, int index2)The inverse of the splitAt() method: join fe1.bytes[0..index1) and fe2.bytes[0..index2) in a single byte array and deserialize a FieldElement out of it.static FieldElementjoinAt(FieldElement fe1, int index1, FieldElement fe2, int index2, boolean checkZeroAfterIdx)The inverse of the splitAt() method: join fe1.bytes[0..index1) and fe2.bytes[0..index2) in a single byte array and deserialize a FieldElement out of it.protected voidnativePrintFieldElementBytes()protected byte[]nativeSerializeFieldElement()voidprintFieldElementBytes()byte[]serializeFieldElement()List<FieldElement>splitAt(int index)Split this FieldElement into two FieldElements.
-
-
-
Method Detail
-
createFromLong
public static FieldElement createFromLong(long value)
Convert an integer value to a field element- Parameters:
value- - A positive integer value to be converted to a field element- Returns:
- The field element represented with the positive integer value provided as input
-
createRandom
public static FieldElement createRandom(long seed)
-
createRandom
public static FieldElement createRandom()
-
createRandom
public static FieldElement createRandom(Random r)
-
nativeSerializeFieldElement
protected byte[] nativeSerializeFieldElement()
-
serializeFieldElement
public byte[] serializeFieldElement()
-
deserialize
public static FieldElement deserialize(byte[] fieldElementBytes) throws IllegalArgumentException
Deserialize a FieldElement from "fieldElementBytes"- Parameters:
fieldElementBytes- bytes of the FieldElement to be deserialized- Returns:
- The deserialized FieldElement
- Throws:
IllegalArgumentException- If fieldElementBytes.len() > FIELD_ELEMENT_LENGTH or if the bytes represent an invalid FieldElement
-
splitAt
public List<FieldElement> splitAt(int index) throws IndexOutOfBoundsException
Split this FieldElement into two FieldElements. Split will happen at the specified index: one FieldElement will be read from the original bytes [0..index) and the other ones from the original bytes [index..FIELD_ELEMENT_LENGTH)- Parameters:
index- - a valid integer in the range (0, FIELD_ELEMENT_LENGTH)- Returns:
- a List made up of two FieldElements, read from the original one split at index
- Throws:
IndexOutOfBoundsException- in case of illegal index (i.e. index <= 0 OR index >= FIELD_ELEMENT_LENGTH)
-
joinAt
public static FieldElement joinAt(FieldElement fe1, int index1, FieldElement fe2, int index2, boolean checkZeroAfterIdx) throws IllegalArgumentException
The inverse of the splitAt() method: join fe1.bytes[0..index1) and fe2.bytes[0..index2) in a single byte array and deserialize a FieldElement out of it.- Parameters:
fe1- - the first FieldElement to joinindex1- - the number of bytes to consider from the first FieldElementfe2- - the second FieldElement to joinindex2- - the number of bytes to consider from the second FieldElementcheckZeroAfterIdx- - if enabled, check that fe1.bytes[index1..] and fe2.bytes[index2..] are all 0s.- Returns:
- the FieldElement obtained from combining fe1.bytes[0..index1) with fe2.bytes[0..index2)
- Throws:
IllegalArgumentException- if the combine operation would produce a byte array bigger than FIELD_ELEMENT_LENGTH
-
joinAt
public static FieldElement joinAt(FieldElement fe1, int index1, FieldElement fe2, int index2) throws IllegalArgumentException
The inverse of the splitAt() method: join fe1.bytes[0..index1) and fe2.bytes[0..index2) in a single byte array and deserialize a FieldElement out of it.- Parameters:
fe1- - the first FieldElement to joinindex1- - the number of bytes to consider from the first FieldElementfe2- - the second FieldElement to joinindex2- - the number of bytes to consider from the second FieldElement- Returns:
- the FieldElement obtained from combining fe1.bytes[0..index1) with fe2.bytes[0..index2)
- Throws:
IllegalArgumentException- if the combine operation would produce a byte array bigger than FIELD_ELEMENT_LENGTH
-
nativePrintFieldElementBytes
protected void nativePrintFieldElementBytes()
-
printFieldElementBytes
public void printFieldElementBytes()
-
freeFieldElement
public void freeFieldElement()
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
-