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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: scryptomath-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 80e02124d44cd11d0f25862eb53ad12bc5ff1209e637409dfe285eb364e1f1e7
MD5 d30720c70dfcd82691a64de571eb1a39
BLAKE2b-256 7f0dda0b67dad33e40061ad859f0e4af20c8de64ae8995cde249173ef8b8a7f1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: scryptomath-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 42e3712c70379b0a7637b9f88c18b9c9819ae5c3a66e03f67b76add5c6ea01bf
MD5 ecc8d72bcd3ee8fef83473bf4858aa1d
BLAKE2b-256 4c6025edf517d4a2db6cbb8fb1ed862b9949ddacf6dd05264a3c762478d57c22

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