-
Notifications
You must be signed in to change notification settings - Fork 41
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
[6.0.0] Conversion from Long-encoded nBits representation to BigInt and back #962
base: v6.0.0
Are you sure you want to change the base?
Changes from all commits
0843902
49dce78
1c1049e
37bb86c
6f8981f
d0f1b7d
fcc7f0f
6e47167
9e4e098
1016323
93748f1
a3cb64d
8941a8e
c8d75cd
c9d0889
299fdd5
a90e7be
37b23cd
aaa2aa9
a20c04f
2c8df31
6981c34
d0eef4c
6128fbd
ee25e40
e4a611d
c5c37ff
e87ad02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,18 +4,21 @@ import org.ergoplatform._ | |
import org.ergoplatform.validation._ | ||
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.data.ExactIntegral.{ByteIsExactIntegral, IntIsExactIntegral, LongIsExactIntegral, ShortIsExactIntegral} | ||
import sigma.data.NumericOps.BigIntIsExactIntegral | ||
import sigma.data.OverloadHack.Overloaded1 | ||
import sigma.data.{DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants} | ||
import sigma.data.{CBigInt, DataValueComparer, KeyValueColl, Nullable, RType, SigmaConstants} | ||
import sigma.eval.{CostDetails, ErgoTreeEvaluator, TracedCost} | ||
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 | ||
|
@@ -1806,6 +1809,18 @@ case object SGlobalMethods extends MonoTypeMethods { | |
"Decode a number from big endian bytes.", | ||
ArgInfo("first", "Bytes which are big-endian encoded number.")) | ||
|
||
private lazy val EnDecodeNBitsCost = FixedCost(JitCost(5)) // the same cost for nbits encoding and decoding | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cost is too low. And encoding is cheaper than decoding (because of array creation). |
||
|
||
lazy val encodeNBitsMethod: SMethod = SMethod( | ||
this, "encodeNbits", SFunc(Array(SGlobal, SBigInt), SLong), 6, EnDecodeNBitsCost) | ||
.withIRInfo(MethodCallIrBuilder) | ||
.withInfo(MethodCall, "Encode big integer number as nbits", ArgInfo("bigInt", "Big integer")) | ||
|
||
lazy val decodeNBitsMethod: SMethod = SMethod( | ||
this, "decodeNbits", SFunc(Array(SGlobal, SLong), SBigInt), 7, EnDecodeNBitsCost) | ||
.withIRInfo(MethodCallIrBuilder) | ||
.withInfo(MethodCall, "Decode nbits-encoded big integer number", ArgInfo("nbits", "NBits-encoded argument")) | ||
|
||
lazy val serializeMethod = SMethod(this, "serialize", | ||
SFunc(Array(SGlobal, tT), SByteArray, Array(paramT)), 3, DynamicCost) | ||
.withIRInfo(MethodCallIrBuilder) | ||
|
@@ -1841,6 +1856,8 @@ case object SGlobalMethods extends MonoTypeMethods { | |
groupGeneratorMethod, | ||
xorMethod, | ||
serializeMethod, | ||
encodeNBitsMethod, | ||
decodeNBitsMethod, | ||
fromBigEndianBytesMethod | ||
) | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import sigma.eval.Extensions.EvalCollOps | |
import sigma.serialization.{DataSerializer, GroupElementSerializer, SigmaSerializer} | ||
import sigma.serialization.{GroupElementSerializer, SerializerException, SigmaSerializer} | ||
import sigma.util.Extensions.BigIntegerOps | ||
import sigma.util.NBitsUtils | ||
import sigma.validation.SigmaValidationSettings | ||
import sigma.{AvlTree, BigInt, Box, Coll, CollBuilder, Evaluation, GroupElement, SigmaDslBuilder, SigmaProp, VersionContext} | ||
|
||
|
@@ -178,6 +179,14 @@ class CSigmaDslBuilder extends SigmaDslBuilder { dsl => | |
|
||
override def groupGenerator: GroupElement = _generatorElement | ||
|
||
def encodeNbits(bi: BigInt): Long = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
NBitsUtils.encodeCompactBits(bi.asInstanceOf[CBigInt].wrappedValue) | ||
} | ||
|
||
def decodeNbits(l: Long): BigInt = { | ||
CBigInt(NBitsUtils.decodeCompactBits(l).bigInteger) | ||
} | ||
|
||
/** | ||
* @return the identity of the Dlog group used in ErgoTree | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit confusing comment, says "@return big integer" while the actual return type is Long.