Ejemplo n.º 1
0
        public (byte[], bool) Run(byte[] inputData)
        {
            inputData ??= Bytes.Empty;
            if (inputData.Length % PairSize > 0)
            {
                // note that it will not happen in case of null / 0 length
                return(Bytes.Empty, false);
            }

            (byte[], bool)result;

            Span <byte> output  = stackalloc byte[32];
            bool        success = ShamatarLib.BlsPairing(inputData, output);

            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Bytes.Empty, false);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public (byte[], bool) Run(byte[] inputData, IReleaseSpec releaseSpec)
        {
            const int expectedInputLength = 4 * BlsParams.LenFp;

            if (inputData.Length != expectedInputLength)
            {
                return(Array.Empty <byte>(), false);
            }

            // Span<byte> inputDataSpan = stackalloc byte[expectedInputLength];
            // inputData.PrepareEthInput(inputDataSpan);

            (byte[], bool)result;

            Span <byte> output  = stackalloc byte[2 * BlsParams.LenFp];
            bool        success = ShamatarLib.BlsG1Add(inputData, output);

            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Array.Empty <byte>(), false);
            }

            return(result);
        }
Ejemplo n.º 3
0
        public (byte[], bool) Run(byte[] inputData)
        {
            const int expectedInputLength = 64;

            if (inputData.Length != expectedInputLength)
            {
                return(Array.Empty <byte>(), false);
            }

            // Span<byte> inputDataSpan = stackalloc byte[expectedInputLength];
            // inputData.PrepareEthInput(inputDataSpan);

            (byte[], bool)result;

            Span <byte> output  = stackalloc byte[128];
            bool        success = ShamatarLib.BlsMapToG1(inputData, output);

            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Array.Empty <byte>(), false);
            }

            return(result);
        }
Ejemplo n.º 4
0
        public (byte[], bool) Run(byte[] inputData, IReleaseSpec releaseSpec)
        {
            Metrics.Bn256PairingPrecompile++;
            inputData ??= Array.Empty <byte>();

            (byte[], bool)result;
            if (inputData.Length % PairSize > 0)
            {
                // note that it will not happen in case of null / 0 length
                result = (Array.Empty <byte>(), false);
            }
            else
            {
                /* we modify input in place here and this is save for EVM but not
                 * safe in benchmarks so we need to remember to clone */
                Span <byte> output          = stackalloc byte[64];
                Span <byte> inputDataSpan   = inputData.AsSpan();
                Span <byte> inputReshuffled = stackalloc byte[PairSize];
                for (int i = 0; i < inputData.Length / PairSize; i++)
                {
                    inputDataSpan.Slice(i * PairSize + 0, 64).CopyTo(inputReshuffled.Slice(0, 64));
                    inputDataSpan.Slice(i * PairSize + 64, 32).CopyTo(inputReshuffled.Slice(96, 32));
                    inputDataSpan.Slice(i * PairSize + 96, 32).CopyTo(inputReshuffled.Slice(64, 32));
                    inputDataSpan.Slice(i * PairSize + 128, 32).CopyTo(inputReshuffled.Slice(160, 32));
                    inputDataSpan.Slice(i * PairSize + 160, 32).CopyTo(inputReshuffled.Slice(128, 32));
                    inputReshuffled.CopyTo(inputDataSpan.Slice(i * PairSize, PairSize));
                }

                bool success = ShamatarLib.Bn256Pairing(inputData.AsSpan(), output);

                if (success)
                {
                    result = (output.Slice(0, 32).ToArray(), true);
                }
                else
                {
                    result = (Array.Empty <byte>(), false);
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        public (byte[], bool) Run(byte[] inputData)
        {
            Span<byte> inputDataSpan = stackalloc byte[4 * BlsParams.LenFp + BlsParams.LenFr];
            inputData.PrepareEthInput(inputDataSpan);

            (byte[], bool) result;
            
            Span<byte> output = stackalloc byte[4 * BlsParams.LenFp];
            bool success = ShamatarLib.BlsG2Mul(inputDataSpan, output);
            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Bytes.Empty, false);
            }

            return result;
        }
Ejemplo n.º 6
0
        public unsafe (ReadOnlyMemory <byte>, bool) Run(ReadOnlyMemory <byte> inputData, IReleaseSpec releaseSpec)
        {
            Metrics.Bn256AddPrecompile++;
            Span <byte> inputDataSpan = stackalloc byte[128];

            inputData.PrepareEthInput(inputDataSpan);

            Span <byte> output  = stackalloc byte[64];
            bool        success = ShamatarLib.Bn256Add(inputDataSpan, output);

            (byte[], bool)result;
            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Array.Empty <byte>(), false);
            }

            return(result);
        }
Ejemplo n.º 7
0
        public unsafe (byte[], bool) Run(byte[] inputData)
        {
            Metrics.Bn256AddPrecompile++;
            Span <byte> inputDataSpan = stackalloc byte[128];

            inputData.PrepareEthInput(inputDataSpan);

            Span <byte> output  = stackalloc byte[64];
            bool        success = ShamatarLib.Bn256Add(inputDataSpan, output);

            (byte[], bool)result;
            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Bytes.Empty, false);
            }

            return(result);
        }
Ejemplo n.º 8
0
        public (byte[], bool) Run(byte[] inputData)
        {
            Span <byte> inputDataSpan = stackalloc byte[64];

            inputData.PrepareEthInput(inputDataSpan);

            (byte[], bool)result;

            Span <byte> output  = stackalloc byte[128];
            bool        success = ShamatarLib.BlsMapToG1(inputDataSpan, output);

            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Bytes.Empty, false);
            }

            return(result);
        }
Ejemplo n.º 9
0
        public (ReadOnlyMemory <byte>, bool) Run(ReadOnlyMemory <byte> inputData, IReleaseSpec releaseSpec)
        {
            if (inputData.Length % PairSize > 0 || inputData.Length == 0)
            {
                return(Array.Empty <byte>(), false);
            }

            (byte[], bool)result;

            Span <byte> output  = stackalloc byte[32];
            bool        success = ShamatarLib.BlsPairing(inputData.Span, output);

            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Array.Empty <byte>(), false);
            }

            return(result);
        }
Ejemplo n.º 10
0
        public (byte[], bool) Run(byte[] inputData, IReleaseSpec releaseSpec)
        {
            inputData ??= Array.Empty <byte>();
            if (inputData.Length % ItemSize > 0 || inputData.Length == 0)
            {
                return(Array.Empty <byte>(), false);
            }

            (byte[], bool)result;

            Span <byte> output  = stackalloc byte[2 * BlsParams.LenFp];
            bool        success = ShamatarLib.BlsG1MultiExp(inputData, output);

            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Array.Empty <byte>(), false);
            }

            return(result);
        }
Ejemplo n.º 11
0
        public (byte[], bool) Run(byte[] inputData)
        {
            inputData ??= Array.Empty <byte>();
            if (inputData.Length % PairSize > 0 || inputData.Length == 0)
            {
                return(Array.Empty <byte>(), false);
            }

            (byte[], bool)result;

            Span <byte> output  = stackalloc byte[32];
            bool        success = ShamatarLib.BlsPairing(inputData, output);

            if (success)
            {
                result = (output.ToArray(), true);
            }
            else
            {
                result = (Array.Empty <byte>(), false);
            }

            return(result);
        }