Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement hints on uint384 lib (Part 1) #960

Merged
merged 32 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ada5a2b
Add hint code for UINT348_UNSIGNED_DIV_REM
fmoletta Apr 11, 2023
d1b7e84
Add file for uint348 files
fmoletta Apr 11, 2023
c409d01
Add pack & split for uint348
fmoletta Apr 12, 2023
93c6f4e
Move comment
fmoletta Apr 12, 2023
82dbf2b
Implement uint348_unsigned_div_rem hint
fmoletta Apr 12, 2023
df9deb2
Add integration test
fmoletta Apr 12, 2023
7b2f5d9
Add integration test
fmoletta Apr 12, 2023
b1a3fbb
Add unit tests
fmoletta Apr 12, 2023
110605a
Add hint on split_128
fmoletta Apr 12, 2023
8cc3ce7
Test split_128 hint
fmoletta Apr 12, 2023
378788d
Add add_no_uint384_hint
fmoletta Apr 12, 2023
271a38c
Fix hint + add tests
fmoletta Apr 12, 2023
ed7de28
Add hint code for UINT348_UNSIGNED_DIV_REM_EXPAND
fmoletta Apr 12, 2023
be408c3
Msc fixes
fmoletta Apr 12, 2023
8c6fb8c
Add integration test
fmoletta Apr 12, 2023
512a80e
Reduce Uint384_expand representation to the 3 used limbs
fmoletta Apr 12, 2023
089398e
Add unit test
fmoletta Apr 12, 2023
6852a87
Add hint code for UINT384_SQRT
fmoletta Apr 13, 2023
e3e05be
Add implementation for hint on sqrt
fmoletta Apr 13, 2023
faf4765
Integration test
fmoletta Apr 13, 2023
d30d404
Add unit tests
fmoletta Apr 13, 2023
aa9ce34
Merge branch 'main' of github.com:lambdaclass/cairo-rs into uint348-h…
fmoletta Apr 13, 2023
f257b56
Fix missing directive
fmoletta Apr 13, 2023
4218e80
Run cairo-format
fmoletta Apr 13, 2023
e1d0100
Add changelog entry
fmoletta Apr 13, 2023
51c7dc3
Spelling
fmoletta Apr 13, 2023
3f8bdfb
Update src/hint_processor/builtin_hint_processor/uint384.rs
fmoletta Apr 13, 2023
43f3977
Update src/hint_processor/builtin_hint_processor/uint384.rs
fmoletta Apr 13, 2023
0045914
Update src/hint_processor/builtin_hint_processor/uint384.rs
fmoletta Apr 13, 2023
4f89202
Make hint code more readable
fmoletta Apr 13, 2023
24e06fe
Merge branch 'main' into uint348-hints
fmoletta Apr 14, 2023
6861479
fix fmt
fmoletta Apr 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,108 @@

#### Upcoming Changes

* Implement hints on uint384 lib (Part 1) [#960](https://github.com/lambdaclass/cairo-rs/pull/960)

`BuiltinHintProcessor` now supports the following hints:

```python
def split(num: int, num_bits_shift: int, length: int):
a = []
for _ in range(length):
a.append( num & ((1 << num_bits_shift) - 1) )
num = num >> num_bits_shift
return tuple(a)

def pack(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

a = pack(ids.a, num_bits_shift = 128)
div = pack(ids.div, num_bits_shift = 128)
quotient, remainder = divmod(a, div)

quotient_split = split(quotient, num_bits_shift=128, length=3)
assert len(quotient_split) == 3

ids.quotient.d0 = quotient_split[0]
ids.quotient.d1 = quotient_split[1]
ids.quotient.d2 = quotient_split[2]

remainder_split = split(remainder, num_bits_shift=128, length=3)
ids.remainder.d0 = remainder_split[0]
ids.remainder.d1 = remainder_split[1]
ids.remainder.d2 = remainder_split[2]
```

```python
ids.low = ids.a & ((1<<128) - 1)
ids.high = ids.a >> 128
```

```python
sum_d0 = ids.a.d0 + ids.b.d0
ids.carry_d0 = 1 if sum_d0 >= ids.SHIFT else 0
sum_d1 = ids.a.d1 + ids.b.d1 + ids.carry_d0
ids.carry_d1 = 1 if sum_d1 >= ids.SHIFT else 0
sum_d2 = ids.a.d2 + ids.b.d2 + ids.carry_d1
ids.carry_d2 = 1 if sum_d2 >= ids.SHIFT else 0
```

```python
def split(num: int, num_bits_shift: int, length: int):
a = []
for _ in range(length):
a.append( num & ((1 << num_bits_shift) - 1) )
num = num >> num_bits_shift
return tuple(a)

def pack(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

def pack2(z, num_bits_shift: int) -> int:
limbs = (z.b01, z.b23, z.b45)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

a = pack(ids.a, num_bits_shift = 128)
div = pack2(ids.div, num_bits_shift = 128)
quotient, remainder = divmod(a, div)

quotient_split = split(quotient, num_bits_shift=128, length=3)
assert len(quotient_split) == 3

ids.quotient.d0 = quotient_split[0]
ids.quotient.d1 = quotient_split[1]
ids.quotient.d2 = quotient_split[2]

remainder_split = split(remainder, num_bits_shift=128, length=3)
ids.remainder.d0 = remainder_split[0]
ids.remainder.d1 = remainder_split[1]
ids.remainder.d2 = remainder_split[2]
```

```python
from starkware.python.math_utils import isqrt

def split(num: int, num_bits_shift: int, length: int):
a = []
for _ in range(length):
a.append( num & ((1 << num_bits_shift) - 1) )
num = num >> num_bits_shift
return tuple(a)

def pack(z, num_bits_shift: int) -> int:
limbs = (z.d0, z.d1, z.d2)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

a = pack(ids.a, num_bits_shift=128)
root = isqrt(a)
assert 0 <= root < 2 ** 192
root_split = split(root, num_bits_shift=128, length=3)
ids.root.d0 = root_split[0]
ids.root.d1 = root_split[1]
ids.root.d2 = root_split[2]
```
* Implement hint on `uint256_mul_div_mod`[#957](https://github.com/lambdaclass/cairo-rs/pull/957)

`BuiltinHintProcessor` now supports the following hint:
Expand Down
2 changes: 1 addition & 1 deletion cairo_programs/is_quad_residue_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ func main{output_ptr: felt*}() {

check_quad_res(inputs, expected, 0);

return();
return ();
}
Loading