Skip to content

Commit

Permalink
Update eth.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto committed May 12, 2024
1 parent dda0561 commit 3d5fe0e
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abigen!(
);
abigen!(
#[allow(missing_docs)]
#[sol(rpc, bytecode="608060405234801561000f575f80fd5b506108b18061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c80630a7e4b9614610038578063d3dc6d1f14610061575b5f80fd5b61004b61004636600461047c565b610074565b60405161005891906105bc565b60405180910390f35b61004b61006f3660046105ff565b6101e5565b606083516001600160401b0381111561008f5761008f6103ae565b6040519080825280602002602001820160405280156100b8578160200160208202803683370190505b5090505f5b84518110156101dd575f8582815181106100d9576100d9610699565b60200260200101518060200190518101906100f491906106ad565b90505f8112801561010b57610108826106d8565b91505b5f86848151811061011e5761011e610699565b6020026020010151600a61013291906107d4565b90505f86858151811061014757610147610699565b60200260200101516001901b90505f6101618583856102bf565b9050828380610172576101726107df565b8387096101809060026107f3565b106101935761019060018261080a565b90505b8361019e57806101a7565b6101a7816106d8565b8787815181106101b9576101b9610699565b602002602001018181525050505050505080806101d59061081d565b9150506100bd565b509392505050565b606081516001600160401b03811115610200576102006103ae565b604051908082528060200260200182016040528015610229578160200160208202803683370190505b5090505f5b82518110156102b9577f30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f00000018084838151811061026b5761026b610699565b602002602001015160070b6102809190610835565b61028a919061085c565b82828151811061029c5761029c610699565b6020908102919091010152806102b18161081d565b91505061022e565b50919050565b5f80805f19858709858702925082811083820303915050805f036102f6578382816102ec576102ec6107df565b04925050506103a7565b8084116103415760405162461bcd60e51b81526020600482015260156024820152744d6174683a206d756c446976206f766572666c6f7760581b604482015260640160405180910390fd5b5f848688098519600190810187169687900496828603819004959092119093035f82900391909104909201919091029190911760038402600290811880860282030280860282030280860282030280860282030280860282030280860290910302029150505b9392505050565b634e487b7160e01b5f52604160045260245ffd5b604051601f8201601f191681016001600160401b03811182821017156103ea576103ea6103ae565b604052919050565b5f6001600160401b0382111561040a5761040a6103ae565b5060051b60200190565b5f82601f830112610423575f80fd5b81356020610438610433836103f2565b6103c2565b82815260059290921b84018101918181019086841115610456575f80fd5b8286015b84811015610471578035835291830191830161045a565b509695505050505050565b5f805f6060848603121561048e575f80fd5b83356001600160401b03808211156104a4575f80fd5b818601915086601f8301126104b7575f80fd5b813560206104c7610433836103f2565b82815260059290921b8401810191818101908a8411156104e5575f80fd5b8286015b8481101561056e57803586811115610500575f8081fd5b8701603f81018d13610511575f8081fd5b84810135604088821115610527576105276103ae565b610539601f8301601f191688016103c2565b8281528f8284860101111561054d575f8081fd5b82828501898301375f928101880192909252508452509183019183016104e9565b5097505087013592505080821115610584575f80fd5b61059087838801610414565b935060408601359150808211156105a5575f80fd5b506105b286828701610414565b9150509250925092565b602080825282518282018190525f9190848201906040850190845b818110156105f3578351835292840192918401916001016105d7565b50909695505050505050565b5f6020808385031215610610575f80fd5b82356001600160401b03811115610625575f80fd5b8301601f81018513610635575f80fd5b8035610643610433826103f2565b81815260059190911b82018301908381019087831115610661575f80fd5b928401925b8284101561068e5783358060070b811461067f575f8081fd5b82529284019290840190610666565b979650505050505050565b634e487b7160e01b5f52603260045260245ffd5b5f602082840312156106bd575f80fd5b5051919050565b634e487b7160e01b5f52601160045260245ffd5b5f600160ff1b82016106ec576106ec6106c4565b505f0390565b600181815b8085111561072c57815f1904821115610712576107126106c4565b8085161561071f57918102915b93841c93908002906106f7565b509250929050565b5f82610742575060016107ce565b8161074e57505f6107ce565b8160018114610764576002811461076e5761078a565b60019150506107ce565b60ff84111561077f5761077f6106c4565b50506001821b6107ce565b5060208310610133831016604e8410600b84101617156107ad575081810a6107ce565b6107b783836106f2565b805f19048211156107ca576107ca6106c4565b0290505b92915050565b5f6103a78383610734565b634e487b7160e01b5f52601260045260245ffd5b80820281158282048414176107ce576107ce6106c4565b808201808211156107ce576107ce6106c4565b5f6001820161082e5761082e6106c4565b5060010190565b8082018281125f831280158216821582161715610854576108546106c4565b505092915050565b5f8261087657634e487b7160e01b5f52601260045260245ffd5b50069056fea26469706673582212200b8a0a357f7d2a8895754f5b26e714ec153b59420bdab2d0ad696eb17a0f235164736f6c63430008140033")]
#[sol(rpc, bytecode="608060405234801561000f575f80fd5b50610a8b8061001d5f395ff3fe608060405234801561000f575f80fd5b506004361061003f575f3560e01c80630a7e4b9614610043578063b404abab1461006c578063d3dc6d1f1461007f575b5f80fd5b6100566100513660046105b6565b610092565b60405161006391906106f6565b60405180910390f35b61005661007a366004610739565b610203565b61005661008d3660046107c4565b61033c565b606083516001600160401b038111156100ad576100ad6104e8565b6040519080825280602002602001820160405280156100d6578160200160208202803683370190505b5090505f5b84518110156101fb575f8582815181106100f7576100f7610853565b60200260200101518060200190518101906101129190610867565b90505f811280156101295761012682610892565b91505b5f86848151811061013c5761013c610853565b6020026020010151600a610150919061098e565b90505f86858151811061016557610165610853565b60200260200101516001901b90505f61017f8583856103fd565b905082838061019057610190610999565b83870961019e9060026109ad565b106101b1576101ae6001826109c4565b90505b836101bc57806101c5565b6101c581610892565b8787815181106101d7576101d7610853565b602002602001018181525050505050505080806101f3906109d7565b9150506100db565b509392505050565b606081516001600160401b0381111561021e5761021e6104e8565b604051908082528060200260200182016040528015610247578160200160208202803683370190505b5090505f5b8251811015610336575f83828151811061026857610268610853565b6020026020010151121580156102a457505f80516020610a3683398151915283828151811061029957610299610853565b602002602001015111155b6102ed5760405162461bcd60e51b8152602060048201526015602482015274125b9d985b1a5908199a595b1908195b195b595b9d605a1b60448201526064015b60405180910390fd5b8281815181106102ff576102ff610853565b602002602001015182828151811061031957610319610853565b60209081029190910101528061032e816109d7565b91505061024c565b50919050565b606081516001600160401b03811115610357576103576104e8565b604051908082528060200260200182016040528015610380578160200160208202803683370190505b5090505f5b8251811015610336575f80516020610a36833981519152808483815181106103af576103af610853565b602002602001015160070b6103c491906109ef565b6103ce9190610a16565b8282815181106103e0576103e0610853565b6020908102919091010152806103f5816109d7565b915050610385565b5f80805f19858709858702925082811083820303915050805f036104345783828161042a5761042a610999565b04925050506104e1565b80841161047b5760405162461bcd60e51b81526020600482015260156024820152744d6174683a206d756c446976206f766572666c6f7760581b60448201526064016102e4565b5f848688098519600190810187169687900496828603819004959092119093035f82900391909104909201919091029190911760038402600290811880860282030280860282030280860282030280860282030280860282030280860290910302029150505b9392505050565b634e487b7160e01b5f52604160045260245ffd5b604051601f8201601f191681016001600160401b0381118282101715610524576105246104e8565b604052919050565b5f6001600160401b03821115610544576105446104e8565b5060051b60200190565b5f82601f83011261055d575f80fd5b8135602061057261056d8361052c565b6104fc565b82815260059290921b84018101918181019086841115610590575f80fd5b8286015b848110156105ab5780358352918301918301610594565b509695505050505050565b5f805f606084860312156105c8575f80fd5b83356001600160401b03808211156105de575f80fd5b818601915086601f8301126105f1575f80fd5b8135602061060161056d8361052c565b82815260059290921b8401810191818101908a84111561061f575f80fd5b8286015b848110156106a85780358681111561063a575f8081fd5b8701603f81018d1361064b575f8081fd5b84810135604088821115610661576106616104e8565b610673601f8301601f191688016104fc565b8281528f82848601011115610687575f8081fd5b82828501898301375f92810188019290925250845250918301918301610623565b50975050870135925050808211156106be575f80fd5b6106ca8783880161054e565b935060408601359150808211156106df575f80fd5b506106ec8682870161054e565b9150509250925092565b602080825282518282018190525f9190848201906040850190845b8181101561072d57835183529284019291840191600101610711565b50909695505050505050565b5f602080838503121561074a575f80fd5b82356001600160401b0381111561075f575f80fd5b8301601f8101851361076f575f80fd5b803561077d61056d8261052c565b81815260059190911b8201830190838101908783111561079b575f80fd5b928401925b828410156107b9578335825292840192908401906107a0565b979650505050505050565b5f60208083850312156107d5575f80fd5b82356001600160401b038111156107ea575f80fd5b8301601f810185136107fa575f80fd5b803561080861056d8261052c565b81815260059190911b82018301908381019087831115610826575f80fd5b928401925b828410156107b95783358060070b8114610844575f8081fd5b8252928401929084019061082b565b634e487b7160e01b5f52603260045260245ffd5b5f60208284031215610877575f80fd5b5051919050565b634e487b7160e01b5f52601160045260245ffd5b5f600160ff1b82016108a6576108a661087e565b505f0390565b600181815b808511156108e657815f19048211156108cc576108cc61087e565b808516156108d957918102915b93841c93908002906108b1565b509250929050565b5f826108fc57506001610988565b8161090857505f610988565b816001811461091e576002811461092857610944565b6001915050610988565b60ff8411156109395761093961087e565b50506001821b610988565b5060208310610133831016604e8410600b8410161715610967575081810a610988565b61097183836108ac565b805f19048211156109845761098461087e565b0290505b92915050565b5f6104e183836108ee565b634e487b7160e01b5f52601260045260245ffd5b80820281158282048414176109885761098861087e565b808201808211156109885761098861087e565b5f600182016109e8576109e861087e565b5060010190565b8082018281125f831280158216821582161715610a0e57610a0e61087e565b505092915050565b5f82610a3057634e487b7160e01b5f52601260045260245ffd5b50069056fe30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001a26469706673582212200034995b2b5991300d54d46b8b569fdaad34c590716304d33ba67eac46c8a61764736f6c63430008140033")]
contract QuantizeData {
/**
* @notice EZKL P value
Expand Down Expand Up @@ -200,6 +200,17 @@ abigen!(
output[i] = uint256(quantized_data[i] + int(ORDER)) % ORDER;
}
}

function check_is_valid_field_element(
int256[] memory quantized_data
) public pure returns (uint256[] memory output) {
output = new uint256[](quantized_data.length);
for (uint i; i < quantized_data.length; i++) {
// assert it is a positive number and less than ORDER
require(quantized_data[i] >= 0 && uint256(quantized_data[i]) <= ORDER, "Invalid field element");
output[i] = uint256(quantized_data[i]);
}
}
}
);

Expand Down Expand Up @@ -634,6 +645,9 @@ pub async fn setup_test_contract<M: 'static + Provider<Http<Client>, Ethereum>>(
} else if input.is_field() {
let input = input.to_field(0);
let hex_str_fr = format!("{:?}", input);
// remove the 0x prefix
let hex_str_fr = &hex_str_fr[2..];
println!("hex_str_fr: {:#?}", hex_str_fr);
scaled_by_decimals_data.push(I256::from_raw(U256::from_str_radix(&hex_str_fr, 16)?));
decimals.push(0);
}
Expand Down Expand Up @@ -834,20 +848,24 @@ pub async fn evm_quantize<M: 'static + Provider<Http<Client>, Ethereum>>(

debug!("evm quantization results: {:#?}", results);

let results_i64 = results
.iter()
.map(|x| i64::from_str(&x.to_string()).unwrap())
.collect::<Vec<i64>>();

let felts = contract.to_field_element(results_i64).call().await?.output;
debug!("evm quantization contract results: {:#?}", felts,);
let mut felts = vec![];

for x in results {
let felt = match i64::from_str(&x.to_string()) {
Ok(x) => contract.to_field_element(vec![x]).call().await?.output[0],
Err(_) => {
contract
.check_is_valid_field_element(vec![x])
.call()
.await?
.output[0]
}
};
felts.push(PrimeField::from_str_vartime(&felt.to_string()).unwrap());
}

let results = felts
.iter()
.map(|x| PrimeField::from_str_vartime(&x.to_string()).unwrap())
.collect::<Vec<Fr>>();
debug!("evm quantized felts: {:#?}", results,);
Ok(results.to_vec())
debug!("evm quantized felts: {:#?}", felts,);
Ok(felts)
}

/// Generates the contract factory for a solidity verifier. The factory is used to deploy the contract
Expand Down

0 comments on commit 3d5fe0e

Please sign in to comment.