Skip to content

Commit bee78c6

Browse files
committed
fix(golang): keep a self-encoding integer's EncryptValue, and convert a query's operand as a write's
asKind converted every integer to a built-in type before a seal, through reflect, so a defined integer type with its own EncryptValue lost it: a write stored the bare value while a query, which passed the value as it was, asked for the encoded one, and the encoder's own refusals were skipped on writes. A value that writes itself now crosses as it is. Field.query now runs each operand through asKind too, so a query and a write of one field take the same conversion and the same range check, and a query outside the field's range is refused naming the field.
1 parent 347a2e8 commit bee78c6

4 files changed

Lines changed: 71 additions & 6 deletions

File tree

‎languages/golang/encrypt/gensupport/field.go‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,33 @@ func (f Field[T]) Range(ctx context.Context, c *encrypt.Cipher, operator string,
102102

103103
// query derives an exact operator's terms, and refuses an answer of another
104104
// shape than the generated method's: a probabilistic term has a query type
105-
// of its own (Matches).
105+
// of its own (Matches). Each operand is converted to the field's kind as a
106+
// written value is ([asKind]), so the query finds what a write stored.
106107
func (f Field[T]) query(ctx context.Context, c *encrypt.Cipher, operator string, input any, parts int) ([][]byte, error) {
107108
if f.err != nil {
108109
return nil, f.err
109110
}
110111
if c == nil {
111112
return nil, fmt.Errorf("%w: gensupport: %s: a query needs a cipher", encrypt.ErrEncoding, f.name)
112113
}
114+
pair, isPair := input.([]any)
113115
values := []any{input}
114-
if pair, ok := input.([]any); ok {
116+
if isPair {
115117
values = pair
116118
}
117-
for _, v := range values {
118-
if err := checkValue(v); err != nil {
119+
converted := make([]any, len(values))
120+
for i, v := range values {
121+
value, err := asKind(f.kind(), v)
122+
if err != nil {
119123
return nil, fmt.Errorf("gensupport: %s: %w", f.name, err)
120124
}
125+
converted[i] = value
121126
}
122-
term, err := c.QueryTerm(ctx, f.plan, f.name, operator, input)
127+
operand := converted[0]
128+
if isPair {
129+
operand = converted
130+
}
131+
term, err := c.QueryTerm(ctx, f.plan, f.name, operator, operand)
123132
if err != nil {
124133
return nil, err
125134
}

‎languages/golang/encrypt/gensupport/gensupport_internal_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/cipherstash/stack/languages/golang/encrypt"
1414
"github.com/cipherstash/stack/languages/golang/internal/record"
15+
"github.com/cipherstash/vitaminc/bindings/go/vcffi"
1516
"github.com/cipherstash/vitaminc/bindings/go/vcvalue"
1617
)
1718

@@ -438,3 +439,24 @@ func TestAsKindConvertsAnIntegerThatFitsAndRefusesOneThatDoesNot(t *testing.T) {
438439
}
439440
}
440441
}
442+
443+
// encodedInt is an integer type that writes itself, as an int32 of its value
444+
// plus 100.
445+
type encodedInt int16
446+
447+
func (v encodedInt) EncryptValue(enc vcffi.Encoder) error {
448+
enc.Int32(int32(v) + 100)
449+
return nil
450+
}
451+
452+
// A value whose type writes itself crosses as it is, whatever the kind: a
453+
// conversion to a built-in integer would drop its EncryptValue, so a write
454+
// would store one value and a query, which keeps it, ask for another.
455+
func TestAsKindLeavesAValueThatWritesItself(t *testing.T) {
456+
for _, kind := range []Kind{Int16, Int32, Int64} {
457+
got, err := asKind(kind, encodedInt(7))
458+
if err != nil || got != encodedInt(7) {
459+
t.Errorf("asKind(%s, encodedInt(7)) = %v (%T), %v; want encodedInt(7)", kind, got, got, err)
460+
}
461+
}
462+
}

‎languages/golang/encrypt/gensupport/widen.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/cipherstash/stack/languages/golang/encrypt"
10+
"github.com/cipherstash/vitaminc/bindings/go/vcffi"
1011
"github.com/cipherstash/vitaminc/bindings/go/vcvalue"
1112
)
1213

@@ -20,6 +21,11 @@ import (
2021
// with encrypt.ErrEncoding: a Go type wider than the kind is accepted by
2122
// stashgen and range checked here, value by value.
2223
//
24+
// A value whose type writes itself (vcffi.Encryptable) crosses as it is too:
25+
// converting it to a built-in integer would drop its EncryptValue, and the
26+
// engine holds what it writes to the field's kind. A write and a query of one
27+
// field both convert here ([Field.Encrypt], [Field.Term]), so they agree.
28+
//
2329
// The error names the kind and not the value, which is plaintext.
2430
func asKind(kind Kind, v any) (any, error) {
2531
if err := checkValue(v); err != nil {
@@ -29,7 +35,7 @@ func asKind(kind Kind, v any) (any, error) {
2935
return v, nil
3036
}
3137
switch v.(type) {
32-
case vcvalue.Int128, vcvalue.Uint128:
38+
case vcvalue.Int128, vcvalue.Uint128, vcffi.Encryptable:
3339
return v, nil
3440
}
3541
rv := reflect.ValueOf(v)

‎languages/golang/encrypt/widened_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ func TestIntegersOfOtherWidthsSealAtTheirEQLTypesWidth(t *testing.T) {
5353
t.Errorf("a uint32 into BigintEq stores hm %s, an int64 %s", got, want)
5454
}
5555

56+
// A query converts its operand as a write does, so it finds what the
57+
// write stored: the same query as an int32 or int64 field's.
58+
age, err := testusers.WidenedFields.Age.Eq(ctx, cipher, 34)
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
wantAge, err := testusers.ProfileFields.Age.Eq(ctx, cipher, 34)
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
if string(age) != string(wantAge) {
67+
t.Errorf("an int16 IntegerEq query = %s, an int32's %s", age, wantAge)
68+
}
69+
rank, err := testusers.WidenedFields.Rank.Eq(ctx, cipher, math.MaxUint32)
70+
if err != nil {
71+
t.Fatal(err)
72+
}
73+
wantRank, err := testusers.ProfileFields.Rank.Eq(ctx, cipher, math.MaxUint32)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
if string(rank) != string(wantRank) {
78+
t.Errorf("a uint32 BigintEq query = %s, an int64's %s", rank, wantRank)
79+
}
80+
if _, err := testusers.WidenedFields.Count.Eq(ctx, cipher, math.MaxInt32+1); !errors.Is(err, encrypt.ErrEncoding) {
81+
t.Errorf("an int64 query outside IntegerEq's range: %v, want ErrEncoding", err)
82+
}
83+
5684
for _, count := range []int64{math.MaxInt32 + 1, math.MinInt32 - 1} {
5785
_, err := testusers.EncryptWidened(ctx, cipher, []testusers.Widened{{Count: count}})
5886
if !errors.Is(err, encrypt.ErrEncoding) {

0 commit comments

Comments
 (0)