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.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: scryptomath-0.0.2.tar.gz
  • Upload date:
  • Size: 4.4 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.2.tar.gz
Algorithm Hash digest
SHA256 cee5f2011e4f4a0e7683db06d9e4393e88ba2dffeef03b8a60ffc8e78027c8e8
MD5 a8f373b06e593fb17d96c484c0240a2b
BLAKE2b-256 f8dbf99a5cef231807d49747c832e6e2da9b58da3972a0ddd0ad3a9bf9e49e73

See more details on using hashes here.

File details

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

File metadata

  • Download URL: scryptomath-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 028d6e727e087f1f094cca4f516d1a28185ea2f4b8887c24f37542d2e1e7a2f4
MD5 40e87cb02bc2e41628fb9c8dc9a2d1a1
BLAKE2b-256 2f13673a291c002a7c4798640818a688a7c83f23b477cde90899a72b806f89b1

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