# sss-vlang

An implementation of Shamir's Secret Sharing algorithm in V (vlang).

[![Build status](https://builds.sr.ht/~ser/sss-vlang/.build.yml.svg)](https://builds.sr.ht/~ser/sss-vlang/.build.yml?)
★ [Releases](https://downloads.ser1.net/software/sss-vlang/)
★ [License](https://hg.sr.ht/~ser/sss-vlang/raw/LICENSE?rev=tip)
★ [Changelog](https://hg.sr.ht/~ser/sss-vlang/browse/CHANGELOG.md?rev=tip)
★ [Issues](https://todo.sr.ht/~ser/sss-vlang)
★ [Mailing List](https://lists.sr.ht/~ser/sss-vlang)  

## About

The library does not have any external dependencies.

This is a translation from [sssa-golang](https://github.com/SSSaaS/sssa-golang).
sssa-golang has been [independently declared](https://dsprenkels.github.io/sss/)
to be well-written, so it's (IMO) a good project from which to steal.

This project fails more reliably than the Go project when error conditions are
encountered; sssa-golang ignores many errors that can be returned by the
libraries it uses; sss-vlang always propagates these errors out to the caller.

This library differs from sssa-golang in that there is no global prime variable,
and since it has no state, it is thread-safe.

## Getting / Building

There should be pre-built binaries for Linux; if you would like binaries for
your platform, send a message to the mailing list.

To compile yourself, have V installed, check out the repository, and then
compile the binary you want. There are two: a CLI and a GUI.

*WARNING* You currently need the unreleased weekly (head) of V to compile sss-
vlang, for `math.big.Integer.mod_euclid()`.

```bash
$ hg clone https://hg.sr.ht/~ser/sss-vlang
$ cd sss-vlang
$ v build -o sssc ./sssc
$ v build -o sssg ./sssg
```

## Usage

This library can be used in other projects. The project includes two utility
programs.

### As a library

```v
// Total shares are the number of shares to generate; min_shares is the minimum
// number of shares required to re-combine the secret.
// `new()` returns an error if min_shares > total_shares
secs := sss.new(min_shares, total_shares, secret) or {return}
println('${secs}')
assert secret == combine(secs) or {return}
```

A custom prime number may be provided, either as a math.big.Integer, or as a
string representation for large primes.

```
secs2 := sss.new(min_shares, total_shares, secret, sprime: '115792089237316195423570985008687907853269984665640564039457584007913129639747')
secs3 := sss.new(min_shares, total_shares, secret, iprime: 7)
```
  
The default prime is large, and should be sufficient for most uses. If a
custom prime is provided to create shares, it must also be provided to
correctly combine the shares.

### Command line tool (sssc)

```bash
$ sssc --help
# ...
$ sssc gen 2 5 'the silly monkey ate a persimmon'
dnsUlZEoRADs7ITapK9lyMUl7r5Vs_CCqLsMPSGswvU=U2fgOCWsztrMipBTflGh4rxogidSXVH6OSw12ZnQ7lk=
R2NJnAWJ2akXWrX6DjFaXxRg5NxeBPcMm1yrBOM6p9A=P4s19g6O9OQiAvih5ZDuumOoEDxIB-1ywC4gHMz7XOc=
SvJTCcSrawjhQd_uJhTrTFce_cXiW2q5C5sf_J9k-rU=qRtKDasVN0DVs4i2sFGVPBdGiSnaFQTdVJYQP3YQVAo=
ShxW-qTtXWOxwWKt9BgS1QXCayE4mdzxJgZ3WxwiiY0=Uz5tQ_l8im8WX4i1fR3vjZjZyPHuj9ndqcIUZDkLoMo=
UAid5jzVg1Ch_Ub0Ypyo9KWLCOPv6XktZcsYr5Qd6Zc=TQnI7JO4Zz5fAxbtfzF1ZDBE5n4iMHZOMkUA6_f-i7I=
$ sssc comb \
  R2NJnAWJ2akXWrX6DjFaXxRg5NxeBPcMm1yrBOM6p9A=P4s19g6O9OQiAvih5ZDuumOoEDxIB-1ywC4gHMz7XOc= \
  UAid5jzVg1Ch_Ub0Ypyo9KWLCOPv6XktZcsYr5Qd6Zc=TQnI7JO4Zz5fAxbtfzF1ZDBE5n4iMHZOMkUA6_f-i7I=
the silly monkey ate a persimmon
```

### GUI tool (sssg)

[*Note* This is a WIP. It can correctly generate shares from a secret; the
state of combining is in flux, and the UI overall is unpolished.]{.sidenote}

```bash
$ sssg
```

## References

- [Shamir Secret Sharing library](https://dsprenkels.github.io/sss/)
- [The Go library](https://github.com/SSSaaS/sssa-golang) from which this code was derived.
- [The API Library for SSSaaS](https://github.com/SSSAAS/sssaas-golang).
