Skip to content

Commit

Permalink
feat: allow for hashed and elgamal vis for DA test data (#549)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Ethan Cemer <[email protected]>
  • Loading branch information
alexander-camuto and ethan-crypto authored Oct 15, 2023
1 parent d0a5ac5 commit 3a3b7d5
Show file tree
Hide file tree
Showing 13 changed files with 895 additions and 111 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ jobs:
run: cargo nextest run --release --verbose tests_evm::kzg_evm_on_chain_output_prove_and_verify --test-threads 1
- name: KZG prove and verify tests (EVM + on chain inputs & outputs)
run: cargo nextest run --release --verbose tests_evm::kzg_evm_on_chain_input_output_prove_and_verify --test-threads 1
- name: KZG prove and verify tests (EVM + on chain inputs & outputs hashes)
run: cargo nextest run --release --verbose tests_evm::kzg_evm_on_chain_input_output_hashed_prove_and_verify --test-threads 1
- name: KZG prove and verify tests (EVM)
run: cargo nextest run --release --verbose tests_evm::kzg_evm_prove_and_verify --test-threads 1
- name: KZG prove and verify tests (EVM + hashed inputs)
Expand Down Expand Up @@ -445,7 +447,7 @@ jobs:
run: cargo nextest run neg_tests::neg_examples_

python-tests:
runs-on: 256gb
runs-on: self-hosted
needs: [build, library-tests, docs]
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -549,8 +551,10 @@ jobs:
# # now dump the contents of the file into a file called kaggle.json
# echo $KAGGLE_API_KEY > /home/ubuntu/.kaggle/kaggle.json
# chmod 600 /home/ubuntu/.kaggle/kaggle.json
- name: Hashed DA tutorial
run: source .env/bin/activate; cargo nextest run py_tests::tests::run_notebook_::tests_24_expects
- name: Little transformer tutorial
run: source .env/bin/activate; cargo nextest run py_tests::tests::run_notebook_::tests_8_expects --no-capture
run: source .env/bin/activate; cargo nextest run py_tests::tests::run_notebook_::tests_8_expects
- name: Stacked Regression tutorial
run: source .env/bin/activate; cargo nextest run py_tests::tests::run_notebook_::tests_23_expects
- name: Linear Regression tutorial
Expand Down Expand Up @@ -593,7 +597,7 @@ jobs:
run: source .env/bin/activate; cargo nextest run py_tests::tests::run_notebook_::tests_2_expects
- name: Hashed tutorial
run: source .env/bin/activate; cargo nextest run py_tests::tests::run_notebook_::tests_3_expects
- name: Data attestation tutorial
- name: DA tutorial
run: source .env/bin/activate; cargo nextest run py_tests::tests::run_notebook_::tests_5_expects
- name: Variance tutorial
run: source .env/bin/activate; cargo nextest run py_tests::tests::run_notebook_::tests_6_expects
Expand Down
4 changes: 0 additions & 4 deletions contracts/AttestData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ contract DataAttestation is LoadInstances {
if (mulmod(uint256(x), scale, decimals) * 2 >= decimals) {
output += 1;
}
// In the interest of keeping feature parity with the quantization done on the EZKL cli,
// we set the fixed point value type to be int128. Any value greater than that will throw an error
// as it does on the EZKL cli.
require(output <= uint128(type(int128).max), "Significant bit truncation");
quantized_data = neg ? -int256(output): int256(output);
}
/**
Expand Down
47 changes: 29 additions & 18 deletions contracts/QuantizeData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
pragma solidity ^0.8.17;

contract QuantizeData {


/**
* @notice EZKL P value
* @dev In order to prevent the verifier from accepting two version of the same instance, n and the quantity (n + P), where n + P <= 2^256, we require that all instances are stricly less than P. a
* @dev The reason for this is that the assmebly code of the verifier performs all arithmetic operations modulo P and as a consequence can't distinguish between n and n + P.
*/
uint256 constant ORDER = uint256(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001);
uint256 constant ORDER =
uint256(
0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
);

/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
function mulDiv(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
Expand Down Expand Up @@ -96,29 +102,34 @@ contract QuantizeData {
return result;
}
}
function quantize_data(bytes[] memory data, uint256[] memory decimals, uint256[] memory scales) external pure returns (int128[] memory quantized_data) {
quantized_data = new int128[](data.length);
for(uint i; i < data.length; i++){

function quantize_data(
bytes[] memory data,
uint256[] memory decimals,
uint256[] memory scales
) external pure returns (int256[] memory quantized_data) {
quantized_data = new int256[](data.length);
for (uint i; i < data.length; i++) {
int x = abi.decode(data[i], (int256));
bool neg = x < 0;
if (neg) x = -x;
uint denom = 10**decimals[i];
uint output = mulDiv(uint256(x), scales[i], denom);
if (mulmod(uint256(x), scales[i], denom)*2 >= denom) {
uint denom = 10 ** decimals[i];
uint scale = 1 << scales[i];
uint output = mulDiv(uint256(x), scale, denom);
if (mulmod(uint256(x), scale, denom) * 2 >= denom) {
output += 1;
}
// In the interest of keeping feature parity with the quantization done on the EZKL cli,
// we set the fixed point value type to be int128. Any value greater than that will throw an error
// as it does on the EZKL cli.
require(output <= uint128(type(int128).max), "Significant bit truncation");
quantized_data[i] = neg ? int128(-int256(output)): int128(int256(output));

quantized_data[i] = neg ? -int256(output) : int256(output);
}
}

function to_field_element(int128[] memory quantized_data) public pure returns(uint256[] memory output) {
function to_field_element(
int128[] memory quantized_data
) public pure returns (uint256[] memory output) {
output = new uint256[](quantized_data.length);
for(uint i; i < quantized_data.length; i++){
for (uint i; i < quantized_data.length; i++) {
output[i] = uint256(quantized_data[i] + int(ORDER)) % ORDER;
}
}
}
}
4 changes: 2 additions & 2 deletions contracts/TestReads.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
pragma solidity ^0.8.17;

contract TestReads {

int[] public arr;

constructor(int256[] memory _numbers) {
for(uint256 i = 0; i < _numbers.length; i++) {
for (uint256 i = 0; i < _numbers.length; i++) {
arr.push(_numbers[i]);
}
}
Expand Down
Loading

0 comments on commit 3a3b7d5

Please sign in to comment.