leo/examples/hackers-delight/ntzseals
Collin Chin d44457fc32
[Feature] Implement leo execute (#2491)
* bump snarkvm rev

* update default gitignore

* impl leo execute

* bump snarkvm 0.14.5

* modify examples wip

* update run.sh examples

* impl env file

* clippy warning

* fix auction example

* fix auction example env

* generate new private key for new env - tests failing due to env not found err

* commit error changes

* Fix tests; clippy

* Get examples working

* leo build checks that build dir is well formed; clippy

* Clean up

* Update examples/README.md

Co-authored-by: d0cd <pranavsaig@gmail.com>
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>

* do not commit .avm files

* use snarkvm commands

---------

Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
Co-authored-by: Pranav Gaddamadugu <pranav@aleo.org>
Co-authored-by: d0cd <pranavsaig@gmail.com>
2023-07-19 18:04:09 -07:00
..
build [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
inputs Reorganize hackers delight examples 2022-09-13 17:22:01 +02:00
src More examples 2022-10-06 00:17:51 -07:00
.env [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
.gitignore [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
program.json [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
README.md Reorganize hackers delight examples 2022-09-13 17:22:01 +02:00

src/ntzseals.leo

Build Guide

To compile and run this Leo program, run:

leo run

The Algorithm

This algorithm is detailed in "Hacker's Delight, 2nd edition" by Henry S. Warren, section 5-4, figure 5-25.

First we isolate the rightmost 1 bit in the 32-bit input by using the C idiom x & (-x). In Leo, the -x is written as 0u32.sub_wrapped(x).

A constant was discovered with the property that when it was multiplied by by 0, 1, 2, 4, ... 2**31, the 33 values all had different patterns of their 6 highest bits. The constant was also chosen to make multiplication easy to do with a small number of shifts and adds on conventional hardware.

In the algorithm, the 6 high bits are used as an index into a table, where 31 of the entries are unused. The 33 used values were chosen so that they gave the correct number of trailing zeros for the inputs.

This algorithm was proposed by David Seal in the comp.sys.acorn.tech newsgroup, February 16, 1994: https://groups.google.com/g/comp.sys.acorn.tech/c/blRy-AiIQ-0/m/3JxNHeKN75IJ

A further post that includes the table used was made by Michael Williams in the comp.arch.arithmetic newsgroup, December 4, 1998: https://groups.google.com/g/comp.arch.arithmetic/c/yBt-QHRVEGE/m/stFPPMD0b7AJ