From f56a5e041ff98d4be0a2178169e4aeb8dee5994e Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Wed, 22 May 2024 16:58:08 +0800 Subject: [PATCH] add alt bn128 tests --- tests/__tests__/test_alt_bn128_api.ava.js | 75 +++++++++++++++++++ tests/__tests__/test_math_api.ava.js | 2 - tests/package.json | 4 +- tests/src/alt_bn128_api.js | 87 +++++++++++++++++++++++ 4 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 tests/__tests__/test_alt_bn128_api.ava.js create mode 100644 tests/src/alt_bn128_api.js diff --git a/tests/__tests__/test_alt_bn128_api.ava.js b/tests/__tests__/test_alt_bn128_api.ava.js new file mode 100644 index 000000000..7471129cd --- /dev/null +++ b/tests/__tests__/test_alt_bn128_api.ava.js @@ -0,0 +1,75 @@ +import { Worker } from "near-workspaces"; +import test from "ava"; + +test.before(async (t) => { + // Init the worker and start a Sandbox server + const worker = await Worker.init(); + + // Prepare sandbox for tests, create accounts, deploy contracts, etx. + const root = worker.rootAccount; + + // Deploy the test contract. + const altBn128ApiContract = await root.devDeploy("build/alt_bn128_api.wasm"); + + // Test users + const ali = await root.createSubAccount("ali"); + + // Save state for test runs + t.context.worker = worker; + t.context.accounts = { root, altBn128ApiContract, ali }; +}); + +test.after.always(async (t) => { + await t.context.worker.tearDown().catch((error) => { + console.log("Failed to tear down the worker:", error); + }); +}); + +test("test_alt_bn128_g1_sum", async (t) => { + const { ali, altBn128ApiContract } = t.context.accounts; + let r = await ali.callRaw(altBn128ApiContract, "test_alt_bn128_g1_sum", ""); + t.deepEqual( + Buffer.from(r.result.status.SuccessValue, "base64"), + Buffer.from([ + 11, 49, 94, 29, 152, 111, 116, 138, 248, 2, 184, 8, 159, 80, 169, 45, 149, + 48, 32, 49, 37, 6, 133, 105, 171, 194, 120, 44, 195, 17, 180, 35, 137, + 154, 4, 192, 211, 244, 93, 200, 2, 44, 0, 64, 26, 108, 139, 147, 88, 235, + 242, 23, 253, 52, 110, 236, 67, 99, 176, 2, 186, 198, 228, 25, + ]) + ); +}); + +test("test_alt_bn128_g1_multiexp", async (t) => { + const { ali, altBn128ApiContract } = t.context.accounts; + let r = await ali.callRaw( + altBn128ApiContract, + "test_alt_bn128_g1_multiexp", + "" + ); + t.deepEqual( + Buffer.from(r.result.status.SuccessValue, "base64"), + Buffer.from([ + 150, 94, 159, 52, 239, 226, 181, 150, 77, 86, 90, 186, 102, 219, 243, 204, + 36, 128, 164, 209, 106, 6, 62, 124, 235, 104, 223, 195, 30, 204, 42, 20, + 13, 158, 14, 197, 133, 73, 43, 171, 28, 68, 82, 116, 244, 164, 36, 251, + 244, 8, 234, 40, 118, 55, 216, 187, 242, 39, 213, 160, 192, 184, 28, 23, + ]) + ); +}); + +test("test_alt_bn128_pairing_check", async (t) => { + const { ali, altBn128ApiContract } = t.context.accounts; + let r = await ali.call( + altBn128ApiContract, + "test_alt_bn128_pairing_check_valid", + {} + ); + t.is(r, true); + + r = await ali.call( + altBn128ApiContract, + "test_alt_bn128_pairing_check_invalid", + {} + ); + t.is(r, false); +}); diff --git a/tests/__tests__/test_math_api.ava.js b/tests/__tests__/test_math_api.ava.js index 298f352e9..7abfa0968 100644 --- a/tests/__tests__/test_math_api.ava.js +++ b/tests/__tests__/test_math_api.ava.js @@ -91,5 +91,3 @@ test("ecrecover", async (t) => { ]) ); }); - -// TODO add test for alt_bn256 functions diff --git a/tests/package.json b/tests/package.json index 56c4b0ed0..672077486 100644 --- a/tests/package.json +++ b/tests/package.json @@ -29,6 +29,7 @@ "build:date-serialization": "near-sdk-js build src/date-serialization.ts build/date-serialization.wasm", "build:middlewares": "near-sdk-js build src/middlewares.ts build/middlewares.wasm", "build:migrate": "near-sdk-js build src/migrate.ts build/migrate.wasm", + "build:alt-bn128-api": "near-sdk-js build src/alt_bn128_api.js build/alt_bn128_api.wasm", "test": "ava", "test:context-api": "ava __tests__/test_context_api.ava.js", "test:math-api": "ava __tests__/test_math_api.ava.js", @@ -54,7 +55,8 @@ "test:constructor-validation": "ava __tests__/constructor_validation.ava.js", "test:migrate": "ava __tests__/decorators/migrate.ava.js", "test:middlewares": "ava __tests__/test-middlewares.ava.js", - "test:abi": "ava __tests__/abi/abi.ava.js" + "test:abi": "ava __tests__/abi/abi.ava.js", + "test:alt-bn128-api": "ava __tests__/test_alt_bn128_api.ava.js" }, "author": "Near Inc ", "license": "Apache-2.0", diff --git a/tests/src/alt_bn128_api.js b/tests/src/alt_bn128_api.js new file mode 100644 index 000000000..0789d3dfa --- /dev/null +++ b/tests/src/alt_bn128_api.js @@ -0,0 +1,87 @@ +import { near, bytes } from "near-sdk-js"; + +export function test_alt_bn128_g1_sum() { + // Originated from https://github.com/near/nearcore/blob/8cd095ffc98a6507ed2d2a8982a6a3e42ebc1b62/runtime/near-test-contracts/estimator-contract/src/lib.rs#L557-L720 + let buffer = new Uint8Array([ + 0, 11, 49, 94, 29, 152, 111, 116, 138, 248, 2, 184, 8, 159, 80, 169, 45, + 149, 48, 32, 49, 37, 6, 133, 105, 171, 194, 120, 44, 195, 17, 180, 35, 137, + 154, 4, 192, 211, 244, 93, 200, 2, 44, 0, 64, 26, 108, 139, 147, 88, 235, + 242, 23, 253, 52, 110, 236, 67, 99, 176, 2, 186, 198, 228, 25, + ]); + near.valueReturnRaw(near.altBn128G1Sum(buffer)); +} + +export function test_alt_bn128_g1_multiexp() { + // Originated from https://github.com/near/nearcore/blob/8cd095ffc98a6507ed2d2a8982a6a3e42ebc1b62/runtime/near-test-contracts/estimator-contract/src/lib.rs#L557-L720 + let buffer = new Uint8Array([ + 16, 238, 91, 161, 241, 22, 172, 158, 138, 252, 202, 212, 136, 37, 110, 231, + 118, 220, 8, 45, 14, 153, 125, 217, 227, 87, 238, 238, 31, 138, 226, 8, 238, + 185, 12, 155, 93, 126, 144, 248, 200, 177, 46, 245, 40, 162, 169, 80, 150, + 211, 157, 13, 10, 36, 44, 232, 173, 32, 32, 115, 123, 2, 9, 47, 190, 148, + 181, 91, 69, 6, 83, 40, 65, 222, 251, 70, 81, 73, 60, 142, 130, 217, 176, + 20, 69, 75, 40, 167, 41, 180, 244, 5, 142, 215, 135, 35, + ]); + near.valueReturnRaw(near.altBn128G1Multiexp(buffer)); +} + +export function test_alt_bn128_pairing_check_valid() { + // Taken from https://github.com/near/nearcore/blob/8cd095ffc98a6507ed2d2a8982a6a3e42ebc1b62/runtime/near-vm-runner/src/logic/tests/alt_bn128.rs#L239-L250 + let valid_pair = new Uint8Array([ + 117, 10, 217, 99, 113, 78, 234, 67, 183, 90, 26, 58, 200, 86, 195, 123, 42, + 184, 213, 88, 224, 248, 18, 200, 108, 6, 181, 6, 28, 17, 99, 7, 36, 134, 53, + 115, 192, 180, 3, 113, 76, 227, 174, 147, 50, 174, 79, 74, 151, 195, 172, + 10, 211, 210, 26, 92, 117, 246, 65, 237, 168, 104, 16, 4, 1, 26, 3, 219, 6, + 13, 193, 115, 77, 230, 27, 13, 242, 214, 195, 9, 213, 99, 135, 12, 160, 202, + 114, 135, 175, 42, 116, 172, 79, 234, 26, 41, 212, 111, 192, 129, 124, 112, + 57, 107, 38, 244, 230, 222, 240, 36, 65, 238, 133, 188, 19, 43, 148, 59, + 205, 40, 161, 179, 173, 228, 88, 169, 231, 29, 17, 67, 163, 51, 165, 187, + 101, 44, 250, 24, 68, 101, 92, 128, 203, 190, 51, 85, 9, 43, 58, 136, 68, + 180, 92, 110, 185, 168, 107, 129, 45, 30, 187, 22, 100, 17, 75, 93, 216, + 125, 23, 212, 11, 186, 199, 204, 1, 140, 133, 11, 82, 44, 65, 222, 20, 26, + 48, 26, 132, 220, 25, 213, 93, 25, 79, 176, 4, 149, 151, 243, 11, 131, 253, + 233, 121, 38, 222, 15, 118, 117, 200, 214, 175, 233, 130, 181, 193, 167, + 255, 153, 169, 240, 207, 235, 28, 31, 83, 74, 69, 179, 6, 150, 72, 67, 74, + 166, 130, 83, 82, 115, 123, 111, 208, 221, 64, 43, 237, 213, 186, 235, 7, + 56, 251, 179, 95, 233, 159, 23, 109, 173, 85, 103, 8, 165, 235, 226, 218, + 79, 72, 120, 172, 251, 20, 83, 121, 201, 140, 98, 170, 246, 121, 218, 19, + 115, 42, 135, 60, 239, 30, 32, 49, 170, 171, 204, 196, 197, 160, 158, 168, + 47, 23, 110, 139, 123, 222, 222, 245, 98, 125, 208, 70, 39, 110, 186, 146, + 254, 66, 185, 118, 3, 78, 32, 47, 179, 197, 93, 79, 240, 204, 78, 236, 133, + 213, 173, 117, 94, 63, 154, 68, 89, 236, 138, 0, 247, 242, 212, 245, 33, + 249, 0, 35, 246, 233, 0, 124, 86, 198, 162, 201, 54, 19, 26, 196, 75, 254, + 71, 70, 238, 51, 2, 23, 185, 152, 139, 134, 65, 107, 129, 114, 244, 47, 251, + 240, 80, 193, 23, + ]); + near.valueReturn(near.altBn128PairingCheck(valid_pair)); +} + +export function test_alt_bn128_pairing_check_invalid() { + // Taken from https://github.com/near/nearcore/blob/8cd095ffc98a6507ed2d2a8982a6a3e42ebc1b62/runtime/near-vm-runner/src/logic/tests/alt_bn128.rs#L254-L265 + let invalid_pair = new Uint8Array([ + 117, 10, 217, 99, 113, 78, 234, 67, 183, 90, 26, 58, 200, 86, 195, 123, 42, + 184, 213, 88, 224, 248, 18, 200, 108, 6, 181, 6, 28, 17, 99, 7, 36, 134, 53, + 115, 192, 180, 3, 113, 76, 227, 174, 147, 50, 174, 79, 74, 151, 195, 172, + 10, 211, 210, 26, 92, 117, 246, 65, 237, 168, 104, 16, 4, 1, 26, 3, 219, 6, + 13, 193, 115, 77, 230, 27, 13, 242, 214, 195, 9, 213, 99, 135, 12, 160, 202, + 114, 135, 175, 42, 116, 172, 79, 234, 26, 41, 212, 111, 192, 129, 124, 112, + 57, 107, 38, 244, 230, 222, 240, 36, 65, 238, 133, 188, 19, 43, 148, 59, + 205, 40, 161, 179, 173, 228, 88, 169, 231, 29, 17, 67, 163, 51, 165, 187, + 101, 44, 250, 24, 68, 101, 92, 128, 203, 190, 51, 85, 9, 43, 58, 136, 68, + 180, 92, 110, 185, 168, 107, 129, 45, 30, 187, 22, 100, 17, 75, 93, 216, + 125, 23, 212, 11, 186, 199, 204, 1, 140, 133, 11, 82, 44, 65, 222, 20, 26, + 48, 26, 132, 220, 25, 213, 93, 25, 117, 10, 217, 99, 113, 78, 234, 67, 183, + 90, 26, 58, 200, 86, 195, 123, 42, 184, 213, 88, 224, 248, 18, 200, 108, 6, + 181, 6, 28, 17, 99, 7, 36, 134, 53, 115, 192, 180, 3, 113, 76, 227, 174, + 147, 50, 174, 79, 74, 151, 195, 172, 10, 211, 210, 26, 92, 117, 246, 65, + 237, 168, 104, 16, 4, 109, 173, 85, 103, 8, 165, 235, 226, 218, 79, 72, 120, + 172, 251, 20, 83, 121, 201, 140, 98, 170, 246, 121, 218, 19, 115, 42, 135, + 60, 239, 30, 32, 49, 170, 171, 204, 196, 197, 160, 158, 168, 47, 23, 110, + 139, 123, 222, 222, 245, 98, 125, 208, 70, 39, 110, 186, 146, 254, 66, 185, + 118, 3, 78, 32, 47, 179, 197, 93, 79, 240, 204, 78, 236, 133, 213, 173, 117, + 94, 63, 154, 68, 89, 236, 138, 0, 247, 242, 212, 245, 33, 249, 0, 35, 246, + 233, 0, 124, 86, 198, 162, 201, 54, 19, 26, 196, 75, 254, 71, 70, 238, 51, + 2, 23, 185, 152, 139, 134, 65, 107, 129, 114, 244, 47, 251, 240, 80, 193, + 23, + ]); + near.valueReturn(near.altBn128PairingCheck(invalid_pair)); +}