Skip to content

[6.0] Cleanup for 6.0 branch #1054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/sigma/ast/SType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ trait SNumericType extends SProduct with STypeCompanion {

object SNumericType extends STypeCompanion {

// TODO v6.0: this typeId is now shadowed by SGlobal.typeId
// this typeId is now shadowed by SGlobal.typeId
// see https://github.com/ScorexFoundation/sigmastate-interpreter/issues/667
override def typeId: TypeCode = 106: Byte

Expand Down Expand Up @@ -541,7 +541,7 @@ case object SUnsignedBigInt extends SPrimType with SEmbeddable with SNumericType
val RelationOpType = SFunc(Array(SUnsignedBigInt, SUnsignedBigInt), SBoolean)

/** The maximum size of BigInteger value in byte array representation. */
val MaxSizeInBytes: Long = SigmaConstants.MaxBigIntSizeInBytes.value // todo: 256 bits or more?
val MaxSizeInBytes: Long = SigmaConstants.MaxBigIntSizeInBytes.value

override def numericTypeIndex: Int = 5

Expand Down
96 changes: 2 additions & 94 deletions core/shared/src/main/scala/sigma/data/CBigInt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ case class CBigInt(override val wrappedValue: BigInteger) extends BigInt with Wr

override def or(that: BigInt): BigInt = CBigInt(wrappedValue.or(that.asInstanceOf[CBigInt].wrappedValue))

override def xor(that: BigInt): BigInt = CBigInt(wrappedValue.xor(that.asInstanceOf[CBigInt].wrappedValue))
override def xor(that: BigInt): BigInt = CBigInt(wrappedValue.xor(that.asInstanceOf[CBigInt].wrappedValue).toSignedBigIntValueExact)

override def shiftLeft(n: Int): BigInt = CBigInt(wrappedValue.shiftLeft(n).toSignedBigIntValueExact)

override def shiftRight(n: Int): BigInt = CBigInt(wrappedValue.shiftRight(n).toSignedBigIntValueExact)

override def toUnsigned: UnsignedBigInt = {
if(this.wrappedValue.compareTo(BigInteger.ZERO) < 0){
if (this.wrappedValue.compareTo(BigInteger.ZERO) < 0) {
throw new ArithmeticException("BigInteger argument for .toUnsigned is negative");
} else {
CUnsignedBigInt(this.wrappedValue)
Expand All @@ -70,95 +70,3 @@ case class CBigInt(override val wrappedValue: BigInteger) extends BigInt with Wr
}

}

/** A default implementation of [[UnsignedBigInt]] interface.
*
* @see [[UnsignedBigInt]] for detailed descriptions
*/
case class CUnsignedBigInt(override val wrappedValue: BigInteger) extends UnsignedBigInt with WrapperOf[BigInteger] {

if (wrappedValue.compareTo(BigInteger.ZERO) < 0) {
throw new IllegalArgumentException(s"Attempt to create unsigned value from negative big integer $wrappedValue")
}

if (wrappedValue.bitLength() > 256) {
throw new IllegalArgumentException(s"Too big unsigned big int value $wrappedValue")
}

override def toByte: Byte = wrappedValue.toByteExact

override def toShort: Short = wrappedValue.toShortExact

override def toInt: Int = wrappedValue.toIntExact

override def toLong: Long = wrappedValue.toLongExact

override def toBytes: Coll[Byte] = Colls.fromArray(BigIntegers.asUnsignedByteArray(wrappedValue))

override def compareTo(that: UnsignedBigInt): Int =
wrappedValue.compareTo(that.asInstanceOf[CUnsignedBigInt].wrappedValue)

override def add(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.add(that.asInstanceOf[CUnsignedBigInt].wrappedValue).toUnsignedBigIntValueExact)

override def subtract(that: UnsignedBigInt): UnsignedBigInt = {
CUnsignedBigInt(wrappedValue.subtract(that.asInstanceOf[CUnsignedBigInt].wrappedValue).toUnsignedBigIntValueExact)
}

override def multiply(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.multiply(that.asInstanceOf[CUnsignedBigInt].wrappedValue).toUnsignedBigIntValueExact)

override def divide(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.divide(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def mod(m: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.mod(m.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def min(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.min(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def max(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.max(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def and(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.and(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def or(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.or(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def modInverse(m: UnsignedBigInt): UnsignedBigInt = {
CUnsignedBigInt(wrappedValue.modInverse(m.asInstanceOf[CUnsignedBigInt].wrappedValue))
}

override def plusMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt = {
val thatBi = that.asInstanceOf[CUnsignedBigInt].wrappedValue
val mBi = m.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(wrappedValue.add(thatBi).mod(mBi))
}

override def subtractMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt = {
val thatBi = that.asInstanceOf[CUnsignedBigInt].wrappedValue
val mBi = m.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(wrappedValue.subtract(thatBi).mod(mBi))
}

override def multiplyMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt = {
val thatBi = that.asInstanceOf[CUnsignedBigInt].wrappedValue
val mBi = m.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(wrappedValue.multiply(thatBi).mod(mBi))
}

/**
* @return a big integer whose value is `this xor that`
*/
def xor(that: UnsignedBigInt): UnsignedBigInt = {
CUnsignedBigInt(wrappedValue.xor(that.asInstanceOf[CUnsignedBigInt].wrappedValue))
}

override def shiftLeft(n: Int): UnsignedBigInt = CUnsignedBigInt(wrappedValue.shiftLeft(n).toUnsignedBigIntValueExact)

override def shiftRight(n: Int): UnsignedBigInt = CUnsignedBigInt(wrappedValue.shiftRight(n).toUnsignedBigIntValueExact)

override def bitwiseInverse(): UnsignedBigInt = {
val bytes = BigIntegers.asUnsignedByteArray(32, wrappedValue)
val res: Array[Byte] = bytes.map(b => (~b & 0xff).toByte)
CUnsignedBigInt(BigIntegers.fromUnsignedByteArray(res))
}

override def toSigned(): BigInt = {
CBigInt(wrappedValue.toSignedBigIntValueExact)
}

}
101 changes: 101 additions & 0 deletions core/shared/src/main/scala/sigma/data/CUnsignedBigInt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package sigma.data

import sigma.{BigInt, Coll, Colls, UnsignedBigInt}
import sigma.crypto.BigIntegers
import sigma.util.Extensions.BigIntegerOps

import java.math.BigInteger


/** A default implementation of [[UnsignedBigInt]] interface.
*
* @see [[UnsignedBigInt]] for detailed descriptions
*/
case class CUnsignedBigInt(override val wrappedValue: BigInteger) extends UnsignedBigInt with WrapperOf[BigInteger] {

if (wrappedValue.compareTo(BigInteger.ZERO) < 0) {
throw new IllegalArgumentException(s"Attempt to create unsigned value from negative big integer $wrappedValue")
}

if (wrappedValue.bitLength() > 256) {
throw new IllegalArgumentException(s"Too big unsigned big int value $wrappedValue")
}

override def toByte: Byte = wrappedValue.toByteExact

override def toShort: Short = wrappedValue.toShortExact

override def toInt: Int = wrappedValue.toIntExact

override def toLong: Long = wrappedValue.toLongExact

override def toBytes: Coll[Byte] = Colls.fromArray(BigIntegers.asUnsignedByteArray(wrappedValue))

override def compareTo(that: UnsignedBigInt): Int =
wrappedValue.compareTo(that.asInstanceOf[CUnsignedBigInt].wrappedValue)

override def add(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.add(that.asInstanceOf[CUnsignedBigInt].wrappedValue).toUnsignedBigIntValueExact)

override def subtract(that: UnsignedBigInt): UnsignedBigInt = {
CUnsignedBigInt(wrappedValue.subtract(that.asInstanceOf[CUnsignedBigInt].wrappedValue).toUnsignedBigIntValueExact)
}

override def multiply(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.multiply(that.asInstanceOf[CUnsignedBigInt].wrappedValue).toUnsignedBigIntValueExact)

override def divide(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.divide(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def mod(m: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.mod(m.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def min(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.min(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def max(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.max(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def and(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.and(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def or(that: UnsignedBigInt): UnsignedBigInt = CUnsignedBigInt(wrappedValue.or(that.asInstanceOf[CUnsignedBigInt].wrappedValue))

override def modInverse(m: UnsignedBigInt): UnsignedBigInt = {
CUnsignedBigInt(wrappedValue.modInverse(m.asInstanceOf[CUnsignedBigInt].wrappedValue))
}

override def plusMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt = {
val thatBi = that.asInstanceOf[CUnsignedBigInt].wrappedValue
val mBi = m.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(wrappedValue.add(thatBi).mod(mBi))
}

override def subtractMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt = {
val thatBi = that.asInstanceOf[CUnsignedBigInt].wrappedValue
val mBi = m.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(wrappedValue.subtract(thatBi).mod(mBi))
}

override def multiplyMod(that: UnsignedBigInt, m: UnsignedBigInt): UnsignedBigInt = {
val thatBi = that.asInstanceOf[CUnsignedBigInt].wrappedValue
val mBi = m.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(wrappedValue.multiply(thatBi).mod(mBi))
}

/**
* @return a big integer whose value is `this xor that`
*/
def xor(that: UnsignedBigInt): UnsignedBigInt = {
CUnsignedBigInt(wrappedValue.xor(that.asInstanceOf[CUnsignedBigInt].wrappedValue))
}

override def shiftLeft(n: Int): UnsignedBigInt = CUnsignedBigInt(wrappedValue.shiftLeft(n).toUnsignedBigIntValueExact)

override def shiftRight(n: Int): UnsignedBigInt = CUnsignedBigInt(wrappedValue.shiftRight(n).toUnsignedBigIntValueExact)

override def bitwiseInverse(): UnsignedBigInt = {
val bytes = BigIntegers.asUnsignedByteArray(32, wrappedValue)
val res: Array[Byte] = bytes.map(b => (~b & 0xff).toByte)
CUnsignedBigInt(BigIntegers.fromUnsignedByteArray(res))
}

override def toSigned(): BigInt = {
CBigInt(wrappedValue.toSignedBigIntValueExact)
}

}

6 changes: 2 additions & 4 deletions core/shared/src/test/scala/sigma/CollsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ class CollsTests extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers
}
}
VersionContext.withVersions(VersionContext.JitActivationVersion, VersionContext.JitActivationVersion) {
// TODO v5.0: make it work
// equalLengthMapped(pairs, squared(inc)) // problem fixed in v5.0
equalLengthMapped(pairs, squared(inc)) // problem fixed in v5.0
}

equalLength(pairs.append(pairs))
Expand All @@ -76,8 +75,7 @@ class CollsTests extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers
}
}
VersionContext.withVersions(VersionContext.JitActivationVersion, VersionContext.JitActivationVersion) {
// TODO v5.0: make it work
// equalLengthMapped(pairs.append(pairs), squared(inc)) // problem fixed in v5.0
equalLengthMapped(pairs.append(pairs), squared(inc)) // problem fixed in v5.0
}
}
}
Expand Down
23 changes: 3 additions & 20 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ package sigma.ast
import org.ergoplatform._
import org.ergoplatform.validation._
import sigma.{UnsignedBigInt, _}
import sigma.{Coll, VersionContext, _}
import sigma.{Coll, VersionContext}
import sigma.Evaluation.stypeToRType
import sigma._
import sigma.{VersionContext, _}
import sigma.ast.SCollection.{SBooleanArray, SBoxArray, SByteArray, SByteArray2, SHeaderArray}
import sigma.ast.SGlobalMethods.{decodeNBitsMethod, encodeNBitsMethod}
import sigma.ast.SMethod.{MethodCallIrBuilder, MethodCostFunc, javaMethodOf}
import sigma.ast.SType.{TypeCode, paramT, tT}
import sigma.ast.syntax.{SValue, ValueOps}
import sigma.ast.syntax.ValueOps
import sigma.data.ExactIntegral.{ByteIsExactIntegral, IntIsExactIntegral, LongIsExactIntegral, ShortIsExactIntegral}
import sigma.data.NumericOps.BigIntIsExactIntegral
import sigma.data.OverloadHack.Overloaded1
import sigma.data.UnsignedBigIntNumericOps.UnsignedBigIntIsExactIntegral
import sigma.data.{CBigInt, CUnsignedBigInt, DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants}
import sigma.data.{CBigInt, CUnsignedBigInt, DataValueComparer, KeyValueColl, RType, SigmaConstants}
import sigma.eval.{CostDetails, ErgoTreeEvaluator, TracedCost}
import sigma.pow.Autolykos2PowValidation
import sigma.reflection.RClass
import sigma.serialization.CoreByteWriter.ArgInfo
import sigma.serialization.{DataSerializer, SigmaByteWriter, SigmaSerializer}
import sigma.util.NBitsUtils
import sigma.utils.SparseArrayContainer

import scala.annotation.unused
Expand Down Expand Up @@ -471,15 +467,6 @@ object SNumericTypeMethods extends MethodsContainer {
case object SBooleanMethods extends MonoTypeMethods {
/** Type for which this container defines methods. */
override def ownerType: SMonoType = SBoolean

val ToByte = "toByte"
protected override def getMethods() = super.getMethods()
/* TODO soft-fork: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/479
++ Seq(
SMethod(this, ToByte, SFunc(this, SByte), 1)
.withInfo(PropertyCall, "Convert true to 1 and false to 0"),
)
*/
}

/** Methods of ErgoTree type `Byte`. */
Expand Down Expand Up @@ -646,10 +633,6 @@ case object SGroupElementMethods extends MonoTypeMethods {
.withInfo(PropertyCall, "Inverse element of the group.")

protected override def getMethods(): Seq[SMethod] = {
/* TODO soft-fork: https://github.com/ScorexFoundation/sigmastate-interpreter/issues/479
SMethod(this, "isIdentity", SFunc(this, SBoolean), 1)
.withInfo(PropertyCall, "Checks if this value is identity element of the eliptic curve group."),
*/
val v5Methods = Seq(
GetEncodedMethod,
ExponentiateMethod,
Expand Down
2 changes: 1 addition & 1 deletion sigma-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sigmastate-js",
"version": "0.4.2",
"version": "0.6",
"description": "Sigma.js library",
"files": [
"dist/",
Expand Down
Loading