Skip to content

Commit

Permalink
merging with unsigned big int PR
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Sep 17, 2024
2 parents 3f6f8d5 + cb51ba8 commit c63ade1
Show file tree
Hide file tree
Showing 41 changed files with 3,861 additions and 327 deletions.
1 change: 1 addition & 0 deletions core/jvm/src/main/scala/sigma/crypto/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ object Platform {
case _: Int => tpe == SInt
case _: Long => tpe == SLong
case _: BigInt => tpe == SBigInt
case _: UnsignedBigInt => tpe == SUnsignedBigInt
case _: String => tpe == SString // TODO v6.0: remove this case (see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/905)
case _: GroupElement => tpe.isGroupElement
case _: SigmaProp => tpe.isSigmaProp
Expand Down
2 changes: 2 additions & 0 deletions core/shared/src/main/scala/sigma/Evaluation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object Evaluation {
case SAny => AnyType
case SUnit => UnitType
case SBigInt => BigIntRType
case SUnsignedBigInt => UnsignedBigIntRType
case SBox => BoxRType
case SContext => ContextRType
case SGlobal => SigmaDslBuilderRType
Expand Down Expand Up @@ -67,6 +68,7 @@ object Evaluation {
case AnyType => SAny
case UnitType => SUnit
case BigIntRType => SBigInt
case UnsignedBigIntRType => SUnsignedBigInt
case GroupElementRType => SGroupElement
case AvlTreeRType => SAvlTree
case ot: OptionType[_] => SOption(rtypeToSType(ot.tA))
Expand Down
182 changes: 178 additions & 4 deletions core/shared/src/main/scala/sigma/SigmaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import java.math.BigInteger
import sigma.data._

/**
* All `modQ` operations assume that Q is a global constant (an order of the only one cryptographically strong group
* which is used for all cryptographic operations).
* So it is globally and implicitly used in all methods.
* */
* Base class for signed 256-bits integers
*/
trait BigInt {
/** Convert this BigInt value to Byte.
* @throws ArithmeticException if overflow happens.
Expand Down Expand Up @@ -155,8 +153,180 @@ trait BigInt {
*/
def or(that: BigInt): BigInt
def |(that: BigInt): BigInt = or(that)

/**
* @return a big integer whose value is `this xor that`
*/
def xor(that: BigInt): BigInt

/**
* @return a 256-bit signed integer whose value is (this << n). The shift distance, n, may be negative,
* in which case this method performs a right shift. (Computes floor(this * 2n).)
*/
def shiftLeft(n: Int): BigInt

/**
* @return a 256-bit signed integer whose value is (this >> n). Sign extension is performed. The shift distance, n,
* may be negative, in which case this method performs a left shift. (Computes floor(this / 2n).)
*/
def shiftRight(n: Int): BigInt

/**
* @return unsigned representation of this BigInt, or exception if its value is negative
*/
def toUnsigned: UnsignedBigInt

/**
* @return unsigned representation of this BigInt modulo `m`. Cryptographic mod operation is done, ie result is
* non-negative always
*/
def toUnsignedMod(m: UnsignedBigInt): UnsignedBigInt
}


trait UnsignedBigInt {
/** Convert this BigInt value to Byte.
* @throws ArithmeticException if overflow happens.
*/
def toByte: Byte

/** Convert this BigInt value to Short.
* @throws ArithmeticException if overflow happens.
*/
def toShort: Short

/** Convert this BigInt value to Int.
* @throws ArithmeticException if overflow happens.
*/
def toInt: Int

/** Convert this BigInt value to Int.
* @throws ArithmeticException if overflow happens.
*/
def toLong: Long

/** Returns a big-endian representation of this BigInt in a collection of bytes.
* For example, the value {@code 0x1213141516171819} would yield the
* byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}}.
* @since 2.0
*/
def toBytes: Coll[Byte]


/** Compares this numeric with that numeric for order. Returns a negative integer, zero, or a positive integer as the
* `this` is less than, equal to, or greater than `that`.
*/
def compareTo(that: UnsignedBigInt): Int

/** Returns a BigInt whose value is {@code (this + that)}.
*
* @param that value to be added to this BigInt.
* @return { @code this + that}
*/
def add(that: UnsignedBigInt): UnsignedBigInt
def +(that: UnsignedBigInt): UnsignedBigInt = add(that)

/** Returns a BigInt whose value is {@code (this - that)}.
*
* @param that value to be subtracted from this BigInt.
* @return { @code this - that}
*/
def subtract(that: UnsignedBigInt): UnsignedBigInt

def -(that: UnsignedBigInt): UnsignedBigInt = subtract(that)

/** Returns a BigInt whose value is {@code (this * that)}.
*
* @implNote An implementation may offer better algorithmic
* performance when { @code that == this}.
* @param that value to be multiplied by this BigInt.
* @return { @code this * that}
*/
def multiply(that: UnsignedBigInt): UnsignedBigInt
def *(that: UnsignedBigInt): UnsignedBigInt = multiply(that)

/** Returns a BigInt whose value is {@code (this / that)}.
*
* @param that value by which this BigInt is to be divided.
* @return { @code this / that}
* @throws ArithmeticException if { @code that} is zero.
*/
def divide(that: UnsignedBigInt): UnsignedBigInt
def /(that: UnsignedBigInt): UnsignedBigInt = divide(that)

/**
* Returns a BigInt whose value is {@code (this mod m}). This method
* differs from {@code remainder} in that it always returns a
* <i>non-negative</i> BigInteger.
*
* @param m the modulus.
* @return { @code this mod m}
* @throws ArithmeticException { @code m} &le; 0
* @see #remainder
*/
def mod(m: UnsignedBigInt): UnsignedBigInt
def %(m: UnsignedBigInt): UnsignedBigInt = mod(m)

/**
* Returns the minimum of this BigInteger and {@code val}.
*
* @param that value with which the minimum is to be computed.
* @return the BigInteger whose value is the lesser of this BigInteger and
* { @code val}. If they are equal, either may be returned.
*/
def min(that: UnsignedBigInt): UnsignedBigInt

/**
* Returns the maximum of this BigInteger and {@code val}.
*
* @param that value with which the maximum is to be computed.
* @return the BigInteger whose value is the greater of this and
* { @code val}. If they are equal, either may be returned.
*/
def max(that: UnsignedBigInt): UnsignedBigInt

/** Returns a BigInteger whose value is `(this & that)`.
* @param that value to be AND'ed with this BigInteger.
* @return `this & that`
*/
def and(that: UnsignedBigInt): UnsignedBigInt
def &(that: UnsignedBigInt): UnsignedBigInt = and(that)

/** Returns a BigInteger whose value is `(this | that)`.
*
* @param that value to be OR'ed with this BigInteger.
* @return `this | that`
*/
def or(that: UnsignedBigInt): UnsignedBigInt
def |(that: UnsignedBigInt): UnsignedBigInt = or(that)

def modInverse(m: UnsignedBigInt): UnsignedBigInt
def plusMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt
def subtractMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt
def multiplyMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt

/**
* @return a big integer whose value is `this xor that`
*/
def xor(that: UnsignedBigInt): UnsignedBigInt

/**
* @return a 256-bit signed integer whose value is (this << n). The shift distance, n, may be negative,
* in which case this method performs a right shift. (Computes floor(this * 2n).)
*/
def shiftLeft(n: Int): UnsignedBigInt

/**
* @return a 256-bit signed integer whose value is (this >> n). Sign extension is performed. The shift distance, n,
* may be negative, in which case this method performs a left shift. (Computes floor(this / 2n).)
*/
def shiftRight(n: Int): UnsignedBigInt

def toSigned(): BigInt
}



/** Base class for points on elliptic curves. */
trait GroupElement {
/** Checks if the provided element is an identity element. */
Expand All @@ -169,6 +339,8 @@ trait GroupElement {
*/
def exp(k: BigInt): GroupElement

def expUnsigned(k: UnsignedBigInt): GroupElement

/** Group operation. */
def multiply(that: GroupElement): GroupElement

Expand Down Expand Up @@ -753,6 +925,8 @@ trait SigmaDslBuilder {
/** Create DSL big integer from existing `java.math.BigInteger`*/
def BigInt(n: BigInteger): BigInt

def UnsignedBigInt(n: BigInteger): UnsignedBigInt

/** Extract `java.math.BigInteger` from DSL's `BigInt` type*/
def toBigInteger(n: BigInt): BigInteger

Expand Down
Loading

0 comments on commit c63ade1

Please sign in to comment.