|
1 | 1 | #![cfg_attr(not(feature = "std"), no_main)]
|
2 | 2 | #![cfg_attr(not(feature = "std"), no_std)]
|
3 | 3 |
|
4 |
| -use core::str::FromStr; |
5 |
| - |
6 |
| -use num_bigint::BigUint; |
7 |
| -use openvm_algebra_guest::{ |
8 |
| - moduli_setup::{moduli_declare, moduli_init}, |
9 |
| - Field, IntMod, |
10 |
| -}; |
| 4 | +use hex_literal::hex; |
| 5 | +use openvm_algebra_guest::{moduli_setup::moduli_init, IntMod}; |
11 | 6 | use openvm_ecc_guest::{
|
| 7 | + ed25519::{Ed25519Coord, Ed25519Point}, |
12 | 8 | edwards::TwistedEdwardsPoint,
|
13 |
| - te_setup::{te_declare, te_init}, |
14 |
| - Group, |
| 9 | + te_setup::te_init, |
| 10 | + CyclicGroup, Group, |
15 | 11 | };
|
16 | 12 |
|
17 |
| -moduli_declare! { |
18 |
| - Edwards25519Coord { modulus = "57896044618658097711785492504343953926634992332820282019728792003956564819949" }, |
19 |
| -} |
20 |
| - |
21 | 13 | moduli_init! {
|
22 | 14 | "57896044618658097711785492504343953926634992332820282019728792003956564819949",
|
23 | 15 | }
|
24 | 16 |
|
25 |
| -impl Field for Edwards25519Coord { |
26 |
| - const ZERO: Self = <Self as IntMod>::ZERO; |
27 |
| - const ONE: Self = <Self as IntMod>::ONE; |
28 |
| - |
29 |
| - type SelfRef<'a> = &'a Self; |
30 |
| - |
31 |
| - fn double_assign(&mut self) { |
32 |
| - IntMod::double_assign(self); |
33 |
| - } |
34 |
| - |
35 |
| - fn square_assign(&mut self) { |
36 |
| - IntMod::square_assign(self); |
37 |
| - } |
38 |
| -} |
39 |
| - |
40 |
| -// a = 57896044618658097711785492504343953926634992332820282019728792003956564819948 |
41 |
| -// d = 37095705934669439343138083508754565189542113879843219016388785533085940283555 |
42 |
| -// encoded in little endian, 32 limbs of 8 bits each |
43 |
| -const CURVE_A: Edwards25519Coord = Edwards25519Coord::from_const_bytes([ |
44 |
| - 236, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
45 |
| - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127, |
46 |
| -]); |
47 |
| -const CURVE_D: Edwards25519Coord = Edwards25519Coord::from_const_bytes([ |
48 |
| - 163, 120, 89, 19, 202, 77, 235, 117, 171, 216, 65, 65, 77, 10, 112, 0, 152, 232, 121, 119, 121, |
49 |
| - 64, 199, 140, 115, 254, 111, 43, 238, 108, 3, 82, |
50 |
| -]); |
51 |
| - |
52 |
| -te_declare! { |
53 |
| - Edwards25519Point { |
54 |
| - mod_type = Edwards25519Coord, |
55 |
| - a = CURVE_A, |
56 |
| - d = CURVE_D |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 | 17 | te_init! {
|
61 |
| - Edwards25519Point, |
| 18 | + Ed25519Point, |
62 | 19 | }
|
63 | 20 |
|
64 | 21 | openvm::entry!(main);
|
65 | 22 |
|
66 |
| -fn string_to_coord(s: &str) -> Edwards25519Coord { |
67 |
| - Edwards25519Coord::from_le_bytes(&BigUint::from_str(s).unwrap().to_bytes_le()) |
68 |
| -} |
69 |
| - |
70 | 23 | pub fn main() {
|
71 | 24 | setup_all_moduli();
|
72 | 25 | setup_all_te_curves();
|
73 | 26 |
|
74 | 27 | // Base point of edwards25519
|
75 |
| - let x1 = string_to_coord( |
76 |
| - "15112221349535400772501151409588531511454012693041857206046113283949847762202", |
77 |
| - ); |
78 |
| - let y1 = string_to_coord( |
79 |
| - "46316835694926478169428394003475163141307993866256225615783033603165251855960", |
80 |
| - ); |
| 28 | + let mut p1 = Ed25519Point::GENERATOR; |
81 | 29 |
|
82 | 30 | // random point on edwards25519
|
83 |
| - let x2 = Edwards25519Coord::from_u32(2); |
84 |
| - let y2 = string_to_coord( |
85 |
| - "11879831548380997166425477238087913000047176376829905612296558668626594440753", |
86 |
| - ); |
| 31 | + let x2 = Ed25519Coord::from_u32(2); |
| 32 | + let y2 = Ed25519Coord::from_be_bytes(&hex!( |
| 33 | + "1A43BF127BDDC4D71FF910403C11DDB5BA2BCDD2815393924657EF111E712631" |
| 34 | + )); |
| 35 | + let mut p2 = Ed25519Point::from_xy(x2, y2).unwrap(); |
87 | 36 |
|
88 | 37 | // This is the sum of (x1, y1) and (x2, y2).
|
89 |
| - let x3 = string_to_coord( |
90 |
| - "44969869612046584870714054830543834361257841801051546235130567688769346152934", |
91 |
| - ); |
92 |
| - let y3 = string_to_coord( |
93 |
| - "50796027728050908782231253190819121962159170739537197094456293084373503699602", |
94 |
| - ); |
| 38 | + let x3 = Ed25519Coord::from_be_bytes(&hex!( |
| 39 | + "636C0B519B2C5B1E0D3BFD213F45AFD5DAEE3CECC9B68CF88615101BC78329E6" |
| 40 | + )); |
| 41 | + let y3 = Ed25519Coord::from_be_bytes(&hex!( |
| 42 | + "704D8868CB335A7B609D04B9CD619511675691A78861F1DFF7A5EBC389C7EA92" |
| 43 | + )); |
95 | 44 |
|
96 | 45 | // This is 2 * (x1, y1)
|
97 |
| - let x4 = string_to_coord( |
98 |
| - "39226743113244985161159605482495583316761443760287217110659799046557361995496", |
99 |
| - ); |
100 |
| - let y4 = string_to_coord( |
101 |
| - "12570354238812836652656274015246690354874018829607973815551555426027032771563", |
102 |
| - ); |
103 |
| - |
104 |
| - let mut p1 = Edwards25519Point::from_xy(x1.clone(), y1.clone()).unwrap(); |
105 |
| - let mut p2 = Edwards25519Point::from_xy(x2, y2).unwrap(); |
| 46 | + let x4 = Ed25519Coord::from_be_bytes(&hex!( |
| 47 | + "56B98CC045559AD2BBC45CAB58D842ECEE264DB9395F6014B772501B62BB7EE8" |
| 48 | + )); |
| 49 | + let y4 = Ed25519Coord::from_be_bytes(&hex!( |
| 50 | + "1BCA918096D89C83A15105DF343DC9F7510494407750226DAC0A7620ACE77BEB" |
| 51 | + )); |
106 | 52 |
|
107 | 53 | // Generic add can handle equal or unequal points.
|
108 | 54 | let p3 = &p1 + &p2;
|
|
0 commit comments