Esempio n. 1
0
    def test_prove_2(self):
        bpi = bp.BulletProofBuilder()
        val = crypto.sc_init((1 << 30) - 1 + 16)
        mask = crypto.random_scalar()

        bp_res = bpi.prove(val, mask)
        bpi.verify(bp_res)
Esempio n. 2
0
    def test_masks(self):
        bpi = bp.BulletProofBuilder()
        self.mask_consistency_check(bpi)

        # Randomized masks
        bpi.use_det_masks = False
        self.mask_consistency_check(bpi)
Esempio n. 3
0
    def test_prove(self):
        bpi = bp.BulletProofBuilder()
        val = crypto.sc_init(123)
        mask = crypto.sc_init(432)

        bp_res = bpi.prove(val, mask)
        bpi.verify(bp_res)
Esempio n. 4
0
    def test_prove_random_masks(self):
        bpi = bp.BulletProofBuilder()
        bpi.use_det_masks = False  # trully randomly generated mask vectors
        val = crypto.sc_init((1 << 30) - 1 + 16)
        mask = crypto.random_scalar()

        bp_res = bpi.prove(val, mask)
        bpi.verify(bp_res)
Esempio n. 5
0
def prove_range_bp_batch(amounts, masks):
    """Calculates Bulletproof in batches"""
    from apps.monero.xmr import bulletproof as bp

    bpi = bp.BulletProofBuilder()
    bp_proof = bpi.prove_batch([crypto.sc_init(a) for a in amounts], masks)
    del (bpi, bp)
    gc.collect()

    return bp_proof
Esempio n. 6
0
 def test_verify_batch_1(self):
     bpi = bp.BulletProofBuilder()
     bpi.verify_batch([self.bproof_1()])
     bpi.verify_batch([self.bproof_2()])
     bpi.verify_batch([self.bproof_4()])
     bpi.verify_batch([self.bproof_8()])
     bpi.verify_batch([self.bproof_16()])
     with self.assertRaises(Exception):
         bpi.verify_batch([self.bproof_2_invalid()])
     with self.assertRaises(Exception):
         bpi.verify_batch([self.bproof_2_invalid()])
Esempio n. 7
0
def prove_range_bp_batch(
        amounts: list[int],
        masks: list[crypto.Scalar],
        bp_plus: bool = False) -> Bulletproof | BulletproofPlus:
    """Calculates Bulletproof in batches"""
    from apps.monero.xmr import bulletproof as bp

    bpi = bp.BulletProofPlusBuilder() if bp_plus else bp.BulletProofBuilder()
    bp_proof = bpi.prove_batch([crypto.Scalar(a) for a in amounts], masks)
    del (bpi, bp)
    gc.collect()

    return bp_proof
Esempio n. 8
0
    def test_prove_testnet(self):
        bpi = bp.BulletProofBuilder()
        val = crypto.sc_init(123)
        mask = crypto.sc_init(432)

        bp_res = bpi.prove_testnet(val, mask)
        bpi.verify_testnet(bp_res)

        try:
            bp_res.S[0] += 1
            bpi.verify(bp_res)
            self.fail("Verification should have failed")
        except:
            pass
Esempio n. 9
0
def verify_bp(bp_proof, amounts, masks):
    """Verifies Bulletproof"""
    from apps.monero.xmr import bulletproof as bp

    if amounts:
        bp_proof.V = []
        for i in range(len(amounts)):
            C = crypto.gen_commitment(masks[i], amounts[i])
            crypto.scalarmult_into(C, C, crypto.sc_inv_eight())
            bp_proof.V.append(crypto.encodepoint(C))

    bpi = bp.BulletProofBuilder()
    res = bpi.verify(bp_proof)
    gc.collect()
    return res
Esempio n. 10
0
def verify_bp(
    bp_proof: Bulletproof | BulletproofPlus,
    amounts: list[int],
    masks: list[crypto.Scalar],
) -> bool:
    """Verifies Bulletproof"""
    from apps.monero.xmr import bulletproof as bp

    if amounts:
        bp_proof.V = []
        for i in range(len(amounts)):
            C = crypto.gen_commitment_into(None, masks[i], amounts[i])
            crypto.scalarmult_into(C, C, crypto_helpers.INV_EIGHT_SC)
            bp_proof.V.append(crypto_helpers.encodepoint(C))

    from apps.monero.xmr.serialize_messages.tx_rsig_bulletproof import BulletproofPlus

    bpi = (bp.BulletProofPlusBuilder() if isinstance(bp_proof, BulletproofPlus)
           else bp.BulletProofBuilder())
    res = bpi.verify(bp_proof)
    gc.collect()
    return res
Esempio n. 11
0
    async def diag(ctx, msg, **kwargs) -> Failure:
        log.debug(__name__, "----diagnostics")
        gc.collect()

        if msg.ins == 0:
            check_mem(0)
            return retit()

        elif msg.ins == 1:
            check_mem(1)
            micropython.mem_info(1)
            return retit()

        elif msg.ins == 2:
            log.debug(__name__, "_____________________________________________")
            log.debug(__name__, "_____________________________________________")
            log.debug(__name__, "_____________________________________________")
            return retit()

        elif msg.ins == 3:
            pass

        elif msg.ins == 4:
            total = 0
            monero = 0

            for k, v in sys.modules.items():
                log.info(__name__, "Mod[%s]: %s", k, v)
                total += 1
                if k.startswith("apps.monero"):
                    monero += 1
            log.info(__name__, "Total modules: %s, Monero modules: %s", total, monero)
            return retit()

        elif msg.ins in [5, 6, 7]:
            check_mem()
            from apps.monero.xmr import bulletproof as bp

            check_mem("BP Imported")
            from apps.monero.xmr import crypto

            check_mem("Crypto Imported")

            bpi = bp.BulletProofBuilder()
            bpi.gc_fnc = gc.collect
            bpi.gc_trace = log_trace

            vals = [crypto.Scalar((1 << 30) - 1 + 16), crypto.Scalar(22222)]
            masks = [crypto.random_scalar(), crypto.random_scalar()]
            check_mem("BP pre input")

            if msg.ins == 5:
                bp_res = bpi.prove_testnet(vals[0], masks[0])
                check_mem("BP post prove")
                bpi.verify_testnet(bp_res)
                check_mem("BP post verify")

            elif msg.ins == 6:
                bp_res = bpi.prove(vals[0], masks[0])
                check_mem("BP post prove")
                bpi.verify(bp_res)
                check_mem("BP post verify")

            elif msg.ins == 7:
                bp_res = bpi.prove_batch(vals, masks)
                check_mem("BP post prove")
                bpi.verify(bp_res)
                check_mem("BP post verify")

            return retit()

        return retit()
Esempio n. 12
0
 def test_prove_batch16(self):
     bpi = bp.BulletProofBuilder()
     sv = [crypto.sc_init(137*i) for i in range(16)]
     gamma = [crypto.sc_init(991*i) for i in range(16)]
     proof = bpi.prove_batch(sv, gamma)
     bpi.verify_batch([proof])
Esempio n. 13
0
 def test_prove_batch(self):
     bpi = bp.BulletProofBuilder()
     sv = [crypto.sc_init(123), crypto.sc_init(768)]
     gamma = [crypto.sc_init(456), crypto.sc_init(901)]
     proof = bpi.prove_batch(sv, gamma)
     bpi.verify_batch([proof])
Esempio n. 14
0
 def test_verify(self):
     bpi = bp.BulletProofBuilder()
     self.assertTrue(bpi.verify(self.bproof_1()))
     self.assertTrue(bpi.verify(self.bproof_2()))
     self.assertTrue(bpi.verify(self.bproof_4()))
Esempio n. 15
0
    def test_verify_testnet(self):
        bpi = bp.BulletProofBuilder()

        # fmt: off
        bp_proof = Bulletproof(
            V=[
                bytes([
                    0x67, 0x54, 0xbf, 0x40, 0xcb, 0x45, 0x63, 0x0d, 0x4b, 0xea,
                    0x08, 0x9e, 0xd7, 0x86, 0xec, 0x3c, 0xe5, 0xbd, 0x4e, 0xed,
                    0x8f, 0xf3, 0x25, 0x76, 0xae, 0xca, 0xb8, 0x9e, 0xf2, 0x5e,
                    0x41, 0x16
                ])
            ],
            A=bytes([
                0x96, 0x10, 0x17, 0x66, 0x87, 0x7e, 0xef, 0x97, 0xb3, 0x82,
                0xfb, 0x8e, 0x0c, 0x2a, 0x93, 0x68, 0x9e, 0x05, 0x22, 0x07,
                0xe3, 0x30, 0x94, 0x20, 0x58, 0x6f, 0x5d, 0x01, 0x6d, 0x4e,
                0xd5, 0x88
            ]),
            S=bytes([
                0x50, 0x51, 0x38, 0x32, 0x96, 0x20, 0x7c, 0xc9, 0x60, 0x4d,
                0xac, 0x7c, 0x7c, 0x21, 0xf9, 0xad, 0x1c, 0xc2, 0x2d, 0xee,
                0x88, 0x7b, 0xa2, 0xe2, 0x61, 0x81, 0x46, 0xf5, 0x99, 0xc3,
                0x12, 0x57
            ]),
            T1=bytes([
                0x1a, 0x7d, 0x06, 0x51, 0x41, 0xe6, 0x12, 0xbe, 0xad, 0xd7,
                0x68, 0x60, 0x85, 0xfc, 0xc4, 0x86, 0x0b, 0x39, 0x4b, 0x06,
                0xf7, 0xca, 0xb3, 0x29, 0xdf, 0x1d, 0xbf, 0x96, 0x5f, 0xbe,
                0x8c, 0x87
            ]),
            T2=bytes([
                0x57, 0xae, 0x91, 0x04, 0xfa, 0xac, 0xf3, 0x73, 0x75, 0xf2,
                0x83, 0xd6, 0x9a, 0xcb, 0xef, 0xe4, 0xfc, 0xe5, 0x37, 0x55,
                0x52, 0x09, 0xb5, 0x60, 0x6d, 0xab, 0x46, 0x85, 0x01, 0x23,
                0x9e, 0x47
            ]),
            taux=bytes([
                0x44, 0x7a, 0x87, 0xd9, 0x5f, 0x1b, 0x17, 0xed, 0x53, 0x7f,
                0xc1, 0x4f, 0x91, 0x9b, 0xca, 0x68, 0xce, 0x20, 0x43, 0xc0,
                0x88, 0xf1, 0xdf, 0x12, 0x7b, 0xd7, 0x7f, 0xe0, 0x27, 0xef,
                0xef, 0x0d
            ]),
            mu=bytes([
                0x32, 0xf9, 0xe4, 0xe1, 0xc2, 0xd8, 0xe4, 0xb0, 0x0d, 0x49,
                0xd1, 0x02, 0xbc, 0xcc, 0xf7, 0xa2, 0x5a, 0xc7, 0x28, 0xf3,
                0x05, 0xb5, 0x64, 0x2e, 0xde, 0xcf, 0x01, 0x61, 0xb8, 0x62,
                0xfb, 0x0d
            ]),
            L=[
                bytes([
                    0xde, 0x71, 0xca, 0x09, 0xf9, 0xd9, 0x1f, 0xa2, 0xae, 0xdf,
                    0x39, 0x49, 0x04, 0xaa, 0x6b, 0x58, 0x67, 0x9d, 0x61, 0xa6,
                    0xfa, 0xec, 0x81, 0xf6, 0x4c, 0x15, 0x09, 0x9d, 0x10, 0x21,
                    0xff, 0x39
                ]),
                bytes([
                    0x90, 0x47, 0xbf, 0xf0, 0x1f, 0x72, 0x47, 0x4e, 0xd5, 0x58,
                    0xfb, 0xc1, 0x16, 0x43, 0xb7, 0xd8, 0xb1, 0x00, 0xa4, 0xa3,
                    0x19, 0x9b, 0xda, 0x5b, 0x27, 0xd3, 0x6c, 0x5a, 0x87, 0xf8,
                    0xf0, 0x28
                ]),
                bytes([
                    0x03, 0x45, 0xef, 0x57, 0x19, 0x8b, 0xc7, 0x38, 0xb7, 0xcb,
                    0x9c, 0xe7, 0xe8, 0x23, 0x27, 0xbb, 0xd3, 0x54, 0xcb, 0x38,
                    0x3c, 0x24, 0x8a, 0x60, 0x11, 0x20, 0x92, 0x99, 0xec, 0x35,
                    0x71, 0x9f
                ]),
                bytes([
                    0x7a, 0xb6, 0x36, 0x42, 0x36, 0x83, 0xf3, 0xa6, 0xc1, 0x24,
                    0xc5, 0x63, 0xb0, 0x4c, 0x8b, 0xef, 0x7c, 0x77, 0x25, 0x83,
                    0xa8, 0xbb, 0x8b, 0x57, 0x75, 0x1c, 0xb6, 0xd7, 0xca, 0xc9,
                    0x0d, 0x78
                ]),
                bytes([
                    0x9d, 0x79, 0x66, 0x21, 0x64, 0x72, 0x97, 0x08, 0xa0, 0x5a,
                    0x94, 0x5a, 0x94, 0x7b, 0x11, 0xeb, 0x4e, 0xe9, 0x43, 0x2f,
                    0x08, 0xa2, 0x57, 0xa5, 0xd5, 0x99, 0xb0, 0xa7, 0xde, 0x78,
                    0x80, 0xb7
                ]),
                bytes([
                    0x9f, 0x88, 0x5c, 0xa5, 0xeb, 0x08, 0xef, 0x1a, 0xcf, 0xbb,
                    0x1d, 0x04, 0xc5, 0x47, 0x24, 0x37, 0x49, 0xe4, 0x4e, 0x9c,
                    0x5d, 0x56, 0xd0, 0x97, 0xfd, 0x8a, 0xe3, 0x23, 0x1d, 0xab,
                    0x16, 0x03
                ]),
            ],
            R=[
                bytes([
                    0xae, 0x89, 0xeb, 0xa8, 0x5b, 0xd5, 0x65, 0xd6, 0x9f, 0x2a,
                    0xfd, 0x04, 0x66, 0xad, 0xb1, 0xf3, 0x5e, 0xf6, 0x60, 0xa7,
                    0x26, 0x94, 0x3b, 0x72, 0x5a, 0x5c, 0x80, 0xfa, 0x0f, 0x75,
                    0x48, 0x27
                ]),
                bytes([
                    0xc9, 0x1a, 0x61, 0x70, 0x6d, 0xea, 0xea, 0xb2, 0x42, 0xff,
                    0x27, 0x3b, 0x8e, 0x94, 0x07, 0x75, 0x40, 0x7d, 0x33, 0xde,
                    0xfc, 0xbd, 0x53, 0xa0, 0x2a, 0xf9, 0x0c, 0x36, 0xb0, 0xdd,
                    0xbe, 0x8d
                ]),
                bytes([
                    0xb7, 0x39, 0x7a, 0x0e, 0xa1, 0x42, 0x0f, 0x94, 0x62, 0x24,
                    0xcf, 0x54, 0x75, 0xe3, 0x0b, 0x0f, 0xfb, 0xcb, 0x67, 0x7b,
                    0xbc, 0x98, 0x36, 0x01, 0x9f, 0x73, 0xa0, 0x70, 0xa1, 0x7e,
                    0xf0, 0xcf
                ]),
                bytes([
                    0x40, 0x06, 0xd4, 0xfa, 0x22, 0x7c, 0x82, 0xbf, 0xe8, 0xe0,
                    0x35, 0x13, 0x28, 0xa2, 0xb9, 0x51, 0xa3, 0x37, 0x34, 0xc0,
                    0xa6, 0x43, 0xd6, 0xb7, 0x7a, 0x40, 0xae, 0xf9, 0x36, 0x0e,
                    0xe3, 0xcc
                ]),
                bytes([
                    0x88, 0x38, 0x64, 0xe9, 0x63, 0xe3, 0x33, 0xd9, 0xf6, 0xca,
                    0x47, 0xc4, 0xc7, 0x36, 0x70, 0x01, 0xd2, 0xe4, 0x8c, 0x9f,
                    0x25, 0xc2, 0xce, 0xcf, 0x81, 0x89, 0x4f, 0x24, 0xcb, 0xb8,
                    0x40, 0x73
                ]),
                bytes([
                    0xdc, 0x35, 0x65, 0xed, 0x6b, 0xb0, 0xa7, 0x1a, 0x1b, 0xf3,
                    0xd6, 0xfb, 0x47, 0x00, 0x48, 0x00, 0x20, 0x6d, 0xd4, 0xeb,
                    0xff, 0xb9, 0xdc, 0x43, 0x30, 0x8a, 0x90, 0xfe, 0x43, 0x74,
                    0x75, 0x68
                ]),
            ],
            a=bytes([
                0xb4, 0x8e, 0xc2, 0x31, 0xce, 0x05, 0x9a, 0x7a, 0xbc, 0x82,
                0x8c, 0x30, 0xb3, 0xe3, 0x80, 0x86, 0x05, 0xb8, 0x4c, 0x93,
                0x9a, 0x8e, 0xce, 0x39, 0x0f, 0xb6, 0xee, 0x28, 0xf6, 0x7e,
                0xd5, 0x07
            ]),
            b=bytes([
                0x47, 0x10, 0x62, 0xc2, 0xad, 0xc7, 0xe2, 0xc9, 0x14, 0x6f,
                0xf4, 0xd1, 0xfe, 0x52, 0xa9, 0x1a, 0xe4, 0xb6, 0xd0, 0x25,
                0x4b, 0x19, 0x80, 0x7c, 0xcd, 0x62, 0x62, 0x1d, 0x97, 0x20,
                0x71, 0x0b
            ]),
            t=bytes([
                0x47, 0x06, 0xea, 0x76, 0x8f, 0xdb, 0xa3, 0x15, 0xe0, 0x2c,
                0x6b, 0x25, 0xa1, 0xf7, 0x3c, 0xc8, 0x1d, 0x97, 0xa6, 0x52,
                0x48, 0x75, 0x37, 0xf9, 0x1e, 0x14, 0xac, 0xb1, 0x2a, 0x34,
                0xc6, 0x06
            ]))
        # fmt: on

        self.assertTrue(bpi.verify_testnet(bp_proof))