Skip to content

Commit

Permalink
adding UnsignedBigInt to JS reflection , pt1
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Nov 1, 2024
1 parent a18967c commit 2d8af9d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/js/src/main/scala/sigma/crypto/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ object Platform {
case _: Boolean => tpe == SBoolean
case _: Byte | _: Short | _: Int | _: Long => tpe.isInstanceOf[SNumericType]
case _: BigInt => tpe == SBigInt
case _: UnsignedBigInt => tpe == SUnsignedBigInt
case _: String => tpe == SString
case _: GroupElement => tpe.isGroupElement
case _: SigmaProp => tpe.isSigmaProp
Expand Down
14 changes: 13 additions & 1 deletion core/js/src/main/scala/sigma/js/Isos.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigma.js

import sigma.{Coll, Colls}
import sigma.data.{CBigInt, Iso, RType}
import sigma.data.{CBigInt, CUnsignedBigInt, Iso, RType}

import java.math.BigInteger
import scala.reflect.ClassTag
Expand Down Expand Up @@ -42,6 +42,18 @@ object Isos {
}
}

implicit val isoUnsignedBigInt: Iso[js.BigInt, sigma.UnsignedBigInt] = new Iso[js.BigInt, sigma.UnsignedBigInt] {
override def to(x: js.BigInt): sigma.UnsignedBigInt = {
CUnsignedBigInt(new BigInteger(x.toString(10)))
}

override def from(x: sigma.UnsignedBigInt): js.BigInt = {
val bi = x.asInstanceOf[CUnsignedBigInt].wrappedValue
val s = bi.toString(10)
js.BigInt(s)
}
}

implicit val isoBigIntToLong: Iso[js.BigInt, Long] = new Iso[js.BigInt, Long] {
override def to(x: js.BigInt): Long = java.lang.Long.parseLong(x.toString(10))

Expand Down
15 changes: 15 additions & 0 deletions core/shared/src/main/scala/sigma/reflection/ReflectionData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ object ReflectionData {
},
mkMethod(clazz, "divide", paramTypes) { (obj, args) =>
obj.asInstanceOf[UnsignedBigInt].divide(args(0).asInstanceOf[UnsignedBigInt])
},
mkMethod(clazz, "plusMod", paramTypes) { (obj, args) =>
obj.asInstanceOf[UnsignedBigInt].plusMod(args(0).asInstanceOf[UnsignedBigInt], args(1).asInstanceOf[UnsignedBigInt])
},
mkMethod(clazz, "subtractMod", paramTypes) { (obj, args) =>
obj.asInstanceOf[UnsignedBigInt].subtractMod(args(0).asInstanceOf[UnsignedBigInt], args(1).asInstanceOf[UnsignedBigInt])
},
mkMethod(clazz, "multiplyMod", paramTypes) { (obj, args) =>
obj.asInstanceOf[UnsignedBigInt].multiplyMod(args(0).asInstanceOf[UnsignedBigInt], args(1).asInstanceOf[UnsignedBigInt])
},
mkMethod(clazz, "mod", paramTypes) { (obj, args) =>
obj.asInstanceOf[UnsignedBigInt].mod(args(0).asInstanceOf[UnsignedBigInt])
},
mkMethod(clazz, "modInverse", paramTypes) { (obj, args) =>
obj.asInstanceOf[UnsignedBigInt].modInverse(args(0).asInstanceOf[UnsignedBigInt])
}
)
)
Expand Down
1 change: 1 addition & 0 deletions data/js/src/main/scala/sigma/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ object Platform {
case v: Long => Nullable(mkConstant[SLong.type](v, SLong))
case v: BigInteger => Nullable(mkConstant[SBigInt.type](CBigInt(v), SBigInt))
case n: sigma.BigInt => Nullable(mkConstant[SBigInt.type](n, SBigInt))
case n: sigma.UnsignedBigInt => Nullable(mkConstant[SUnsignedBigInt.type](n, SUnsignedBigInt))
case ge: GroupElement => Nullable(mkConstant[SGroupElement.type](ge, SGroupElement))
case b: Boolean => Nullable(if (b) TrueLeaf else FalseLeaf)
case v: String => Nullable(mkConstant[SString.type](v, SString))
Expand Down
6 changes: 6 additions & 0 deletions data/js/src/main/scala/sigma/js/Value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ object Value extends js.Object {
case sigma.BigIntRType =>
val v = data.asInstanceOf[js.BigInt]
CBigInt(new BigInteger(v.toString(16), 16))
case sigma.UnsignedBigIntRType =>
val v = data.asInstanceOf[js.BigInt]
CUnsignedBigInt(new BigInteger(v.toString(16), 16))
case sigma.GroupElementRType =>
val ge = data.asInstanceOf[GroupElement]
CGroupElement(ge.point)
Expand Down Expand Up @@ -121,6 +124,9 @@ object Value extends js.Object {
case sigma.BigIntRType =>
val hex = value.asInstanceOf[sigma.BigInt].toBigInteger.toString(10)
js.BigInt(hex)
case sigma.UnsignedBigIntRType =>
val hex = value.asInstanceOf[sigma.BigInt].toBigInteger.toString(10)
js.BigInt(hex)
case sigma.GroupElementRType =>
val point = value.asInstanceOf[CGroupElement].wrappedValue.asInstanceOf[Platform.Ecp]
new GroupElement(point)
Expand Down
1 change: 0 additions & 1 deletion data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,6 @@ case object SUnsignedBigIntMethods extends SNumericTypeMethods {
// todo: costing
final val ModInverseCostInfo = ToNBitsCostInfo

// todo: check ids before and after merging with other PRs introducing new methods for Numeric
val ModInverseMethod = SMethod(this, "modInverse", SFunc(Array(this.ownerType, this.ownerType), this.ownerType), 14, ModInverseCostInfo.costKind)
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")
Expand Down
44 changes: 44 additions & 0 deletions sc/shared/src/main/scala/sigma/compiler/ir/GraphIRReflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,50 @@ object GraphIRReflection {
)
}

{ val clazz = classOf[SigmaDsl#UnsignedBigInt]
val ctx = null.asInstanceOf[SigmaDsl] // ok! type level only
registerClassEntry(clazz,
methods = Map(
mkMethod(clazz, "add", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].add(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "max", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].max(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "min", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].min(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "subtract", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].subtract(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "multiply", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].multiply(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "mod", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].mod(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "divide", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].divide(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "plusMod", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].plusMod(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]], args(1).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "subtractMod", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].subtractMod(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]], args(1).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "multiplyMod", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].multiplyMod(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]], args(1).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "mod", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].mod(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
},
mkMethod(clazz, "modInverse", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.UnsignedBigInt].modInverse(args(0).asInstanceOf[ctx.Ref[ctx.UnsignedBigInt]])
}
)
)
}

{ val clazz = classOf[sigma.compiler.ir.wrappers.sigma.Colls#CollBuilder]
val ctx = null.asInstanceOf[IRContext] // ok! type level only
registerClassEntry(clazz,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ object UnsignedBigInt extends EntityObject("UnsignedBigInt") {
}
}


implicit object LiftableUnsignedBigInt extends Liftable[SUnsignedBigInt, UnsignedBigInt] {
lazy val eW: Elem[UnsignedBigInt] = unsignedBigIntElement
lazy val sourceType: RType[SUnsignedBigInt] = {
Expand Down Expand Up @@ -522,7 +521,7 @@ object UnsignedBigInt extends EntityObject("UnsignedBigInt") {
override protected def collectMethods: Map[RMethod, MethodDesc] = {
super.collectMethods ++
Elem.declaredMethods(RClass(classOf[UnsignedBigInt]), RClass(classOf[UnsignedBigInt]), Set(
"add", "subtract", "multiply", "divide", "mod", "min", "max"
"add", "subtract", "multiply", "divide", "mod", "min", "max", "plusMod", "subtractMod", "multiplyMod"
))
}
}
Expand Down

0 comments on commit 2d8af9d

Please sign in to comment.