Skip to content

Commit 58d3615

Browse files
refactored context parameters to use krns and current_rns
1 parent b884b9e commit 58d3615

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

kerngen/high_parser/types.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Context(BaseModel):
158158
@classmethod
159159
def from_string(cls, line: str):
160160
"""Construct context from a string"""
161-
scheme, poly_order, max_rns, *optionals = line.split()
161+
scheme, poly_order, key_rns, current_rns, *optionals = line.split()
162162
optional_dict = OptionsDictParser.parse(optionals)
163163
int_poly_order = int(poly_order)
164164
if (
@@ -170,15 +170,19 @@ def from_string(cls, line: str):
170170
f"Poly order `{int_poly_order}` must be power of two >= {MIN_POLY_SIZE} and < {MAX_POLY_SIZE}"
171171
)
172172

173-
int_max_rns = int(max_rns)
174-
int_key_rns = int_max_rns
175-
int_key_rns += optional_dict.pop("krns_delta")
173+
int_key_rns = int(key_rns)
174+
int_current_rns = int(current_rns)
175+
176+
if int_key_rns <= int_current_rns:
177+
raise ValueError(
178+
f"Current RNS must be less than Key RNS: current_rns={current_rns}, key_rns={key_rns}"
179+
)
176180

177181
return cls(
178182
scheme=scheme.upper(),
179183
poly_order=int_poly_order,
180-
max_rns=int_max_rns,
181184
key_rns=int_key_rns,
185+
max_rns=int_current_rns,
182186
**optional_dict,
183187
)
184188

kerngen/tests/test_kerngen.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_op(kerngen_path, gen_op_data):
4747
def test_missing_context(kerngen_path):
4848
"""Test kerngen raises an exception when context is not the first line of
4949
input"""
50-
input_string = "ADD a b c\nCONTEXT BGV 16384 4\n"
50+
input_string = "ADD a b c\nCONTEXT BGV 16384 4 3\n"
5151
result = execute_process(
5252
[kerngen_path],
5353
data_in=input_string,
@@ -59,7 +59,7 @@ def test_missing_context(kerngen_path):
5959

6060
def test_multiple_contexts(kerngen_path):
6161
"""Test kerngen raises an exception when more than one context is given"""
62-
input_string = "CONTEXT BGV 16384 4\nData a 2\nCONTEXT BGV 16384 4\n"
62+
input_string = "CONTEXT BGV 16384 4 2\nData a 2\nCONTEXT BGV 16384 4 2\n"
6363
result = execute_process(
6464
[kerngen_path],
6565
data_in=input_string,
@@ -71,7 +71,7 @@ def test_multiple_contexts(kerngen_path):
7171

7272
def test_context_options_without_key(kerngen_path):
7373
"""Test kerngen raises an exception when more than one context is given"""
74-
input_string = "CONTEXT BGV 16384 4 1\nData a 2\n"
74+
input_string = "CONTEXT BGV 16384 3 2 1\nData a 2\n"
7575
result = execute_process(
7676
[kerngen_path],
7777
data_in=input_string,
@@ -86,7 +86,7 @@ def test_context_options_without_key(kerngen_path):
8686

8787
def test_context_unsupported_options_variable(kerngen_path):
8888
"""Test kerngen raises an exception when more than one context is given"""
89-
input_string = "CONTEXT BGV 16384 4 test=3\nData a 2\n"
89+
input_string = "CONTEXT BGV 16384 3 2 test=3\nData a 2\n"
9090
result = execute_process(
9191
[kerngen_path],
9292
data_in=input_string,
@@ -99,7 +99,7 @@ def test_context_unsupported_options_variable(kerngen_path):
9999
@pytest.mark.parametrize("invalid", [-1, 256, 0.1, "str"])
100100
def test_context_option_invalid_values(kerngen_path, invalid):
101101
"""Test kerngen raises an exception if value is out of range for correct key"""
102-
input_string = f"CONTEXT BGV 16384 4 krns_delta={invalid}\nData a 2\n"
102+
input_string = f"CONTEXT BGV 16384 3 2 krns_delta={invalid}\nData a 2\n"
103103
result = execute_process(
104104
[kerngen_path],
105105
data_in=input_string,
@@ -115,7 +115,7 @@ def test_context_option_invalid_values(kerngen_path, invalid):
115115
def test_unrecognised_opname(kerngen_path):
116116
"""Test kerngen raises an exception when receiving an unrecognised
117117
opname"""
118-
input_string = "CONTEXT BGV 16384 4\nOPERATION a b c\n"
118+
input_string = "CONTEXT BGV 16384 3 2\nOPERATION a b c\n"
119119
result = execute_process(
120120
[kerngen_path],
121121
data_in=input_string,
@@ -129,7 +129,7 @@ def test_unrecognised_opname(kerngen_path):
129129

130130
def test_invalid_scheme(kerngen_path):
131131
"""Test kerngen raises an exception when receiving an invalid scheme"""
132-
input_string = "CONTEXT SCHEME 16384 4\nADD a b c\n"
132+
input_string = "CONTEXT SCHEME 16384 4 3\nADD a b c\n"
133133
result = execute_process(
134134
[kerngen_path],
135135
data_in=input_string,
@@ -142,7 +142,7 @@ def test_invalid_scheme(kerngen_path):
142142
@pytest.mark.parametrize("invalid_poly", [16000, 2**12, 2**13, 2**18])
143143
def test_invalid_poly_order(kerngen_path, invalid_poly):
144144
"""Poly order should be powers of two >= 2^14 and <= 2^17"""
145-
input_string = "CONTEXT BGV " + str(invalid_poly) + " 4\nADD a b c\n"
145+
input_string = "CONTEXT BGV " + str(invalid_poly) + " 4 3\nADD a b c\n"
146146
result = execute_process(
147147
[kerngen_path],
148148
data_in=input_string,
@@ -181,7 +181,7 @@ def test_parse_results_multiple_context():
181181
def fixture_gen_op_data(request):
182182
"""Given an op name, return both the input and expected output strings"""
183183
in_lines = (
184-
"CONTEXT BGV 16384 4",
184+
"CONTEXT BGV 16384 4 3",
185185
"Data a 2",
186186
"Data b 2",
187187
"Data c 2",

0 commit comments

Comments
 (0)