Taken from https://hackmd.io/WIobK-QvS3enrzc33PD_2Q?both=, this was written by arnaucube.
R1CS to CCS overview
- R1CS instance:
- CCS instance:
- R1CS-to-CCS parameters:
, ,
Then, we can see that the CCS relation:
in our R1CS-to-CCS parameters is equivalent to
which is equivalent to the R1CS relation:
Sage prototype
The following code implements in Sage the translation of R1CS into CCS by following the description from page 4 of the CCS paper.
To run it:
- need Sage installed
- copy&paste the follwing code into a sage file (eg.
r1cs-to-ccs.sage
)
- and then run it by
> sage r1cs-to-ccs.sage
def matrix_vector_product(M, v):
n = M.nrows()
r = [F(0)] * n
for i in range(0, n):
for j in range(0, M.ncols()):
r[i] += M[i][j] * v[j]
return r
def hadamard_product(a, b):
n = len(a)
r = [None] * n
for i in range(0, n):
r[i] = a[i] * b[i]
return r
def vec_add(a, b):
n = len(a)
r = [None] * n
for i in range(0, n):
r[i] = a[i] + b[i]
return r
def vec_elem_mul(a, s):
r = [None] * len(a)
for i in range(0, len(a)):
r[i] = a[i] * s
return r
F = GF(101)
A = matrix([
[F(0), 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[5, 0, 0, 0, 0, 1],
])
B = matrix([
[F(0), 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0],
])
C = matrix([
[F(0), 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0],
])
print("R1CS matrices:")
print("A:", A)
print("B:", B)
print("C:", C)
z = [F(1), 3, 35, 9, 27, 30]
print("z:", z)
assert len(z) == A.ncols()
n = A.ncols()
m = A.nrows()
Az = matrix_vector_product(A, z)
Bz = matrix_vector_product(B, z)
Cz = matrix_vector_product(C, z)
print("\nR1CS relation check (Az ∘ Bz == Cz):", hadamard_product(Az, Bz) == Cz)
assert hadamard_product(Az, Bz) == Cz
print("\ntranslate R1CS into CCS:")
t=3
q=2
d=2
S1=[0,1]
S2=[2]
S = [S1, S2]
c0=1
c1=-1
c = [c0, c1]
M = [A, B, C]
print("CCS values:")
print("n: %s, m: %s, t: %s, q: %s, d: %s" % (n, m, t, q, d))
print("M:", M)
print("z:", z)
print("S:", S)
print("c:", c)
r = [F(0)] * m
for i in range(0, q):
hadamard_output = [F(1)]*m
for j in S[i]:
hadamard_output = hadamard_product(hadamard_output,
matrix_vector_product(M[j], z))
r = vec_add(r, vec_elem_mul(hadamard_output, c[i]))
print("\nCCS relation check (∑ cᵢ ⋅ ◯ Mⱼ z == 0):", r == [0]*m)
assert r == [0]*m