Election-Tech-Initiative / Election-Tech-Initiative/electionguard-core2
✨ Expose big math functions on group element instances
- Dominant language
- C#
- Stars
- 14
- Forks
- 12
- PR merge metrics
- No merged PRs in 30d
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Suggestion
in bigmath.cs we compute math operations immutably (as expected):
```csharp
public static ElementModP MultModP(ElementModP lhs, ElementModP rhs)
{
var status = External.MultModP(lhs.Handle, rhs.Handle,
out var value);
status.ThrowIfError();
return value.IsInvalid ? null : new ElementModP(value);
}
```
in elementmodp.cs we compute them mutably but we have to dispose of the old object since it is using the same underlying c function operations.
public void MultModP(ElementModP rhs)
{
var status = NativeInterface.ElementModP.MultModP(Handle, rhs.Handle,
out NativeInterface.ElementModP.ElementModPHandle value);
Handle.Dispose();
status.ThrowIfError();
Handle = value;
}
one thing we could do is expose these commonly used functions (mul, mod, add, etc.) as instance members in the c++ code so we can take advantage of swap.
There are really 2 patterns here i think:
1. `BigMath.StaticFunction(a,b)` does not mutate input parameters and returns a new object
2. `ElementModP.InstanceFunction(b)` mutates the instance and may or may not return this
for use case 2 we can let the core library do the heavy lifting and avoid excess construction.
### Possible Implementation
_No response_
### Anything else?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.