Skip to content

Commit

Permalink
Merge pull request #215 from Crypto-TII/fix/polynomial_generation_of_…
Browse files Browse the repository at this point in the history
…or_component

Fix/polynomial generation of or component
  • Loading branch information
peacker authored Apr 6, 2024
2 parents cb28bed + de667cd commit 03ebbc5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
47 changes: 38 additions & 9 deletions claasp/components/or_component.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# ****************************************************************************
# Copyright 2023 Technology Innovation Institute
#
Expand Down Expand Up @@ -44,14 +43,43 @@ def algebraic_polynomials(self, model):
sage: or_component = gift.get_component_from_id("or_0_4")
sage: algebraic = AlgebraicModel(gift)
sage: or_component.algebraic_polynomials(algebraic)
[or_0_4_y0 + 1,
or_0_4_y1 + 1,
...
or_0_4_y30 + 1,
or_0_4_y31 + 1]
[or_0_4_x0*or_0_4_x32 + or_0_4_y0 + or_0_4_x32 + or_0_4_x0,
or_0_4_x1*or_0_4_x33 + or_0_4_y1 + or_0_4_x33 + or_0_4_x1,
or_0_4_x2*or_0_4_x34 + or_0_4_y2 + or_0_4_x34 + or_0_4_x2,
or_0_4_x3*or_0_4_x35 + or_0_4_y3 + or_0_4_x35 + or_0_4_x3,
or_0_4_x4*or_0_4_x36 + or_0_4_y4 + or_0_4_x36 + or_0_4_x4,
or_0_4_x5*or_0_4_x37 + or_0_4_y5 + or_0_4_x37 + or_0_4_x5,
or_0_4_x6*or_0_4_x38 + or_0_4_y6 + or_0_4_x38 + or_0_4_x6,
or_0_4_x7*or_0_4_x39 + or_0_4_y7 + or_0_4_x39 + or_0_4_x7,
or_0_4_x8*or_0_4_x40 + or_0_4_y8 + or_0_4_x40 + or_0_4_x8,
or_0_4_x9*or_0_4_x41 + or_0_4_y9 + or_0_4_x41 + or_0_4_x9,
or_0_4_x10*or_0_4_x42 + or_0_4_y10 + or_0_4_x42 + or_0_4_x10,
or_0_4_x11*or_0_4_x43 + or_0_4_y11 + or_0_4_x43 + or_0_4_x11,
or_0_4_x12*or_0_4_x44 + or_0_4_y12 + or_0_4_x44 + or_0_4_x12,
or_0_4_x13*or_0_4_x45 + or_0_4_y13 + or_0_4_x45 + or_0_4_x13,
or_0_4_x14*or_0_4_x46 + or_0_4_y14 + or_0_4_x46 + or_0_4_x14,
or_0_4_x15*or_0_4_x47 + or_0_4_y15 + or_0_4_x47 + or_0_4_x15,
or_0_4_x16*or_0_4_x48 + or_0_4_y16 + or_0_4_x48 + or_0_4_x16,
or_0_4_x17*or_0_4_x49 + or_0_4_y17 + or_0_4_x49 + or_0_4_x17,
or_0_4_x18*or_0_4_x50 + or_0_4_y18 + or_0_4_x50 + or_0_4_x18,
or_0_4_x19*or_0_4_x51 + or_0_4_y19 + or_0_4_x51 + or_0_4_x19,
or_0_4_x20*or_0_4_x52 + or_0_4_y20 + or_0_4_x52 + or_0_4_x20,
or_0_4_x21*or_0_4_x53 + or_0_4_y21 + or_0_4_x53 + or_0_4_x21,
or_0_4_x22*or_0_4_x54 + or_0_4_y22 + or_0_4_x54 + or_0_4_x22,
or_0_4_x23*or_0_4_x55 + or_0_4_y23 + or_0_4_x55 + or_0_4_x23,
or_0_4_x24*or_0_4_x56 + or_0_4_y24 + or_0_4_x56 + or_0_4_x24,
or_0_4_x25*or_0_4_x57 + or_0_4_y25 + or_0_4_x57 + or_0_4_x25,
or_0_4_x26*or_0_4_x58 + or_0_4_y26 + or_0_4_x58 + or_0_4_x26,
or_0_4_x27*or_0_4_x59 + or_0_4_y27 + or_0_4_x59 + or_0_4_x27,
or_0_4_x28*or_0_4_x60 + or_0_4_y28 + or_0_4_x60 + or_0_4_x28,
or_0_4_x29*or_0_4_x61 + or_0_4_y29 + or_0_4_x61 + or_0_4_x29,
or_0_4_x30*or_0_4_x62 + or_0_4_y30 + or_0_4_x62 + or_0_4_x30,
or_0_4_x31*or_0_4_x63 + or_0_4_y31 + or_0_4_x63 + or_0_4_x31]
"""
ninputs = self.input_bit_size
noutputs = self.output_bit_size
ors_number = self.description[1] - 1
word_size = noutputs
ring_R = model.ring()
input_vars = [self.id + "_" + model.input_postfix + str(i) for i in range(ninputs)]
Expand All @@ -61,10 +89,11 @@ def algebraic_polynomials(self, model):
def or_polynomial(x0, x1):
return x0 * x1 + x0 + x1

x = [ring_R.one() for _ in range(noutputs)]
for word_vars in words_vars:
x = [words_vars[0][_] for _ in range(noutputs)]
for or_itr in range(ors_number):
for i in range(noutputs):
x[i] = or_polynomial(x[i], word_vars[i])
x[i] = or_polynomial(x[i], words_vars[or_itr + 1][i])

y = list(map(ring_R, output_vars))

polynomials = [y[i] + x[i] for i in range(noutputs)]
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/components/or_component_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def test_algebraic_polynomials():
algebraic = AlgebraicModel(gift)
algebraic_polynomials = or_component.algebraic_polynomials(algebraic)

assert str(algebraic_polynomials[0]) == "or_0_4_y0 + 1"
assert str(algebraic_polynomials[1]) == "or_0_4_y1 + 1"
assert str(algebraic_polynomials[-2]) == "or_0_4_y30 + 1"
assert str(algebraic_polynomials[-1]) == "or_0_4_y31 + 1"
assert str(algebraic_polynomials[0]) == "or_0_4_x0*or_0_4_x32 + or_0_4_y0 + or_0_4_x32 + or_0_4_x0"
assert str(algebraic_polynomials[1]) == "or_0_4_x1*or_0_4_x33 + or_0_4_y1 + or_0_4_x33 + or_0_4_x1"
assert str(algebraic_polynomials[-2]) == "or_0_4_x30*or_0_4_x62 + or_0_4_y30 + or_0_4_x62 + or_0_4_x30"
assert str(algebraic_polynomials[-1]) == "or_0_4_x31*or_0_4_x63 + or_0_4_y31 + or_0_4_x63 + or_0_4_x31"


def test_cp_constraints():
Expand Down

0 comments on commit 03ebbc5

Please sign in to comment.