Skip to content

Commit

Permalink
bitwiseInverse
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Nov 5, 2024
1 parent ac4bbbc commit a87bfb3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sigma.data

import sigma._
import sigma.crypto.BigIntegers
import sigma.data.UnsignedBigIntOrderingOps.UnsignedBigIntOrdering
import sigma.eval.Extensions.IntExt

Expand Down Expand Up @@ -98,9 +99,13 @@ object UnsignedBigIntNumericOps {
override def toBigEndianBytes(x: UnsignedBigInt): Coll[Byte] = x.toBytes

/**
* @return a numeric value which is inverse of `x` (every bit, including sign, is flipped)
* @return a numeric value which is inverse of `x` (every bit is flipped)
*/
override def bitwiseInverse(x: UnsignedBigInt): UnsignedBigInt = ???
override def bitwiseInverse(x: UnsignedBigInt): UnsignedBigInt = {
val bytes = BigIntegers.asUnsignedByteArray(x.asInstanceOf[CUnsignedBigInt].wrappedValue)
val res: Array[Byte] = bytes.map(b => (b ^ Byte.MinValue).toByte)
CUnsignedBigInt(BigIntegers.fromUnsignedByteArray(res))
}

/**
* @return a numeric value which is `this | that`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,23 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("UnsignedBigInt.bitwiseInverse") {
def bitwiseInverseTest(): Assertion = test("UnsignedBigInt.bitwiseInverse", env, ext,
s"""{
| val b = unsignedBigInt("${CryptoConstants.groupOrder}")
| val bi = b.bitwiseInverse
| bi.bitwiseInverse == b
|}""".stripMargin,
null
)

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


property("Byte.bitwiseInverse") {
def bitwiseInverseTest(): Assertion = test("Byte.bitwiseInverse", env, ext,
Expand Down

0 comments on commit a87bfb3

Please sign in to comment.