Skip to content

Commit

Permalink
bitwise or/and/xor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Nov 11, 2024
1 parent 77be8a6 commit 626edc8
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
20 changes: 17 additions & 3 deletions data/shared/src/main/scala/sigma/data/UnsignedBigIntegerOps.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sigma.data

import debox.cfor
import scorex.util.encode.Base16
import sigma._
import sigma.crypto.BigIntegers
import sigma.data.UnsignedBigIntOrderingOps.UnsignedBigIntOrdering
Expand Down Expand Up @@ -110,17 +112,29 @@ object UnsignedBigIntNumericOps {
/**
* @return a numeric value which is `this | that`
*/
override def bitwiseOr(x: UnsignedBigInt, y: UnsignedBigInt): UnsignedBigInt = ???
override def bitwiseOr(x: UnsignedBigInt, y: UnsignedBigInt): UnsignedBigInt = {
val vx = x.asInstanceOf[CUnsignedBigInt].wrappedValue
val vy = y.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(vx.or(vy))
}

/**
* @return a numeric value which is `this && that`
*/
override def bitwiseAnd(x: UnsignedBigInt, y: UnsignedBigInt): UnsignedBigInt = ???
override def bitwiseAnd(x: UnsignedBigInt, y: UnsignedBigInt): UnsignedBigInt = {
val vx = x.asInstanceOf[CUnsignedBigInt].wrappedValue
val vy = y.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(vx.and(vy))
}

/**
* @return a numeric value which is `this xor that`
*/
override def bitwiseXor(x: UnsignedBigInt, y: UnsignedBigInt): UnsignedBigInt = ???
override def bitwiseXor(x: UnsignedBigInt, y: UnsignedBigInt): UnsignedBigInt = {
val vx = x.asInstanceOf[CUnsignedBigInt].wrappedValue
val vy = y.asInstanceOf[CUnsignedBigInt].wrappedValue
CUnsignedBigInt(vx.xor(vy))
}

/**
* @return a value which is (this << n). The shift distance, n, may be negative,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,40 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("UnsignedBigInt.bitwiseOr") {
def bitwiseOrTest(): Assertion = test("BigInt.bitwiseOr", env, ext,
s"""{
| val x = unsignedBigInt("${CryptoConstants.groupOrder}")
| x.bitwiseOr(x) == x
|}""".stripMargin,
null
)

if (VersionContext.current.isV6SoftForkActivated) {
bitwiseOrTest()
} else {
an[sigma.validation.ValidationException] shouldBe thrownBy(bitwiseOrTest())
}
}

property("UnsignedBigInt.bitwiseOr - 2") {
def bitwiseOrTest(): Assertion = test("BigInt.bitwiseOr", env, ext,
s"""{
| val x = unsignedBigInt("${CryptoConstants.groupOrder}")
| val y = unsignedBigInt("121")
| val z = unsignedBigInt("115792089237316195423570985008687907852837564279074904382605163141518161494393")
| x.bitwiseOr(y) == z && y.bitwiseOr(x) == z
|}""".stripMargin,
null
)

if (VersionContext.current.isV6SoftForkActivated) {
bitwiseOrTest()
} else {
an[sigma.validation.ValidationException] shouldBe thrownBy(bitwiseOrTest())
}
}

property("BigInt.bitwiseAnd") {
def bitwiseAndTest(): Assertion = test("BigInt.bitwiseAnd", env, ext,
s"""{
Expand All @@ -955,6 +989,43 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("UnsignedBigInt.bitwiseAnd") {
def bitwiseAndTest(): Assertion = test("UnsignedBigInt.bitwiseAnd", env, ext,
s"""{
| val x = unsignedBigInt("${CryptoConstants.groupOrder}")
| val y = 0.toBigInt.toUnsigned
| x.bitwiseAnd(y) == y
|}""".stripMargin,
null
)

if (VersionContext.current.isV6SoftForkActivated) {
bitwiseAndTest()
} else {
an[sigma.validation.ValidationException] shouldBe thrownBy(bitwiseAndTest())
}
}

property("UnsignedBigInt.bitwiseAnd - 2") {
def bitwiseAndTest(): Assertion = test("UnsignedBigInt.bitwiseAnd", env, ext,
s"""{
| val x = unsignedBigInt("${CryptoConstants.groupOrder}")
| val y = unsignedBigInt("1157920892373161954235709850086879078528375642790749043826051631415181614337")
| val z = unsignedBigInt("1157920892373161954235709850086879078522970439492889181512311797126516834561")
|
| // cross-checked with wolfram alpha
| x.bitwiseAnd(y) == z
|}""".stripMargin,
null
)

if (VersionContext.current.isV6SoftForkActivated) {
bitwiseAndTest()
} else {
an[sigma.validation.ValidationException] shouldBe thrownBy(bitwiseAndTest())
}
}

property("Short.bitwiseAnd") {
val customExt = Map(
1.toByte -> ShortConstant(32767)
Expand Down Expand Up @@ -995,6 +1066,26 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("UnsignedBigInt.bitwiseXor") {
def bitwiseAndTest(): Assertion = test("UnsignedBigInt.bitwiseXor", env, ext,
s"""{
| val x = unsignedBigInt("${CryptoConstants.groupOrder}")
| val y = unsignedBigInt("1157920892373161954235709850086879078528375642790749043826051631415181614337")
| val z = unsignedBigInt("114634168344943033469335275158601028774319999042879875063406591178680309439552")
|
| // cross-checked with wolfram alpha
| x.bitwiseXor(y) == z
|}""".stripMargin,
null
)

if (VersionContext.current.isV6SoftForkActivated) {
bitwiseAndTest()
} else {
an[sigma.validation.ValidationException] shouldBe thrownBy(bitwiseAndTest())
}
}

property("Byte.shiftLeft") {
def shiftLeftTest(): Assertion = test("Byte.shiftLeft", env, ext,
s"""{
Expand Down

0 comments on commit 626edc8

Please sign in to comment.