Fix issues highlighted in #2156

This commit is contained in:
Tom-OriginStorage 2022-11-05 15:25:06 +08:00
parent e5d053e471
commit 64bd319f3d
3 changed files with 21 additions and 23 deletions

View File

@ -2,16 +2,12 @@
## Build Guide
To compile this Aleo program, run:
```bash
aleo build
```
To run this program, run:
```bash
leo run fibonacci
```
## Overview
This example shows how to calculate Fibonacci number using the fast-doubling method.
This example shows how to calculate Fibonacci number using the [fast-doubling method](https://math.stackexchange.com/questions/1124590/need-help-understanding-fibonacci-fast-doubling-proof).
It takes the input data from `inputs/fibonacci.in`

View File

@ -1,3 +1,3 @@
// The program input for fibonacci/src/main.leo
[main]
public n: u128 = 63u128;
[fibonacci]
public n: u8 = 63u8;

View File

@ -1,43 +1,45 @@
program fibonacci.aleo {
// This calculates the n-th fibonacci number (up to 64th)
transition fibonacci(public n: u128) -> u128 {
transition fibonacci(public n: u8) -> u128 {
console.assert(n <= 64u8);
let f0: u128 = 0u128;
let f1: u128 = 1u128;
let c: u16 = 0u16;
let c: u8 = 0u8;
let z: u128 = reverse_bits(n);
let z: u8 = reverse_bits(n);
for i:u16 in 0u16..8u16 {
if n > 0u128 {
for i:u8 in 0u8..8u8 {
if n > 0u8 {
let f2i1: u128 = f1 * f1 + f0 * f0;
let f2i: u128 = f0 * (2u128 * f1 - f0);
if z & 1u128.shl(c) == 0u128 {
if z & 1u8.shl(c) == 0u8 {
f0 = f2i;
f1 = f2i1;
} else {
f0 = f2i1;
f1 = f2i + f2i1;
}
c = c + 1u16;
n = n >> 1u16;
c = c + 1u8;
n = n >> 1u8;
}
}
return f0;
}
function reverse_bits(n: u128) -> u128 {
let reverse: u128 = 0u128;
function reverse_bits(n: u8) -> u8 {
let reverse: u8 = 0u8;
for i:u128 in 0u128..128u128 {
if n > 0u128 {
reverse = reverse << 1u16;
for i:u8 in 0u8..8u8 {
if n > 0u8 {
reverse = reverse << 1u8;
if n & 1u128 == 1u128 {
reverse ^= 1u128;
if n & 1u8 == 1u8 {
reverse ^= 1u8;
}
n = n >> 1u16;
n = n >> 1u8;
}
}