From d9c7e2b46df8a939b7826cdc2e4560fb68853677 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 11 Jan 2025 13:51:02 +0000 Subject: [PATCH 01/60] custom_sfix using rabbit --- Compiler/types.py | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/Compiler/types.py b/Compiler/types.py index 5caa21051..6aa49d665 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5107,6 +5107,148 @@ def update(self, other): assert self.m == other.m self.v.update(other.v) + +class custom_sfix(sfix): + + def _RabbitLTB(self, R, x, k): + """ + res = R c) + w3_bits = cbits.bit_decompose_clear(w[3], 64) + + movs(s, sint.conv(w[1] ^ w[2] ^ w3_bits[0])) + + + def rabbitLTS(self, a, b): + # try: + # print("!!! in rabbitLTS, other=", other.v) + # except: + # print("rabbitLTS: print 1 failed") + + res = sint() + self.rabbitLTC(res, a - b, 0, program.bit_length) + return res + + + # These are based on the implementation + # self.rabbitLTS(a, b) = rabbitLTC(a-b, 0) + + def __lt__(self, other): + print("!!! in __le__, calling rabbitLTS") + a = self.v + b = other.v + result = self.rabbitLTS(a, b) + return result + + + def __le__(self, other): + print("!!! in __le__, calling rabbitLTS") + a = self.v + b = other.v + result = not self.rabbitLTS(b, a) + return result + + + def __gt__(self, other): + print("!!! in __gt__, calling rabbitLTS") + a = self.v + b = other.v + result = self.rabbitLTS(b, a) + return result + + + def __ge__(self, other): + print("!!! in __ge__, calling rabbitLTS") + a = self.v + b = other.v + result = not self.rabbitLTS(b, a) + return result + + + def __eq__(self, other): + print("!!! in __eq__, calling rabbitLTS") + a = self.v + b = other.v + result = (not self.rabbitLTS(a, b)) and (not self.rabbitLTS(b, a)) + return result + + + def __ne__(self, other): + print("!!! in __ne__, calling rabbitLTS") + a = self.v + b = other.v + result = not (not self.rabbitLTS(a, b)) and (not self.rabbitLTS(b, a)) + return result + + sfix.unreduced_type = unreduced_sfix sfix.set_precision(16, 31) From b84f89c7ac6f7f798c26af4e9009951426d31bcf Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 11 Jan 2025 15:41:38 +0000 Subject: [PATCH 02/60] replace not with 1 - --- Compiler/types.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 6aa49d665..a89e93901 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5119,7 +5119,7 @@ def _RabbitLTB(self, R, x, k): """ from .GC.types import sbit, cbits - print("!!! in _RabbitLT") + print("!!! in _RabbitLTB") R_bits = cbits.bit_decompose_clear(R, 64) y = [sbit() for i in range(k)] z = [sbit() for i in range(k)] @@ -5213,7 +5213,7 @@ def __le__(self, other): print("!!! in __le__, calling rabbitLTS") a = self.v b = other.v - result = not self.rabbitLTS(b, a) + result = 1 - self.rabbitLTS(b, a) return result @@ -5229,7 +5229,7 @@ def __ge__(self, other): print("!!! in __ge__, calling rabbitLTS") a = self.v b = other.v - result = not self.rabbitLTS(b, a) + result = 1 - self.rabbitLTS(b, a) return result @@ -5237,7 +5237,7 @@ def __eq__(self, other): print("!!! in __eq__, calling rabbitLTS") a = self.v b = other.v - result = (not self.rabbitLTS(a, b)) and (not self.rabbitLTS(b, a)) + result = (1 - self.rabbitLTS(a, b)) and (1 - self.rabbitLTS(b, a)) return result @@ -5245,7 +5245,7 @@ def __ne__(self, other): print("!!! in __ne__, calling rabbitLTS") a = self.v b = other.v - result = not (not self.rabbitLTS(a, b)) and (not self.rabbitLTS(b, a)) + result = 1 - (1 - self.rabbitLTS(a, b)) and (1 - self.rabbitLTS(b, a)) return result From d948fbcc02896b3f5492def9437996f6f0d8895b Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 11 Jan 2025 16:12:03 +0000 Subject: [PATCH 03/60] replace and with * --- Compiler/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index a89e93901..43fb636c2 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5237,7 +5237,7 @@ def __eq__(self, other): print("!!! in __eq__, calling rabbitLTS") a = self.v b = other.v - result = (1 - self.rabbitLTS(a, b)) and (1 - self.rabbitLTS(b, a)) + result = (1 - self.rabbitLTS(a, b)) * (1 - self.rabbitLTS(b, a)) return result @@ -5245,7 +5245,7 @@ def __ne__(self, other): print("!!! in __ne__, calling rabbitLTS") a = self.v b = other.v - result = 1 - (1 - self.rabbitLTS(a, b)) and (1 - self.rabbitLTS(b, a)) + result = 1 - (1 - self.rabbitLTS(a, b)) * (1 - self.rabbitLTS(b, a)) return result From c9eda84a86a078a42f3ec8449ca2a4a2dd113922 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 1 Feb 2025 20:10:53 +0200 Subject: [PATCH 04/60] adding fixes after fixes --- Compiler/types.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 43fb636c2..34a167734 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5181,12 +5181,16 @@ def rabbitLTC(self, s, a, c, BIT_SIZE = 64): w[2] = self._RabbitLTB(masked_b, r_bits, BIT_SIZE) print("!!! in rabbitLTC. w1=%s, w2=%s", w[1].reveal(), w[2].reveal()) - w[3] = cint(masked_b > c) + w[3] = cint(masked_b < (M - R)) w3_bits = cbits.bit_decompose_clear(w[3], 64) movs(s, sint.conv(w[1] ^ w[2] ^ w3_bits[0])) + def rabbitLTS_fix(self, a, b): + return 1 - self.rabbitLTS(b, a) + + def rabbitLTS(self, a, b): # try: # print("!!! in rabbitLTS, other=", other.v) @@ -5205,7 +5209,7 @@ def __lt__(self, other): print("!!! in __le__, calling rabbitLTS") a = self.v b = other.v - result = self.rabbitLTS(a, b) + result = self.rabbitLTS_fix(a, b) return result @@ -5213,7 +5217,7 @@ def __le__(self, other): print("!!! in __le__, calling rabbitLTS") a = self.v b = other.v - result = 1 - self.rabbitLTS(b, a) + result = 1 - self.rabbitLTS_fix(b, a) return result @@ -5221,7 +5225,7 @@ def __gt__(self, other): print("!!! in __gt__, calling rabbitLTS") a = self.v b = other.v - result = self.rabbitLTS(b, a) + result = self.rabbitLTS_fix(b, a) return result @@ -5229,7 +5233,7 @@ def __ge__(self, other): print("!!! in __ge__, calling rabbitLTS") a = self.v b = other.v - result = 1 - self.rabbitLTS(b, a) + result = 1 - self.rabbitLTS_fix(b, a) return result @@ -5237,7 +5241,7 @@ def __eq__(self, other): print("!!! in __eq__, calling rabbitLTS") a = self.v b = other.v - result = (1 - self.rabbitLTS(a, b)) * (1 - self.rabbitLTS(b, a)) + result = (1 - self.rabbitLTS_fix(a, b)) * (1 - self.rabbitLTS_fix(b, a)) return result @@ -5245,7 +5249,7 @@ def __ne__(self, other): print("!!! in __ne__, calling rabbitLTS") a = self.v b = other.v - result = 1 - (1 - self.rabbitLTS(a, b)) * (1 - self.rabbitLTS(b, a)) + result = 1 - (1 - self.rabbitLTS_fix(a, b)) * (1 - self.rabbitLTS_fix(b, a)) return result From 00fef6a843dca5657be799ffbcdfdb124f70a336 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 1 Feb 2025 20:41:27 +0200 Subject: [PATCH 05/60] try to fix it --- Compiler/types.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 34a167734..bcbf81a90 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5152,7 +5152,7 @@ def and_op(x, y, z=None): return total - def rabbitLTC(self, s, a, c, BIT_SIZE = 64): + def rabbitLTC(self, a, c, BIT_SIZE = 64): """ s = (c ?< a) @@ -5171,7 +5171,7 @@ def rabbitLTC(self, s, a, c, BIT_SIZE = 64): r, r_bits = sint.get_edabit(length_eda, True) masked_a = (a + r).reveal() - masked_b = masked_a + M - R + masked_b = masked_a + M - R # mod M print("!!! in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, c=%s", M, R, masked_a, masked_b, c) @@ -5182,9 +5182,10 @@ def rabbitLTC(self, s, a, c, BIT_SIZE = 64): print("!!! in rabbitLTC. w1=%s, w2=%s", w[1].reveal(), w[2].reveal()) w[3] = cint(masked_b < (M - R)) - w3_bits = cbits.bit_decompose_clear(w[3], 64) + #w3_bits = cbits.bit_decompose_clear(w[3], 64) - movs(s, sint.conv(w[1] ^ w[2] ^ w3_bits[0])) + return 1 - (w[1] - w[2] + w[3]) + #movs(s, sint.conv(w[1] ^ w[2] ^ w3_bits[0])) def rabbitLTS_fix(self, a, b): @@ -5197,8 +5198,7 @@ def rabbitLTS(self, a, b): # except: # print("rabbitLTS: print 1 failed") - res = sint() - self.rabbitLTC(res, a - b, 0, program.bit_length) + res = self.rabbitLTC(a - b, 0, program.bit_length) return res @@ -5233,7 +5233,7 @@ def __ge__(self, other): print("!!! in __ge__, calling rabbitLTS") a = self.v b = other.v - result = 1 - self.rabbitLTS_fix(b, a) + result = 1 - self.rabbitLTS_fix(a, b) return result From bf3aa3150c04ecacac3a7878745a5dc013d5e87a Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 1 Feb 2025 21:00:53 +0200 Subject: [PATCH 06/60] add modulo --- Compiler/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/types.py b/Compiler/types.py index bcbf81a90..1e4e94688 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5171,7 +5171,7 @@ def rabbitLTC(self, a, c, BIT_SIZE = 64): r, r_bits = sint.get_edabit(length_eda, True) masked_a = (a + r).reveal() - masked_b = masked_a + M - R # mod M + masked_b = (masked_a + M - R) % M print("!!! in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, c=%s", M, R, masked_a, masked_b, c) From 293b0daf21eb18ff7b17ec25e2a53e0a03945736 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 1 Feb 2025 21:06:17 +0200 Subject: [PATCH 07/60] add print_ln --- Compiler/types.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 1e4e94688..0d313af91 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5119,7 +5119,7 @@ def _RabbitLTB(self, R, x, k): """ from .GC.types import sbit, cbits - print("!!! in _RabbitLTB") + library.print_ln("!!! in _RabbitLTB") R_bits = cbits.bit_decompose_clear(R, 64) y = [sbit() for i in range(k)] z = [sbit() for i in range(k)] @@ -5173,14 +5173,14 @@ def rabbitLTC(self, a, c, BIT_SIZE = 64): masked_a = (a + r).reveal() masked_b = (masked_a + M - R) % M - print("!!! in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, c=%s", M, R, masked_a, masked_b, c) + library.print_ln("!!! in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, c=%s", M, R, masked_a, masked_b, c) w = [None, None, None, None] w[1] = self._RabbitLTB(masked_a, r_bits, BIT_SIZE) w[2] = self._RabbitLTB(masked_b, r_bits, BIT_SIZE) - print("!!! in rabbitLTC. w1=%s, w2=%s", w[1].reveal(), w[2].reveal()) + library.print_ln("!!! in rabbitLTC. w1=%s, w2=%s", w[1].reveal(), w[2].reveal()) w[3] = cint(masked_b < (M - R)) #w3_bits = cbits.bit_decompose_clear(w[3], 64) @@ -5206,7 +5206,7 @@ def rabbitLTS(self, a, b): # self.rabbitLTS(a, b) = rabbitLTC(a-b, 0) def __lt__(self, other): - print("!!! in __le__, calling rabbitLTS") + library.print_ln("!!! in __le__, calling rabbitLTS") a = self.v b = other.v result = self.rabbitLTS_fix(a, b) @@ -5214,7 +5214,7 @@ def __lt__(self, other): def __le__(self, other): - print("!!! in __le__, calling rabbitLTS") + library.print_ln("!!! in __le__, calling rabbitLTS") a = self.v b = other.v result = 1 - self.rabbitLTS_fix(b, a) @@ -5222,7 +5222,7 @@ def __le__(self, other): def __gt__(self, other): - print("!!! in __gt__, calling rabbitLTS") + library.print_ln("!!! in __gt__, calling rabbitLTS") a = self.v b = other.v result = self.rabbitLTS_fix(b, a) @@ -5230,7 +5230,7 @@ def __gt__(self, other): def __ge__(self, other): - print("!!! in __ge__, calling rabbitLTS") + library.print_ln("!!! in __ge__, calling rabbitLTS") a = self.v b = other.v result = 1 - self.rabbitLTS_fix(a, b) @@ -5238,7 +5238,7 @@ def __ge__(self, other): def __eq__(self, other): - print("!!! in __eq__, calling rabbitLTS") + library.print_ln("!!! in __eq__, calling rabbitLTS") a = self.v b = other.v result = (1 - self.rabbitLTS_fix(a, b)) * (1 - self.rabbitLTS_fix(b, a)) @@ -5246,7 +5246,7 @@ def __eq__(self, other): def __ne__(self, other): - print("!!! in __ne__, calling rabbitLTS") + library.print_ln("!!! in __ne__, calling rabbitLTS") a = self.v b = other.v result = 1 - (1 - self.rabbitLTS_fix(a, b)) * (1 - self.rabbitLTS_fix(b, a)) From 76f017f7345b2de61bb20eacd67977afd33a2cc1 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 11:45:02 +0200 Subject: [PATCH 08/60] new day, new attempt --- Compiler/types.py | 50 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 0d313af91..a214c1d8b 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5110,7 +5110,15 @@ def update(self, other): class custom_sfix(sfix): - def _RabbitLTB(self, R, x, k): + def LTBits(R, x, BIT_SIZE): + R_bits = cint.bit_decompose(R, BIT_SIZE) + y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] + z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] + return sum((1-R_bits[i]) & w[i] for i in range(BIT_SIZE)) + + + def Dragos_RabbitLTB(self, R, x, k): """ res = R Date: Sun, 2 Feb 2025 12:05:00 +0200 Subject: [PATCH 09/60] add self --- Compiler/types.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Compiler/types.py b/Compiler/types.py index a214c1d8b..d53603cca 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5110,7 +5110,8 @@ def update(self, other): class custom_sfix(sfix): - def LTBits(R, x, BIT_SIZE): + # R: clear-text; x: edabit in binary format + def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] From 56159b9004c7d0cf48c0a995258768c69875a7f1 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 12:46:56 +0200 Subject: [PATCH 10/60] debug --- Compiler/types.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index d53603cca..2b2cb650b 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5161,7 +5161,7 @@ def and_op(x, y, z=None): return total - def rabbitLTC(self, a, c, BIT_SIZE = 64): + def rabbitLTC(self, a, c, BIT_SIZE = 32): """ s = (c ?< a) @@ -5175,22 +5175,23 @@ def rabbitLTC(self, a, c, BIT_SIZE = 64): from .GC.types import cbits length_eda = BIT_SIZE - M = P_VALUES[64] + M = P_VALUES[32] R = (M - 1) // 2 r, r_bits = sint.get_edabit(length_eda, True) masked_a = (a + r).reveal() masked_b = masked_a + M - R - library.print_ln("[DEBUG CARMEN] in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, c=%s", M, R, masked_a, masked_b, c) + library.print_ln("[DEBUG CARMEN] in rabbitLTC comparing a=%s < c=%s", a, c) + library.print_ln("[DEBUG CARMEN] in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, eda_bit=", M, R, masked_a, masked_b, r.reveal()) w = [None, None, None, None] - library.print_ln("[DEBUG CARMEN]: comparing R=%s < x=%s", masked_a, r.reveal()) + library.print_ln("[DEBUG CARMEN]: comparing masked_a=%s < edabit=%s", masked_a, r.reveal()) w[1] = self.LTBits(masked_a, r_bits, BIT_SIZE) library.print_ln("[DEBUG CARMEN]: result of comparison = %s", w[1].reveal()) - library.print_ln("[DEBUG CARMEN]: comparing R=%s < x=%s", masked_b, r.reveal()) + library.print_ln("[DEBUG CARMEN]: comparing masked_b=%s < edabit=%s", masked_b, r.reveal()) w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) library.print_ln("[DEBUG CARMEN]: result of comparison = %s", w[2].reveal()) From 79eb954982bc9faccdb500b748b9b853d7b92cb6 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 12:58:46 +0200 Subject: [PATCH 11/60] add reveal --- Compiler/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/types.py b/Compiler/types.py index 2b2cb650b..3280336a8 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5182,7 +5182,7 @@ def rabbitLTC(self, a, c, BIT_SIZE = 32): masked_a = (a + r).reveal() masked_b = masked_a + M - R - library.print_ln("[DEBUG CARMEN] in rabbitLTC comparing a=%s < c=%s", a, c) + library.print_ln("[DEBUG CARMEN] in rabbitLTC comparing a=%s < c=%s", a.reveal(), c) library.print_ln("[DEBUG CARMEN] in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, eda_bit=", M, R, masked_a, masked_b, r.reveal()) w = [None, None, None, None] From d6a6180b3583030fbe0ba5dfb0a82fde0e57ce3c Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 17:04:44 +0200 Subject: [PATCH 12/60] fix printing --- Compiler/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/types.py b/Compiler/types.py index 3280336a8..535c72ba0 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5183,7 +5183,7 @@ def rabbitLTC(self, a, c, BIT_SIZE = 32): masked_b = masked_a + M - R library.print_ln("[DEBUG CARMEN] in rabbitLTC comparing a=%s < c=%s", a.reveal(), c) - library.print_ln("[DEBUG CARMEN] in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, eda_bit=", M, R, masked_a, masked_b, r.reveal()) + library.print_ln("[DEBUG CARMEN] in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, eda_bit=%s", M, R, masked_a, masked_b, r.reveal()) w = [None, None, None, None] From 6f2509d926ac241f75f193598b9e14829f83c3be Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 17:20:28 +0200 Subject: [PATCH 13/60] p of 64 bits --- Compiler/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 535c72ba0..9412a094d 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5116,7 +5116,7 @@ def LTBits(self, R, x, BIT_SIZE): y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - return sum((1-R_bits[i]) & w[i] for i in range(BIT_SIZE)) + return 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) def Dragos_RabbitLTB(self, R, x, k): @@ -5175,7 +5175,7 @@ def rabbitLTC(self, a, c, BIT_SIZE = 32): from .GC.types import cbits length_eda = BIT_SIZE - M = P_VALUES[32] + M = P_VALUES[64] R = (M - 1) // 2 r, r_bits = sint.get_edabit(length_eda, True) From d93a8379a1b25d9187fbff8f1b80d297352535da Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 18:14:45 +0200 Subject: [PATCH 14/60] infinite debugging --- Compiler/library.py | 19 +++++++++++++++++++ Compiler/types.py | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Compiler/library.py b/Compiler/library.py index 5f7e42e83..d695ee8d2 100644 --- a/Compiler/library.py +++ b/Compiler/library.py @@ -131,6 +131,25 @@ def print_ln(s='', *args, **kwargs): """ print_str(str(s) + '\n', *args, **kwargs) + +def print_without_ln(s='', *args, **kwargs): + """ Print line, with optional args for adding variables/registers + with ``%s``. By default only player 0 outputs, but the ``-I`` + command-line option changes that. + + :param s: Python string with same number of ``%s`` as length of :py:obj:`args` + :param args: list of public values (regint/cint/int/cfix/cfloat/localint) + :param print_secrets: whether to output secret shares + + Example: + + .. code:: + + print_ln('a is %s.', a.reveal()) + """ + print_str(str(s), *args, **kwargs) + + def print_both(s, end='\n'): """ Print line during compilation and execution. """ print(s, end=end) diff --git a/Compiler/types.py b/Compiler/types.py index 9412a094d..fe6d308cd 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5113,10 +5113,30 @@ class custom_sfix(sfix): # R: clear-text; x: edabit in binary format def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) + library.print_ln("LTBits: R=%s", R) + library.print_without_ln("LTBits: R_bits= ") + + for i in range(BIT_SIZE): + library.print_without_ln(R_bits[i]) + + library.print_without_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] + for i in range(BIT_SIZE): + library.print_without_ln(y[i]) + + library.print_without_ln("\nLTBits: z= ") z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + for i in range(BIT_SIZE): + library.print_without_ln(R_bits[i]) + + library.print_without_ln("\nLTBits: w= ") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - return 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) + for i in range(BIT_SIZE): + library.print_without_ln(R_bits[i]) + + return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) + library.print_ln("LTBits: return = ") + return return_value def Dragos_RabbitLTB(self, R, x, k): @@ -5187,17 +5207,20 @@ def rabbitLTC(self, a, c, BIT_SIZE = 32): w = [None, None, None, None] - library.print_ln("[DEBUG CARMEN]: comparing masked_a=%s < edabit=%s", masked_a, r.reveal()) + library.print_ln("[DEBUG CARMEN]: w1 comparing masked_a=%s < edabit=%s", masked_a, r.reveal()) + library.print_ln("[DEBUG CARMEN]: masked_a - edabit= %s", (masked_a - r.reveal())) w[1] = self.LTBits(masked_a, r_bits, BIT_SIZE) - library.print_ln("[DEBUG CARMEN]: result of comparison = %s", w[1].reveal()) + library.print_ln("[DEBUG CARMEN]: result of comparison w1 = %s", w[1].reveal()) - library.print_ln("[DEBUG CARMEN]: comparing masked_b=%s < edabit=%s", masked_b, r.reveal()) + library.print_ln("[DEBUG CARMEN]: w2 comparing masked_b=%s < edabit=%s", masked_b, r.reveal()) + library.print_ln("[DEBUG CARMEN]: masked_b - edabit= %s", (masked_b - r.reveal())) w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) - library.print_ln("[DEBUG CARMEN]: result of comparison = %s", w[2].reveal()) + library.print_ln("[DEBUG CARMEN]: result of comparison w2 = %s", w[2].reveal()) - library.print_ln("lastly, comparing in cleartext %s < %s", masked_b, M - R) + library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) + library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) w[3] = cint(masked_b < (M - R)) - library.print_ln("[DEBUG CARMEN]: result of comparison = %s", w[3]) + library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) #w3_bits = cbits.bit_decompose_clear(w[3], 64) final_result = 1 - (w[1] - w[2] + w[3]) @@ -5226,9 +5249,11 @@ def rabbitLTS(self, a, b): # self.rabbitLTS(a, b) = rabbitLTC(a-b, 0) def __lt__(self, other): - library.print_ln("[DEBUG CARMEN] in __le__, calling rabbitLTS") + library.print_ln("[DEBUG CARMEN] in __lt__, calling rabbitLTS") a = self.v b = other.v + library.print_ln("[DEBUG CARMEN] in __lt__, comparing self=%s with other=%s", self.v.reveal(), other.v.reveal()) + library.print_ln("[DEBUG CARMEN] in __lt__, comparing a=%s with b=%s", a.reveal(), b.reveal()) result = self.rabbitLTS_fix(a, b) return result From 87aef70d66e40b9966013ebce0d369e8ee0fb3f0 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 18:33:57 +0200 Subject: [PATCH 15/60] add reveal --- Compiler/types.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index fe6d308cd..1613c3141 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5122,20 +5122,20 @@ def LTBits(self, R, x, BIT_SIZE): library.print_without_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln(y[i]) + library.print_without_ln(y[i].reveal()) library.print_without_ln("\nLTBits: z= ") z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] for i in range(BIT_SIZE): - library.print_without_ln(R_bits[i]) + library.print_without_ln(z[i].reveal()) library.print_without_ln("\nLTBits: w= ") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln(R_bits[i]) + library.print_without_ln(w[i].reveal()) return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) - library.print_ln("LTBits: return = ") + library.print_ln("\nLTBits: return =%s", return_value.reveal()) return return_value From 33a657e7cc2981000154639d7d7ab3c780ac5d97 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 18:47:08 +0200 Subject: [PATCH 16/60] trying to print a binary value..... --- Compiler/types.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 1613c3141..c83367fff 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5117,22 +5117,34 @@ def LTBits(self, R, x, BIT_SIZE): library.print_without_ln("LTBits: R_bits= ") for i in range(BIT_SIZE): - library.print_without_ln(R_bits[i]) + if (R_bits[i] == 0): + library.print_without_ln("0") + else: + library.print_without_ln("1") library.print_without_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln(y[i].reveal()) + if (y[i].reveal() == 0): + library.print_without_ln("0") + else: + library.print_without_ln("1") library.print_without_ln("\nLTBits: z= ") z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] for i in range(BIT_SIZE): - library.print_without_ln(z[i].reveal()) + if (z[i].reveal() == 0): + library.print_without_ln("0") + else: + library.print_without_ln("1") library.print_without_ln("\nLTBits: w= ") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln(w[i].reveal()) + if (w[i].reveal() == 0): + library.print_without_ln("0") + else: + library.print_without_ln("1") return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) From fcd3560c32d95ecb3f7410242fc543be6d2e662a Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 19:45:36 +0200 Subject: [PATCH 17/60] still debugging... --- Compiler/types.py | 43 +++++++------------------------------------ 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index c83367fff..2eee71b72 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5113,38 +5113,9 @@ class custom_sfix(sfix): # R: clear-text; x: edabit in binary format def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) - library.print_ln("LTBits: R=%s", R) - library.print_without_ln("LTBits: R_bits= ") - - for i in range(BIT_SIZE): - if (R_bits[i] == 0): - library.print_without_ln("0") - else: - library.print_without_ln("1") - - library.print_without_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - if (y[i].reveal() == 0): - library.print_without_ln("0") - else: - library.print_without_ln("1") - - library.print_without_ln("\nLTBits: z= ") z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] - for i in range(BIT_SIZE): - if (z[i].reveal() == 0): - library.print_without_ln("0") - else: - library.print_without_ln("1") - - library.print_without_ln("\nLTBits: w= ") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - if (w[i].reveal() == 0): - library.print_without_ln("0") - else: - library.print_without_ln("1") return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) @@ -5193,7 +5164,7 @@ def and_op(x, y, z=None): return total - def rabbitLTC(self, a, c, BIT_SIZE = 32): + def rabbitLTZ(self, x, BIT_SIZE = 64): """ s = (c ?< a) @@ -5208,13 +5179,13 @@ def rabbitLTC(self, a, c, BIT_SIZE = 32): length_eda = BIT_SIZE M = P_VALUES[64] - R = (M - 1) // 2 + R = (M - 1) // 2 # for marking 0 r, r_bits = sint.get_edabit(length_eda, True) - masked_a = (a + r).reveal() - masked_b = masked_a + M - R + masked_a = (x + r).reveal() + masked_b = (x + r + M - R).reveal() - library.print_ln("[DEBUG CARMEN] in rabbitLTC comparing a=%s < c=%s", a.reveal(), c) + library.print_ln("[DEBUG CARMEN] in rabbitLTC comparing x=%s < c=0", x.reveal()) library.print_ln("[DEBUG CARMEN] in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, eda_bit=%s", M, R, masked_a, masked_b, r.reveal()) w = [None, None, None, None] @@ -5235,7 +5206,7 @@ def rabbitLTC(self, a, c, BIT_SIZE = 32): library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) #w3_bits = cbits.bit_decompose_clear(w[3], 64) - final_result = 1 - (w[1] - w[2] + w[3]) + final_result = w[1] - w[2] + w[3] library.print_ln("[DEBUG CARMEN]: final result before return = %s", final_result.reveal()) return final_result #movs(s, sint.conv(w[1] ^ w[2] ^ w3_bits[0])) @@ -5253,7 +5224,7 @@ def rabbitLTS(self, a, b): # print("rabbitLTS: print 1 failed") library.print_ln("[DEBUG CARMEN]: in the LTS") - res = self.rabbitLTC(a - b, 0, program.bit_length) + res = self.rabbitLTZ(a - b) return res From 1c0feb0cad927d21c13d85e346e993c7f5c9074c Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 20:12:53 +0200 Subject: [PATCH 18/60] a=try dragos --- Compiler/types.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 2eee71b72..61869c73d 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5131,7 +5131,7 @@ def Dragos_RabbitLTB(self, R, x, k): """ from .GC.types import sbit, cbits - library.print_ln("[DEBUG CARMEN] in _RabbitLTB") + library.print_ln("[DEBUG CARMEN] in _RabbitLTB dragos") R_bits = cbits.bit_decompose_clear(R, 64) y = [sbit() for i in range(k)] z = [sbit() for i in range(k)] @@ -5192,12 +5192,12 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("[DEBUG CARMEN]: w1 comparing masked_a=%s < edabit=%s", masked_a, r.reveal()) library.print_ln("[DEBUG CARMEN]: masked_a - edabit= %s", (masked_a - r.reveal())) - w[1] = self.LTBits(masked_a, r_bits, BIT_SIZE) + w[1] = self.Dragos_RabbitLTB(masked_a, r_bits, BIT_SIZE) library.print_ln("[DEBUG CARMEN]: result of comparison w1 = %s", w[1].reveal()) library.print_ln("[DEBUG CARMEN]: w2 comparing masked_b=%s < edabit=%s", masked_b, r.reveal()) library.print_ln("[DEBUG CARMEN]: masked_b - edabit= %s", (masked_b - r.reveal())) - w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) + w[2] = self.Dragos_RabbitLTB(masked_b, r_bits, BIT_SIZE) library.print_ln("[DEBUG CARMEN]: result of comparison w2 = %s", w[2].reveal()) library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) From 07691761d11e988df0086b0f7fd82dc4ade0ad97 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 20:29:40 +0200 Subject: [PATCH 19/60] going back --- Compiler/types.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 61869c73d..04a532446 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5114,13 +5114,20 @@ class custom_sfix(sfix): def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] - z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + z = floatingpoint.PreOpL(floatingpoint.or_op, y) + [0] w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - + return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) return return_value + # z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + # w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] + + # return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) + # library.print_ln("\nLTBits: return =%s", return_value.reveal()) + # return return_value + def Dragos_RabbitLTB(self, R, x, k): """ @@ -5192,12 +5199,12 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("[DEBUG CARMEN]: w1 comparing masked_a=%s < edabit=%s", masked_a, r.reveal()) library.print_ln("[DEBUG CARMEN]: masked_a - edabit= %s", (masked_a - r.reveal())) - w[1] = self.Dragos_RabbitLTB(masked_a, r_bits, BIT_SIZE) + w[1] = self.LTBits(masked_a, r_bits, BIT_SIZE) library.print_ln("[DEBUG CARMEN]: result of comparison w1 = %s", w[1].reveal()) library.print_ln("[DEBUG CARMEN]: w2 comparing masked_b=%s < edabit=%s", masked_b, r.reveal()) library.print_ln("[DEBUG CARMEN]: masked_b - edabit= %s", (masked_b - r.reveal())) - w[2] = self.Dragos_RabbitLTB(masked_b, r_bits, BIT_SIZE) + w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) library.print_ln("[DEBUG CARMEN]: result of comparison w2 = %s", w[2].reveal()) library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) From bc5fd159afe74263686bfc1da7c3386292cde67c Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 20:53:57 +0200 Subject: [PATCH 20/60] add .output() --- Compiler/types.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Compiler/types.py b/Compiler/types.py index 04a532446..675f4cea3 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5113,9 +5113,24 @@ class custom_sfix(sfix): # R: clear-text; x: edabit in binary format def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) + library.print_ln("\nLTBits: R_bits=") + for i in range(BIT_SIZE): + R_bits[i].output() + + library.print_ln("\nLTBits: y=") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] + for i in range(BIT_SIZE): + y[i].output() + + library.print_ln("\nLTBits: z=") z = floatingpoint.PreOpL(floatingpoint.or_op, y) + [0] + for i in range(BIT_SIZE): + z[i].output() + + library.print_ln("\nLTBits: w=") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] + for i in range(BIT_SIZE): + w[i].output() return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) From dc9b9afd9d5fcdbe4498189655cfc5f30b1dbf3b Mon Sep 17 00:00:00 2001 From: Carmen Date: Sun, 2 Feb 2025 21:18:18 +0200 Subject: [PATCH 21/60] trying to print... --- Compiler/types.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 675f4cea3..0da070472 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5115,22 +5115,22 @@ def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) library.print_ln("\nLTBits: R_bits=") for i in range(BIT_SIZE): - R_bits[i].output() + library.print_without_ln(R_bits[i]) - library.print_ln("\nLTBits: y=") + library.print_without_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - y[i].output() + library.print_without_ln(y[i].reveal()) - library.print_ln("\nLTBits: z=") - z = floatingpoint.PreOpL(floatingpoint.or_op, y) + [0] + library.print_without_ln("\nLTBits: z= ") + z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] for i in range(BIT_SIZE): - z[i].output() + library.print_without_ln(z[i].reveal()) - library.print_ln("\nLTBits: w=") + library.print_without_ln("\nLTBits: w= ") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - w[i].output() + library.print_without_ln(w[i].reveal()) return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) From 62557d648da0b1682a522068e889de99cd786db5 Mon Sep 17 00:00:00 2001 From: Carmen Date: Mon, 3 Feb 2025 09:34:27 +0200 Subject: [PATCH 22/60] use print_ln --- Compiler/types.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 0da070472..9505b7410 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5115,22 +5115,22 @@ def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) library.print_ln("\nLTBits: R_bits=") for i in range(BIT_SIZE): - library.print_without_ln(R_bits[i]) + library.print_ln(R_bits[i]) - library.print_without_ln("\nLTBits: y= ") + library.print_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - library.print_without_ln(y[i].reveal()) + for i in range(3): + library.print_ln(y[i].reveal()) - library.print_without_ln("\nLTBits: z= ") + library.print_ln("\nLTBits: z= ") z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] for i in range(BIT_SIZE): - library.print_without_ln(z[i].reveal()) + library.print_ln(z[i].reveal()) - library.print_without_ln("\nLTBits: w= ") + library.print_ln("\nLTBits: w= ") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln(w[i].reveal()) + library.print_ln(w[i].reveal()) return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) From 1f1d02a3a9263207c3e80c47707578546149741e Mon Sep 17 00:00:00 2001 From: Carmen Date: Mon, 3 Feb 2025 09:40:52 +0200 Subject: [PATCH 23/60] add %s --- Compiler/types.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 9505b7410..cc8cc71aa 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5114,23 +5114,23 @@ class custom_sfix(sfix): def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) library.print_ln("\nLTBits: R_bits=") - for i in range(BIT_SIZE): - library.print_ln(R_bits[i]) + for i in range(3): + library.print_ln("%s", R_bits[i]) library.print_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] for i in range(3): - library.print_ln(y[i].reveal()) + library.print_ln("%s", y[i].reveal()) library.print_ln("\nLTBits: z= ") z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] - for i in range(BIT_SIZE): - library.print_ln(z[i].reveal()) + for i in range(3): + library.print_ln("%s", z[i].reveal()) library.print_ln("\nLTBits: w= ") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - library.print_ln(w[i].reveal()) + for i in range(3): + library.print_ln("%s", w[i].reveal()) return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) From f1fd91cbb2eb96e7462cbef46352c00c418f13e6 Mon Sep 17 00:00:00 2001 From: Carmen Date: Wed, 5 Feb 2025 22:14:37 +0200 Subject: [PATCH 24/60] add prints --- Compiler/types.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index cc8cc71aa..8425d8413 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5114,23 +5114,23 @@ class custom_sfix(sfix): def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) library.print_ln("\nLTBits: R_bits=") - for i in range(3): - library.print_ln("%s", R_bits[i]) + for i in range(BIT_SIZE): + library.print_without_ln("%s", R_bits[i]) library.print_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] - for i in range(3): - library.print_ln("%s", y[i].reveal()) + for i in range(BIT_SIZE): + library.print_without_ln("%s", y[i].reveal()) library.print_ln("\nLTBits: z= ") z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] - for i in range(3): - library.print_ln("%s", z[i].reveal()) + for i in range(BIT_SIZE): + library.print_without_ln("%s", z[i].reveal()) library.print_ln("\nLTBits: w= ") w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - for i in range(3): - library.print_ln("%s", w[i].reveal()) + for i in range(BIT_SIZE): + library.print_without_ln("%s", w[i].reveal()) return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) From 1af4340c5fdb90f050d89d58418fb7b766fec909 Mon Sep 17 00:00:00 2001 From: Carmen Date: Wed, 5 Feb 2025 21:40:06 +0100 Subject: [PATCH 25/60] add prints --- Compiler/types.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Compiler/types.py b/Compiler/types.py index 8425d8413..f7c694d3f 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5112,11 +5112,16 @@ class custom_sfix(sfix): # R: clear-text; x: edabit in binary format def LTBits(self, R, x, BIT_SIZE): + library.print_ln("!!!!! In LTBits") R_bits = cint.bit_decompose(R, BIT_SIZE) - library.print_ln("\nLTBits: R_bits=") + library.print_ln("\nLTBits: R_bits= ") for i in range(BIT_SIZE): library.print_without_ln("%s", R_bits[i]) + library.print_ln("\nLTBits: edabit= ") + for i in range(BIT_SIZE): + library.print_without_ln("%s", x[i].reveal()) + library.print_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] for i in range(BIT_SIZE): @@ -5134,6 +5139,7 @@ def LTBits(self, R, x, BIT_SIZE): return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return =%s", return_value.reveal()) + library.print_ln("!!!!! end LTBits") return return_value # z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] From 440aa9c10df1d2f6aa3128619ebd8f4d8f876e2b Mon Sep 17 00:00:00 2001 From: Carmen Date: Wed, 5 Feb 2025 22:24:32 +0100 Subject: [PATCH 26/60] still --- Compiler/types.py | 52 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index f7c694d3f..b96d2cf79 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5127,18 +5127,58 @@ def LTBits(self, R, x, BIT_SIZE): for i in range(BIT_SIZE): library.print_without_ln("%s", y[i].reveal()) - library.print_ln("\nLTBits: z= ") - z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + library.print_ln("\nLTBits: z1 inv inv= ") + z1 = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] for i in range(BIT_SIZE): library.print_without_ln("%s", z[i].reveal()) - library.print_ln("\nLTBits: w= ") - w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] + library.print_ln("\nLTBits: z2 inv notinv= ") + z2 = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1]) + [0] + for i in range(BIT_SIZE): + library.print_without_ln("%s", z[i].reveal()) + + library.print_ln("\nLTBits: z3 notinv inv= ") + z3 = floatingpoint.PreOpL(floatingpoint.or_op, y)[::-1] + [0] + for i in range(BIT_SIZE): + library.print_without_ln("%s", z[i].reveal()) + + library.print_ln("\nLTBits: z4 notnot= ") + z4 = floatingpoint.PreOpL(floatingpoint.or_op)+ [0] + for i in range(BIT_SIZE): + library.print_without_ln("%s", z[i].reveal()) + + library.print_ln("\nLTBits: w1 from z1= ") + w1 = [z1[i] - z1[i + 1] for i in range(BIT_SIZE)] + for i in range(BIT_SIZE): + library.print_without_ln("%s", w[i].reveal()) + + library.print_ln("\nLTBits: w2 from z2= ") + w2 = [z2[i] - z2[i + 1] for i in range(BIT_SIZE)] + for i in range(BIT_SIZE): + library.print_without_ln("%s", w[i].reveal()) + + library.print_ln("\nLTBits: w3 from z3= ") + w3 = [z3[i] - z3[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): library.print_without_ln("%s", w[i].reveal()) - return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return =%s", return_value.reveal()) + library.print_ln("\nLTBits: w4 from z4= ") + w4 = [z4[i] - z4[i + 1] for i in range(BIT_SIZE)] + for i in range(BIT_SIZE): + library.print_without_ln("%s", w[i].reveal()) + + return_value = 1 - sum((R_bits[i] & w1[i]) for i in range(BIT_SIZE)) + library.print_ln("\nLTBits: return for z1=%s", return_value.reveal()) + + return_value = 1 - sum((R_bits[i] & w2[i]) for i in range(BIT_SIZE)) + library.print_ln("\nLTBits: return for z2 =%s", return_value.reveal()) + + return_value = 1 - sum((R_bits[i] & w3 [i]) for i in range(BIT_SIZE)) + library.print_ln("\nLTBits: return for z3=%s", return_value.reveal()) + + return_value = 1 - sum((R_bits[i] & w4[i]) for i in range(BIT_SIZE)) + library.print_ln("\nLTBits: return for z4 =%s", return_value.reveal()) + library.print_ln("!!!!! end LTBits") return return_value From 3fef1ce634b6aafcb2e3dac9e70bf883fdf203bd Mon Sep 17 00:00:00 2001 From: Carmen Date: Wed, 5 Feb 2025 22:39:42 +0100 Subject: [PATCH 27/60] fix typo --- Compiler/types.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index b96d2cf79..a24fabcbb 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5130,42 +5130,42 @@ def LTBits(self, R, x, BIT_SIZE): library.print_ln("\nLTBits: z1 inv inv= ") z1 = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] for i in range(BIT_SIZE): - library.print_without_ln("%s", z[i].reveal()) + library.print_without_ln("%s", z1[i].reveal()) library.print_ln("\nLTBits: z2 inv notinv= ") z2 = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1]) + [0] for i in range(BIT_SIZE): - library.print_without_ln("%s", z[i].reveal()) + library.print_without_ln("%s", z2[i].reveal()) library.print_ln("\nLTBits: z3 notinv inv= ") z3 = floatingpoint.PreOpL(floatingpoint.or_op, y)[::-1] + [0] for i in range(BIT_SIZE): - library.print_without_ln("%s", z[i].reveal()) + library.print_without_ln("%s", z3[i].reveal()) library.print_ln("\nLTBits: z4 notnot= ") z4 = floatingpoint.PreOpL(floatingpoint.or_op)+ [0] for i in range(BIT_SIZE): - library.print_without_ln("%s", z[i].reveal()) + library.print_without_ln("%s", z4[i].reveal()) library.print_ln("\nLTBits: w1 from z1= ") w1 = [z1[i] - z1[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln("%s", w[i].reveal()) + library.print_without_ln("%s", w1[i].reveal()) library.print_ln("\nLTBits: w2 from z2= ") w2 = [z2[i] - z2[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln("%s", w[i].reveal()) + library.print_without_ln("%s", w2[i].reveal()) library.print_ln("\nLTBits: w3 from z3= ") w3 = [z3[i] - z3[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln("%s", w[i].reveal()) + library.print_without_ln("%s", w3[i].reveal()) library.print_ln("\nLTBits: w4 from z4= ") w4 = [z4[i] - z4[i + 1] for i in range(BIT_SIZE)] for i in range(BIT_SIZE): - library.print_without_ln("%s", w[i].reveal()) + library.print_without_ln("%s", w4[i].reveal()) return_value = 1 - sum((R_bits[i] & w1[i]) for i in range(BIT_SIZE)) library.print_ln("\nLTBits: return for z1=%s", return_value.reveal()) From 94ab935a8f06dfdb5e6fcd531147dd90b404dd98 Mon Sep 17 00:00:00 2001 From: Carmen Date: Wed, 5 Feb 2025 22:57:07 +0100 Subject: [PATCH 28/60] add y --- Compiler/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/types.py b/Compiler/types.py index a24fabcbb..691e70d21 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5143,7 +5143,7 @@ def LTBits(self, R, x, BIT_SIZE): library.print_without_ln("%s", z3[i].reveal()) library.print_ln("\nLTBits: z4 notnot= ") - z4 = floatingpoint.PreOpL(floatingpoint.or_op)+ [0] + z4 = floatingpoint.PreOpL(floatingpoint.or_op, y)+ [0] for i in range(BIT_SIZE): library.print_without_ln("%s", z4[i].reveal()) From aa28c06c24d3968e17f72b7ab08d657055bc89f7 Mon Sep 17 00:00:00 2001 From: Carmen Date: Tue, 11 Feb 2025 19:56:00 +0000 Subject: [PATCH 29/60] return z1 --- Compiler/types.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 691e70d21..e08957b9b 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5167,20 +5167,20 @@ def LTBits(self, R, x, BIT_SIZE): for i in range(BIT_SIZE): library.print_without_ln("%s", w4[i].reveal()) - return_value = 1 - sum((R_bits[i] & w1[i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return for z1=%s", return_value.reveal()) + return_value1 = 1 - sum((R_bits[i] & w1[i]) for i in range(BIT_SIZE)) + library.print_ln("\nLTBits: return for z1=%s", return_value1.reveal()) - return_value = 1 - sum((R_bits[i] & w2[i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return for z2 =%s", return_value.reveal()) + return_value2 = 1 - sum((R_bits[i] & w2[i]) for i in range(BIT_SIZE)) + library.print_ln("\nLTBits: return for z2 =%s", return_value2.reveal()) - return_value = 1 - sum((R_bits[i] & w3 [i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return for z3=%s", return_value.reveal()) + return_value3 = 1 - sum((R_bits[i] & w3[i]) for i in range(BIT_SIZE)) + library.print_ln("\nLTBits: return for z3=%s", return_value3.reveal()) - return_value = 1 - sum((R_bits[i] & w4[i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return for z4 =%s", return_value.reveal()) + return_value4 = 1 - sum((R_bits[i] & w4[i]) for i in range(BIT_SIZE)) + library.print_ln("\nLTBits: return for z4 =%s", return_value4.reveal()) library.print_ln("!!!!! end LTBits") - return return_value + return return_value1 # z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] # w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] From dbf3c4123fb147746fa84db132743c16bf5343de Mon Sep 17 00:00:00 2001 From: Carmen Date: Tue, 11 Feb 2025 20:35:23 +0000 Subject: [PATCH 30/60] trying to fix w3 --- Compiler/types.py | 77 +++++++++-------------------------------------- 1 file changed, 15 insertions(+), 62 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index e08957b9b..cd34c39e4 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5114,73 +5114,25 @@ class custom_sfix(sfix): def LTBits(self, R, x, BIT_SIZE): library.print_ln("!!!!! In LTBits") R_bits = cint.bit_decompose(R, BIT_SIZE) - library.print_ln("\nLTBits: R_bits= ") - for i in range(BIT_SIZE): - library.print_without_ln("%s", R_bits[i]) + # library.print_ln("\nLTBits: R_bits= ") + # for i in range(BIT_SIZE): + # library.print_without_ln("%s", R_bits[i]) - library.print_ln("\nLTBits: edabit= ") - for i in range(BIT_SIZE): - library.print_without_ln("%s", x[i].reveal()) + # library.print_ln("\nLTBits: edabit= ") + # for i in range(BIT_SIZE): + # library.print_without_ln("%s", x[i].reveal()) - library.print_ln("\nLTBits: y= ") + # library.print_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - library.print_without_ln("%s", y[i].reveal()) + # for i in range(BIT_SIZE): + # library.print_without_ln("%s", y[i].reveal()) - library.print_ln("\nLTBits: z1 inv inv= ") - z1 = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] - for i in range(BIT_SIZE): - library.print_without_ln("%s", z1[i].reveal()) - - library.print_ln("\nLTBits: z2 inv notinv= ") - z2 = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1]) + [0] - for i in range(BIT_SIZE): - library.print_without_ln("%s", z2[i].reveal()) - - library.print_ln("\nLTBits: z3 notinv inv= ") - z3 = floatingpoint.PreOpL(floatingpoint.or_op, y)[::-1] + [0] - for i in range(BIT_SIZE): - library.print_without_ln("%s", z3[i].reveal()) - - library.print_ln("\nLTBits: z4 notnot= ") - z4 = floatingpoint.PreOpL(floatingpoint.or_op, y)+ [0] - for i in range(BIT_SIZE): - library.print_without_ln("%s", z4[i].reveal()) - - library.print_ln("\nLTBits: w1 from z1= ") - w1 = [z1[i] - z1[i + 1] for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - library.print_without_ln("%s", w1[i].reveal()) - - library.print_ln("\nLTBits: w2 from z2= ") - w2 = [z2[i] - z2[i + 1] for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - library.print_without_ln("%s", w2[i].reveal()) - - library.print_ln("\nLTBits: w3 from z3= ") - w3 = [z3[i] - z3[i + 1] for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - library.print_without_ln("%s", w3[i].reveal()) - - library.print_ln("\nLTBits: w4 from z4= ") - w4 = [z4[i] - z4[i + 1] for i in range(BIT_SIZE)] - for i in range(BIT_SIZE): - library.print_without_ln("%s", w4[i].reveal()) - - return_value1 = 1 - sum((R_bits[i] & w1[i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return for z1=%s", return_value1.reveal()) - - return_value2 = 1 - sum((R_bits[i] & w2[i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return for z2 =%s", return_value2.reveal()) - - return_value3 = 1 - sum((R_bits[i] & w3[i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return for z3=%s", return_value3.reveal()) - - return_value4 = 1 - sum((R_bits[i] & w4[i]) for i in range(BIT_SIZE)) - library.print_ln("\nLTBits: return for z4 =%s", return_value4.reveal()) + z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] + return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) library.print_ln("!!!!! end LTBits") - return return_value1 + return return_value # z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] # w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] @@ -5270,7 +5222,8 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) - w[3] = cint(masked_b < (M - R)) + aux = masked_b - (M - R) + w[3] = cint(aux < 0) library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) #w3_bits = cbits.bit_decompose_clear(w[3], 64) From c22ccd9aff7ae27e195a66b039a7c7b002fb8deb Mon Sep 17 00:00:00 2001 From: Carmen Date: Tue, 11 Feb 2025 21:05:45 +0000 Subject: [PATCH 31/60] new w3 --- Compiler/types.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index cd34c39e4..b78d8f449 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5222,8 +5222,11 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) - aux = masked_b - (M - R) - w[3] = cint(aux < 0) + + if (masked_b < 0): + masked_b += M # for modular arithmetic + + w[3] = cint(masked_b < (M - R)) library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) #w3_bits = cbits.bit_decompose_clear(w[3], 64) From afc4d43f80827f86644361b25cfb0ed8192e160d Mon Sep 17 00:00:00 2001 From: Carmen Date: Tue, 11 Feb 2025 21:19:52 +0000 Subject: [PATCH 32/60] fix truth value --- Compiler/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/types.py b/Compiler/types.py index b78d8f449..1abd3b80c 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5223,7 +5223,7 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) - if (masked_b < 0): + if (cint(masked_b < 0)): masked_b += M # for modular arithmetic w[3] = cint(masked_b < (M - R)) From c233a0c82ded8b5a92cce6cfbba880dc77f11dec Mon Sep 17 00:00:00 2001 From: Carmen Date: Tue, 11 Feb 2025 21:39:43 +0000 Subject: [PATCH 33/60] add trick to remove if-branch --- Compiler/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 1abd3b80c..d28cb0b43 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5223,8 +5223,8 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) - if (cint(masked_b < 0)): - masked_b += M # for modular arithmetic + w_aux = cint(masked_b < 0) + masked_b += (w_aux * M) # for modular arithmetic w[3] = cint(masked_b < (M - R)) library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) From 44d300cb764842d65c4532f63223619168f473f9 Mon Sep 17 00:00:00 2001 From: Carmen Date: Tue, 11 Feb 2025 22:05:31 +0000 Subject: [PATCH 34/60] add print --- Compiler/types.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Compiler/types.py b/Compiler/types.py index d28cb0b43..c0e8ccdd3 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5226,6 +5226,10 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): w_aux = cint(masked_b < 0) masked_b += (w_aux * M) # for modular arithmetic + library.print_ln("[DEBUG CARMEN]: w_aux %s", w_aux) + library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) + library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) + w[3] = cint(masked_b < (M - R)) library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) #w3_bits = cbits.bit_decompose_clear(w[3], 64) From e858c434bca595dd2bd155c150f639c01d14ffc8 Mon Sep 17 00:00:00 2001 From: Carmen Date: Wed, 12 Feb 2025 20:05:30 +0000 Subject: [PATCH 35/60] try with R 0 --- Compiler/types.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index c0e8ccdd3..051c3b431 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5199,7 +5199,7 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): length_eda = BIT_SIZE M = P_VALUES[64] - R = (M - 1) // 2 # for marking 0 + R = 0 r, r_bits = sint.get_edabit(length_eda, True) masked_a = (x + r).reveal() @@ -5223,14 +5223,7 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) - w_aux = cint(masked_b < 0) - masked_b += (w_aux * M) # for modular arithmetic - - library.print_ln("[DEBUG CARMEN]: w_aux %s", w_aux) - library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) - library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) - - w[3] = cint(masked_b < (M - R)) + w[3] = cint(masked_b < 0) library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) #w3_bits = cbits.bit_decompose_clear(w[3], 64) From de4a1d2952b3d001a422cbf3c10c47ba7dc0603d Mon Sep 17 00:00:00 2001 From: Carmen Date: Wed, 12 Feb 2025 20:33:55 +0000 Subject: [PATCH 36/60] fixes and remove prints --- Compiler/types.py | 50 +++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 051c3b431..6603c6d0a 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5112,7 +5112,7 @@ class custom_sfix(sfix): # R: clear-text; x: edabit in binary format def LTBits(self, R, x, BIT_SIZE): - library.print_ln("!!!!! In LTBits") + # library.print_ln("!!!!! In LTBits") R_bits = cint.bit_decompose(R, BIT_SIZE) # library.print_ln("\nLTBits: R_bits= ") # for i in range(BIT_SIZE): @@ -5131,7 +5131,7 @@ def LTBits(self, R, x, BIT_SIZE): w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) - library.print_ln("!!!!! end LTBits") + # library.print_ln("!!!!! end LTBits") return return_value # z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] @@ -5205,31 +5205,31 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): masked_a = (x + r).reveal() masked_b = (x + r + M - R).reveal() - library.print_ln("[DEBUG CARMEN] in rabbitLTC comparing x=%s < c=0", x.reveal()) - library.print_ln("[DEBUG CARMEN] in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, eda_bit=%s", M, R, masked_a, masked_b, r.reveal()) + # library.print_ln("[DEBUG CARMEN] in rabbitLTC comparing x=%s < c=0", x.reveal()) + # library.print_ln("[DEBUG CARMEN] in rabbitLTC. M=%s, R=%s, masked_a=%s, masked_b=%s, eda_bit=%s", M, R, masked_a, masked_b, r.reveal()) w = [None, None, None, None] - library.print_ln("[DEBUG CARMEN]: w1 comparing masked_a=%s < edabit=%s", masked_a, r.reveal()) - library.print_ln("[DEBUG CARMEN]: masked_a - edabit= %s", (masked_a - r.reveal())) + # library.print_ln("[DEBUG CARMEN]: w1 comparing masked_a=%s < edabit=%s", masked_a, r.reveal()) + # library.print_ln("[DEBUG CARMEN]: masked_a - edabit= %s", (masked_a - r.reveal())) w[1] = self.LTBits(masked_a, r_bits, BIT_SIZE) - library.print_ln("[DEBUG CARMEN]: result of comparison w1 = %s", w[1].reveal()) + # library.print_ln("[DEBUG CARMEN]: result of comparison w1 = %s", w[1].reveal()) - library.print_ln("[DEBUG CARMEN]: w2 comparing masked_b=%s < edabit=%s", masked_b, r.reveal()) - library.print_ln("[DEBUG CARMEN]: masked_b - edabit= %s", (masked_b - r.reveal())) + # library.print_ln("[DEBUG CARMEN]: w2 comparing masked_b=%s < edabit=%s", masked_b, r.reveal()) + # library.print_ln("[DEBUG CARMEN]: masked_b - edabit= %s", (masked_b - r.reveal())) w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) - library.print_ln("[DEBUG CARMEN]: result of comparison w2 = %s", w[2].reveal()) + # library.print_ln("[DEBUG CARMEN]: result of comparison w2 = %s", w[2].reveal()) - library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) - library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) + # library.print_ln("[DEBUG CARMEN]: w3 comparing in cleartext %s < %s", masked_b, M - R) + # library.print_ln("[DEBUG CARMEN]: masked_b - (M - R)= %s", (masked_b - (M - R))) w[3] = cint(masked_b < 0) - library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) + # library.print_ln("[DEBUG CARMEN]: result of comparison w3 = %s", w[3]) #w3_bits = cbits.bit_decompose_clear(w[3], 64) - final_result = w[1] - w[2] + w[3] - library.print_ln("[DEBUG CARMEN]: final result before return = %s", final_result.reveal()) - return final_result + result = w[1] - w[2] + w[3] + # library.print_ln("[DEBUG CARMEN]: final result before return = %s", final_result.reveal()) + return 1 - result #movs(s, sint.conv(w[1] ^ w[2] ^ w3_bits[0])) @@ -5244,7 +5244,7 @@ def rabbitLTS(self, a, b): # except: # print("rabbitLTS: print 1 failed") - library.print_ln("[DEBUG CARMEN]: in the LTS") + # library.print_ln("[DEBUG CARMEN]: in the LTS") res = self.rabbitLTZ(a - b) return res @@ -5253,17 +5253,17 @@ def rabbitLTS(self, a, b): # self.rabbitLTS(a, b) = rabbitLTC(a-b, 0) def __lt__(self, other): - library.print_ln("[DEBUG CARMEN] in __lt__, calling rabbitLTS") + # library.print_ln("[DEBUG CARMEN] in __lt__, calling rabbitLTS") a = self.v b = other.v - library.print_ln("[DEBUG CARMEN] in __lt__, comparing self=%s with other=%s", self.v.reveal(), other.v.reveal()) - library.print_ln("[DEBUG CARMEN] in __lt__, comparing a=%s with b=%s", a.reveal(), b.reveal()) + # library.print_ln("[DEBUG CARMEN] in __lt__, comparing self=%s with other=%s", self.v.reveal(), other.v.reveal()) + # library.print_ln("[DEBUG CARMEN] in __lt__, comparing a=%s with b=%s", a.reveal(), b.reveal()) result = self.rabbitLTS_fix(a, b) return result def __le__(self, other): - library.print_ln("[DEBUG CARMEN] in __le__, calling rabbitLTS") + # library.print_ln("[DEBUG CARMEN] in __le__, calling rabbitLTS") a = self.v b = other.v result = 1 - self.rabbitLTS_fix(b, a) @@ -5271,7 +5271,7 @@ def __le__(self, other): def __gt__(self, other): - library.print_ln("[DEBUG CARMEN] in __gt__, calling rabbitLTS") + # library.print_ln("[DEBUG CARMEN] in __gt__, calling rabbitLTS") a = self.v b = other.v result = self.rabbitLTS_fix(b, a) @@ -5279,7 +5279,7 @@ def __gt__(self, other): def __ge__(self, other): - library.print_ln("[DEBUG CARMEN] in __ge__, calling rabbitLTS") + # library.print_ln("[DEBUG CARMEN] in __ge__, calling rabbitLTS") a = self.v b = other.v result = 1 - self.rabbitLTS_fix(a, b) @@ -5287,7 +5287,7 @@ def __ge__(self, other): def __eq__(self, other): - library.print_ln("[DEBUG CARMEN] in __eq__, calling rabbitLTS") + # library.print_ln("[DEBUG CARMEN] in __eq__, calling rabbitLTS") a = self.v b = other.v result = (1 - self.rabbitLTS_fix(a, b)) * (1 - self.rabbitLTS_fix(b, a)) @@ -5295,7 +5295,7 @@ def __eq__(self, other): def __ne__(self, other): - library.print_ln("[DEBUG CARMEN] in __ne__, calling rabbitLTS") + # library.print_ln("[DEBUG CARMEN] in __ne__, calling rabbitLTS") a = self.v b = other.v result = 1 - (1 - self.rabbitLTS_fix(a, b)) * (1 - self.rabbitLTS_fix(b, a)) From 0ca76cf7a11c81eac7ad4b972b862c2776dd25c8 Mon Sep 17 00:00:00 2001 From: Carmen Date: Fri, 14 Feb 2025 19:23:02 +0000 Subject: [PATCH 37/60] cleanup --- Compiler/types.py | 106 +--------------------------------------------- 1 file changed, 1 insertion(+), 105 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 6603c6d0a..4b48cc85b 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5112,89 +5112,20 @@ class custom_sfix(sfix): # R: clear-text; x: edabit in binary format def LTBits(self, R, x, BIT_SIZE): - # library.print_ln("!!!!! In LTBits") R_bits = cint.bit_decompose(R, BIT_SIZE) - # library.print_ln("\nLTBits: R_bits= ") - # for i in range(BIT_SIZE): - # library.print_without_ln("%s", R_bits[i]) - - # library.print_ln("\nLTBits: edabit= ") - # for i in range(BIT_SIZE): - # library.print_without_ln("%s", x[i].reveal()) - - # library.print_ln("\nLTBits: y= ") y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] - # for i in range(BIT_SIZE): - # library.print_without_ln("%s", y[i].reveal()) - z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) - # library.print_ln("!!!!! end LTBits") return return_value - # z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] - # w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - - # return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) - # library.print_ln("\nLTBits: return =%s", return_value.reveal()) - # return return_value - - - def Dragos_RabbitLTB(self, R, x, k): - """ - res = R Date: Mon, 17 Feb 2025 19:18:44 +0000 Subject: [PATCH 38/60] override abs --- Compiler/types.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Compiler/types.py b/Compiler/types.py index 4b48cc85b..62e8c3591 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5198,6 +5198,11 @@ def __ne__(self, other): return result + def __abs__(self): + """ Absolute value. """ + return (self < custom_sfix(0)).if_else(-self, self) + + sfix.unreduced_type = unreduced_sfix sfix.set_precision(16, 31) From 10cb1da5b18c11c1f518a4071112dfb720320242 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 1 Mar 2025 19:09:55 +0000 Subject: [PATCH 39/60] add vectorise --- Compiler/types.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 62e8c3591..ea6233d9f 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5111,6 +5111,7 @@ def update(self, other): class custom_sfix(sfix): # R: clear-text; x: edabit in binary format + @vectorize def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] @@ -5120,6 +5121,7 @@ def LTBits(self, R, x, BIT_SIZE): return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) return return_value + @vectorize def rabbitLTZ(self, x, BIT_SIZE = 64): """ s = (c ?< a) @@ -5143,11 +5145,11 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): result = w[1] - w[2] + w[3] return sint(1 - result) - + @vectorize def rabbitLTS_fix(self, a, b): return 1 - self.rabbitLTS(b, a) - + @vectorize def rabbitLTS(self, a, b): res = self.rabbitLTZ(a - b) return res @@ -5155,49 +5157,49 @@ def rabbitLTS(self, a, b): # These are based on the implementation # self.rabbitLTS(a, b) = rabbitLTC(a-b, 0) - + @vectorize def __lt__(self, other): a = self.v b = other.v result = self.rabbitLTS_fix(a, b) return result - + @vectorize def __le__(self, other): a = self.v b = other.v result = 1 - self.rabbitLTS_fix(b, a) return result - + @vectorize def __gt__(self, other): a = self.v b = other.v result = self.rabbitLTS_fix(b, a) return result - + @vectorize def __ge__(self, other): a = self.v b = other.v result = 1 - self.rabbitLTS_fix(a, b) return result - + @vectorize def __eq__(self, other): a = self.v b = other.v result = (1 - self.rabbitLTS_fix(a, b)) * (1 - self.rabbitLTS_fix(b, a)) return result - + @vectorize def __ne__(self, other): a = self.v b = other.v result = 1 - (1 - self.rabbitLTS_fix(a, b)) * (1 - self.rabbitLTS_fix(b, a)) return result - + @vectorize def __abs__(self): """ Absolute value. """ return (self < custom_sfix(0)).if_else(-self, self) From 8a88dab26a310eca8d658dd96be65adc6a0f09e8 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 14:26:37 +0000 Subject: [PATCH 40/60] WIP: add comparison_rabbit program argument --- Compiler/non_linear.py | 9 +++++++++ Compiler/program.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index 147af9f20..c0db7b957 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -46,6 +46,11 @@ def trunc(self, a, k, m, signed): return self._trunc(a, k, m, signed) def ltz(self, a, k): + prog = program.Program.prog + if prog.options.comparison_rabbit: + return -10 + + # else, use truncation return -self.trunc(a, k, k - 1, True) class Masking(NonLinear): @@ -126,6 +131,10 @@ def eqz(self, a, k): return 1 - types.sintbit.conv(KORL(self.bit_dec(a, k, k, True))) def ltz(self, a, k): + prog = program.Program.prog + if prog.options.comparison_rabbit: + return -20 + if k + 1 < self.prime.bit_length(): # https://dl.acm.org/doi/10.1145/3474123.3486757 # "negative" values wrap around when doubling, thus becoming odd diff --git a/Compiler/program.py b/Compiler/program.py index 5d68948ef..484b338c8 100644 --- a/Compiler/program.py +++ b/Compiler/program.py @@ -69,6 +69,7 @@ class defaults: stop = False insecure = False keep_cisc = False + comparison_rabbit = False class Program(object): @@ -203,11 +204,19 @@ def __init__(self, args, options=defaults, name=None): gc.inputbvec, gc.reveal, ] - self.use_trunc_pr = False + """ Setting whether to use special probabilistic truncation. """ - self.use_dabit = options.mixed + self.use_trunc_pr = False + """ Setting whether to use daBits for non-linear functionality. """ + self.use_dabit = options.mixed + + """ Setting whether to use edaBits for non-linear functionality. """ self._edabit = options.edabit + + """ Setting whether to use the Rabbit protocol for comparison. """ + self._comparison_rabbit = options.comparison_rabbit + """ Whether to use the low-level INVPERM instruction (only implemented with the assumption of a semi-honest two-party environment)""" self._invperm = options.invperm self._split = False @@ -676,6 +685,19 @@ def use_edabit(self, change=None): else: self._edabit = change + def use_comparison_rabbit(self, change=None): + """Setting whether to use the rabbit comparison protocol (default: false). + + :param change: change setting if not :py:obj:`None` + :returns: setting if :py:obj:`change` is :py:obj:`None` + """ + if change is None: + if not self._comparison_rabbit: + self.relevant_opts.add("comparison_rabbit") + return self._comparison_rabbit + else: + self._comparison_rabbit = change + def use_invperm(self, change=None): """ Set whether to use the low-level INVPERM instruction to inverse a permutation (see sint.inverse_permutation). The INVPERM instruction assumes a semi-honest two-party environment. If false, a general protocol implemented in the high-level language is used. @@ -750,6 +772,8 @@ def options_from_args(self): self.always_raw(True) if "edabit" in self.args: self.use_edabit(True) + if "comparison_rabbit" in self.args: + self.use_comparison_rabbit(True) if "invperm" in self.args: self.use_invperm(True) if "linear_rounds" in self.args: From 75b5c97b9fa0eb742861082f73ae92d161dcdf65 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 14:58:32 +0000 Subject: [PATCH 41/60] add comparison_rabbit in CompilerLib --- Compiler/compilerLib.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Compiler/compilerLib.py b/Compiler/compilerLib.py index 8c4f41b8d..66febc85c 100644 --- a/Compiler/compilerLib.py +++ b/Compiler/compilerLib.py @@ -206,6 +206,12 @@ def build_option_parser(self): dest="edabit", help="mixing arithmetic and binary computation using edaBits", ) + parser.add_option( + "--comparison_rabbit", + action="store_true", + dest="comparison_rabbit", + help="using the rabbit comparison protocol for known prime modulus, instead of truncation", + ) parser.add_option( "-Z", "--split", From 8fd63efd5c3a9cc2e19296ff87c43b8fda4993d0 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 15:38:38 +0000 Subject: [PATCH 42/60] debug attempt 1 --- Compiler/non_linear.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index c0db7b957..86b24fc3f 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -46,6 +46,7 @@ def trunc(self, a, k, m, signed): return self._trunc(a, k, m, signed) def ltz(self, a, k): + return a * cint(1) prog = program.Program.prog if prog.options.comparison_rabbit: return -10 @@ -131,6 +132,7 @@ def eqz(self, a, k): return 1 - types.sintbit.conv(KORL(self.bit_dec(a, k, k, True))) def ltz(self, a, k): + return a * cint(10) prog = program.Program.prog if prog.options.comparison_rabbit: return -20 @@ -180,4 +182,5 @@ def trunc_round_nearest(self, a, k, m, signed): return super(Ring, self).trunc_round_nearest(a, k, m, signed) def ltz(self, a, k): + return a * cint(100) return LtzRing(a, k) From 82054bf9b93defc5acfeb79c79c0ad7d5a663cee Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 16:15:27 +0000 Subject: [PATCH 43/60] add print --- Compiler/non_linear.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index 86b24fc3f..daf3e54c9 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -46,8 +46,9 @@ def trunc(self, a, k, m, signed): return self._trunc(a, k, m, signed) def ltz(self, a, k): - return a * cint(1) prog = program.Program.prog + library.print_ln("!!! rabbit flag=%s", prog.options.comparison_rabbit) + if prog.options.comparison_rabbit: return -10 @@ -132,7 +133,6 @@ def eqz(self, a, k): return 1 - types.sintbit.conv(KORL(self.bit_dec(a, k, k, True))) def ltz(self, a, k): - return a * cint(10) prog = program.Program.prog if prog.options.comparison_rabbit: return -20 @@ -182,5 +182,4 @@ def trunc_round_nearest(self, a, k, m, signed): return super(Ring, self).trunc_round_nearest(a, k, m, signed) def ltz(self, a, k): - return a * cint(100) return LtzRing(a, k) From 5ebccab859d944838c6f3ea5991e0f3a1cbcf953 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 16:31:35 +0000 Subject: [PATCH 44/60] raise comp error for debugging --- Compiler/non_linear.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index daf3e54c9..40a169e34 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -47,10 +47,11 @@ def trunc(self, a, k, m, signed): def ltz(self, a, k): prog = program.Program.prog - library.print_ln("!!! rabbit flag=%s", prog.options.comparison_rabbit) if prog.options.comparison_rabbit: - return -10 + raise CompilerError('comparison rabbit true') + else: + raise CompilerError('comparison rabbit false') # else, use truncation return -self.trunc(a, k, k - 1, True) From 5fcc8af5fe34811fd0d1785adae41cf3e3ea2c00 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 18:18:02 +0000 Subject: [PATCH 45/60] add rabbit code to non_linear.py --- Compiler/non_linear.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index 40a169e34..b1ce88b78 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -44,14 +44,41 @@ def trunc(self, a, k, m, signed): if m == 0: return a return self._trunc(a, k, m, signed) + + def LTBits(self, R, x, BIT_SIZE): + R_bits = cint.bit_decompose(R, BIT_SIZE) + y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] + z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] + + return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) + return return_value + + def rabbitLTZ(self, x, BIT_SIZE = 64): + """ + s = (x Date: Sat, 22 Mar 2025 18:47:27 +0000 Subject: [PATCH 46/60] remove sum one-liner --- Compiler/non_linear.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index b1ce88b78..ba81271a8 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -51,7 +51,12 @@ def LTBits(self, R, x, BIT_SIZE): z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) + #return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) + bit_sum = 0 + for i in range(BIT_SIZE): + bit_sum += R_bits[i] & w[i] + + return_value = 1 - bit_sum return return_value def rabbitLTZ(self, x, BIT_SIZE = 64): From 53d9d22830fccf237f4f31400b19754c0013080c Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 19:07:51 +0000 Subject: [PATCH 47/60] try sintbit --- Compiler/non_linear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index ba81271a8..79b2ead91 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -56,7 +56,7 @@ def LTBits(self, R, x, BIT_SIZE): for i in range(BIT_SIZE): bit_sum += R_bits[i] & w[i] - return_value = 1 - bit_sum + return_value = types.sintbit(1) - bit_sum return return_value def rabbitLTZ(self, x, BIT_SIZE = 64): From 0ebd7cf1e942accfef6ccd7429ae1a95131c64dd Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 19:24:38 +0000 Subject: [PATCH 48/60] try converting the other term as well --- Compiler/non_linear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index 79b2ead91..5e648923d 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -56,7 +56,7 @@ def LTBits(self, R, x, BIT_SIZE): for i in range(BIT_SIZE): bit_sum += R_bits[i] & w[i] - return_value = types.sintbit(1) - bit_sum + return_value = types.sintbit(1) - types.sintbit(bit_sum) return return_value def rabbitLTZ(self, x, BIT_SIZE = 64): From 73987ac1ad699e7e5e78af50514fe5e583a99859 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 22 Mar 2025 19:48:36 +0000 Subject: [PATCH 49/60] go back to sum() --- Compiler/non_linear.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index 5e648923d..d2ed26762 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -51,13 +51,7 @@ def LTBits(self, R, x, BIT_SIZE): z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] - #return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) - bit_sum = 0 - for i in range(BIT_SIZE): - bit_sum += R_bits[i] & w[i] - - return_value = types.sintbit(1) - types.sintbit(bit_sum) - return return_value + return types.sintbit(1) - types.sintbit(sum((R_bits[i] & w[i]) for i in range(BIT_SIZE))) def rabbitLTZ(self, x, BIT_SIZE = 64): """ From 816499c50b88c4cfa63566fba64f7a4fae64a1d6 Mon Sep 17 00:00:00 2001 From: Carmen Date: Mon, 24 Mar 2025 19:46:32 +0000 Subject: [PATCH 50/60] add prints in non_linear.py --- Compiler/non_linear.py | 10 ++++++++++ Compiler/program.py | 7 ++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index d2ed26762..92600c653 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -46,6 +46,7 @@ def trunc(self, a, k, m, signed): return self._trunc(a, k, m, signed) def LTBits(self, R, x, BIT_SIZE): + library.print_ln("in LTBits") R_bits = cint.bit_decompose(R, BIT_SIZE) y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] @@ -67,14 +68,23 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): masked_a = (x + r).reveal() masked_b = (x + r + M - R).reveal() w = [None, None, None, None] + + library.print_ln("w1, comparing: masked_a=%s edabit=%s", masked_a, r.reveal()) w[1] = self.LTBits(masked_a, r_bits, BIT_SIZE) + + library.print_ln("w2, comparing: masked_b=%s edabit=%s", masked_b, r.reveal()) w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) + + library.print_ln("w3, comparing: masked_b=%s with zero", masked_a) w[3] = cint(masked_b < 0) result = w[1] - w[2] + w[3] + + library.print_ln("w1=%s w2=%s w3=%s result=%s", w[1].reveal(), w[2].reveal(), w[3], result.reveal()) return sint(1 - result) def ltz(self, a, k): + library.print_ln("a=%s k=%s", a.reveal(), k) prog = program.Program.prog if prog.options.comparison_rabbit: return self.rabbitLTZ(a, k) diff --git a/Compiler/program.py b/Compiler/program.py index 484b338c8..7a32d6731 100644 --- a/Compiler/program.py +++ b/Compiler/program.py @@ -205,16 +205,13 @@ def __init__(self, args, options=defaults, name=None): gc.reveal, ] - """ Setting whether to use special probabilistic truncation. """ self.use_trunc_pr = False + """ Setting whether to use special probabilistic truncation. """ - """ Setting whether to use daBits for non-linear functionality. """ self.use_dabit = options.mixed + """ Setting whether to use daBits for non-linear functionality. """ - """ Setting whether to use edaBits for non-linear functionality. """ self._edabit = options.edabit - - """ Setting whether to use the Rabbit protocol for comparison. """ self._comparison_rabbit = options.comparison_rabbit """ Whether to use the low-level INVPERM instruction (only implemented with the assumption of a semi-honest two-party environment)""" From c782d37800bcf1b868c00173f76895487f8bd249 Mon Sep 17 00:00:00 2001 From: Carmen Date: Mon, 24 Mar 2025 20:09:54 +0000 Subject: [PATCH 51/60] try 32 bits --- Compiler/non_linear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index 92600c653..422ea101a 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -61,7 +61,7 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): """ length_eda = BIT_SIZE - M = P_VALUES[64] # TODO: get program.prime + M = P_VALUES[32] # TODO: get program.prime R = 0 r, r_bits = sint.get_edabit(length_eda, True) From 8efeb8825230f6939c4d9fcd4393f87e6d0b25a6 Mon Sep 17 00:00:00 2001 From: Carmen Date: Fri, 28 Mar 2025 18:44:56 +0000 Subject: [PATCH 52/60] debugging --- Compiler/non_linear.py | 4 ++-- Compiler/types.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Compiler/non_linear.py b/Compiler/non_linear.py index 422ea101a..fc0f420a0 100644 --- a/Compiler/non_linear.py +++ b/Compiler/non_linear.py @@ -59,9 +59,9 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): s = (x Date: Sat, 29 Mar 2025 12:30:32 +0000 Subject: [PATCH 53/60] try to make it work for field --- Compiler/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/types.py b/Compiler/types.py index 673680117..f98e7e125 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5133,7 +5133,7 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("custom sfix: bitsize = %s", BIT_SIZE) M = P_VALUES[64] - R = 0 + R = (M - 1) // 2 # for field; use 0 for ring r, r_bits = sint.get_edabit(length_eda, True) masked_a = (x + r).reveal() From cf02affa0689d6903ffa2859ab3a8fc3da743f72 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 29 Mar 2025 12:53:49 +0000 Subject: [PATCH 54/60] debug for fields --- Compiler/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index f98e7e125..33d80882c 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5146,8 +5146,8 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) library.print_ln("w2, comparing: masked_b=%s edabit=%s w2=%s", masked_b, r.reveal(), w[2].reveal()) - w[3] = cint(masked_b < 0) - library.print_ln("w3, comparing: masked_b=%s with zero, w3=%s", masked_b, w[3].reveal()) + w[3] = cint(masked_b < (M - R)) + library.print_ln("w3, comparing: masked_b=%s with %s, w3=%s", masked_b, M - R, w[3].reveal()) result = w[1] - w[2] + w[3] From 1066833f7cbe3a4ca9bba2fa6cb6da2a6d4961f7 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 29 Mar 2025 13:19:30 +0000 Subject: [PATCH 55/60] testing a theory --- Compiler/types.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Compiler/types.py b/Compiler/types.py index 33d80882c..6d75f1f73 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5144,6 +5144,7 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): library.print_ln("w1, comparing: masked_a=%s edabit=%s w1=%s", masked_a, r.reveal(), w[1].reveal()) w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) + w[2] = 1 - w[2] library.print_ln("w2, comparing: masked_b=%s edabit=%s w2=%s", masked_b, r.reveal(), w[2].reveal()) w[3] = cint(masked_b < (M - R)) From f9446598d9040497f55b128c40d727171677ab4c Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 29 Mar 2025 14:42:47 +0000 Subject: [PATCH 56/60] use < 0 --- Compiler/types.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 6d75f1f73..67db0e330 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5137,17 +5137,16 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): r, r_bits = sint.get_edabit(length_eda, True) masked_a = (x + r).reveal() - masked_b = (x + r + M - R).reveal() + masked_b = (x + r + M - R).reveal() # masked_a w = [None, None, None, None] w[1] = self.LTBits(masked_a, r_bits, BIT_SIZE) library.print_ln("w1, comparing: masked_a=%s edabit=%s w1=%s", masked_a, r.reveal(), w[1].reveal()) w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) - w[2] = 1 - w[2] library.print_ln("w2, comparing: masked_b=%s edabit=%s w2=%s", masked_b, r.reveal(), w[2].reveal()) - w[3] = cint(masked_b < (M - R)) + w[3] = cint(masked_b < 0) library.print_ln("w3, comparing: masked_b=%s with %s, w3=%s", masked_b, M - R, w[3].reveal()) result = w[1] - w[2] + w[3] From 210c21ab4b633d1031e69c0de607b5ac407256f5 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 29 Mar 2025 15:07:57 +0000 Subject: [PATCH 57/60] debug ltbits --- Compiler/types.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 67db0e330..1fdf6e09d 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5114,10 +5114,31 @@ class custom_sfix(sfix): @vectorize def LTBits(self, R, x, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) + library.print_ln("\nLTBits: R_bits= ") + for i in range(BIT_SIZE): + library.print_without_ln("%s", R_bits[i]) + + library.print_ln("\nLTBits: edabit= ") + for i in range(BIT_SIZE): + library.print_without_ln("%s", x[i].reveal()) + y = [x[i].bit_xor(R_bits[i]) for i in range(BIT_SIZE)] + library.print_ln("\nLTBits: y= ") + for i in range(BIT_SIZE): + library.print_without_ln("%s", y[i].reveal()) + z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + library.print_ln("\nLTBits: z= ") + for i in range(BIT_SIZE): + library.print_without_ln("%s", z[i].reveal()) + w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] + library.print_ln("\nLTBits: w= ") + for i in range(BIT_SIZE): + library.print_without_ln("%s", w[i].reveal()) + s = sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) + library.print_ln("Sum=%s", s.reveal()) return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) return return_value @@ -5128,7 +5149,6 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): BIT_SIZE: bit length of a """ - from .GC.types import cbits length_eda = BIT_SIZE library.print_ln("custom sfix: bitsize = %s", BIT_SIZE) @@ -5151,7 +5171,7 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): result = w[1] - w[2] + w[3] - library.print_ln("final result = %s (returning negation of)", result.reveal()) + library.print_ln("final result = %s and 1- result=%s", result.reveal(), (1-result).reveal()) return sint(1 - result) @vectorize From a9b579e7c1d4cacbc70da0fbe6bc5941eba10b93 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 29 Mar 2025 15:43:18 +0000 Subject: [PATCH 58/60] try to fix bitlt --- Compiler/types.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 1fdf6e09d..8d0738356 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5127,12 +5127,14 @@ def LTBits(self, R, x, BIT_SIZE): for i in range(BIT_SIZE): library.print_without_ln("%s", y[i].reveal()) - z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + #z = floatingpoint.PreOpL(floatingpoint.or_op, y[::-1])[::-1] + [0] + z = floatingpoint.PreOpL(floatingpoint.or_op, y) library.print_ln("\nLTBits: z= ") for i in range(BIT_SIZE): library.print_without_ln("%s", z[i].reveal()) - w = [z[i] - z[i + 1] for i in range(BIT_SIZE)] + z = [0] + z + w = [z[i] - z[i - 1] for i in range(BIT_SIZE, 0, -1)] library.print_ln("\nLTBits: w= ") for i in range(BIT_SIZE): library.print_without_ln("%s", w[i].reveal()) From 931246bee99f7c9a8b6546e49908cdd0da8196eb Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 29 Mar 2025 16:05:59 +0000 Subject: [PATCH 59/60] reverse w --- Compiler/types.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Compiler/types.py b/Compiler/types.py index 8d0738356..7fe4021f3 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5135,6 +5135,7 @@ def LTBits(self, R, x, BIT_SIZE): z = [0] + z w = [z[i] - z[i - 1] for i in range(BIT_SIZE, 0, -1)] + w = w[::-1] library.print_ln("\nLTBits: w= ") for i in range(BIT_SIZE): library.print_without_ln("%s", w[i].reveal()) From 6241f2c31a7b23de1d0a9cc9eddadbdf13cacfd2 Mon Sep 17 00:00:00 2001 From: Carmen Date: Sat, 29 Mar 2025 16:42:20 +0000 Subject: [PATCH 60/60] even more debugging prints --- Compiler/types.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Compiler/types.py b/Compiler/types.py index 7fe4021f3..5be8e105c 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -5112,13 +5112,18 @@ class custom_sfix(sfix): # R: clear-text; x: edabit in binary format @vectorize - def LTBits(self, R, x, BIT_SIZE): + def LTBits(self, R, x, r, BIT_SIZE): R_bits = cint.bit_decompose(R, BIT_SIZE) library.print_ln("\nLTBits: R_bits= ") for i in range(BIT_SIZE): library.print_without_ln("%s", R_bits[i]) + edabit = cint.bit_decompose(r.reveal(), BIT_SIZE) library.print_ln("\nLTBits: edabit= ") + for i in range(BIT_SIZE): + library.print_without_ln("%s", edabit[i].reveal()) + + library.print_ln("\nLTBits from bits: x = ") for i in range(BIT_SIZE): library.print_without_ln("%s", x[i].reveal()) @@ -5141,7 +5146,7 @@ def LTBits(self, R, x, BIT_SIZE): library.print_without_ln("%s", w[i].reveal()) s = sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) - library.print_ln("Sum=%s", s.reveal()) + library.print_ln("\nSum=%s", s.reveal()) return_value = 1 - sum((R_bits[i] & w[i]) for i in range(BIT_SIZE)) return return_value @@ -5163,10 +5168,10 @@ def rabbitLTZ(self, x, BIT_SIZE = 64): masked_b = (x + r + M - R).reveal() # masked_a w = [None, None, None, None] - w[1] = self.LTBits(masked_a, r_bits, BIT_SIZE) + w[1] = self.LTBits(masked_a, r_bits, r, BIT_SIZE) library.print_ln("w1, comparing: masked_a=%s edabit=%s w1=%s", masked_a, r.reveal(), w[1].reveal()) - w[2] = self.LTBits(masked_b, r_bits, BIT_SIZE) + w[2] = self.LTBits(masked_b, r_bits, r, BIT_SIZE) library.print_ln("w2, comparing: masked_b=%s edabit=%s w2=%s", masked_b, r.reveal(), w[2].reveal()) w[3] = cint(masked_b < 0)