Skip to main content

Math library to support writing Python tests for Scrypto blueprints

Project description

scryptomath for Python

Why

In more complex Scrypto blueprints you need to calculate your test values against which you want to compare your outputs first. A good approach to achieve that is to calculate them on a higher level without optimisations staying close to the mathematical specification.

This allows you to validate your mathematical specification more easily, including numerical precision boundaries, by investigating and resolving differences in the calculation results between the Scrypto and Python implementations.

Features

Fixed precision decimal types in Python

This library supports you implementing your Python prototype by providing Scrypto decimal types with fixed precision math in Python:

  • Decimal
  • BalancedDecimal
  • PreciseDecimal

Currently the following operations are implemented (more can be added):

  • __abs__
  • __add__
  • __sub__
  • __mul__
  • __truediv__ ("normal" division)
  • __pow__ (exact pow result)
  • sqrt() (exact s)
  • powi() (powi implementation Scrypto is using to match results and not identical to result of __pow__)

However, use this wisely if necessary only because depending on the use case you rather want to calculate your test assertion outputs with very high precision (like 500 decimal places) in Python first. That's what we are doing by default for Ociswap and only use the fixed decimal point types if it is required to match the test values.

Disclaimer: if you are using other operations than the ones above a normal Python decimal.Decimal with the full precision you have specified is returned. In that case the return value is not being trimmed down to the fixed decimal places of the Scrypto decimal.

Code generation for decimal constants in Scrypto

Currently, the macros dec! and pdec! can't be used for constants in Scrypto. To workaround that limitation we have implemented Scrypto code generation for initialising decimal constants directly in Scrypto:

>>> from scryptomath import Decimal
>>> Decimal(3).powi(4).scrypto
'Decimal(BnumI256::from_digits([7213023705161793536, 4, 0, 0]))'

Which can be directly used in your Scrypto blueprint like this:

const MY_CONST: Decimal = Decimal(BnumI256::from_digits([7213023705161793536, 4, 0, 0]));

Eventually, we assume Scrypto probably will support compile time macros for dec! and pdec!, but in the meantime this is working very well as a workaround.

Contribute

If you have any requirements for additional data types just open an issue or pull request. Happy to add more :)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scryptomath-0.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

scryptomath-0.0.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file scryptomath-0.0.1.tar.gz.

File metadata

  • Download URL: scryptomath-0.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for scryptomath-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ba1f90977f0a8bd4d86c210d8b333c8cb20052499adf1daaa61d5c05b6af56be
MD5 8cac7511bb7c55092126146617aedcb7
BLAKE2b-256 a6290378095db7117991974bcd280768300d28c0152a8d8457b05664a9d7d663

See more details on using hashes here.

File details

Details for the file scryptomath-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: scryptomath-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for scryptomath-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e46e2830be558d7e834f6a35a7988ccc07d262ef57b498b4501e84752072dfc1
MD5 ae9e70ed9051746cd6bb8810da91abc5
BLAKE2b-256 88c8d07b30557dde8525569a24003b01573484b500088127bdeb3d3562620b2d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page