From c63b841899f310efba8c3f67436d239b63bb915e Mon Sep 17 00:00:00 2001 From: arnaucube Date: Sun, 10 Oct 2021 11:36:07 +0200 Subject: [PATCH] Add blog link, small pending updates --- README.md | 6 +- go-shamirsecretsharing/README.md | 10 +- go-shamirsecretsharing/shamirsecretsharing.go | 20 +- .../shamirsecretsharing_test.go | 8 +- shamirsecretsharing-rs/.gitignore | 1 + shamirsecretsharing-rs/Cargo.lock | 273 ------------------ shamirsecretsharing-rs/README.md | 2 +- shamirsecretsharing-rs/src/lib.rs | 86 +++--- 8 files changed, 73 insertions(+), 333 deletions(-) delete mode 100644 shamirsecretsharing-rs/Cargo.lock diff --git a/README.md b/README.md index 8a7fc7c..5f5ff5e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # Shamir's Secret Sharing -This repo contains `Rust` & `Go` implementations of [Shamir's Secret Sharing](https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing) algorithm. The `Go` implementation also has a compiled Web Assembly (WASM) version from the Go code to be used from the browser. +> Warning: this has been done to study, do not use. + +This repo contains `Rust` & `Go` implementations of [Shamir's Secret Sharing](https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing) algorithm. A blog post explaining the concepts can be found at https://arnaucube.com/blog/shamir-secret-sharing.html . + +The `Go` implementation also has a compiled Web Assembly (WASM) version from the Go code to be used from the browser. - `go`: [go-shamirsecretsharing](https://github.com/arnaucube/shamirsecretsharing/tree/master/go-shamirsecretsharing): Go lib + WASM lib - `rust`: [shamirsecretsharing-rs](https://github.com/arnaucube/shamirsecretsharing/tree/master/shamirsecretsharing-rs) diff --git a/go-shamirsecretsharing/README.md b/go-shamirsecretsharing/README.md index 4414fb1..2a13636 100644 --- a/go-shamirsecretsharing/README.md +++ b/go-shamirsecretsharing/README.md @@ -21,7 +21,7 @@ Call the library from javascript: // Create shares from a secret // nNeededShares: number of secrets needed // nShares: number of shares -// p: random point +// p: size of finite field // k: secret to share createShares(nNeededShares, nShares, p, k); ``` @@ -29,12 +29,12 @@ createShares(nNeededShares, nShares, p, k); ## Usage from Go ```go // define secret to share -k, ok := new(big.Int).SetString("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 10) +k, ok := new(big.Int).SetString("12345678901234567890123456789012345678", 10) assert.True(t, ok) -// define random prime -p, err := rand.Prime(rand.Reader, bits/2) -assert.Nil(t, err) +// define the field +p, ok := new(big.Int).SetString("170141183460469231731687303715884105727", 10) +assert.True(t, ok) // define how many shares want to generate nShares := big.NewInt(int64(6)) diff --git a/go-shamirsecretsharing/shamirsecretsharing.go b/go-shamirsecretsharing/shamirsecretsharing.go index 5178801..dab40aa 100644 --- a/go-shamirsecretsharing/shamirsecretsharing.go +++ b/go-shamirsecretsharing/shamirsecretsharing.go @@ -6,15 +6,21 @@ import ( "math/big" ) -const ( - // bits = 1024 - bits = 2048 -) +func randBigInt(p *big.Int) (*big.Int, error) { + b := make([]byte, 32) + _, err := rand.Read(b) + if err != nil { + return nil, err + } + r := new(big.Int).SetBytes(b) + rp := new(big.Int).Mod(r, p) + return rp, nil +} // Create calculates the secrets to share from given parameters // t: number of secrets needed // n: number of shares -// p: random point +// p: size of finite field // k: secret to share func Create(t, n, p, k *big.Int) (result [][]*big.Int, err error) { if k.Cmp(p) > 0 { @@ -24,11 +30,11 @@ func Create(t, n, p, k *big.Int) (result [][]*big.Int, err error) { var basePolynomial []*big.Int basePolynomial = append(basePolynomial, k) for i := 0; i < int(t.Int64())-1; i++ { - randPrime, err := rand.Prime(rand.Reader, bits/2) + x, err := randBigInt(p) if err != nil { return result, err } - basePolynomial = append(basePolynomial, randPrime) + basePolynomial = append(basePolynomial, x) } //calculate shares, based on the basePolynomial diff --git a/go-shamirsecretsharing/shamirsecretsharing_test.go b/go-shamirsecretsharing/shamirsecretsharing_test.go index a32e2a3..b94390e 100644 --- a/go-shamirsecretsharing/shamirsecretsharing_test.go +++ b/go-shamirsecretsharing/shamirsecretsharing_test.go @@ -2,7 +2,6 @@ package shamirsecretsharing import ( "bytes" - "crypto/rand" "math/big" "testing" @@ -10,11 +9,12 @@ import ( ) func TestCreate(t *testing.T) { - k, ok := new(big.Int).SetString("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 10) + k, ok := new(big.Int).SetString("12345678901234567890123456789012345678", 10) assert.True(t, ok) - p, err := rand.Prime(rand.Reader, bits/2) - assert.Nil(t, err) + // 2 ** 127 - 1 + p, ok := new(big.Int).SetString("170141183460469231731687303715884105727", 10) + assert.True(t, ok) nShares := big.NewInt(int64(6)) nNeededShares := big.NewInt(int64(3)) diff --git a/shamirsecretsharing-rs/.gitignore b/shamirsecretsharing-rs/.gitignore index ea8c4bf..96ef6c0 100644 --- a/shamirsecretsharing-rs/.gitignore +++ b/shamirsecretsharing-rs/.gitignore @@ -1 +1,2 @@ /target +Cargo.lock diff --git a/shamirsecretsharing-rs/Cargo.lock b/shamirsecretsharing-rs/Cargo.lock deleted file mode 100644 index a67d394..0000000 --- a/shamirsecretsharing-rs/Cargo.lock +++ /dev/null @@ -1,273 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -[[package]] -name = "autocfg" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "bitflags" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "fuchsia-cprng" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "libc" -version = "0.2.58" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "num" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-complex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", - "num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-bigint" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-complex" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-integer" -version = "0.1.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-iter" -version = "0.1.39" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-rational" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-traits" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", - "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_chacha" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_core" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "rand_hc" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_isaac" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_jitter" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_os" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", - "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_pcg" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_xorshift" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rdrand" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "shamirsecretsharing-rs" -version = "0.0.1" -dependencies = [ - "num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "winapi" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[metadata] -"checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" -"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" -"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" -"checksum libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "6281b86796ba5e4366000be6e9e18bf35580adf9e63fbe2294aadb587613a319" -"checksum num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf4825417e1e1406b3782a8ce92f4d53f26ec055e3622e1881ca8e9f5f9e08db" -"checksum num-bigint 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "57450397855d951f1a41305e54851b1a7b8f5d2e349543a02a2effe25459f718" -"checksum num-complex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fcb0cf31fb3ff77e6d2a6ebd6800df7fdcd106f2ad89113c9130bcd07f93dffc" -"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" -"checksum num-iter 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "76bd5272412d173d6bf9afdf98db8612bbabc9a7a830b7bfc9c188911716132e" -"checksum num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f2885278d5fe2adc2f75ced642d52d879bffaceb5a2e0b1d4309ffdfb239b454" -"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" -"checksum rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9" -"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" -"checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" -"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" -"checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0" -"checksum rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" -"checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" -"checksum rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" -"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" -"checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" -"checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" -"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" -"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" -"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/shamirsecretsharing-rs/README.md b/shamirsecretsharing-rs/README.md index 3b6ae4c..44047e3 100644 --- a/shamirsecretsharing-rs/README.md +++ b/shamirsecretsharing-rs/README.md @@ -4,7 +4,7 @@ Shamir's Secret Sharing in Rust ## Usage ```rust -// create 6 shares from k, given the rand p +// create 6 shares from k, on the Fp // where to recover will be needed 3 shares let s = create(3, 6, &p, &k); diff --git a/shamirsecretsharing-rs/src/lib.rs b/shamirsecretsharing-rs/src/lib.rs index f361ac4..33524fc 100644 --- a/shamirsecretsharing-rs/src/lib.rs +++ b/shamirsecretsharing-rs/src/lib.rs @@ -1,35 +1,34 @@ -extern crate rand; extern crate num; extern crate num_bigint; extern crate num_traits; +extern crate rand; use std::str::FromStr; -use num_bigint::RandBigInt; use num::pow::pow; use num::Integer; - +use num_bigint::RandBigInt; use num_bigint::{BigInt, ToBigInt}; -use num_traits::{Zero, One}; +use num_traits::{One, Zero}; fn modulus(a: &BigInt, m: &BigInt) -> BigInt { - ((a%m) + m) % m + ((a % m) + m) % m } -pub fn create(t: u32, n: u32,p: &BigInt, k: &BigInt) -> Vec<[BigInt;2]> { +pub fn create(t: u32, n: u32, p: &BigInt, k: &BigInt) -> Vec<[BigInt; 2]> { // t: number of secrets needed // n: number of shares - // p: random point + // p: size of finite field // k: secret to share - if k>p { + if k > p { println!("\nERROR: need k = Vec::new(); base_polynomial.push(k.clone()); - for _ in 0..t as usize-1 { + for _ in 0..t as usize - 1 { let mut rng = rand::thread_rng(); let a = rng.gen_bigint(1024); base_polynomial.push(a); @@ -37,11 +36,11 @@ pub fn create(t: u32, n: u32,p: &BigInt, k: &BigInt) -> Vec<[BigInt;2]> { // calculate shares, based on the base_polynomial let mut shares: Vec = Vec::new(); - for i in 1..n+1 { + for i in 1..n + 1 { let mut p_res: BigInt = Zero::zero(); let mut x = 0; for pol_elem in &base_polynomial { - if x==0 { + if x == 0 { p_res = p_res + pol_elem; } else { let i_pow = pow(i, x); @@ -49,23 +48,23 @@ pub fn create(t: u32, n: u32,p: &BigInt, k: &BigInt) -> Vec<[BigInt;2]> { p_res = p_res + curr_elem; p_res = modulus(&p_res, p); } - x = x+1; + x = x + 1; } shares.push(p_res); } pack_shares(shares) } -fn pack_shares(shares: Vec) -> Vec<[BigInt;2]> { - let mut r: Vec<[BigInt;2]> = Vec::new(); +fn pack_shares(shares: Vec) -> Vec<[BigInt; 2]> { + let mut r: Vec<[BigInt; 2]> = Vec::new(); for i in 0..shares.len() { - let curr: [BigInt;2] = [shares[i].clone(), (i+1).to_bigint().unwrap()]; + let curr: [BigInt; 2] = [shares[i].clone(), (i + 1).to_bigint().unwrap()]; r.push(curr); } r } -fn unpack_shares(s: Vec<[BigInt;2]>) -> (Vec, Vec) { +fn unpack_shares(s: Vec<[BigInt; 2]>) -> (Vec, Vec) { let mut shares: Vec = Vec::new(); let mut is: Vec = Vec::new(); for i in 0..s.len() { @@ -100,9 +99,9 @@ pub fn kalinski_inv(a: &BigInt, modulo: &BigInt) -> BigInt { // This Phase I indeed is the Binary GCD algorithm , a version o Stein's algorithm // which tries to remove the expensive division operation away from the Classical // Euclidean GDC algorithm replacing it for Bit-shifting, subtraction and comparaison. - // + // // Output = `a^(-1) * 2^k (mod l)` where `k = log2(modulo) == Number of bits`. - // + // // Stein, J.: Computational problems associated with Racah algebra.J. Comput. Phys.1, 397–405 (1967). let phase1 = |a: &BigInt| -> (BigInt, u64) { assert!(a != &BigInt::zero()); @@ -114,35 +113,31 @@ pub fn kalinski_inv(a: &BigInt, modulo: &BigInt) -> BigInt { let mut k = 0u64; while v > BigInt::zero() { - match(u.is_even(), v.is_even(), u > v, v >= u) { + match (u.is_even(), v.is_even(), u > v, v >= u) { // u is even (true, _, _, _) => { - u = u >> 1; s = s << 1; - }, + } // u isn't even but v is even (false, true, _, _) => { - v = v >> 1; r = &r << 1; - }, + } // u and v aren't even and u > v (false, false, true, _) => { - u = &u - &v; u = u >> 1; r = &r + &s; s = &s << 1; - }, + } // u and v aren't even and v > u (false, false, false, true) => { - v = &v - &u; v = v >> 1; s = &r + &s; r = &r << 1; - }, + } (false, false, false, false) => panic!("Unexpected error has ocurred."), } k += 1; @@ -155,8 +150,8 @@ pub fn kalinski_inv(a: &BigInt, modulo: &BigInt) -> BigInt { // Phase II performs some adjustments to obtain // the Montgomery inverse. - // - // We implement it as a clousure to be able to grap the + // + // We implement it as a clousure to be able to grap the // kalinski_inv scope to get `modulo` variable. let phase2 = |r: &BigInt, k: &u64| -> BigInt { let mut rr = r.clone(); @@ -166,13 +161,13 @@ pub fn kalinski_inv(a: &BigInt, modulo: &BigInt) -> BigInt { match rr.is_even() { true => { rr = rr >> 1; - }, + } false => { rr = (rr + modulo) >> 1; } } } - rr + rr }; let (r, z) = phase1(&a.clone()); @@ -180,7 +175,7 @@ pub fn kalinski_inv(a: &BigInt, modulo: &BigInt) -> BigInt { phase2(&r, &z) } -pub fn lagrange_interpolation(p: &BigInt, shares_packed: Vec<[BigInt;2]>) -> BigInt { +pub fn lagrange_interpolation(p: &BigInt, shares_packed: Vec<[BigInt; 2]>) -> BigInt { let mut res_n: BigInt = Zero::zero(); let mut res_d: BigInt = Zero::zero(); let (shares, sh_i) = unpack_shares(shares_packed); @@ -198,7 +193,8 @@ pub fn lagrange_interpolation(p: &BigInt, shares_packed: Vec<[BigInt;2]>) -> Big } let numerator: BigInt = &shares[i] * &lagrange_numerator; - let quo: BigInt = (&numerator / &lagrange_denominator) + (&lagrange_denominator ) % &lagrange_denominator; + let quo: BigInt = + (&numerator / &lagrange_denominator) + (&lagrange_denominator) % &lagrange_denominator; if quo != Zero::zero() { res_n = res_n + quo; } else { @@ -218,7 +214,6 @@ pub fn lagrange_interpolation(p: &BigInt, shares_packed: Vec<[BigInt;2]>) -> Big r } - #[cfg(test)] mod tests { @@ -227,15 +222,16 @@ mod tests { #[test] fn test_create_and_lagrange_interpolation() { - let mut rng = rand::thread_rng(); - let p = rng.gen_biguint(1024).to_bigint().unwrap(); - println!("p: {:?}", p); - let k = BigInt::parse_bytes(b"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 10).unwrap(); + // 2 ** 127 - 1 + let p = BigInt::parse_bytes(b"170141183460469231731687303715884105727", 10).unwrap(); + println!("p: {:?}", p.to_string()); + + let k = BigInt::parse_bytes(b"12345678901234567890123456789012345678", 10).unwrap(); let s = create(3, 6, &p, &k); // println!("s: {:?}", s); - let mut shares_to_use: Vec<[BigInt;2]> = Vec::new(); + let mut shares_to_use: Vec<[BigInt; 2]> = Vec::new(); shares_to_use.push(s[2].clone()); shares_to_use.push(s[1].clone()); shares_to_use.push(s[0].clone()); @@ -263,10 +259,16 @@ mod tests { // Tested: 182687704666362864775460604089535377456991567872 // Expected for: inverse_mod(a, l) computed on SageMath: // `7155219595916845557842258654134856828180378438239419449390401977965479867845`. - let modul3 = BigInt::from_str("7237005577332262213973186563042994240857116359379907606001950938285454250989").unwrap(); + let modul3 = BigInt::from_str( + "7237005577332262213973186563042994240857116359379907606001950938285454250989", + ) + .unwrap(); let d = BigInt::from_str("182687704666362864775460604089535377456991567872").unwrap(); - let res4 = kalinski_inv(&d, &modul3); - let expected4 = BigInt::from_str("7155219595916845557842258654134856828180378438239419449390401977965479867845").unwrap(); + let res4 = kalinski_inv(&d, &modul3); + let expected4 = BigInt::from_str( + "7155219595916845557842258654134856828180378438239419449390401977965479867845", + ) + .unwrap(); assert_eq!(expected4, res4); } }