GithubHelp home page GithubHelp logo

Comments (6)

Helios-vmg avatar Helios-vmg commented on June 2, 2024 1

I think the ultimate source of the issue is that the interface is exposing an implementation detail to the caller. Secret sharing is used to split strings of bits, not integers. If the library internally uses arbitrary precision numbers to implement the secret sharing scheme, and it is important for these numbers to always be positive, the library should only accept raw byte buffers and use whatever method is appropriate to deserialize these buffers into positive integers. It shouldn't be the responsibility of the caller to sanitize its own input.

from secretsharingdotnet.

shinji-san avatar shinji-san commented on June 2, 2024

Thank you.

I will try to find time to fix the bug in the next few days.

from secretsharingdotnet.

shinji-san avatar shinji-san commented on June 2, 2024

The following solution is not a workaround, only another way to use the library. Pls use it until the bug is fixed:

        static double TestShamir2()
        {
            int ok = 0;
            const int total = 1000;
            var sss = new ShamirsSecretSharing<BigInteger>(new ExtendedEuclideanAlgorithm<BigInteger>());
            var rng = new Random();
            for (int i = 0; i < total; i++)
            {
                var message = new byte[32];
                rng.NextBytes(message);
                const int n = 5;
                var s = Convert.ToBase64String(message);
                var shares = sss.MakeShares((n + 1) / 2, n, s);
                var reconstructed =
                    Convert.FromBase64String(sss.Reconstruction(shares.Take((n + 1) / 2).ToArray())
                        .ToString());
                if (message.SequenceEqual(reconstructed))
                    ok++;
            }
            return (double) ok / total;
        }

from secretsharingdotnet.

shinji-san avatar shinji-san commented on June 2, 2024

Bug is fixed and the fix is part of the next release (>v0.5.0).

from secretsharingdotnet.

shinji-san avatar shinji-san commented on June 2, 2024

Hi @Helios-vmg ,

Please see the Whitepaper

The coefficients a_1 ..a_k-1 in q(x) are randomly chosen from a uniform distribution over the integers in [0, p)

Further more the modular arithmetic (mathematical) is used. So the secret must be a positive value.

The code below generates randomly negative secrets.

The following function returns a double around 0.5, instead of 1 as would be expected:

static double TestShamir()
{
    int ok = 0;
    const int total = 1000;
    var sss = new ShamirsSecretSharing<BigInteger>(new ExtendedEuclideanAlgorithm<BigInteger>());
    var rng = new Random();
    for (int i = 0; i < total; i++)
    {
        var message = new byte[32];
        rng.NextBytes(message);
        const int n = 5;
        var secret = new Secret<BigInteger>(Convert.ToBase64String(message));
        var shares = sss.MakeShares((n + 1) / 2, n, secret);
        var reconstructed =
            Convert.FromBase64String(sss.Reconstruction(shares.Take((n + 1) / 2).ToArray())
                .ToBase64());
        if (message.SequenceEqual(reconstructed))
            ok++;
    }
    return (double) ok / total;
}

I will fix the message array in the unit test project, because the two's complement representation always interprets the highest-order bit of the last byte in the array (byte at position message.Length - 1) as the sign bit. A negative sign must be avoid.

from secretsharingdotnet.

shinji-san avatar shinji-san commented on June 2, 2024

Thank you for your Feedback. Now I have an idea to fix it. However the new version of this library can't be used to reconstruct secrets which are splitted by version 0.6.0 or older. It is a breaking change.

from secretsharingdotnet.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.