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 (18 decimal places)
  • PreciseDecimal (36 decimal places)

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(I192::from_digits([7213023705161793536, 4, 0]))'

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

const MY_CONST: Decimal = Decimal(I192::from_digits([7213023705161793536, 4, 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.4.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for scryptomath-0.0.4.tar.gz
Algorithm Hash digest
SHA256 eaa13991abb6b798abb80782fd86b7868473417a14dcd6c6f162d6541102aba7
MD5 f9d54a71226efc2d19a5a0c877f506c8
BLAKE2b-256 b1372b698d64e89cd4d211cab72ca3d19e082a1793cdea3d25fa2b9e601b53c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: scryptomath-0.0.4-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.8

File hashes

Hashes for scryptomath-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 01a6fefa5cd42f07a81ea4cda29eac0865508469c57631cce25d34c422fc57cd
MD5 e90a2523ff541376509a4ea847586443
BLAKE2b-256 92d8785ef1516c7d56b23c36baa61cc08d928d606fea1f1e55321e5142ed0e6f

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