Skip to content

Commit

Permalink
update to cairo-lang 0.13.2a0 and add fustat tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Jul 29, 2024
1 parent 9fcaa04 commit 84c8ccb
Show file tree
Hide file tree
Showing 32 changed files with 337,642 additions and 371,807 deletions.
26 changes: 17 additions & 9 deletions .github/workflows/fustat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: 3.10.14
- name: Install dependencies
run: |
python -m venv venv
source venv/bin/activate
echo 'export PYTHONPATH="$PWD:$PYTHONPATH"' >> venv/bin/activate
pip install cairo-lang==0.13.2a0
- name: Install GNU Parallel
run: sudo apt-get update && sudo apt-get install -y parallel
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '^1.17'
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: rustfmt, clippy

- name: Setup repo
run: make setup
- name: Check cairo Formatting
run: |
source venv/bin/activate && ./tools/make/fustat_format_check.sh
- name: Compile cairo files
run: source venv/bin/activate && make build
- name: Run fustat programs
run: |
source venv/bin/activate && make build
source venv/bin/activate
pytest tests/fustat_programs/test_fustat.py -n auto -s
17 changes: 11 additions & 6 deletions hydra/extension_field_modulo_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ModuloCircuit,
ModuloCircuitElement,
WriteOps,
BATCH_SIZE,
)
from hydra.poseidon_transcript import CairoPoseidonTranscript

Expand Down Expand Up @@ -122,7 +123,7 @@ def _init_accumulator(self, extension_degree: int = None):
else:
return EuclideanPolyAccumulator(
lhs=self.set_or_get_constant(0),
R=[self.set_or_get_constant(0)] * extension_degree,
R=[None] * extension_degree,
R_evaluated=self.set_or_get_constant(0),
)

Expand Down Expand Up @@ -463,9 +464,9 @@ def update_LHS_state(
# Update LHS
LHS = self.mul(LHS, LHS_current_eval)

ci_XY_of_z = self.mul(s1, LHS)
ci_XY_of_z = self.mul(s1, LHS, "ci_XY_of_z")

LHS_acc = self.add(self.acc[acc_index].lhs, ci_XY_of_z)
LHS_acc = self.add(self.acc[acc_index].lhs, ci_XY_of_z, "LHS_acc")

# Update LHS only.
self.acc[acc_index] = EuclideanPolyAccumulator(
Expand Down Expand Up @@ -779,10 +780,14 @@ def compile_circuit_cairo_zero(

elif dw_array_name in ["add_offsets_ptr", "mul_offsets_ptr"]:
num_instructions = len(dw_values)
instructions_needed = (8 - (num_instructions % 8)) % 8
for left, right, result in dw_values:
instructions_needed = (
BATCH_SIZE - (num_instructions % BATCH_SIZE)
) % BATCH_SIZE
for left, right, result, comment in dw_values:
code += (
f"\t dw {left};\n" + f"\t dw {right};\n" + f"\t dw {result};\n"
f"\t dw {left}; // {comment}\n"
+ f"\t dw {right};\n"
+ f"\t dw {result};\n"
)
if instructions_needed > 0:
first_triplet = dw_values[0]
Expand Down
23 changes: 18 additions & 5 deletions hydra/modulo_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from hydra.modulo_circuit_structs import Cairo1SerializableStruct


BATCH_SIZE = 1


class WriteOps(Enum):
"""
Enum for the source of a write operation in the value segment.
Expand Down Expand Up @@ -232,6 +235,7 @@ def get_dw_lookups(self) -> dict:
item.instruction.left_offset,
item.instruction.right_offset,
item.instruction.result_offset,
item.instruction.comment,
)
)
for assert_eq_instruction in self.assert_eq_instructions:
Expand All @@ -242,6 +246,7 @@ def get_dw_lookups(self) -> dict:
assert_eq_instruction.left_offset,
assert_eq_instruction.right_offset,
assert_eq_instruction.result_offset,
assert_eq_instruction.comment,
)
)
for output_elmt in self.output:
Expand Down Expand Up @@ -269,7 +274,9 @@ def print(self):
else:
row += f"{'_':^9}|"
row += f"{val.write_source.name:^8}|"
row += f"\t{RED}{val.emulated_felt.value}{RESET}"
import numpy as np

row += f"\t{RED}{np.base_repr(val.emulated_felt.value, base=36)}{RESET} {val.instruction.comment if val.instruction else ''}"

print(row)

Expand Down Expand Up @@ -494,6 +501,10 @@ def mul(
b: ModuloCircuitElement,
comment: str | None = None,
) -> ModuloCircuitElement:
if a is None and type(b) == ModuloCircuitElement:
return self.set_or_get_constant(0)
elif b is None and type(a) == ModuloCircuitElement:
return self.set_or_get_constant(0)
assert (
type(a) == type(b) == ModuloCircuitElement
), f"Expected ModuloElement, got {type(a)}, {a} and {type(b)}, {b}"
Expand Down Expand Up @@ -802,11 +813,13 @@ def compile_circuit_cairo_zero(
elif dw_array_name in ["add_offsets_ptr", "mul_offsets_ptr"]:
num_instructions = len(dw_values)
instructions_needed = (
8 - (num_instructions % 8)
) % 8 # Must be a multiple of 8 (currently)
for left, right, result in dw_values:
BATCH_SIZE - (num_instructions % BATCH_SIZE)
) % BATCH_SIZE # Must be a multiple of 8 (currently)
for left, right, result, comment in dw_values:
code += (
f"\t dw {left};\n" + f"\t dw {right};\n" + f"\t dw {result};\n"
f"\t dw {left}; // {comment}\n"
+ f"\t dw {right};\n"
+ f"\t dw {result};\n"
)
if instructions_needed > 0:
first_triplet = dw_values[0]
Expand Down
30 changes: 15 additions & 15 deletions hydra/precompiled_circuits/all_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -2413,21 +2413,21 @@ def _run_circuit_inner(self, input: list[PyFelt]) -> ExtensionFieldModuloCircuit
"params": None,
"filename": "extf_mul",
},
# CircuitID.FINAL_EXP_PART_1: {
# "class": FinalExpPart1Circuit,
# "params": None,
# "filename": "final_exp",
# },
# CircuitID.FINAL_EXP_PART_2: {
# "class": FinalExpPart2Circuit,
# "params": None,
# "filename": "final_exp",
# },
# CircuitID.MULTI_MILLER_LOOP: {
# "class": MultiMillerLoop,
# "params": [{"n_pairs": k} for k in [1, 2, 3]],
# "filename": "multi_miller_loop",
# },
CircuitID.FINAL_EXP_PART_1: {
"class": FinalExpPart1Circuit,
"params": None,
"filename": "final_exp",
},
CircuitID.FINAL_EXP_PART_2: {
"class": FinalExpPart2Circuit,
"params": None,
"filename": "final_exp",
},
CircuitID.MULTI_MILLER_LOOP: {
"class": MultiMillerLoop,
"params": [{"n_pairs": k} for k in [1, 2, 3]],
"filename": "multi_miller_loop",
},
CircuitID.MULTI_PAIRING_CHECK: {
"class": MultiPairingCheck,
"params": [{"n_pairs": k} for k in [2, 3]],
Expand Down
9 changes: 3 additions & 6 deletions src/fustat/ec_ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,12 @@ func msm{
points = list(zip(points[0::2], points[1::2]))
scalars = pack_felt_ptr(memory, ids.scalars._reference_value, 2*ids.n)
scalars_low, scalars_high = scalars[0::2], scalars[1::2]
dss_low = construct_digit_vectors(scalars_low)
dss_high = construct_digit_vectors(scalars_high)
dss_shifted = construct_digit_vectors([2**128])
print(f"\nComputing MSM with {ids.n} input points!")
t0=time.time()
print(f"Deriving the Sums of logarithmic derivatives of elliptic curve Diviors interpolating the {ids.n} input points with multiplicities...")
Q_low, SumDlogDivLow = cli.ecip_functions(points, dss_low)
Q_high, SumDlogDivHigh = cli.ecip_functions(points, dss_high)
Q_high_shifted, SumDlogDivShifted = cli.ecip_functions([Q_high], dss_shifted)
Q_low, SumDlogDivLow = cli.ecip_functions(points, scalars_low)
Q_high, SumDlogDivHigh = cli.ecip_functions(points, scalars_high)
Q_high_shifted, SumDlogDivShifted = cli.ecip_functions([Q_high], [2**128])
print(f"Time taken: {time.time() - t0}s")
print(f"Filling Function Field elements and results point")
Expand Down
4 changes: 2 additions & 2 deletions src/fustat/modulo_circuit.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ func run_extension_field_modulo_circuit{
tempvar range_check96_ptr = range_check96_ptr + circuit.constants_ptr_len * N_LIMBS +
circuit.input_len + circuit.commitments_len + circuit.witnesses_len;

write_felts_to_value_segment(values_start=&Z, n=1);
write_felts_to_value_segment(values_start=RLC_coeffs, n=circuit.N_Euclidean_equations);
write_felts_to_value_segment(values_start=&Z, n=1);
%{ print(f"\tZ and felt252 written to value segment") %}
%{ print(f"\tRunning ModuloBuiltin circuit...") %}
run_mod_p_circuit(
Expand Down Expand Up @@ -260,8 +260,8 @@ func run_extension_field_modulo_circuit_continuation{
tempvar range_check96_ptr = range_check96_ptr + circuit.constants_ptr_len * N_LIMBS +
circuit.input_len + circuit.commitments_len + circuit.witnesses_len;

write_felts_to_value_segment(values_start=&Z, n=1);
write_felts_to_value_segment(values_start=RLC_coeffs, n=circuit.N_Euclidean_equations);
write_felts_to_value_segment(values_start=&Z, n=1);
%{ print(f"\tZ and felt252 written to value segment") %}
%{ print(f"\tRunning ModuloBuiltin circuit...") %}
run_mod_p_circuit(
Expand Down
42 changes: 6 additions & 36 deletions src/fustat/precompiled_circuits/dummy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,56 +44,26 @@ func get_DUMMY_circuit(curve_id: felt) -> (circuit: ModuloCircuit*) {
constants_ptr_loc:
add_offsets_ptr_loc:
dw 4;
dw 4; // None
dw 8;
dw 0;
dw 8;
dw 8; // None
dw 12;
dw 16;
dw 12;
dw 12; // None
dw 20;
dw 8;
dw 4;
dw 8;
dw 0;
dw 4;
dw 8;
dw 0;
dw 4;
dw 8;
dw 0;
dw 4;
dw 8;
dw 0;
dw 4;
dw 8;
dw 0;
mul_offsets_ptr_loc:
dw 4;
dw 4; // None
dw 12;
dw 0;
dw 8;
dw 8; // None
dw 12;
dw 24;
dw 12;
dw 12; // None
dw 28;
dw 8;
dw 4;
dw 12;
dw 0;
dw 4;
dw 12;
dw 0;
dw 4;
dw 12;
dw 0;
dw 4;
dw 12;
dw 0;
dw 4;
dw 12;
dw 0;
output_offsets_ptr_loc:
dw 8;
Expand Down
Loading

0 comments on commit 84c8ccb

Please sign in to comment.