Skip to content

Commit

Permalink
add bit_00 support, bn pairing, and full tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Jul 23, 2024
1 parent 38bc870 commit 8645370
Show file tree
Hide file tree
Showing 10 changed files with 33,299 additions and 14,737 deletions.
16 changes: 16 additions & 0 deletions hydra/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,22 @@ def precompute_lineline_sparsity(curve_id: int):
return ll_sparsity[0:12]


def replace_consecutive_zeros(lst):
result = []
i = 0
while i < len(lst):
if i < len(lst) - 1 and lst[i] == 0 and lst[i + 1] == 0:
result.append(3) # Replace consecutive zeros with 3
i += 2
elif lst[i] == -1:
result.append(2) # Replace -1 with 2
i += 1
else:
result.append(lst[i])
i += 1
return result


if __name__ == "__main__":
from tools.extension_trick import v_to_gnark, gnark_to_v, w_to_gnark, gnark_to_w
from random import randint
Expand Down
9 changes: 6 additions & 3 deletions hydra/extension_field_modulo_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ def update_LHS_state(
assert len(Ps_sparsities) == len(
Ps
), f"len(Ps_sparsities)={len(Ps_sparsities)} != len(Ps)={len(Ps)}"

for i, sparsity in enumerate(Ps_sparsities):
if sparsity:
assert all(
Expand All @@ -451,8 +450,12 @@ def update_LHS_state(
self.accumulate_poly_instructions[acc_index].Pis_of_Z[-1].append(LHS)
for i in range(1, len(Ps)):
if Ps[i - 1] == Ps[i]:
# Consecutives elements are the same : Squaring
LHS_current_eval = LHS_current_eval
# Consecutives elements are the same : retrieve previous evaluation.
LHS_current_eval = self.accumulate_poly_instructions[
acc_index
].Pis_of_Z[-1][-1]

# Todo : support smarter analysis to save a few muls

else:
LHS_current_eval = self.eval_poly_in_precomputed_Z(
Expand Down
14 changes: 9 additions & 5 deletions hydra/modulo_circuit_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def extract_from_circuit_output(
code += "};"
return code

def serialize(self, raw: bool = False) -> str:
def serialize(self, raw: bool = False, is_option: bool = False) -> str:
if self.elmts is None:
raw_struct = "Option::None"
if raw:
Expand All @@ -359,10 +359,12 @@ def serialize(self, raw: bool = False) -> str:
else:
assert len(self.elmts) == 12
raw_struct = f"{self.__class__.__name__}{{{','.join([f'w{i}: {int_to_u384(self.elmts[i].value)}' for i in range(len(self))])}}}"
if is_option:
raw_struct = f"Option::Some({raw_struct})"
if raw:
return raw_struct
else:
return f"let {self.name}:{self.__class__.__name__} = {raw_struct};\n"
return f"let {self.name} = {raw_struct};\n"

def dump_to_circuit_input(self) -> str:
code = ""
Expand Down Expand Up @@ -393,20 +395,22 @@ def extract_from_circuit_output(
code += "};"
return code

def serialize(self, raw: bool = False) -> str:
def serialize(self, raw: bool = False, is_option: bool = False) -> str:
if self.elmts is None:
raw_struct = "Option::None"
if raw:
return raw_struct
else:
return f"let {self.name}:Option<{self.__class__.__name__}> = {raw_struct};\n"
else:
assert len(self.elmts) == 11
assert len(self.elmts) == 11, f"Expected 11 elements, got {len(self.elmts)}"
raw_struct = f"{self.__class__.__name__}{{{','.join([f'w{i}: {int_to_u384(self.elmts[i].value)}' for i in range(len(self))])}}}"
if is_option:
raw_struct = f"Option::Some({raw_struct})"
if raw:
return raw_struct
else:
return f"let {self.name}:{self.__class__.__name__} = {raw_struct};\n"
return f"let {self.name} = {raw_struct};\n"

def dump_to_circuit_input(self) -> str:
code = ""
Expand Down
Loading

0 comments on commit 8645370

Please sign in to comment.