Skip to content

Commit 219e3a1

Browse files
committed
implement includes
1 parent 5009c5e commit 219e3a1

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/lib/provable/dynamic-array.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,20 @@ class DynamicArrayBase<ProvableValue = any, Value = any> {
511511
}
512512
}
513513

514+
/**
515+
* Checks whether the dynamic array includes a value.
516+
*
517+
* @param value
518+
* @returns
519+
*/
520+
includes(value: ProvableValue): Bool {
521+
let type = this.innerType;
522+
let isIncluded = this.array.map((t) => Provable.equal(this.innerType, t, value));
523+
let isSome = isIncluded.reduce((acc, curr) => acc.or(curr), new Bool(false));
524+
return isSome;
525+
}
526+
527+
514528
// cached variables to not duplicate constraints if we do something like
515529
// array.get(i), array.set(i, ..) on the same index
516530
#indexMasks: Map<Field, Bool[]> = new Map();

src/lib/provable/test/dynamic-array.unit-test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ import { Provable } from '../provable.js';
312312
expectThrows(() => {
313313
bytes.insert(new Field(4), new UInt8(5));
314314
}, 'Cannot insert out-of-bounds');
315+
316+
// Checking inclusion of elements
317+
assert(bytes.includes(new UInt8(1)).toBoolean());
318+
assert(bytes.includes(new UInt8(20)).not().toBoolean());
315319
}
316320

317321
// Using dynamic arrays as private input
@@ -561,6 +565,10 @@ await Provable.runAndCheck(() => {
561565
for (let i = 0; i < 8; i++) {
562566
assert(bytes.get(new Field(i)).value.equals(new Field(i + 1)));
563567
}
568+
569+
// Checking inclusion of elements
570+
assert(bytes.includes(new UInt8(1)).toBoolean());
571+
assert(bytes.includes(new UInt8(20)).not().toBoolean());
564572
},
565573
},
566574
},

0 commit comments

Comments
 (0)