Skip to content

Commit

Permalink
Perf epns (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
shramee authored Jan 1, 2025
1 parent fdffae4 commit 0356bb3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from garaga.starknet.cli.utils import create_directory, get_package_version
from garaga.starknet.groth16_contract_generator.parsing_utils import Groth16VerifyingKey

ECIP_OPS_CLASS_HASH = 0x684D2756A4440C190A5FE54E367C0ABE33AEFA75084DEC2FFFC791B620C80E3
ECIP_OPS_CLASS_HASH = 0x413A1ED3773531DC6862144E21A53F547E97BFFAE4544AB354F3818C78861EC


def precompute_lines_from_vk(vk: Groth16VerifyingKey) -> StructArray:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod Groth16VerifierBLS12_381 {
use super::{N_PUBLIC_INPUTS, vk, ic, precomputed_lines};

const ECIP_OPS_CLASS_HASH: felt252 =
0x684d2756a4440c190a5fe54e367c0abe33aefa75084dec2fffc791b620c80e3;
0x413a1ed3773531dc6862144e21a53f547e97bffae4544ab354f3818c78861ec;

#[storage]
struct Storage {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod Groth16VerifierBN254 {
use super::{N_PUBLIC_INPUTS, vk, ic, precomputed_lines};

const ECIP_OPS_CLASS_HASH: felt252 =
0x684d2756a4440c190a5fe54e367c0abe33aefa75084dec2fffc791b620c80e3;
0x413a1ed3773531dc6862144e21a53f547e97bffae4544ab354f3818c78861ec;

#[storage]
struct Storage {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod FibonacciSequencer {
use core::starknet::ClassHash;

pub const RISC_ZERO_VERIFIER_CLASS_HASH: felt252 =
0x7f3157e83dc0d636c462c77fd8f309351b4a2c710f9a23a44305830a29b0b27;
0x11c3cd9b36eb302d4b5a27a79f78d065c783995000cfe571f60408d11708dc4;

#[storage]
struct Storage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod Risc0Groth16VerifierBN254 {
use super::{N_FREE_PUBLIC_INPUTS, vk, ic, precomputed_lines, T};

const ECIP_OPS_CLASS_HASH: felt252 =
0x684d2756a4440c190a5fe54e367c0abe33aefa75084dec2fffc791b620c80e3;
0x413a1ed3773531dc6862144e21a53f547e97bffae4544ab354f3818c78861ec;

#[storage]
struct Storage {}
Expand Down
33 changes: 24 additions & 9 deletions src/src/utils/neg_3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub fn neg_3_base_le(scalar: u128) -> Array<felt252> {

while scalar != 0 {
let (q, r) = core::traits::DivRem::div_rem(scalar, 3);
let r: felt252 = r.into();

if r == 2 {
if scalar_negative {
Expand All @@ -55,10 +56,10 @@ pub fn neg_3_base_le(scalar: u128) -> Array<felt252> {
} else {
if scalar_negative {
scalar = q;
digits.append(-r.into());
digits.append(-r);
} else {
scalar = q;
digits.append(r.into());
digits.append(r);
}
}
scalar_negative = !scalar_negative;
Expand Down Expand Up @@ -139,23 +140,37 @@ pub fn u128_array_to_epns(
// Where sum_p = sum(digits[i] * (-3)^i for i in [0, 81] if digits[i]==1)
// And sum_n = sum(digits[i] * (-3)^i for i in [0, 81] if digits[i]==-1)
// Returns (abs(sum_p), abs(sum_n), p_sign, n_sign)
pub fn scalar_to_epns(scalar: u128) -> (felt252, felt252, felt252, felt252) {
let mut digits: Array<felt252> = neg_3_base_le(scalar);

pub fn scalar_to_epns(mut scalar: u128) -> (felt252, felt252, felt252, felt252) {
let mut sum_p = 0;
let mut sum_n = 0;

let mut base_power = 1; // Init to (-3)^0

while let Option::Some(digit) = digits.pop_front() {
if digit != 0 {
if digit == 1 {
let mut scalar_negative: bool = false;

while scalar != 0 {
let (q, r) = core::traits::DivRem::div_rem(scalar, 3);
let r: felt252 = r.into();
if r == 0 {
scalar = q;
} else if r == 2 {
if scalar_negative {
scalar = q + 1;
sum_p += base_power;
} else {
scalar = q + 1;
sum_n += base_power;
}
} else {
if scalar_negative {
scalar = q;
sum_n += base_power;
} else {
scalar = q;
sum_p += base_power;
}
}

scalar_negative = !scalar_negative;
base_power = base_power * (-3);
};

Expand Down

0 comments on commit 0356bb3

Please sign in to comment.