Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 4 additions & 12 deletions curdleproofs/curdleproofs/curdleproofs.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def new(
compute_MSM(crs.vec_H, vec_r_a_prime),
)

(same_perm_proof, err) = SamePermutationProof.new(
same_perm_proof = SamePermutationProof.new(
crs_G_vec=crs.vec_G,
crs_H_vec=crs.vec_H,
crs_U=crs.H,
Expand All @@ -100,9 +100,6 @@ def new(
transcript=transcript,
)

if same_perm_proof is None:
raise Exception(err)

r_t = Fr(random.randint(1, Fr.field_modulus))
r_u = Fr(random.randint(1, Fr.field_modulus))
R = compute_MSM(vec_R, vec_a)
Expand Down Expand Up @@ -181,14 +178,14 @@ def verify(
vec_T: List[PointProjective],
vec_U: List[PointProjective],
M: PointProjective,
) -> Tuple[bool, str]:
):
ell = len(vec_R)

transcript = CurdleproofsTranscript(b"curdleproofs")
msm_accumulator = MSMAccumulator()

if is_inf(vec_T[0]):
return False, "vec_T[0] is infinity"
raise Exception("vec_T[0] is infinity")

transcript.append_list(
b"curdleproofs_step1", points_projective_to_bytes(vec_R + vec_S + vec_T + vec_U)
Expand Down Expand Up @@ -259,12 +256,7 @@ def verify(
self.S, vec_S, vec_a
)

msm_verify = msm_accumulator.verify()

if not msm_verify:
return False, "MSM check failed"

return True, ""
msm_accumulator.verify()

def to_json(self):
return {
Expand Down
20 changes: 6 additions & 14 deletions curdleproofs/curdleproofs/grand_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
fr_to_bytes,
)
from curdleproofs.curdleproofs_transcript import CurdleproofsTranscript
from typing import List, Optional, Tuple, TypeVar, Type
from typing import List, TypeVar, Type
from curdleproofs.util import (
PointProjective,
Fr,
Expand Down Expand Up @@ -45,7 +45,7 @@ def new(
vec_b: List[Fr],
vec_b_blinders: List[Fr],
transcript: CurdleproofsTranscript,
) -> Tuple[Optional[T_GrandProductProof], Optional[str]]:
) -> T_GrandProductProof:
n_blinders = len(vec_b_blinders)
ell = len(crs_G_vec)

Expand Down Expand Up @@ -115,7 +115,7 @@ def new(
assert eq(compute_MSM(vec_G, vec_c), C)
assert eq(compute_MSM(vec_G_prime, vec_d), D)

(ipa_proof, err) = IPA.new(
ipa_proof = IPA.new(
crs_G_vec=vec_G,
crs_G_prime_vec=vec_G_prime,
crs_H=crs_U,
Expand All @@ -127,10 +127,7 @@ def new(
transcript=transcript,
)

if ipa_proof is None:
return None, err

return cls(C, r_p, ipa_proof), None
return cls(C, r_p, ipa_proof)

def verify(
self,
Expand All @@ -144,7 +141,7 @@ def verify(
n_blinders: int,
transcript: CurdleproofsTranscript,
msm_accumulator: MSMAccumulator,
) -> Tuple[bool, str]:
):
ell = len(crs_G_vec)

# Step 1
Expand Down Expand Up @@ -182,7 +179,7 @@ def verify(
self.r_p * (beta ** (ell + 1)) + gprod_result * (beta**ell) - Fr.one()
)

(ipa_result, err) = self.ipa_proof.verify(
self.ipa_proof.verify(
crs_G_vec=vec_G,
crs_H=crs_U,
C=self.C,
Expand All @@ -193,11 +190,6 @@ def verify(
msm_accumulator=msm_accumulator,
)

if not ipa_result:
return False, err

return True, ""

def to_json(self):
return {
"C": point_projective_to_json(self.C),
Expand Down
29 changes: 11 additions & 18 deletions curdleproofs/curdleproofs/ipa.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
log2_int,
)
from curdleproofs.curdleproofs_transcript import CurdleproofsTranscript
from typing import List, Optional, Tuple, Type, TypeVar
from typing import List, Tuple, Type, TypeVar
from curdleproofs.util import (
PointProjective,
Fr,
Expand Down Expand Up @@ -89,13 +89,13 @@ def new(
vec_c: List[Fr],
vec_d: List[Fr],
transcript: CurdleproofsTranscript,
) -> Tuple[Optional[T_IPA], Optional[str]]:
) -> T_IPA:
n = len(vec_c)
lg_n = int(log2(n))
if n != 2**lg_n:
return (None, "n != 2 ** lg_n, not a power of 2")
raise Exception("n != 2 ** lg_n, not a power of 2")
if n != len(vec_d):
return (None, "len(vec_c) != len(vec_d)")
raise Exception("len(vec_c) != len(vec_d)")

(vec_r_c, vec_r_d) = generate_ipa_blinders(vec_c, vec_d)

Expand Down Expand Up @@ -155,19 +155,16 @@ def new(
crs_G_vec = G_L
crs_G_prime_vec = G_prime_L

return (
cls(B_c, B_d, vec_L_C, vec_R_C, vec_L_D, vec_R_D, vec_c[0], vec_d[0]),
None,
)
return cls(B_c, B_d, vec_L_C, vec_R_C, vec_L_D, vec_R_D, vec_c[0], vec_d[0])

def verification_scalars(
self, n: int, transcript: CurdleproofsTranscript
) -> Tuple[Tuple[List[Fr], List[Fr], List[Fr], List[Fr]], Optional[str]]:
) -> Tuple[List[Fr], List[Fr], List[Fr], List[Fr]]:
lg_n = len(self.vec_L_C)
if lg_n >= 32:
return (([], [], [], []), "vec_L_C too large")
raise Exception("vec_L_C too large")
elif n != 2**lg_n:
return (([], [], [], []), "n != 2 ** lg_n")
raise Exception("n != 2 ** lg_n")

verification_scalars_bitstring = get_verification_scalars_bitstring(n, lg_n)

Expand All @@ -191,7 +188,7 @@ def verification_scalars(

vec_s_inv = [invert(s) for s in vec_s]

return ((challenges, challenges_inv, vec_s, vec_s_inv), None)
return (challenges, challenges_inv, vec_s, vec_s_inv)

def verify(
self,
Expand All @@ -203,7 +200,7 @@ def verify(
vec_u: List[Fr],
transcript: CurdleproofsTranscript,
msm_accumulator: MSMAccumulator,
) -> Tuple[bool, str]:
):
n = len(crs_G_vec)
# assert(((n != 0) and (n & (n-1) == 0)), "n must be a power of 2")

Expand All @@ -217,11 +214,9 @@ def verify(
alpha = transcript.get_and_append_challenge(b"ipa_alpha")
beta = transcript.get_and_append_challenge(b"ipa_beta")

((vec_gamma, vec_gamma_inv, vec_s, vec_s_inv), err) = self.verification_scalars(
(vec_gamma, vec_gamma_inv, vec_s, vec_s_inv) = self.verification_scalars(
n, transcript
)
if err is not None:
return (False, err)

vec_c_times_s = [self.c_final * s for s in vec_s]
vec_rhs_scalars = vec_c_times_s + [self.c_final * self.d_final * beta]
Expand Down Expand Up @@ -251,8 +246,6 @@ def verify(
)
msm_accumulator.accumulate_check(point_lhs, crs_G_vec, vec_d_div_s)

return (True, "")

def to_json(self):
return {
"B_c": point_projective_to_json(self.B_c),
Expand Down
4 changes: 2 additions & 2 deletions curdleproofs/curdleproofs/msm_accumulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def accumulate_check(
base_affine_int
] + random_factor * Fr(scalar)

def verify(self) -> bool:
def verify(self):
bases: List[Tuple[int, int]]
scalars: List[Fr]
bases, scalars = map(list, zip(*self.base_scalar_map.items())) # type: ignore
Expand All @@ -75,4 +75,4 @@ def verify(self) -> bool:
list(map(int, scalars)),
)
# print("bases", bases, "scalars", scalars, "computed", normalize(computed), "expected", normalize(self.A_c), "eq", eq(computed, self.A_c))
return eq(computed, self.A_c)
assert eq(computed, self.A_c)
4 changes: 2 additions & 2 deletions curdleproofs/curdleproofs/opening.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def verify(
k_r_G: PointProjective,
r_G: PointProjective,
k_G: PointProjective,
) -> bool:
):
transcript.append_list(
b"tracker_opening_proof",
points_projective_to_bytes([k_G, G1, k_r_G, r_G, self.A, self.B]),
Expand All @@ -79,7 +79,7 @@ def verify(
Aprime = add(multiply(G1, int(self.s)), multiply(k_G, int(challenge)))
Bprime = add(multiply(r_G, int(self.s)), multiply(k_r_G, int(challenge)))

return eq(Aprime, self.A) and eq(Bprime, self.B)
assert eq(Aprime, self.A) and eq(Bprime, self.B)

def to_json(self):
return {
Expand Down
19 changes: 7 additions & 12 deletions curdleproofs/curdleproofs/same_msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
log2_int,
)
from curdleproofs.curdleproofs_transcript import CurdleproofsTranscript
from typing import List, Optional, Tuple, Type, TypeVar
from typing import List, Tuple, Type, TypeVar
from curdleproofs.util import PointProjective, Fr, invert
from curdleproofs.msm_accumulator import MSMAccumulator, compute_MSM
from py_ecc.optimized_bls12_381.optimized_curve import (
Expand Down Expand Up @@ -148,12 +148,12 @@ def new(

def verification_scalars(
self, n: int, transcript: CurdleproofsTranscript
) -> Tuple[Optional[Tuple[List[Fr], List[Fr], List[Fr]]], str]:
) -> Tuple[List[Fr], List[Fr], List[Fr]]:
lg_n = len(self.vec_L_A)
if lg_n >= 32:
return None, "lg_n >= 32"
raise Exception("lg_n >= 32")
if 2**lg_n != n:
return None, "2**lg_n != n"
raise Exception("2**lg_n != n")

bitstring = get_verification_scalars_bitstring(n, lg_n)

Expand Down Expand Up @@ -182,7 +182,7 @@ def verification_scalars(
for j in bitstring[i]:
vec_s[i] *= challenges[j]

return (challenges, challenges_inv, vec_s), ""
return (challenges, challenges_inv, vec_s)

def verify(
self,
Expand All @@ -194,7 +194,7 @@ def verify(
vec_U: List[PointProjective],
transcript: CurdleproofsTranscript,
msm_accumulator: MSMAccumulator,
) -> Tuple[bool, str]:
):
n = len(vec_T)

transcript.append_list(
Expand All @@ -209,10 +209,7 @@ def verify(
)
alpha = transcript.get_and_append_challenge(b"same_msm_alpha")

(ret, err) = self.verification_scalars(n, transcript)

if ret is None:
return False, err
ret = self.verification_scalars(n, transcript)

vec_gamma, vec_gamma_inv, vec_s = ret

Expand Down Expand Up @@ -240,8 +237,6 @@ def verify(
)
msm_accumulator.accumulate_check(point_lhs, vec_U, vec_x_times_s)

return True, ""

def to_json(self):
return {
"B_a": point_projective_to_json(self.B_a),
Expand Down
20 changes: 6 additions & 14 deletions curdleproofs/curdleproofs/same_perm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import reduce
from curdleproofs.grand_prod import GrandProductProof
from curdleproofs.curdleproofs_transcript import CurdleproofsTranscript
from typing import List, Optional, Tuple, Type, TypeVar
from typing import List, Type, TypeVar
from curdleproofs.util import (
PointProjective,
Fr,
Expand Down Expand Up @@ -42,7 +42,7 @@ def new(
vec_a_blinders: List[Fr],
vec_m_blinders: List[Fr],
transcript: CurdleproofsTranscript,
) -> Tuple[Optional[T_SAME_PERM_PROOF], str]:
) -> T_SAME_PERM_PROOF:
n_blinders = len(vec_a_blinders)
ell = len(crs_G_vec)

Expand All @@ -66,7 +66,7 @@ def new(
vec_a_blinders[i] + alpha * vec_m_blinders[i] for i in range(0, n_blinders)
]

(grand_product_proof, err) = GrandProductProof.new(
grand_product_proof = GrandProductProof.new(
crs_G_vec=crs_G_vec,
crs_H_vec=crs_H_vec,
crs_U=crs_U,
Expand All @@ -77,10 +77,7 @@ def new(
transcript=transcript,
)

if grand_product_proof is None:
return (None, err or "")

return cls(B, grand_product_proof), ""
return cls(B, grand_product_proof)

def verify(
self,
Expand All @@ -95,7 +92,7 @@ def verify(
n_blinders: int,
transcript: CurdleproofsTranscript,
msm_accumulator: MSMAccumulator,
) -> Tuple[bool, str]:
):
ell = len(crs_G_vec)

# Step 1
Expand All @@ -117,7 +114,7 @@ def verify(
vec_beta_repeated,
)

(grand_prod_verify, err) = self.grand_prod_proof.verify(
self.grand_prod_proof.verify(
crs_G_vec=crs_G_vec,
crs_H_vec=crs_H_vec,
crs_U=crs_U,
Expand All @@ -130,11 +127,6 @@ def verify(
msm_accumulator=msm_accumulator,
)

if not grand_prod_verify:
return (False, err)

return (True, "")

def to_json(self):
return {
"B": point_projective_to_json(self.B),
Expand Down
Loading