diff --git a/core/shared/src/main/scala/sigma/ast/SType.scala b/core/shared/src/main/scala/sigma/ast/SType.scala index ab0c2a811c..1fac650525 100644 --- a/core/shared/src/main/scala/sigma/ast/SType.scala +++ b/core/shared/src/main/scala/sigma/ast/SType.scala @@ -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 @@ -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 diff --git a/core/shared/src/main/scala/sigma/data/CBigInt.scala b/core/shared/src/main/scala/sigma/data/CBigInt.scala index 1fe1c2f503..ca2507fc7a 100644 --- a/core/shared/src/main/scala/sigma/data/CBigInt.scala +++ b/core/shared/src/main/scala/sigma/data/CBigInt.scala @@ -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) @@ -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) - } - -} diff --git a/core/shared/src/main/scala/sigma/data/CUnsignedBigInt.scala b/core/shared/src/main/scala/sigma/data/CUnsignedBigInt.scala new file mode 100644 index 0000000000..1375a2ffb4 --- /dev/null +++ b/core/shared/src/main/scala/sigma/data/CUnsignedBigInt.scala @@ -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) + } + +} + diff --git a/core/shared/src/test/scala/sigma/CollsTests.scala b/core/shared/src/test/scala/sigma/CollsTests.scala index da427ba576..31d3c05e81 100644 --- a/core/shared/src/test/scala/sigma/CollsTests.scala +++ b/core/shared/src/test/scala/sigma/CollsTests.scala @@ -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)) @@ -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 } } } diff --git a/data/shared/src/main/scala/sigma/ast/methods.scala b/data/shared/src/main/scala/sigma/ast/methods.scala index dc2489cb76..6de20b8066 100644 --- a/data/shared/src/main/scala/sigma/ast/methods.scala +++ b/data/shared/src/main/scala/sigma/ast/methods.scala @@ -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 @@ -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`. */ @@ -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, diff --git a/sigma-js/package.json b/sigma-js/package.json index f6900571bd..a210659bcf 100644 --- a/sigma-js/package.json +++ b/sigma-js/package.json @@ -1,6 +1,6 @@ { "name": "sigmastate-js", - "version": "0.4.2", + "version": "0.6", "description": "Sigma.js library", "files": [ "dist/",